Since we can permutate the cards in any order that we wanted and as long as it can spell “Bob” in the end, shouldn’t the below work? Basically, we just have to find the letter b, o, and b on 3 separate cards, whether on the top or the flip side of the card.
*** Edit: I also translate my code to python and compare to the example code and both code split out the same. However, when I submit my code in C, the system just did not accept it at all.
*** Edit: Nevermind :). I debug it and now know why my code was wrong. I forgot about the situation where the ob and be on one card. For example “obb” and “bbb”. That would make my code produce false even though there is an o, because my code would evaluate for the b first. Thank you for this discussion. Without it, I might never beable to debug the code.
*** Edit: OMG, I am so stupid, I can’t believe I forgot, what whould happen if “o” and “b” were together :(. All the code I posted earlier was wrong. The author’s code is the best for looping. The only method that is faster than that is a bit of a cheat. Nonetheless, I put the cheat code first. All the code below the cheat code is just for archiving.
My Correct Code, After Learning About o,b were together on the same line
def spellBob ( s, t ):
result = False
if (s[0] == 'o' or t[0] == 'o') and (s[1] == 'b' or t[1] == 'b') and (s[2] == 'b' or t[2] == 'b'):
result = True
elif (s[1] == 'o' or t[1] == 'o') and (s[0] == 'b' or t[0] == 'b') and (s[2] == 'b' or t[2] == 'b'):
result = True
elif (s[2] == 'o' or t[2] == 'o') and (s[1] == 'b' or t[1] == 'b') and (s[0] == 'b' or t[0] == 'b'):
result = True
return result
WRONG CODE FOR C
int main()
{
int c, i, ind;
char a[3], b[3];
scanf("%d",&c);
for (i = 0; i <= c; i++){
gets(a);
gets(b);
char m[] = "bob";
for ( ind = 0; ind < 3; ind++ ){
if ( a[ind] == m[0] || b[ind] == m[0] ){
m[0] = '1';
} else if ( a[ind] == m[1] || b[ind] == m[1] ){
m[1] = '1';
} else if ( a[ind] == m[2] || b[ind] == m[2] ){
m[2] = '1';
}
}
if ( m[0] == '1' && m[1] == '1' && m[2] == '1'){
printf("yes\n");
} else {
printf("no\n");
}
}
return 0;
}
WRONG CODE FOR Python. Note that the author’s code is right.
T = int(input())
for _ in range(T):
s = input()
t = input()
ok = False
for i in range(3):
if s[i] == 'o' or t[i] == 'o':
cnt = 0
for j in range(3):
if j != i:
if s[j] == 'b' or t[j] == 'b':
cnt += 1
if cnt == 2:
ok = True
print("yes" if ok else "no");
b1 = 'b'
o = 'o'
b2 = 'b'
for i in range(3):
if s[i] == b1 or t[i] == b1:
b1 = '1'
elif s[i] == o or t[i] == o:
o = '1'
elif s[i] == b2 or t[i] == b2:
b2 = '1'
bob = b1 + o + b2
print("yes" if bob == "111" else "no");
print"\n"
I test the following test case again them and they spat out the same result.
STDIN Below:
7
"nbo"
"bcn"
"ebo"
"bcn"
"nbo"
"bcn"
"oxe"
"nob"
"oxe"
"nbb"
"oxe"
"nbb"
"xoo"
"boo"
Wrong Code
b1 = 'b'
o = 'o'
b2 = 'b'
for i in range(3):
if s[i] == o or t[i] == o:
o = '1'
elif s[i] == b1 or t[i] == b1:
b1 = '1'
elif s[i] == b2 or t[i] == b2:
b2 = '1'
bob = b1 + o + b2
print("yes" if bob == "111" else "no");
print"\n"