menu
announcement

Spectrum is now read-only. Learn more about the decision in our official announcement.

Elementsofai

Elements of AI is a two part online course about AI and this community is for the participants of the course to discuss. Please join our new community at https://community.elementsofai.com/

Channels
Team

So you have to follow the example structure in order for tests to pass?

July 5, 2021 at 2:51pm

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 pt
import pandas as pd
portnames = ['PAN', 'AMS', 'CAS', 'NY', 'HEL']
rows = ['PAN', 'AMS', 'CAS', 'NY', 'HEL']
columns = ['PAN', 'AMS', 'CAS', 'NY', 'HEL']
co2 = 0.020
D = [
[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[0].startswith("PAN")]
return panamas
def main():
smallest = list()
panamas = permutations(portnames, len(portnames))
for i in panamas:
distance = df[i[0]][i[1]] + df[i[1]][i[2]] + df[i[2]][i[3]] + df[i[3]][i[4]]
waste = distance \* co2
smallest.append((i, waste))
smallest.sort(key=lambda x: x[1])
print(" ".join(smallest[0][0]), "%.1f kg" % smallest[0][1])
main()

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.
Hey (aay17ush)
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 NYC instead of NY. And you are right in assuming that its the output that counts, so fixing this should work :)
Edited
It's called "NY" in five-by-five distance table and "NYC" in the code.. :D Thank you, got it working now. :)
Oh thanks for pointing that out, will fix it. And glad the code is working now! :)
(aay17ush) Are you sure it's only the output that counts? Exercise 1:
import itertools
portnames = ["PAN", "AMS", "CAS", "NYC", "HEL"]
def permutations(ports):
perms = list(itertools.permutations(ports, len(ports)))
for p in perms:
if p[0] == "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
Edited
(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 ends
print(0)
# this will start the recursion with 0 as the first stop
permutations([0], 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.
Edited

July 7, 2021 at 8:37am
(aay17ush)
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.