2
1

飽き性なもんで

3ヶ月~半年ぐらいで飽きが来て、
年単位でやろうもんならやる気がほぼほぼ0になって片手間で別のこと始めちゃうくらい、
1つのプロジェクトを長くやるのがシンドく感じてしまう人間なので、
同じプロジェクトを何年も出来る人って凄いなって思う。

チェック処理とかは端折ってる

/** ここから定型文 */
ここで記載したクラスについては、
こっちの記事に書いてある、標準入力をList化したりしてるMainクラスから呼び出す予定だし・なんか継承したりしてるYO!!

ちな、Mainを実行して標準入力から入力するのクソ面倒くさいので、
Java編については問題の入力例をパラメータにしたテストクラスとかも作って公開するYO!!

開発・実行環境はこんな感じ
  • VSCode
  • Java 17
  • jUnit 5.9
  • maven

pom.xmlはこんな感じ

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>jp.co.asil</groupId>
    <artifactId>paiza202408</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
/** ここまで定型文 */

実装クラス

WordCollection.java
package jp.co.asil.paiza202408;

import java.util.List;
import java.util.stream.Collectors;

public class WordCollection extends Question {

  List<Word> words;
  List<String> buyWords;

  public WordCollection(List<String> list) {
    super(list);
    int[] cond = List.of(list.get(0).split(" ")).stream().mapToInt(Integer::parseInt).toArray();
    this.words = list.subList(1, 1 + cond[0]).stream().map(line -> new Word(line.split(" ")))
        .collect(Collectors.toList());
    this.buyWords = list.subList(1 + cond[0], list.size()).stream().collect(Collectors.toList());

  }

  @Override
  public List<String> answer() {
    return this.buyWords.stream()
        .map(word -> this.words.stream().filter(w -> w.word.startsWith(word)).mapToLong(w -> w.amount).sum())
        .map(String::valueOf).collect(Collectors.toList());
  }

  class Word {
    String word;
    int amount;

    public Word(String... line) {
      this.word = line[0];
      this.amount = Integer.parseInt(line[1]);
    }
  }

}

テストクラス

WordCollectionTest.java
package jp.co.asil.paiza202408;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

import java.util.List;

import org.junit.jupiter.api.Test;

public class WordCollectionTest {
  @Test
  void testAnswer1() {
    WordCollection testClass = new WordCollection(List.of(
        "6 5",
        "bcac 3",
        "abcd 14",
        "abccjg 92",
        "bcaddgie 2",
        "abcd 6",
        "cb 200",
        "b",
        "a",
        "abcd",
        "gagioheo",
        "cb"));
    assertArrayEquals(testClass.answer().toArray(), new String[] {
        "5",
        "112",
        "20",
        "0",
        "200"
    });
  }

  @Test
  void testAnswer2() {
    WordCollection testClass = new WordCollection(List.of(
        "5 3",
        "paiza 16",
        "pizza 1",
        "paizaio 4",
        "paizapoh 2",
        "pizzaio 8",
        "paiza",
        "pizza",
        "p"));
    assertArrayEquals(testClass.answer().toArray(), new String[] {
        "22",
        "9",
        "31"
    });
  }
}

やっぱり簡単なんだよなぁ。。。

JavaScript版で簡単じゃね?って書いてたら、
【俺スゲーがウゼー】ってコメント貰ったので自重しようかなとも思ったけど、
Javaで書いてもクソ簡単じゃね?

Stream内でStream処理を書いてるので、メチャクチャ読みにくいみたいなアレはありますが、
買う文字リストのループ(Stream)の中で、
商品文字リストのループ(Stream)回してフィルタして合算した結果を返してってやってるだけだぜ?

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1