I'm solving the next technical question (Q1): http://blog.sdeskills.com/qotd-2016-oct-17-resistance-is-futile/
It's almost done, just one task is pending. Evaluate if the input is balanced or not. Checking if parenthesis are in order, that's done, but not to evaluate the tokens.
In a given sub-network cannot have a mix of series / parallel
connections, so (500+200|300) is not allowed.
This is my current code: https://repl.it/EC3i/2 Any idea about how to evaluate the previous expression as wrong?
Try this. This code checks operator sereis and also balanced parentheses.
static boolean isBalanced(String s) {
Deque<Character> operators = new LinkedList<>();
operators.push('#');
for (int i = 0; i < s.length(); ++i) {
if (operators.isEmpty()) return false;
char ch = s.charAt(i);
switch (ch) {
case '(': operators.push('#'); break;
case ')': operators.pop(); break;
case '+':
switch (operators.peek()) {
case '#': operators.pop(); operators.push(ch); break;
case '+': break;
default: return false;
}
break;
case '|':
switch (operators.peek()) {
case '#': operators.pop(); operators.push(ch); break;
case '|': break;
default: return false;
}
break;
}
}
return operators.size() == 1;
}
And JUnit test codes.
#Test
public void testIsBalanced() {
assertTrue(isBalanced("(2)"));
assertTrue(isBalanced("(2+3+3)"));
assertTrue(isBalanced("2+3+3"));
assertTrue(isBalanced("2+(4|5|5)+3"));
assertTrue(isBalanced("2+(4|(2+3+4)|5)+3"));
assertTrue(isBalanced("(2)+3()"));
assertFalse(isBalanced("(2"));
assertFalse(isBalanced("(2))"));
assertFalse(isBalanced("((2)"));
assertFalse(isBalanced("2|3+3"));
assertFalse(isBalanced("2+(4|5+5)+3"));
assertFalse(isBalanced("2+3|3"));
}
Related
This question already has answers here:
In a switch statement, why are all the cases being executed?
(8 answers)
Closed last year.
I am working on the game where the columns of the board are represented by the characters but i would like to assign them an index.
I have decided to use the switch statement in that case, however it does produce the wrong result.
With the current code I attach, it gives me 14 as an index, however since the string is 7h, and it takes h as a char, it should give an index of 7. What Could be an issue? Thanks in advance!
public class Check {
public int columnToInt(char c) {
int index=0;
switch(c) {
case 'a':
index=0;
case 'b':
index=1;
case 'c':
index=2;
case 'd':
index=3;
case 'e':
index=4;
case 'f':
index=5;
case 'g':
index=6;
case 'h':
index=7;
case 'i':
index=8;
case 'j':
index=9;
case 'k':
index=10;
case 'l':
index=11;
case 'm':
index=12;
case 'n':
index=13;
case 'o':
index=14;
}
return index;
}
public static void main(String[] args) {
String myStr = "7h";
char c =myStr.charAt(1);
System.out.println("the char at position 1 is "+c);
Check check = new Check();
int result = check.columnToInt(c);
System.out.println(result);
}
}
Java switch statements can be a bit annoying to use. You need to use break or all the cases after the expected one will be executed as well.
switch(c) {
case 'a':
index=0;
break;
Alternatively you can use a return.
switch(c) {
case 'a':
return 0;
You must add the break keyword for each case.
For example:
case 'a':
index=0;
break;
otherwise next assignments are applied.
I'm writing a section of code for a Rock, Paper, Scissors game. I am writing a method that returns 1, 0 or -1 depending on wether the computer wins, it's a tie, or the user wins, respectively. I have this code so far:
private int nextPlay(char computerMove, char playerMove) {
switch (playerMove) {
case 'R': switch (computerMove) {
case 'R': return 0;
case 'P': return 1;
case 'S': return -1;
}
case 'P': switch (computerMove) {
case 'R': return -1;
case 'P': return 0;
case 'S': return 1;
}
case 'S': switch (computerMove) {
case 'R': return 1;
case 'P': return -1;
case 'S': return 0;
}
}
}
It throws a "Missing Return Statement" at me at the last bracket. Any suggestions?
P.S. the only options available for both computerMove and playerMove are R, P and S!
Others are telling you to add default to your switch statements. Not needed at all in this case, though it's a good general rule to follow.
However, you need to consider what should happen if playerMove and/or computerMove doesn't have one of the 3 expected values ('R', 'P', or 'S').
If computerMove doesn't, you'd want the logic flow to exit the outer switch statement, rather than fall through to the next case (though technically they'd all just fall through then, but still), so add a break in each outer case.
If that breaks out, or if playerMove doesn't have valid value, then logic flow gets to end of method, and there is no return statement there. That is your compilation error.
Best solution here, since you hopefully can't get into that situation, is to declare that to be exceptional, i.e. throw an Exception.
You code could be:
private int nextPlay(char computerMove, char playerMove) {
switch (playerMove) {
case 'R':
switch (computerMove) {
case 'R': return 0;
case 'P': return 1;
case 'S': return -1;
}
break;
case 'P':
switch (computerMove) {
case 'R': return -1;
case 'P': return 0;
case 'S': return 1;
}
break;
case 'S':
switch (computerMove) {
case 'R': return 1;
case 'P': return -1;
case 'S': return 0;
}
break;
}
throw new IllegalStateException("Oops! I messed up!!");
}
But it's better with more descriptive error messages:
private int nextPlay(char computerMove, char playerMove) {
switch (playerMove) {
case 'R':
switch (computerMove) {
case 'R': return 0;
case 'P': return 1;
case 'S': return -1;
}
throw new IllegalArgumentException("Invalid computer move: " + computerMove);
case 'P':
switch (computerMove) {
case 'R': return -1;
case 'P': return 0;
case 'S': return 1;
}
throw new IllegalArgumentException("Invalid computer move: " + computerMove);
case 'S':
switch (computerMove) {
case 'R': return 1;
case 'P': return -1;
case 'S': return 0;
}
throw new IllegalArgumentException("Invalid computer move: " + computerMove);
}
throw new IllegalArgumentException("Invalid player move: " + playerMove);
}
Now, you could add those throw statements in a default clause instead. Same result.
private int nextPlay(char computerMove, char playerMove) {
switch (playerMove) {
case 'R':
switch (computerMove) {
case 'R': return 0;
case 'P': return 1;
case 'S': return -1;
default: throw new IllegalArgumentException("Invalid computer move: " + computerMove);
}
case 'P':
switch (computerMove) {
case 'R': return -1;
case 'P': return 0;
case 'S': return 1;
default: throw new IllegalArgumentException("Invalid computer move: " + computerMove);
}
case 'S':
switch (computerMove) {
case 'R': return 1;
case 'P': return -1;
case 'S': return 0;
default: throw new IllegalArgumentException("Invalid computer move: " + computerMove);
}
default: throw new IllegalArgumentException("Invalid player move: " + playerMove);
}
}
my suggestion
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int x=nextPlay('R','P');
System.out.println(x);
}
private static int nextPlay(char computerMove, char playerMove)
{
if(playerMove=='R')
{
if(computerMove=='R')
return 0;
else if(computerMove=='P')
return 1;
else
return -1;
}
else if(playerMove=='P')
{
if(computerMove=='R')
return -1;
else if(computerMove=='P')
return 0;
else
return 1;
}
else
{
if(computerMove=='R')
return 1;
else if(computerMove=='P')
return -1;
else
return 0;
}
}
Thanks so much everyone! I decided to simply set one of the cases in each switch statement to default (keeping track of which char it is of course) and it resolved the issue.
I know this can cause some issues if the char is anything but the intended one, but my teacher says it is ok and I would use a different method (as suggested) next time!
As a part of my implementation I need to implement iterating over chars as efficient as possible. Here is a part of my source code that I wrote:
public int normalize(char s[], int len) {
for (int i = 0; i < len; i++) {
switch (s[i]) {
//numbers
case EN_D0:
case AR_D0:
s[i]= FA_D0;
break;
case EN_D1:
case AR_D1:
s[i]= FA_D1;
break;
case EN_D2:
case AR_D2:
s[i]= FA_D2;
break;
case EN_D3:
case AR_D3:
s[i]= FA_D3;
break;
case EN_D4:
case AR_D4:
s[i]= FA_D4;
break;
case EN_D5:
case AR_D5:
s[i]= FA_D5;
break;
case EN_D6:
case AR_D6:
s[i]= FA_D6;
break;
case EN_D7:
case AR_D7:
s[i]= FA_D7;
break;
case EN_D8:
case AR_D8:
s[i]= FA_D8;
break;
case EN_D9:
case AR_D9:
s[i]= FA_D9;
break;
//Symboles
case EN_QUESTION_MARK:
s[i]=FA_QUESTION_MARK;
break;
case EN_PERCENT_SIGN:
s[i]=FA_PERCENT_SIGN;
break;
case EN_DASH1:
case EN_DASH2:
case EN_DASH3:
case EN_DASH4:
s[i]=FA_DASH;
break;
case HAMZA_ABOVE:
len = delete(s, i, len);
i--;
break;
default:
break;
}
}
return len;
What is the most efficient way of doing such process? Please consider that I did not put all the conditions here because of it was around 600 different conditions. In addition to consider that this part of code should be run for huge documents that have tremendous amount of chars. So the efficiency really matters.
If all the constants in your case statements and assignments are chars, you can use an array to map source char to target char. The length of the array would be 2^16.
char[] map = new char[65536];
...
map[AR_D7] = FA_D7;
...
map[AR_D9] = FA_D9;
...
Then you loop becomes :
for (int i = 0; i < len; i++)
s[i] = map[s[i]];
The question at hand is each policy No. is a string of 9 characters of which indicates the type of insurance policy
B for building policy
C for Contents Policy
L for Life policy
V for car policy
Each of the remaining 8 characters of the policy number is a decimal digit.
I have tried using charAt but somebody told me there was a slightly better way
public String getpolicyNo(String initpolicyNo) {
/**
* Access method to find the first character of the policy to then
* determine what type of policy it is.
*/
String b = "B";
String C = "C";
String L = "L";
String V = "V";
char c1 = b.charAt(0);
char c2 = C.charAt(0);
char c3 = L.charAt(0);
char c4 = V.charAt(0);
if (c1 == 0) {
System.out.println("Its building" + c1);
return initpolicyNo;
} else {
if (c2 == 0) {
System.out.println("Its Content");
return initpolicyNo;
} else {
if (c3 == 0) {
System.out.println("Its life");
return initpolicyNo;
} else {
if (c4 == 0) {
System.out.println("Its car");
return initpolicyNo;
}
}
}
}
throw new IllegalArgumentException();
}
I'm not looking for you to provide an answer for me, I'm just looking for any possible alternatives and possible suggestions.
Many thanks
Dan
I'm not really sure what you are trying to achieve, but I would write this way:
public String getpolicyNo(String initpolicyNo) {
switch(initPolicyNo.charAt(0))
{
case 'B':
System.out.println("Its building B");
return initpolicyNo;
case 'C':
System.out.println("Its building C");
return initpolicyNo;
case 'L':
System.out.println("Its building L");
return initpolicyNo;
case 'V':
System.out.println("Its building V");
return initpolicyNo;
}
throw new IllegalArgumentException();
}
I recommend you using an Enum. Each value for an Enum has an ordinal value, which you can sort by.
If you want to read an introduction to Enums, the following site will help: Enum Types - The Java Tutorials
enum Policy {
BUILDING_POLICY, CONTENTS_POLICY, LIFE_POLICY, CAR_POLICY
}
For each enum value, you can assign a custom value, with which you can sort.
public void whatItIs(String s){
if(s.length() < 1){
//-- nothing to see here ---
return;
}
//-- case insensitive --
char c = Character.toUpperCase(s.charAt(0));
switch(c){
case 'B':
//-- its a building !--
break;
case 'C':
//-- its a c........ !--
break;
case 'L':
//-- its a l........ !--
break;
case 'V':
//-- its a v........ !--
break;
default:
//-- its something else :(--
}
}
public String getpolicyNo(String initpolicyNo) {
switch (initpolicyNo.charAt(0)) {
case 'B':
System.out.println("Its building" );
break;
case 'C':
System.out.println("Its Content");
break;
case 'L':
System.out.println("Its life");
break;
case 'V':
System.out.println("Its car");
break;
default:
throw new IllegalArgumentException();
break;
}
return initpolicyNo;
}
You could add enums to your code, or you could use a switch statement instead of the if/else block you currently have:
char type = initpolicyNo.charAt(0);
switch (type) {
case 'B':
// do stuff
break;
case 'C':
// do stuff
break;
}
Note: I think you may have a problem in your code in that it seems to me you should be switching on the initpolicyNo, not the actual types?
I have tried using charAt but somebody told me there was a slightly
better way
there is no better way to get the first letter of a string than using charAt.
And rather than having lots of if's/switches and what not, get a bit more OOPs up side your head.
Map<Character, String> myMap = new HashMap<Character, String>() {{
put('B', "Its building");
put('C', "Its content");
put('L', "its life");
put('V', "its vehicle");
}} ;
public void iShouldBeDoingMyOwnWork(String initpolicyNo) {
System.out.println(myMap.get(initpolicyNo.charAt(0)));
}
UPDATE:
The final version of my utility looks like this:
StringBuilder b = new StringBuilder();
for(char c : inLetters.toLowerCase().toCharArray())
{
switch(c)
{
case '0': b.append("0"); break;
case '1': b.append("1"); break;
case '2': case 'a': case 'b': case 'c': b.append("2"); break;
case '3': case 'd': case 'e': case 'f': b.append("3"); break;
case '4': case 'g': case 'h': case 'i': b.append("4"); break;
case '5': case 'j': case 'k': case 'l': b.append("5"); break;
case '6': case 'm': case 'n': case 'o': b.append("6"); break;
case '7': case 'p': case 'q': case 'r': case 's': b.append("7"); break;
case '8': case 't': case 'u': case 'v': b.append("8"); break;
case '9': case 'w': case 'x': case 'y': case 'z': b.append("9"); break;
}
}
return builder.toString();
ORIGINAL QUESTION:
I'm taking on the simple task of converting an alphanumeric phone number to a string of digits. For example, 1-800-HI-HAXOR would become 1-800-44-42967. My initial attempt was to create a nasty switch statement, but I'd love a more elegant, and efficient solution. Here's what I've got:
for(char c : inLetters.toLowerCase().toCharArray())
{
switch(c)
{
case '0': result+="0"; break;
case '1': result+="1"; break;
case '2': case 'a': case 'b': case 'c': result+="2"; break;
case '3': case 'd': case 'e': case 'f': result+="3"; break;
case '4': case 'g': case 'h': case 'i': result+="4"; break;
case '5': case 'j': case 'k': case 'l': result+="5"; break;
case '6': case 'm': case 'n': case 'o': result+="6"; break;
case '7': case 'p': case 'q': case 'r': case 's': result+="7"; break;
case '8': case 't': case 'u': case 'v': result+="8"; break;
case '9': case 'w': case 'x': case 'y': case 'z': result+="9"; break;
}
}
Thanks!
The switch statement is not really that bad. Your algorithm is linear with respect to the length of the phone number. The code is readable and pretty easy to verify by inspection. I wouldn't mess with it, except to add a default case for handling errors. (I'm not a Java programmer, so forgive me if it's called something else.)
If you have to make it faster, a pre-initialized table indexed by character would avoid any comparisons beyond basic error checking. You could even avoid the case conversion by duplicating the values in the table (digit['A'] = digit['a'] = "2";). The cost of initializing the table would be amortized over the total number of conversions.
You could do this using the Apache Commons Lang StringUtils, as follows:
String output = StringUtils.replaceChars(StringUtils.lowerCase(input),
"abcdefghijklmnopqrstuvwxyz",
"22233344455566677778889999");
Assuming speed is not your main concern, of course, and you want a compact solution ;)
Use a Map, where the keys are the letters and digits, and the value is the number on the keypad. (So each keypad number will be indexed by three or four letters and one digit).
Map<Character, Character> keypad = new HashMap<Character, Character>();
...
StringBuilder buf = new StringBuilder(inLetters.length());
for (int idx = 0; idx < inLetters.length(); ++idx) {
Character ch = keypad.get(inLetters.charAt(idx));
if (ch != null)
buf.append(ch);
}
Update: I was curious whether a hand-coded lookup table would perform better than a dense set switch cases. In my casual testing, I found the following code to be the fastest I could come up with:
private static final char[] lut =
"0123456789:;<=>?#22233344455566677778889999[\\]^_`22233344455566677778889999".toCharArray();
private static final char min = lut[0];
String fastest(String letters)
{
int n = letters.length();
char[] buf = new char[n];
while (n-- > 0) {
int ch = letters.charAt(n) - min;
buf[n] = ((ch < 0) || (ch >= lut.length)) ? letters.charAt(n) : lut[ch];
}
return new String(buf);
}
Surprisingly, it was more than twice as fast as similar code using a switch statement (which compiled to a tableswitch instruction). This was just for fun, mind you, but on my laptop, running in a single thread, I could convert 10 million 10-letter-"numbers" in about 1.3 seconds. I was really surprised, because as I understand it, a tableswitch operates in essentially the same way, but I expected it to be faster since it is a JVM instruction.
Of course, unless I were getting paid only for each of a limitless supply of phone numbers I could convert, I would never write code like this. A switch is much more readable, performs well as-is, and is likely to get a free performance boost in some future JVM.
Far and away, the greatest improvement to the original code comes from using a StringBuilder instead of concatenating strings, and that does nothing to impair readability of the code. Using charAt instead of converting the input to a char[] also makes the code simpler and easier to understand and improves performance too. Finally, appending char literals instead of String literals ('1' rather than "1") is a performance improvement that aids readability a little too.
How about simply:
String convert(String inLetters) {
String digits = "22233344455566677778889999";
String alphas = "abcdefghijklmnopqrstuvwxyz";
String result = "";
for (char c : inLetters.toLowerCase().toCharArray()) {
int pos = alphas.indexOf(c);
result += (pos == -1 ? c : digits.charAt(pos));
}
return result;
}
If you want a solution that doesn't force you to enumerate all of the letters, you could do something like:
char convertedChar = c;
if (Character.isLetter(c)) {
//lowercase alphabet ASCII codes: 97 (a)-122 (z)
int charIndex = ((int)c) - 97;
//make adjustments to account for 's' and 'z'
if (charIndex >= 115) { //'s'
charIndex--;
}
if (charIndex == 121) { //'z'-1
charIndex--;
}
convertedChar = (char)(2 + (charIndex/3));
}
result += convertedChar;
If you run this 10^9 times in a tight loop and ctrl-break it a few times, my bet is that nearly every time it will be deep in the string class trying to accomplish one of those innocent-looking "+=" operators.
Switch statements get compiled to a similar form as if-else statements, (each case statement is essentially an if (c == '...') test in disguise) so although this is visually more compact than cascading if's that test for each character, there may or may not be any real performance benefit.
You can potentially streamline it by eliminating some of the comparisons. The key is that char is an integer type (which is why you can switch on a char) so you can use numeric comparison operators. and 'aAssuming your inLetters string only contains alphanumeric characters, this should work... (All other characters will pass through unchanged.)
String result = "";
for (char c : letters.toLowerCase().toCharArray()) {
if (c <= '9') result += c;
else if (c <= 'c') result += "2";
else if (c <= 'f') result += "3";
else if (c <= 'i') result += "4";
else if (c <= 'l') result += "5";
else if (c <= 'o') result += "6";
else if (c <= 's') result += "7";
else if (c <= 'v') result += "8";
else if (c <= 'z') result += "9";
else result += c;
}
The characters of interest have the hexadecimal values: '0' = 0x30, '9' = 0x39, 'a' = 0x61, and 'z' = 0x7a.
Edit: It's better practice to use a StringBuilder and append() to create the string, but for small strings it's not likely to be appreciably faster. (Amdahl's Law demonstrates that the actual speedup you can expect from optimizing code is limited by the percentage of time actually spent in that code.) I only used concatenated strings to make the algorithm clear to the OP.