Test cases with more than two groups added.
import static org.junit.jupiter.api.Assertions.*; import java.util.HashMap; import java.util.Map; import java.util.Random; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; class Tests { final String allChars = "abcdefghijklmnopqrstuvwxyz1234567890"; @Test void test() { doTest(1, "12345 98212"); doTest(1, "9999"); doTest(-1, "abcdefg"); doTest(-1,"zzzzzzz213 9ppppopppo2"); doTest(0,"abcdef 12345"); doTest(-1,"abcd 12345 xy"); } @Test void hiddenTest() { doTest(0,""); doTest(0," "); doTest(0,"a1"); doTest(1,"a1b2c3 d4e5f6"); doTest(0,"a1b2c3 5y5"); doTest(0,"a1b2c3 5y5"); doTest(0,"5y5 a1b2c3 "); doTest(1,"9%$@"); doTest(-1,"~$@#a"); doTest(0,"~$@#"); doTest(0,"0"); doTest(-1,"abcd 12345 xa"); doTest(-1,"abcd 12345 xy a bc"); doTest(-1,"abcd 12345 xy bb aa aa"); } @RepeatedTest(100) void randomTests() { String randomString = generateRandomString(); int sol = sol(randomString); doTest(sol, randomString); } private String generateRandomString() { Random rnd = new Random(); StringBuilder randomString = new StringBuilder(); int finalStringLength =rnd.nextInt(100); for(int i=0;i<finalStringLength;i++) { int currentWordLength = rnd.nextInt(20); for(int j=0;j<currentWordLength;j++) { randomString.append(allChars.charAt(rnd.nextInt(36))); } randomString.append(" "); } return randomString.toString().trim(); } private void doTest(int expected, String str) { int actual = NumbersVsLetters.numbersVsLetters(str); String msg = String.format( "Incorrect answer for String = %s\n Expected: %s\n Actual: %s\n", str, expected, actual); assertEquals(expected, actual, msg); } private static int sol(String str) { if (str.isEmpty() || str.isBlank()) return 0; Map<Character, Integer> allChars = fillMap(); int score = 0; int numberMultiplication = -1; int characterSum = 0; for (char c : str.toCharArray()) { if (c == ' ') { if (numberMultiplication > 0) score += Integer.compare(numberMultiplication, characterSum); else if (characterSum > 0) score--; numberMultiplication = -1; characterSum = 0; } else if (Character.isDigit(c)) numberMultiplication = Character.getNumericValue(c) * Math.abs(numberMultiplication); else if (Character.isAlphabetic(c)) characterSum += allChars.get(c); } if (numberMultiplication > 0) score += Integer.compare(numberMultiplication, characterSum); else if (characterSum > 0) score--; return Integer.compare(score, 0); } private static Map<Character, Integer> fillMap(){ Map<Character, Integer> allChars = new HashMap<>(); for (int i = 0; i < 26; i++) { char c = (char) ('a' + i); allChars.put(c, i + 1); } return allChars; } }
- import static org.junit.jupiter.api.Assertions.*;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Random;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
- class Tests {
- final String allChars = "abcdefghijklmnopqrstuvwxyz1234567890";
- @Test
- void test() {
- doTest(1, "12345 98212");
- doTest(1, "9999");
- doTest(-1, "abcdefg");
- doTest(-1,"zzzzzzz213 9ppppopppo2");
- doTest(0,"abcdef 12345");
- doTest(-1,"abcd 12345 xy");
- }
- @Test
- void hiddenTest() {
- doTest(0,"");
- doTest(0," ");
- doTest(0,"a1");
- doTest(1,"a1b2c3 d4e5f6");
- doTest(0,"a1b2c3 5y5");
- doTest(0,"a1b2c3 5y5");
- doTest(0,"5y5 a1b2c3 ");
- doTest(1,"9%$@");
- doTest(-1,"~$@#a");
- doTest(0,"~$@#");
- doTest(0,"0");
- doTest(-1,"abcd 12345 xa");
- doTest(-1,"abcd 12345 xy a bc");
- doTest(-1,"abcd 12345 xy bb aa aa");
- }
- @RepeatedTest(100)
- void randomTests() {
- String randomString = generateRandomString();
- int sol = sol(randomString);
- doTest(sol, randomString);
- }
- private String generateRandomString() {
- Random rnd = new Random();
- StringBuilder randomString = new StringBuilder();
- int finalStringLength =rnd.nextInt(100);
- for(int i=0;i<finalStringLength;i++) {
- int currentWordLength = rnd.nextInt(20);
- for(int j=0;j<currentWordLength;j++) {
- randomString.append(allChars.charAt(rnd.nextInt(36)));
- }
- randomString.append(" ");
- }
- return randomString.toString().trim();
- }
- private void doTest(int expected, String str) {
- int actual = NumbersVsLetters.numbersVsLetters(str);
- String msg = String.format( "Incorrect answer for String = %s\n Expected: %s\n Actual: %s\n", str, expected, actual);
- assertEquals(expected, actual, msg);
- }
- private static int sol(String str) {
- if (str.isEmpty() || str.isBlank()) return 0;
- Map<Character, Integer> allChars = fillMap();
- int score = 0;
- int numberMultiplication = -1;
- int characterSum = 0;
- for (char c : str.toCharArray()) {
- if (c == ' ') {
- if (numberMultiplication > 0) score += Integer.compare(numberMultiplication, characterSum);
- else if (characterSum > 0) score--;
- numberMultiplication = -1;
- characterSum = 0;
- } else if (Character.isDigit(c)) numberMultiplication = Character.getNumericValue(c) * Math.abs(numberMultiplication);
- else if (Character.isAlphabetic(c)) characterSum += allChars.get(c);
- }
- if (numberMultiplication > 0) score += Integer.compare(numberMultiplication, characterSum);
- else if (characterSum > 0) score--;
- return Integer.compare(score, 0);
- }
- private static Map<Character, Integer> fillMap(){
- Map<Character, Integer> allChars = new HashMap<>();
- for (int i = 0; i < 26; i++) {
- char c = (char) ('a' + i);
- allChars.put(c, i + 1);
- }
- return allChars;
- }
- }