Python Forum

Full Version: Google Foobar- Code works in my IDE but not in foobar. Static method?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello!

I am doing one of the Google foobar challenges and my code returns the right values in my IDE, but not in foobar. I'm new to python and think I'm missing something on how foobar reads the code or calls the function. Help is much appreciated!

Here's my code:

def solution(area):
    totalarea=int(area)
    sq1calc=abs(totalarea ** 0.5)
    sq1 = int(sq1calc)
    sq1area = sq1 ** 2
    sq2calc=abs((totalarea - sq1area) ** 0.5)
    sq2 = int(sq2calc)
    sq2area = sq2 ** 2
    sq3calc=abs((totalarea - sq1area - sq2area) ** 0.5)
    sq3 = int(sq3calc)
    sq3area = sq3 ** 2
    sq4calc=abs((totalarea - sq1area - sq2area - sq3area) ** 0.5)
    sq4 = int(sq4calc)
    sq4area = sq4 ** 2
    print(sq1area,sq2area,sq3area,sq4area, sep=",")
solution()
Two test case input/outputs are provided and my code runs these fine in my IDE.

Test cases:
Input:
solution.solution(15324)
Output:
15129,169,25,1

Input:
solution.solution(12)
Output:
9,1,1,1

I've read that foobar used the 'static method' but I'm not quite sure what that means or how it applies. No error information is provided by foobar.

As a note, if I enter this into foobar, it says Test 2 passes:

def solution(area):
   print ("""9,1,1,1""")
What is line 16 doing? As solution requires a single argument, that line should generate an error.

I would normally expect the function to return data rather than print it. But if that fake one works with just the print statement, I guess they just want the printout.

How do you know that your code is returning different values in foobar? Is it possible that the code is doing the same thing in both foobar and your local system, but that foobar is checking an edge case and thinks the answer is wrong?
(Feb-20-2021, 04:05 AM)bowlofred Wrote: [ -> ]What is line 16 doing? As solution requires a single argument, that line should generate an error.

I would normally expect the function to return data rather than print it. But if that fake one works with just the print statement, I guess they just want the printout.

How do you know that your code is returning different values in foobar? Is it possible that the code is doing the same thing in both foobar and your local system, but that foobar is checking an edge case and thinks the answer is wrong?

Thank you for reading!

Foobar shows the result for each test, the two provided test cases and eight other hidden cases, like below:

Test 1 failed
Test 2 passed!
Test 3 failed [Hidden]
Test 4 failed [Hidden]
Test 5 failed [Hidden]
Test 6 failed [Hidden]
Test 7 failed [Hidden]
Test 8 failed [Hidden]
Test 9 failed [Hidden]
Test 10 failed [Hidden]

So using my code, I know at least Test 1 and 2 should pass.

Line 16 "Solution()" was for testing in my IDE. I input 12 or 15324 in the () to check output. I excluded line 16 in foobar.

How do I use a return statement with , separators between the numbers?
What is the required result of the test? Is it just supposed to print information to stdout? In any particular format? Can you get the first test to pass as well as the second?

If you wanted to return a string, then something like

return ",".join(sq1area,sq2area,sq3area,sq4area)
Or a tuple
return sq1area,sq2area,sq3area,sq4area
But if test2 is passing, the return value doesn't seem to be necessary.
Quote:
def solution(area):

What if you don't pass a parameter? Or what if they pass None, a float, a string?

If you're sometimes passing the test case, then either:
a) that's an accident, and you probably should have failed them all, or
b) the input doesn't match what you think it should be.