dimanche 26 janvier 2020

Google Foobar Question - Python Test Case Failures

So yesterday evening I was lucky enough to get an invitation to the Google foobar challenge. Ironically, I wasn't looking up anything particularly advanced (just the basics of list comprehensions). Anyways, so I'm on level 3 now, and I'm running into an issue where my personal computer's compiler is providing an output that matches a test case, but Google's system fails it. Unfortunately, Google's system does not print out what the actual output is, so troubleshooting is a bit of a challenge.

Currently, I am running on python 3.6 in Spyder. I have no clue what version of python Google is using or whether there are any differences in how they are running theirs in comparison to mine.

The question is looking for something called a "lucky triple" in a list of positive integers. A lucky triple is one such that, for x, y, and z, y can divide into z, and x can divide into y.

My issue is basically this: One of the test cases is [1, 2, 3, 4, 5, 6], which is supposed to output the integer 3 (the three triplets are [1, 2, 4], [1, 2, 6], and [1, 3, 6]). When I run my program, it outputs 3 for this list, which is what the test case is supposed to equal, but I fail this test when I run it in Google's system.

A couple things I've noticed/tried:

  1. I fail 4 of 5 tests in Google's system. The one I pass is an input of [1, 1, 1], which outputs 1. Three of those five tests are hidden, so I cannot practice on them. The other failure is the [1, 2, 3, 4, 5, 6], which should output a 3.
  2. If I force the code to return 3 in all cases, then it passes that one test (and obviously fails the others). To me this means that my code is not returning 3 in Google's system, even though it is returning 3 in mine. It also means that the test case is correctly set to 3.
  3. This is not the first time something like this has happened. On the very first level, I used __ne__, which worked on my system, but not on Googles. I ended up changing things around because I suspected something like that was the case, and when I switched the __ne__ with a lambda function, all my test cases passed.
  4. I saw someone else had a similar issue with the foobar challenge, but their problem was they didn't make their variable a static variable so it was changing every time it was run. This should be a problem here, because I reset the count_of_numbers variable to 0 at every run.

I would rather not spoil the solution if I can help it, so instead of providing my whole code, I'm going to provide some of the functions I used. If this isn't enough to go on, please let me know, and I can provide the whole code. Remember, I'm not asking whether my program works - if my program doesn't work in all cases, that's a separate issue entirely. But I know that it works in the first two cases (at least on my machine). I'm asking why my program outputs 3 for my system but not 3 for Google's.

  1. I use %, which I believe works in both systems because I can force "3" using if 2%1 == 0: return 3.
  2. I create my own function, is_lucky_triple, that returns a Boolean.
  3. I use the len() function.
  4. I use reversed(range()).
  5. I use pop().

Overall, the program is really simple: only 16 lines of code and just a few functions. The basic logic is run through every possible combination of the numbers (maintaining the order of the original list), and increase a counter every time you find a lucky triple.

I really appreciate any help you can give. Any tips for troubleshooting when you can't see the output or error messages would be appreciated as well. Thanks!

Aucun commentaire:

Enregistrer un commentaire