Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1
Line 55 you are using == instead of = Is this falling short of one case?
8th Apr 2022, 12:42 PM
Jayakrishna 🇮🇳
+ 3
Manav Roy your solution exposes a hole in the Code Coach test cases. (There are many in Code Coach tasks!) This input should result in "quiet", but your solution gives false "ALARM": $xxGxTxxGxx The issue is that only the last guard position gets tested in your code. Apparently Code Coach tests don't cover this case.
8th Apr 2022, 1:44 PM
Brian
Brian - avatar
+ 3
Just adding my 2 cents, here is a solution that I used when I did not resort to regex or string manipulation: I used a finite state machine (essentially implementing the regex manually). Two states are of importance - MONEY, when the money symbol was read, and - THIEF, when a thief was encountered. A state transition MONEY-> THIEF or vice versa triggers an alarm. I am in the habit of naming the initial state IDLE. It shows the state the machine is in before it even started processing an input string. Encountering a guard sets the machine back to IDLE. We get the following state transitions: IDLE (x,G) -> IDLE IDLE (T) -> THIEF IDLE ($) -> MONEY THIEF (x,T) -> THIEF THIEF ($) -> "ALARM" THIEF (G) -> IDLE MONEY (x) -> MONEY MONEY (T) -> "ALARM" MONEY (G) -> IDLE where S(c) -> T means, "in state S, on char 'c' transition to state T". "ALARM" short-circuits the process to display ALARM. All states are valid end states. If the end of string has been reached, output "quiet". Just a different idea 🙂
10th Apr 2022, 9:06 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
Jackie oh, now I see how it works. In fact, the guard position is not even relevant. You could delete the line that finds g. Good job!
8th Apr 2022, 2:24 PM
Brian
Brian - avatar
+ 2
Brian Ended up using Jackie 's clever approach. Eliminate the x and check if T is in front or behind $. The guard is irrelevant. But there are those edge cases for empty casino, only the money, only the thief...things that could break the code. The c# code looks so much cleaner though. Rewritten code should be able to pass Brian's $xxGxTxxGxx test. This code is so janky. See Mohammed 's code on how to properly write a c++ code. https://code.sololearn.com/cqu5mc56c0ZW/?ref=app
10th Apr 2022, 9:52 AM
Bob_Li
Bob_Li - avatar
+ 2
Well, i did it this way to solve with C 🙂 I just did not want to present any codes at this time, but merely a different strategy. Interesting and educational, perhaps, especially for those who have never worked with finite state machines before 🙂 Bob_Li
10th Apr 2022, 11:04 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
Pardon my harshness in my critique, but your approach is pretty much a mess, and its correctness practically unprovable. Some issues: There are several code exit points, which makes it hard to trace, where execution ends There are assertion failures. (For example, CheckPreceeding asserts that CasinoArray[index] == '
#x27; on entry.) CheckPreceeding does not cover all possibilities. Fixing CasinoArray to size 8 may be a too conservative an estimate. Apart from that you cout 'quiet' with single quotes, and you have an assignment where you need to have a comparison.
13th Apr 2022, 5:08 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
"Could there be 2 money index?" Nope, I don't think so. Decreasing index by one makes it accidentally correct, I would say. Correcter it would be to pass the index as the function requires (adapt the calling code to the requirements of the callee, not the other way around). The remaining problem is, that CheckPreceeding does not cover all possibilities, as I had mentioned in my previous response.
13th Apr 2022, 6:47 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
Manav Roy I think your index adjustment to CheckPreceeding was what it needed most. I do not see the change updated in your Code Bits. Maybe also increase the size of CasinoArray as Ani Jona 🕊 indicated.
13th Apr 2022, 6:57 AM
Brian
Brian - avatar
+ 1
Approach is correct. But main point if it not working, is lies in how you finding index values?? Note : a<b and b<c then always a<c. No need to check..
8th Apr 2022, 12:28 PM
Jayakrishna 🇮🇳
+ 1
= is assignment operator == is comparison operator a=1 assigns 1 to a a==1 returns true if a=1 else return false.
8th Apr 2022, 1:09 PM
Jayakrishna 🇮🇳
+ 1
Yes. I guessed same but stange it passed all cases as per @Manav!! test cases may don't have this case.
8th Apr 2022, 1:49 PM
Jayakrishna 🇮🇳
+ 1
Jackie the OP was asking about C++, not C#. In C++, use string.find() method.
8th Apr 2022, 2:13 PM
Brian
Brian - avatar
+ 1
I know. but it helps knowing how it's done in another language usually when I struggle I see what the algorithm is in another language and it helps...C# can be one of the difficult languages
8th Apr 2022, 2:14 PM
Jackie
Jackie - avatar
+ 1
String.find and indexOf does the same thing you see that I'm getting at...
8th Apr 2022, 2:17 PM
Jackie
Jackie - avatar
+ 1
Jackie does your code pass the Code Coach tests? It has a logic hole that is similar to the OP's code. Yours only checks the first occurrance of guard position. There can be more than one guard.
8th Apr 2022, 2:17 PM
Brian
Brian - avatar
+ 1
😊 yip it pass the test
8th Apr 2022, 2:18 PM
Jackie
Jackie - avatar
+ 1
Evaluate a given floor of the casino to determine if there is a guard between the money and the thief, if there is not, you will sound an alarm. It clearly states to first check "if there is a guard between the money and the thief" so it wil be logical to check the first occurrence of the guard.. "if there is not, you will sound an alarm." There's other ways aswel like checking if guard is correct position then check if not but it a lot quicker to check it the guard isn't doing its job....
8th Apr 2022, 2:26 PM
Jackie
Jackie - avatar
+ 1
Ani Jona 🕊 That was very good approach. It never occurred to me to view the problem as a state machine. The short-circuiting part sounds interesting. So only the money and the thief triggers the alarm. But you still have to code the idea. Is there a procedure for that? I mean, a process of converting the concept and insight to code?
10th Apr 2022, 10:13 AM
Bob_Li
Bob_Li - avatar