2021年04月04日に参加
学習履歴詳細
【6回目】paizaの問題に取り組む(息抜き)
paizaの問題に取り組む
息抜きにpaizaのスキルチェック問題をやってみた。
問題について詳しくは書けないですが、与えられた文字列(すべて数字)を簡易的な二次元バーコードに変換するというもの。
与えられた仕様が変わった場合に、柔軟に対応できるように書こうとするのに時間がかかった。
解答時間の中央値の3倍時間がかかったので、まだまだ練習が必要だなーと思った。でも楽しかった。
メソッドや変数の命名にも改善点があるなと思った。
書いているうちに、「あれ、このメソッド何の処理してるんだっけ?」と思うことが多かった。
package S_5_myanswer; import java.util.ArrayList; import java.util.List; public class Main { public static final int decimalNum = 9; //バーコード1ブロックの行数 public static final int numberOfCharsPerLine = 3; public static final int numberOfBlocksPerLine = 3; public static final String sharp = "#"; public static final String dot = "."; public static void main(String[] args) { String line = "314159265"; List<List<String>> decoded = new ArrayList<>(); for (int i = 0; i < line.length(); i++){ char ch = line.charAt(i); decoded.add(convertNumToStringList(Character.toString(ch))); } int loopCount = line.length() / 3; int currentCount = 0; int firstIndex = 0; //読み出す回数 while (currentCount < loopCount){ List<String> firstList = decoded.get(firstIndex); List<String> secondList = decoded.get(firstIndex + 1); List<String> thirdList = decoded.get(firstIndex + 2); for (int i = 0; i < 3; i++){ System.out.println(firstList.get(i) + secondList.get(i) + thirdList.get(i)); } firstIndex = firstIndex + numberOfBlocksPerLine; currentCount++; } } public static List<String> convertNumToStringList(String str) { StringBuilder sb = new StringBuilder(); int inputNum = Integer.parseInt(str); int count = decimalNum; List<String> result = new ArrayList<>(); for (int i = inputNum; count > 0; count--) { if (i > 0){ sb.append(sharp); } else { sb.append(dot); } i--; } return convertStringToList(sb.toString()); } public static List<String> convertStringToList(String input){ List<String> result = new ArrayList<String>(); int numberOfLines = decimalNum / numberOfCharsPerLine; int inputNum = decimalNum; int index = 0; for (int i = 0; i < numberOfLines; i++) { StringBuilder element = new StringBuilder(); for (int j = 0; j < numberOfCharsPerLine; j++) { element.append(input.charAt(index)); index++; } result.add(element.toString()); } return result; } }
Java
2023年07月02日(日)
2.1時間