You will be given a String which will be composed of different groups of letters and numbers separated by a blank space.
You will have to calculate for each group whether the value of the sum of the letters is greater, equal or less than the multiplication of the numbers. When the values are equal the group will not count towards the final value.
The value of each letter corresponds to its position in the English alphabet so a=1, b=2, c=3 ... x=24, y=25 and z=26. If there are more groups that have a greater numeric value you will return 1. If the alphabetic value is greater for more groups, you will return -1. If the sum of groups that have greater numeric value is equal to the sum of groups that have greater alphabetic value you will return 0.
Example:
("a1b 42c") ->
a1b(lettersValue(1(a)+2(b)=3)>numbersValue(1))>-1(letters win)
42c(lettersValue(3(c))<numbersValue(4*2=8))->1(numbers win)
returns 0 since 1-1=0(draw)
("12345 9812") ->returns 1
("abc def") -> returns -1
("abcdef 12345") ->returns 0 since the first group has
a greater alphabetic value and the second group has a
greater numeric value
("a1b2c3") ->returns 0 since the sum of the letters
(1(a)+2(b)+3(c)) is equal to the multiplication of the
numbers (1*2*3)
("z27jk 99sd")->
//z27jk
26('z')+10('j')+11('k')>2*7-> 47>14 -> -1
//99sd
9*9>19('s')+4('d')->81>23->1
//returns
1-1->0
("a1 b3 44e")->returns 0+1+1=2->1
Notes:
- There won't be any uppercase letters
- Special characters such as # or $ do not have any value
("$@#") returns 0
- There might be empty Strings
- There can be more than one blank space splitting each group and there can be trailling spaces, for example
("aei 213 ")
- Watch out for groups that only contain a zero!
import java.util.HashMap; import java.util.Map; public class NumbersVsLetters{ public static int numbersVsLetters(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); } public 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 java.util.HashMap;
- import java.util.Map;
- public class NumbersVsLetters{
- public static int numbersVsLetters(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);
- }
- public 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"); } @Test void hiddenTest() { assertEquals(0,NumbersVsLetters.numbersVsLetters("")); assertEquals(0,NumbersVsLetters.numbersVsLetters(" ")); assertEquals(0,NumbersVsLetters.numbersVsLetters("a1")); assertEquals(1,NumbersVsLetters.numbersVsLetters("a1b2c3 d4e5f6")); assertEquals(0,NumbersVsLetters.numbersVsLetters("a1b2c3 5y5")); assertEquals(0,NumbersVsLetters.numbersVsLetters("a1b2c3 5y5")); assertEquals(0,NumbersVsLetters.numbersVsLetters("5y5 a1b2c3 ")); assertEquals(1,NumbersVsLetters.numbersVsLetters("9%$@")); assertEquals(-1,NumbersVsLetters.numbersVsLetters("~$@#a")); assertEquals(0,NumbersVsLetters.numbersVsLetters("~$@#")); assertEquals(0,NumbersVsLetters.numbersVsLetters("0")); } @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() {
assertEquals(1,NumbersVsLetters.numbersVsLetters("12345 98212"));assertEquals(1,NumbersVsLetters.numbersVsLetters("9999"));assertEquals(-1,NumbersVsLetters.numbersVsLetters("abcdefg"));assertEquals(-1,NumbersVsLetters.numbersVsLetters("zzzzzzz213 9ppppopppo2"));assertEquals(0,NumbersVsLetters.numbersVsLetters("abcdef 12345"));- doTest(1, "12345 98212");
- doTest(1, "9999");
- doTest(-1, "abcdefg");
- doTest(-1,"zzzzzzz213 9ppppopppo2");
- doTest(0,"abcdef 12345");
- }
- @Test
- void hiddenTest() {
- assertEquals(0,NumbersVsLetters.numbersVsLetters(""));
- assertEquals(0,NumbersVsLetters.numbersVsLetters(" "));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("a1"));
- assertEquals(1,NumbersVsLetters.numbersVsLetters("a1b2c3 d4e5f6"));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("a1b2c3 5y5"));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("a1b2c3 5y5"));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("5y5 a1b2c3 "));
- assertEquals(1,NumbersVsLetters.numbersVsLetters("9%$@"));
- assertEquals(-1,NumbersVsLetters.numbersVsLetters("~$@#a"));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("~$@#"));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("0"));
- }
- @RepeatedTest(100)
- void randomTests() {
- String randomString = generateRandomString();
assertEquals(KataSolution.sol(randomString),NumbersVsLetters.numbersVsLetters(randomString ));- 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))).append(" ");- 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;
- }
- }
Description
You will be given a String which will be composed of different groups of letters and numbers separated by a blank space. You will have to calculate for each group whether the value of the sum of the letters is greater, equal or less than the multiplication of the numbers. When the values are equal the group will not count towards the final value. The value of each letter corresponds to its position in the English alphabet so a=1, b=2, c=3 ... x=24, y=25 and z=26. If there are more groups that have a greater numeric value you will return 1. If the alphabetic value is greater for more groups, you will return -1. If there are the same number of groups with greater numeric and alphabetic values, you will return 0.
Example:
("12345 9812") -> 1
("abc def") -> -1
("abcdef 12345") -> 0 since the first group has a greater letter value and the second group has a greater numeric value
("a1b2c3") -> 0 since the sum of the letters (1(a)+2(b)+3(c)) is equal to the multiplication of the numbers (1*2*3)
Notes:
- There won't be any uppercase letters
- Special characters such as # or $ do not have any value
- There might be empty Strings
import java.util.HashMap; import java.util.Map; public class NumbersVsLetters{ public static int numbersVsLetters(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); } public 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 java.util.HashMap;
- import java.util.Map;
- public class NumbersVsLetters{
public static int numbersVsLetters(String str) {- public static int numbersVsLetters(String str) {
- if (str.isEmpty() || str.isBlank()) return 0;
Map<Character, Integer> allChars = new HashMap<>();for (int i = 0; i < 26; i++) {char c = (char) ('a' + i);allChars.put(c, i + 1);}- Map<Character, Integer> allChars = fillMap();
- int score = 0;
int numberMultiplication = 1;- int numberMultiplication = -1;
- int characterSum = 0;
boolean hasNumber = false;boolean hasCharacter = false;- for (char c : str.toCharArray()) {
- if (c == ' ') {
if (hasNumber) score += Integer.compare(numberMultiplication, characterSum);else if (hasCharacter) score--;numberMultiplication = 1;- if (numberMultiplication > 0) score += Integer.compare(numberMultiplication, characterSum);
- else if (characterSum > 0) score--;
- numberMultiplication = -1;
- characterSum = 0;
hasNumber = false;hasCharacter = false;- } else if (Character.isDigit(c)) {
numberMultiplication *= Character.getNumericValue(c);hasNumber = true;- numberMultiplication = Character.getNumericValue(c) * Math.abs(numberMultiplication);
- } else if (Character.isAlphabetic(c)) {
- characterSum += allChars.get(c);
hasCharacter = true;- }
- }
if (hasNumber) score += Integer.compare(numberMultiplication, characterSum);else if (hasCharacter) score--;- if (numberMultiplication > 0) score += Integer.compare(numberMultiplication, characterSum);
- else if (characterSum > 0) score--;
- return Integer.compare(score, 0);
- }
- public 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;
- }
- }
Description
You will be given a String which will be composed of different groups of letters and numbers separated by a blank space. You will have to calculate for each group whether the value of the sum of the letters is greater, equal or less than the multiplication of the numbers. The value of each letter corresponds to its position in the alphabet so a=1, b=2, c=3 ... x=24, y=25 and z=26. If there are more groups that have a greater numeric value you will return 1. If the alphabetic value is greater for more groups, you will return -1. If there are the same number of groups with greater numeric and alphabetic values, you will return 0.
Example:
("12345 9812") -> 1
("abc def") -> -1
("abcdef 12345") -> 0 since the first group has a greater letter value and the second group has a greater numeric value
("a1b2c3") -> 0 since the sum of the letters (1(a)+2(b)+3(c)) is equal to the multiplication of the numbers (1*2*3)
Notes:
- There won't be any uppercase letters
- Special characters such as # or $ do not have any value
- There might be empty Strings
import java.util.HashMap; import java.util.Map; public class NumbersVsLetters{ public static int numbersVsLetters(String str) { if (str.isEmpty() || str.isBlank()) return 0; Map<Character, Integer> allChars = new HashMap<>(); for (int i = 0; i < 26; i++) { char c = (char) ('a' + i); allChars.put(c, i + 1); } int score = 0; int numberMultiplication = 1; int characterSum = 0; boolean hasNumber = false; boolean hasCharacter = false; for (char c : str.toCharArray()) { if (c == ' ') { if (hasNumber) score += Integer.compare(numberMultiplication, characterSum); else if (hasCharacter) score--; numberMultiplication = 1; characterSum = 0; hasNumber = false; hasCharacter = false; } else if (Character.isDigit(c)) { numberMultiplication *= Character.getNumericValue(c); hasNumber = true; } else if (Character.isAlphabetic(c)) { characterSum += allChars.get(c); hasCharacter = true; } } if (hasNumber) score += Integer.compare(numberMultiplication, characterSum); else if (hasCharacter) score--; return Integer.compare(score, 0); } }
- import java.util.HashMap;
- import java.util.Map;
- public class NumbersVsLetters{
public static int opt2(String s) {Map<String, Integer> map = new HashMap<>();int result = 0,num=1,letr=0;boolean correcto= false;for (int i = 0; i < 26; i++) {map.put(Character.toString('a' + i), i + 1);}// map.forEach((a,b)->System.out.println(a+"-"+b));String numeros = "", letras = "";for (int c = 0; c < s.length(); c++) {if (!String.valueOf(s.charAt(c)).equals(" ") && s.length()!=c+1) {if (String.valueOf(s.charAt(c)).matches("^\\d$")) {//numeros = numeros + s.charAt(c);correcto=true;num=num*s.charAt(c);} else if (String.valueOf(s.charAt(c)).matches("^[a-z]$")) {//letras = letras + s.charAt(c);}} else {correcto=false;numeros="";letras="";}}System.out.println(numeros);System.out.println(letras);return result;}//Segunda opcion a considerar- public static int numbersVsLetters(String str) {
- if (str.isEmpty() || str.isBlank()) return 0;
- Map<Character, Integer> allChars = new HashMap<>();
- for (int i = 0; i < 26; i++) {
- char c = (char) ('a' + i);
- allChars.put(c, i + 1);
- }
- int score = 0;
- int numberMultiplication = 1;
- int characterSum = 0;
- boolean hasNumber = false;
- boolean hasCharacter = false;
- for (char c : str.toCharArray()) {
- if (c == ' ') {
- if (hasNumber) score += Integer.compare(numberMultiplication, characterSum);
- else if (hasCharacter) score--;
- numberMultiplication = 1;
- characterSum = 0;
- hasNumber = false;
- hasCharacter = false;
- } else if (Character.isDigit(c)) {
- numberMultiplication *= Character.getNumericValue(c);
- hasNumber = true;
- } else if (Character.isAlphabetic(c)) {
- characterSum += allChars.get(c);
- hasCharacter = true;
- }
- }
- if (hasNumber) score += Integer.compare(numberMultiplication, characterSum);
- else if (hasCharacter) score--;
- return Integer.compare(score, 0);
- }
- }
Description
You will be given a String which will be composed of different groups of letters and numbers separated by a blank space. You will have to calculate for each group whether the value of the sum of the letters is greater, equal or less than the multiplication of the numbers. The value of each letter corresponds to its position in the alphabet so a=1, b=2, c=3 ... x=24, y=25 and z=26. If there are more groups that have a greater numeric value you will return 1. If the alphabetic value is greater for more groups, you will return -1. If there are the same number of groups with greater numeric and alphabetic values, you will return 0.
Example:
("12345 9812") -> 1
("abc def") -> -1
("abcdef 12345") -> 0 since the first group has a greater letter value and the second group has a greater numeric value
("a1b2c3") -> 0 since the sum of the letters (1(a)+2(b)+3(c)) is equal to the multiplication of the numbers (1*2*3)
Notes:
- There won't be any uppercase letters
- Special characters such as # or $ do not have any value
- There might be empty Strings
import static org.junit.jupiter.api.Assertions.*; import java.util.HashMap; 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() { assertEquals(1,NumbersVsLetters.numbersVsLetters("12345 98212")); assertEquals(1,NumbersVsLetters.numbersVsLetters("9999")); assertEquals(-1,NumbersVsLetters.numbersVsLetters("abcdefg")); assertEquals(-1,NumbersVsLetters.numbersVsLetters("zzzzzzz213 9ppppopppo2")); assertEquals(0,NumbersVsLetters.numbersVsLetters("abcdef 12345")); } @Test void hiddenTest() { assertEquals(0,NumbersVsLetters.numbersVsLetters("")); assertEquals(0,NumbersVsLetters.numbersVsLetters(" ")); assertEquals(0,NumbersVsLetters.numbersVsLetters("a1")); assertEquals(1,NumbersVsLetters.numbersVsLetters("a1b2c3 d4e5f6")); assertEquals(0,NumbersVsLetters.numbersVsLetters("a1b2c3 5y5")); assertEquals(0,NumbersVsLetters.numbersVsLetters("a1b2c3 5y5")); assertEquals(0,NumbersVsLetters.numbersVsLetters("5y5 a1b2c3 ")); assertEquals(1,NumbersVsLetters.numbersVsLetters("9%$@")); assertEquals(-1,NumbersVsLetters.numbersVsLetters("~$@#a")); assertEquals(0,NumbersVsLetters.numbersVsLetters("~$@#")); assertEquals(0,NumbersVsLetters.numbersVsLetters("0")); } @RepeatedTest(100) void randomTests() { String randomString = generateRandomString(); assertEquals(KataSolution.sol(randomString),NumbersVsLetters.numbersVsLetters(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))).append(" "); } } return randomString.toString().trim(); } }
- import static org.junit.jupiter.api.Assertions.*;
- import java.util.HashMap;
- 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() {
- assertEquals(1,NumbersVsLetters.numbersVsLetters("12345 98212"));
- assertEquals(1,NumbersVsLetters.numbersVsLetters("9999"));
- assertEquals(-1,NumbersVsLetters.numbersVsLetters("abcdefg"));
- assertEquals(-1,NumbersVsLetters.numbersVsLetters("zzzzzzz213 9ppppopppo2"));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("abcdef 12345"));
- }
- @Test
- void hiddenTest() {
- assertEquals(0,NumbersVsLetters.numbersVsLetters(""));
- assertEquals(0,NumbersVsLetters.numbersVsLetters(" "));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("a1"));
- assertEquals(1,NumbersVsLetters.numbersVsLetters("a1b2c3 d4e5f6"));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("a1b2c3 5y5"));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("a1b2c3 5y5"));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("5y5 a1b2c3 "));
- assertEquals(1,NumbersVsLetters.numbersVsLetters("9%$@"));
- assertEquals(-1,NumbersVsLetters.numbersVsLetters("~$@#a"));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("~$@#"));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("0"));
- }
- @RepeatedTest(100)
- void randomTests() {
- String randomString = generateRandomString();
- assertEquals(KataSolution.sol(randomString),NumbersVsLetters.numbersVsLetters(randomString ));
- }
- private String generateRandomString() {
- Random rnd = new Random();
- StringBuilder randomString = new StringBuilder();
int finalStringLength =(int) (Math.random() * 100);- int finalStringLength =rnd.nextInt(100);
- for(int i=0;i<finalStringLength;i++) {
int currentWordLength = (int) (Math.random() * 20);- int currentWordLength = rnd.nextInt(20);
- for(int j=0;j<currentWordLength;j++) {
randomString.append(allChars.charAt((int)(Math.random() * 35))).append(" ");- randomString.append(allChars.charAt(rnd.nextInt(36))).append(" ");
- }
- }
- return randomString.toString().trim();
- }
- }
Description
You will be given a String which will be composed of different groups of letters and numbers separated by a blank space. You will have to calculate for each group whether the value of the sum of the letters is greater, equal or less than the multiplication of the numbers. The value of each letter corresponds to its position in the alphabet so a=1, b=2, c=3 ... x=24, y=25 and z=26. If there are more groups that have a greater numeric value you will return 1. If the alphabetic value is greater for more groups, you will return -1. If there are the same number of groups with greater numeric and alphabetic values, you will return 0.
Example:
("12345 9812") -> 1
("abc def") -> -1
("abcdef 12345") -> 0 since the first group has a greater letter value and the second group has a greater numeric value
("a1b2c3") -> 0 since the sum of the letters (1(a)+2(b)+3(c)) is equal to the multiplication of the numbers (1*2*3)
Notes:
- There won't be any uppercase letters
- Special characters such as # or $ do not have any value
- There might be empty Strings
import static org.junit.jupiter.api.Assertions.*; import java.util.HashMap; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; class Tests { final String allChars = "abcdefghijklmnopqrstuvwxyz1234567890"; @Test void test() { assertEquals(1,NumbersVsLetters.numbersVsLetters("12345 98212")); assertEquals(1,NumbersVsLetters.numbersVsLetters("9999")); assertEquals(-1,NumbersVsLetters.numbersVsLetters("abcdefg")); assertEquals(-1,NumbersVsLetters.numbersVsLetters("zzzzzzz213 9ppppopppo2")); assertEquals(0,NumbersVsLetters.numbersVsLetters("abcdef 12345")); } @Test void hiddenTest() { assertEquals(0,NumbersVsLetters.numbersVsLetters("")); assertEquals(0,NumbersVsLetters.numbersVsLetters(" ")); assertEquals(0,NumbersVsLetters.numbersVsLetters("a1")); assertEquals(1,NumbersVsLetters.numbersVsLetters("a1b2c3 d4e5f6")); assertEquals(0,NumbersVsLetters.numbersVsLetters("a1b2c3 5y5")); assertEquals(0,NumbersVsLetters.numbersVsLetters("a1b2c3 5y5")); assertEquals(0,NumbersVsLetters.numbersVsLetters("5y5 a1b2c3 ")); assertEquals(1,NumbersVsLetters.numbersVsLetters("9%$@")); assertEquals(-1,NumbersVsLetters.numbersVsLetters("~$@#a")); assertEquals(0,NumbersVsLetters.numbersVsLetters("~$@#")); assertEquals(0,NumbersVsLetters.numbersVsLetters("0")); } @RepeatedTest(100) void randomTests() { String randomString = generateRandomString(); assertEquals(KataSolution.sol(randomString),NumbersVsLetters.numbersVsLetters(randomString )); } private String generateRandomString() { StringBuilder randomString = new StringBuilder(); int finalStringLength =(int) (Math.random() * 100); for(int i=0;i<finalStringLength;i++) { int currentWordLength = (int) (Math.random() * 20); for(int j=0;j<currentWordLength;j++) { randomString.append(allChars.charAt((int)(Math.random() * 35))).append(" "); } } return randomString.toString().trim(); } }
- import static org.junit.jupiter.api.Assertions.*;
- import java.util.HashMap;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
- class Tests {
- final String allChars = "abcdefghijklmnopqrstuvwxyz1234567890";
- @Test
- void test() {
- assertEquals(1,NumbersVsLetters.numbersVsLetters("12345 98212"));
- assertEquals(1,NumbersVsLetters.numbersVsLetters("9999"));
- assertEquals(-1,NumbersVsLetters.numbersVsLetters("abcdefg"));
- assertEquals(-1,NumbersVsLetters.numbersVsLetters("zzzzzzz213 9ppppopppo2"));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("abcdef 12345"));
- }
- @Test
- void hiddenTest() {
- assertEquals(0,NumbersVsLetters.numbersVsLetters(""));
- assertEquals(0,NumbersVsLetters.numbersVsLetters(" "));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("a1"));
- assertEquals(1,NumbersVsLetters.numbersVsLetters("a1b2c3 d4e5f6"));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("a1b2c3 5y5"));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("a1b2c3 5y5"));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("5y5 a1b2c3 "));
- assertEquals(1,NumbersVsLetters.numbersVsLetters("9%$@"));
- assertEquals(-1,NumbersVsLetters.numbersVsLetters("~$@#a"));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("~$@#"));
- assertEquals(0,NumbersVsLetters.numbersVsLetters("0"));
- }
- @RepeatedTest(100)
- void randomTests() {
- String randomString = generateRandomString();
assertEquals(discover(randomString),NumbersVsLetters.numbersVsLetters(randomString ));- assertEquals(KataSolution.sol(randomString),NumbersVsLetters.numbersVsLetters(randomString ));
- }
- private String generateRandomString() {
- StringBuilder randomString = new StringBuilder();
- int finalStringLength =(int) (Math.random() * 100);
- for(int i=0;i<finalStringLength;i++) {
- int currentWordLength = (int) (Math.random() * 20);
- for(int j=0;j<currentWordLength;j++) {
- randomString.append(allChars.charAt((int)(Math.random() * 35))).append(" ");
- }
- }
- return randomString.toString().trim();
- }
public static int discover(String valor) {if(valor.isEmpty()||valor.isBlank()) return 0;HashMap<String, Integer> values = new HashMap<>();for(int i=0;i<26;i++) {values.put(Character.toString('a'+i), i+1);}boolean hasNumber = false;int score = 0, numberMultiplication = 1, characterSum = 0, stringPosition = 0;while(stringPosition<valor.length()) {boolean firstEmptySpace = true;while(String.valueOf(valor.charAt(stringPosition)).equals(" ") && stringPosition<valor.length()-1) {stringPosition++;if(firstEmptySpace) {firstEmptySpace = false;if(hasNumber)score+=Integer.compare(numberMultiplication, characterSum);else score--;}numberMultiplication =1;characterSum=0;hasNumber=false;}if(Character.isDigit(valor.charAt(stringPosition))) {numberMultiplication*=Character.getNumericValue(valor.charAt(stringPosition));hasNumber = true;}else if(Character.isAlphabetic(valor.charAt(stringPosition))) characterSum+=values.get(String.valueOf(valor.charAt(stringPosition)));if(stringPosition==valor.length()-1) {if(hasNumber)score+=Integer.compare(numberMultiplication, characterSum);else score--;}stringPosition++;}return Integer.compare(score, 0);}- }
Description
You will be given a String which will be composed of different groups of letters and numbers separated by a blank space. You will have to calculate for each group whether the value of the sum of the letters is greater, equal or less than the multiplication of the numbers. The value of each letter corresponds to its position in the alphabet so a=1, b=2, c=3 ... x=24, y=25 and z=26. If there are more groups that have a greater numeric value you will return 1. If the alphabetic value is greater for all groups, you will return -1. If there are the same number of groups with greater numeric and alphabetic values, you will return 0.
Example:
("12345 9812") -> 1
("abc def") -> -1
("abcdef 12345") -> 0 since the first group has a greater letter value and the second group has a greater numeric value
("a1b2c3") -> 0 since the sum of the letters (1(a)+2(b)+3(c)) is equal to the multiplication of the numbers (1*2*3)
Notes:
- There won't be any uppercase letters
- Special characters such as # or $ do not have any value
- There might be empty Strings
import java.util.HashMap; import java.util.Map; public class DiscoverTheValue{ public static int opt2(String s) { Map<String, Integer> map = new HashMap<>(); int result = 0,num=1,letr=0; boolean correcto= false; for (int i = 0; i < 26; i++) { map.put(Character.toString('a' + i), i + 1); } // map.forEach((a,b)->System.out.println(a+"-"+b)); String numeros = "", letras = ""; for (int c = 0; c < s.length(); c++) { if (!String.valueOf(s.charAt(c)).equals(" ") && s.length()!=c+1) { if (String.valueOf(s.charAt(c)).matches("^\\d$")) { //numeros = numeros + s.charAt(c); correcto=true; num=num*s.charAt(c); } else if (String.valueOf(s.charAt(c)).matches("^[a-z]$")) { //letras = letras + s.charAt(c); } } else { correcto=false; numeros="";letras=""; } } System.out.println(numeros); System.out.println(letras); return result; } //Segunda opcion a considerar public static int discover(String valor) { if(valor.isEmpty()||valor.isBlank()) return 0; HashMap<String, Integer> values = new HashMap<>(); for(int i=0;i<26;i++) { values.put(Character.toString('a'+i), i+1); } boolean hasNumber = false; int score = 0, numberMultiplication = 1, characterSum = 0, stringPosition = 0; while(stringPosition<valor.length()) { boolean firstEmptySpace = true; while(String.valueOf(valor.charAt(stringPosition)).equals(" ") && stringPosition<valor.length()-1) { stringPosition++; if(firstEmptySpace) { firstEmptySpace = false; if(hasNumber)score+=Integer.compare(numberMultiplication, characterSum); else score--; } numberMultiplication =1; characterSum=0; hasNumber=false; } if(Character.isDigit(valor.charAt(stringPosition))) { numberMultiplication*=Character.getNumericValue(valor.charAt(stringPosition)); hasNumber = true; } else if(Character.isAlphabetic(valor.charAt(stringPosition))) characterSum+=values.get(String.valueOf(valor.charAt(stringPosition))); if(stringPosition==valor.length()-1) { if(hasNumber)score+=Integer.compare(numberMultiplication, characterSum); else score--; } stringPosition++; } return Integer.compare(score, 0); } }
- import java.util.HashMap;
- import java.util.Map;
- public class DiscoverTheValue{
public static int hello(String s) {- public static int opt2(String s) {
- Map<String, Integer> map = new HashMap<>();
- int result = 0,num=1,letr=0;
- boolean correcto= false;
- for (int i = 0; i < 26; i++) {
- map.put(Character.toString('a' + i), i + 1);
- }
- // map.forEach((a,b)->System.out.println(a+"-"+b));
- String numeros = "", letras = "";
- for (int c = 0; c < s.length(); c++) {
- if (!String.valueOf(s.charAt(c)).equals(" ") && s.length()!=c+1) {
- if (String.valueOf(s.charAt(c)).matches("^\\d$")) {
- //numeros = numeros + s.charAt(c);
- correcto=true;
- num=num*s.charAt(c);
- } else if (String.valueOf(s.charAt(c)).matches("^[a-z]$")) {
- //letras = letras + s.charAt(c);
- }
- } else {
- correcto=false;
- numeros="";letras="";
- }
- }
- System.out.println(numeros);
- System.out.println(letras);
- return result;
- }
- //Segunda opcion a considerar
- public static int discover(String valor) {
- if(valor.isEmpty()||valor.isBlank()) return 0;
- HashMap<String, Integer> values = new HashMap<>();
- for(int i=0;i<26;i++) {
- values.put(Character.toString('a'+i), i+1);
- }
- boolean hasNumber = false;
- int score = 0, numberMultiplication = 1, characterSum = 0, stringPosition = 0;
- while(stringPosition<valor.length()) {
- boolean firstEmptySpace = true;
- while(String.valueOf(valor.charAt(stringPosition)).equals(" ") && stringPosition<valor.length()-1) {
- stringPosition++;
- if(firstEmptySpace) {
- firstEmptySpace = false;
- if(hasNumber)score+=Integer.compare(numberMultiplication, characterSum);
- else score--;
- }
- numberMultiplication =1;
- characterSum=0;
- hasNumber=false;
- }
- if(Character.isDigit(valor.charAt(stringPosition))) {
- numberMultiplication*=Character.getNumericValue(valor.charAt(stringPosition));
- hasNumber = true;
- }
- else if(Character.isAlphabetic(valor.charAt(stringPosition))) characterSum+=values.get(String.valueOf(valor.charAt(stringPosition)));
- if(stringPosition==valor.length()-1) {
- if(hasNumber)score+=Integer.compare(numberMultiplication, characterSum);
- else score--;
- }
- stringPosition++;
- }
- return Integer.compare(score, 0);
- }
- }
import java.util.HashMap; import java.util.Map; public class DiscoverTheValue{ public static int hello(String s) { Map<String, Integer> map = new HashMap<>(); int result = 0,num=1,letr=0; boolean correcto= false; for (int i = 0; i < 26; i++) { map.put(Character.toString('a' + i), i + 1); } // map.forEach((a,b)->System.out.println(a+"-"+b)); String numeros = "", letras = ""; for (int c = 0; c < s.length(); c++) { if (!String.valueOf(s.charAt(c)).equals(" ") && s.length()!=c+1) { if (String.valueOf(s.charAt(c)).matches("^\\d$")) { //numeros = numeros + s.charAt(c); correcto=true; num=num*s.charAt(c); } else if (String.valueOf(s.charAt(c)).matches("^[a-z]$")) { //letras = letras + s.charAt(c); } } else { correcto=false; numeros="";letras=""; } } System.out.println(numeros); System.out.println(letras); return result; } //Segunda opcion a considerar public static int discover(String valor) { if(valor.isEmpty()||valor.isBlank()) return 0; HashMap<String, Integer> values = new HashMap<>(); for(int i=0;i<26;i++) { values.put(Character.toString('a'+i), i+1); } boolean hasNumber = false; int score = 0, numberMultiplication = 1, characterSum = 0, stringPosition = 0; while(stringPosition<valor.length()) { boolean firstEmptySpace = true; while(String.valueOf(valor.charAt(stringPosition)).equals(" ") && stringPosition<valor.length()-1) { stringPosition++; if(firstEmptySpace) { firstEmptySpace = false; if(hasNumber)score+=Integer.compare(numberMultiplication, characterSum); else score--; } numberMultiplication =1; characterSum=0; hasNumber=false; } if(Character.isDigit(valor.charAt(stringPosition))) { numberMultiplication*=Character.getNumericValue(valor.charAt(stringPosition)); hasNumber = true; } else if(Character.isAlphabetic(valor.charAt(stringPosition))) characterSum+=values.get(String.valueOf(valor.charAt(stringPosition))); if(stringPosition==valor.length()-1) { if(hasNumber)score+=Integer.compare(numberMultiplication, characterSum); else score--; } stringPosition++; } return Integer.compare(score, 0); } }
public class pp{- import java.util.HashMap;
- import java.util.Map;
- public class DiscoverTheValue{
- public static int hello(String s) {
- Map<String, Integer> map = new HashMap<>();
- int result = 0,num=1,letr=0;
- boolean correcto= false;
- for (int i = 0; i < 26; i++) {
- map.put(Character.toString('a' + i), i + 1);
- }
- // map.forEach((a,b)->System.out.println(a+"-"+b));
- String numeros = "", letras = "";
- for (int c = 0; c < s.length(); c++) {
- if (!String.valueOf(s.charAt(c)).equals(" ") && s.length()!=c+1) {
- if (String.valueOf(s.charAt(c)).matches("^\\d$")) {
- //numeros = numeros + s.charAt(c);
- correcto=true;
- num=num*s.charAt(c);
- } else if (String.valueOf(s.charAt(c)).matches("^[a-z]$")) {
- //letras = letras + s.charAt(c);
- }
- } else {
- correcto=false;
- numeros="";letras="";
- }
- }
- System.out.println(numeros);
- System.out.println(letras);
- return result;
- }
- //Segunda opcion a considerar
- public static int discover(String valor) {
- if(valor.isEmpty()||valor.isBlank()) return 0;
- HashMap<String, Integer> values = new HashMap<>();
- for(int i=0;i<26;i++) {
- values.put(Character.toString('a'+i), i+1);
- }
- boolean hasNumber = false;
- int score = 0, numberMultiplication = 1, characterSum = 0, stringPosition = 0;
- while(stringPosition<valor.length()) {
- boolean firstEmptySpace = true;
- while(String.valueOf(valor.charAt(stringPosition)).equals(" ") && stringPosition<valor.length()-1) {
- stringPosition++;
- if(firstEmptySpace) {
- firstEmptySpace = false;
- if(hasNumber)score+=Integer.compare(numberMultiplication, characterSum);
- else score--;
- }
- numberMultiplication =1;
- characterSum=0;
- hasNumber=false;
- }
- if(Character.isDigit(valor.charAt(stringPosition))) {
- numberMultiplication*=Character.getNumericValue(valor.charAt(stringPosition));
- hasNumber = true;
- }
- else if(Character.isAlphabetic(valor.charAt(stringPosition))) characterSum+=values.get(String.valueOf(valor.charAt(stringPosition)));
- if(stringPosition==valor.length()-1) {
- if(hasNumber)score+=Integer.compare(numberMultiplication, characterSum);
- else score--;
- }
- stringPosition++;
- }
- return Integer.compare(score, 0);
- }
- }
import static org.junit.jupiter.api.Assertions.*; import java.util.HashMap; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; class Tests { final String allChars = "abcdefghijklmnopqrstuvwxyz1234567890"; @Test void test() { assertEquals(1,DiscoverTheValue.discover("12345 98212")); assertEquals(1,DiscoverTheValue.discover("9999")); assertEquals(-1,DiscoverTheValue.discover("abcdefg")); assertEquals(-1,DiscoverTheValue.discover("zzzzzzz213 9ppppopppo2")); assertEquals(0,DiscoverTheValue.discover("abcdef 12345")); } @Test void hiddenTest() { assertEquals(0,DiscoverTheValue.discover("")); assertEquals(0,DiscoverTheValue.discover(" ")); assertEquals(0,DiscoverTheValue.discover("a1")); assertEquals(1,DiscoverTheValue.discover("a1b2c3 d4e5f6")); assertEquals(0,DiscoverTheValue.discover("a1b2c3 5y5")); assertEquals(0,DiscoverTheValue.discover("a1b2c3 5y5")); assertEquals(0,DiscoverTheValue.discover("5y5 a1b2c3")); assertEquals(1,DiscoverTheValue.discover("9%$@")); assertEquals(-1,DiscoverTheValue.discover("~$@#a")); } @RepeatedTest(100) void randomTests() { String randomString = generateRandomString(); assertEquals(discover(randomString),DiscoverTheValue.discover(randomString )); } private String generateRandomString() { StringBuilder randomString = new StringBuilder(); int finalStringLength =(int) (Math.random() * 100); for(int i=0;i<finalStringLength;i++) { int currentWordLength = (int) (Math.random() * 20); for(int j=0;j<currentWordLength;j++) { randomString.append(allChars.charAt((int)(Math.random() * 35))).append(" "); } } return randomString.toString().trim(); } public static int discover(String valor) { if(valor.isEmpty()||valor.isBlank()) return 0; HashMap<String, Integer> values = new HashMap<>(); for(int i=0;i<26;i++) { values.put(Character.toString('a'+i), i+1); } boolean hasNumber = false; int score = 0, numberMultiplication = 1, characterSum = 0, stringPosition = 0; while(stringPosition<valor.length()) { boolean firstEmptySpace = true; while(String.valueOf(valor.charAt(stringPosition)).equals(" ") && stringPosition<valor.length()-1) { stringPosition++; if(firstEmptySpace) { firstEmptySpace = false; if(hasNumber)score+=Integer.compare(numberMultiplication, characterSum); else score--; } numberMultiplication =1; characterSum=0; hasNumber=false; } if(Character.isDigit(valor.charAt(stringPosition))) { numberMultiplication*=Character.getNumericValue(valor.charAt(stringPosition)); hasNumber = true; } else if(Character.isAlphabetic(valor.charAt(stringPosition))) characterSum+=values.get(String.valueOf(valor.charAt(stringPosition))); if(stringPosition==valor.length()-1) { if(hasNumber)score+=Integer.compare(numberMultiplication, characterSum); else score--; } stringPosition++; } return Integer.compare(score, 0); } }
- import static org.junit.jupiter.api.Assertions.*;
- import java.util.HashMap;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;// TODO: Replace examples and use TDD by writing your own tests- class Tests {
class SolutionTest {@Testvoid testSomething() {// assertEquals("expected", "actual");}- final String allChars = "abcdefghijklmnopqrstuvwxyz1234567890";
- @Test
- void test() {
- assertEquals(1,DiscoverTheValue.discover("12345 98212"));
- assertEquals(1,DiscoverTheValue.discover("9999"));
- assertEquals(-1,DiscoverTheValue.discover("abcdefg"));
- assertEquals(-1,DiscoverTheValue.discover("zzzzzzz213 9ppppopppo2"));
- assertEquals(0,DiscoverTheValue.discover("abcdef 12345"));
- }
- @Test
- void hiddenTest() {
- assertEquals(0,DiscoverTheValue.discover(""));
- assertEquals(0,DiscoverTheValue.discover(" "));
- assertEquals(0,DiscoverTheValue.discover("a1"));
- assertEquals(1,DiscoverTheValue.discover("a1b2c3 d4e5f6"));
- assertEquals(0,DiscoverTheValue.discover("a1b2c3 5y5"));
- assertEquals(0,DiscoverTheValue.discover("a1b2c3 5y5"));
- assertEquals(0,DiscoverTheValue.discover("5y5 a1b2c3"));
- assertEquals(1,DiscoverTheValue.discover("9%$@"));
- assertEquals(-1,DiscoverTheValue.discover("~$@#a"));
- }
- @RepeatedTest(100)
- void randomTests() {
- String randomString = generateRandomString();
- assertEquals(discover(randomString),DiscoverTheValue.discover(randomString ));
- }
- private String generateRandomString() {
- StringBuilder randomString = new StringBuilder();
- int finalStringLength =(int) (Math.random() * 100);
- for(int i=0;i<finalStringLength;i++) {
- int currentWordLength = (int) (Math.random() * 20);
- for(int j=0;j<currentWordLength;j++) {
- randomString.append(allChars.charAt((int)(Math.random() * 35))).append(" ");
- }
- }
- return randomString.toString().trim();
- }
- public static int discover(String valor) {
- if(valor.isEmpty()||valor.isBlank()) return 0;
- HashMap<String, Integer> values = new HashMap<>();
- for(int i=0;i<26;i++) {
- values.put(Character.toString('a'+i), i+1);
- }
- boolean hasNumber = false;
- int score = 0, numberMultiplication = 1, characterSum = 0, stringPosition = 0;
- while(stringPosition<valor.length()) {
- boolean firstEmptySpace = true;
- while(String.valueOf(valor.charAt(stringPosition)).equals(" ") && stringPosition<valor.length()-1) {
- stringPosition++;
- if(firstEmptySpace) {
- firstEmptySpace = false;
- if(hasNumber)score+=Integer.compare(numberMultiplication, characterSum);
- else score--;
- }
- numberMultiplication =1;
- characterSum=0;
- hasNumber=false;
- }
- if(Character.isDigit(valor.charAt(stringPosition))) {
- numberMultiplication*=Character.getNumericValue(valor.charAt(stringPosition));
- hasNumber = true;
- }
- else if(Character.isAlphabetic(valor.charAt(stringPosition))) characterSum+=values.get(String.valueOf(valor.charAt(stringPosition)));
- if(stringPosition==valor.length()-1) {
- if(hasNumber)score+=Integer.compare(numberMultiplication, characterSum);
- else score--;
- }
- stringPosition++;
- }
- return Integer.compare(score, 0);
- }
- }
We want to know wether there are more words which have a greater alphabetic value than numeric value, to do this we will sum the values of the letters and we will have to compare them with the result of the multiplication of the numbers.
If there are more words that have a greater alphabetic value we will return -1, if it's all the way around and there are more words which multiplication of their numbers is greater we will return 1 and if there are the same amount of words on both ends we will return 0.
To make this clear here are some examples:
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void hiddenTest() { assertEquals(0,""); assertEquals(0,"a1"); assertEquals(1,"a1b2c3 d4e5f6"); } @Test void randomTests() { String allChars = "abcdefghijklmnopqrstuvwxyz1234567890"; StringBuilder randomString = new StringBuilder(); for(int repetitions=0;repetitions<100;repetitions++) { randomString.setLength(0); int finalStringLength =(int) (Math.random() * 100); for(int i=0;i<finalStringLength;i++) { int currentWordLength = (int) (Math.random() * 20); for(int j=0;j<currentWordLength;j++) { randomString.append(allChars.charAt((int)(Math.random() * 35))).append(" "); } randomString.deleteCharAt(randomString.length()-1); } assertEquals("nuestroCodigo","codigoUsuario(randomString.toString())"); } } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
@Testvoid testSomething() {// assertEquals("expected", "actual");}- @Test
- void hiddenTest() {
- assertEquals(0,"");
- assertEquals(0,"a1");
- assertEquals(1,"a1b2c3 d4e5f6");
- }
- @Test
- void randomTests() {
- String allChars = "abcdefghijklmnopqrstuvwxyz1234567890";
- StringBuilder randomString = new StringBuilder();
- for(int repetitions=0;repetitions<100;repetitions++) {
- randomString.setLength(0);
- int finalStringLength =(int) (Math.random() * 100);
- for(int i=0;i<finalStringLength;i++) {
- int currentWordLength = (int) (Math.random() * 20);
- for(int j=0;j<currentWordLength;j++) {
- randomString.append(allChars.charAt((int)(Math.random() * 35))).append(" ");
- }
- randomString.deleteCharAt(randomString.length()-1);
- }
- assertEquals("nuestroCodigo","codigoUsuario(randomString.toString())");
- }
- }
- }
blablabla
public class pepe{
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
// TODO: Replace examples and use TDD by writing your own tests
class SolutionTest {
@Test
void testSomething() {
// assertEquals("expected", "actual");
}
}