(Coderbyte) Simple Symbols - Solução
Have the function SimpleSymbols(str) take the str parameter being passed and determine if it is an acceptable sequence by either returning the string true or false. The str parameter will be composed of + and = symbols with several letters between them (ie. ++d+===+c++==a) and for the string to be true each letter must be surrounded by a + symbol. So the string to the left would be false. The string will not be empty and will have at least one letter.
Solução:
import java.util.*;
import java.io.*;
class Function {
String SimpleSymbols(String str) {
final String LETRAS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < str.length(); i++) {
if (LETRAS.indexOf(str.charAt(i)) > -1) {
if (i == 0 || str.length() < 3) {
return "false";
}
if (str.charAt(i+1) != '+' || str.charAt(i-1) != '+') {
return "false";
}
}
}
return "true";
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
Function c = new Function();
System.out.print(c.SimpleSymbols(s.nextLine()));
}
}
Solução:
import java.util.*;
import java.io.*;
class Function {
String SimpleSymbols(String str) {
final String LETRAS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < str.length(); i++) {
if (LETRAS.indexOf(str.charAt(i)) > -1) {
if (i == 0 || str.length() < 3) {
return "false";
}
if (str.charAt(i+1) != '+' || str.charAt(i-1) != '+') {
return "false";
}
}
}
return "true";
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
Function c = new Function();
System.out.print(c.SimpleSymbols(s.nextLine()));
}
}
Comments
Post a Comment