I'm failing a hidden test case. What am I missing. | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

I'm failing a hidden test case. What am I missing.

The task description is in the linked code. I am struggling to figure out what possibility I'm missing. I thought I had accounted for the possibility of multiple instances of money to guard but that's the only thing I could think of. https://code.sololearn.com/cI36xgphC8D1/?ref=app

6th Mar 2023, 3:31 AM
Malachite
Malachite - avatar
15 Réponses
+ 5
That is actually an interesting mistake. Look at the following line 35: for( long unsigned int i = temp; i >= 0; i-- ) { ... } Let this sink in a bit, and now answer the following question: Since the loop terminating condition is for i to become negative, how is this ever gonna happen if the type is unsigned? There are other variables of unsigned types, e.g. size_t, to which you assign values which might be negative. Go through your code again under these aspects. In addition, you may access the string outside its bounds in the second for loop ("i <= len").
6th Mar 2023, 6:54 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 3
This is overcomplicated, read the challenge description, there are one or more guards, but only one thief and only one $. I removed all the Xs from the string, not required. Regardless of the number of Gs there are only 4 tests: G-T-$, T-G-$, $-T-G and $-G-T. Last tip, if the Xs are removed you can tell straight away if T is right next to $, in which case it doesn't matter where the Gs are => alarm.
7th Mar 2023, 12:02 AM
Alvaro Vallejos
Alvaro Vallejos - avatar
+ 2
Try to find position of <money> and <thief> first. Next check whether there is a guard within the range between <money> and <thief>. When there is none, sound "ALARM"
6th Mar 2023, 5:56 AM
Ipang
+ 2
Yeah I wrote it so that it would accommodate the possibility of an unknown number of instances of $. My code passed with a quick change from size_t to ssize_t. But in hindsight I would definitely remove all other non-essential characters and use str.find() function to search for the 2 sequences that trigger the alarm. The T$ or $T. Anyway thank you everyone for the help. I appreciate it all as it helps to learn ways to finess code and simplify it.
7th Mar 2023, 1:45 AM
Malachite
Malachite - avatar
+ 2
Ah, then in line 11 you perhaps wanted x instead of 'x' :)
7th Mar 2023, 6:02 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
You may also attempt a finite state machine solution. It makes possible a single pass with an arbitrary number of money, thieves, and guards.
7th Mar 2023, 6:04 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 1
Divide and conquer, where there are many spots of money and thieves. Check for <guard> in each range between <money> and <thief>. Sound the alarm when any of the ranges is insecure (no guard in between). I have a working code based on that idea I explained earlier. Hook me up if you want to look, I'll attach its link here ...
6th Mar 2023, 6:20 AM
Ipang
+ 1
Line 10 seems redundant (abandoned idea?), and conditions 15 and 19 can be ORed together.
7th Mar 2023, 5:50 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
0
Have you considered the possibility where there could be more than one "G" in the string? The code tries to find first position of "G", but what if there is a "G" between the "
quot; and the "T" but it's not the first "G"?
6th Mar 2023, 5:48 AM
Ipang
0
It's fine with more than one G. There should be at least one or more G's between T and a $ symbol
6th Mar 2023, 5:51 AM
Malachite
Malachite - avatar
0
The algorithm for that seems complicated for me if there is many locations of money and thieves
6th Mar 2023, 5:58 AM
Malachite
Malachite - avatar
0
This was my new version based on things I realized from what you've mentioned
7th Mar 2023, 2:19 AM
Malachite
Malachite - avatar
0
No I just wanted line 10 to be written as such. I wanted that character to be adaptable. But yes in this specific video it's redundant.
7th Mar 2023, 5:56 AM
Malachite
Malachite - avatar
0
Yeah you were correct Ani. The 'x' was supposed to just be x
7th Mar 2023, 4:09 PM
Malachite
Malachite - avatar