So you have to follow the example structure in order for tests to pass?July 5, 2021 at 2:51pm (Edited 7 months ago)
I rewrote the whole thing and thought it's the result that matters. My code below works just fine but testing fails due to invalid format. :D
from itertools import permutations as ptimport pandas as pdportnames = ['PAN', 'AMS', 'CAS', 'NY', 'HEL']rows = ['PAN', 'AMS', 'CAS', 'NY', 'HEL']columns = ['PAN', 'AMS', 'CAS', 'NY', 'HEL']co2 = 0.020D = [[0,8943,8019,3652,10545],[8943,0,2619,6317,2078],[8019,2619,0,5836,4939],[3652,6317,5836,0,7825],[10545,2078,4939,7825,0]]df = pd.DataFrame(data=D, index=rows, columns=columns)def permutations(route, ports):perms = list(pt(route, ports))panamas = [name for name in perms if name.startswith("PAN")]return panamasdef main():smallest = list()panamas = permutations(portnames, len(portnames))for i in panamas:distance = df[i][i] + df[i][i] + df[i][i] + df[i][i]waste = distance \* co2smallest.append((i, waste))smallest.sort(key=lambda x: x)print(" ".join(smallest), "%.1f kg" % smallest)
July 6, 2021 at 6:57am
Hey (santtu), could you print your output here? I could take a look.
The reason we ask you to follow the format is because the testing framework assumes that format. Allowing for all possible formats leads to a lot of complexity in testing.
Output from the code will be this and the result should be correct.
PAN NY CAS AMS HEL 283.7 kg
I understand the code should follow the format the testing framework assumes. I didn't think that when erasing the premade functions. I assumed it's the output that counts but if it was, one could just write the desired output as code instead of real python syntax. :D
The testing framework assumes for loops instead of permutations from itertools?
(santtu) Got it! You should have
NY. And you are right in assuming that its the output that counts, so fixing this should work :)
(aay17ush) Are you sure it's only the output that counts? Exercise 1:
import itertoolsportnames = ["PAN", "AMS", "CAS", "NYC", "HEL"]def permutations(ports):perms = list(itertools.permutations(ports, len(ports)))for p in perms:if p == "PAN":print(" ".join(p))permutations(portnames)
Outputs this on run:
PAN AMS CAS NYC HEL PAN AMS CAS HEL NYC PAN AMS NYC CAS HEL PAN AMS NYC HEL CAS PAN AMS HEL CAS NYC PAN AMS HEL NYC CAS PAN CAS AMS NYC HEL PAN CAS AMS HEL NYC PAN CAS NYC AMS HEL PAN CAS NYC HEL AMS PAN CAS HEL AMS NYC PAN CAS HEL NYC AMS PAN NYC AMS CAS HEL PAN NYC AMS HEL CAS PAN NYC CAS AMS HEL PAN NYC CAS HEL AMS PAN NYC HEL AMS CAS PAN NYC HEL CAS AMS PAN HEL AMS CAS NYC PAN HEL AMS NYC CAS PAN HEL CAS AMS NYC PAN HEL CAS NYC AMS PAN HEL NYC AMS CAS PAN HEL NYC CAS AMS
But on test it returns this error: Test Failed: TypeError: permutations() takes 1 positional argument but 2 were given
Seems like test framework feeds my permutations function with 2 arguments even though I have one in my code. :D
(santtu) This is the template code you start out with:
portnames = ["PAN", "AMS", "CAS", "NYC", "HEL"]def permutations(route, ports):# write the recursive function here# remember to print out the route as the recursion endsprint(0)# this will start the recursion with 0 as the first stoppermutations(, list(range(1, len(portnames))))
Your function is defined differently which causes the error. I checked the instructions, and I see why you defined it that way - "Fix the permutations function so that it takes a list (the names of the ports) and prints out all the possible orderings of them."
I apologize for the incorrect statement and will correct it. However, it's also a good idea (in general) to copy the template code somewhere for later reference if you're overwriting it.
July 7, 2021 at 8:37am
It's defined differently, I wanted to use itertools as I normally would. You mentioned before that it's the output that counts, but seems like code also counts. I guess I'll have to follow the template.