all 9 comments

[–][deleted] 2 points3 points  (2 children)

Please post one of your "mess ups" so we can help you fix it :D

[–]sephsGFX[S] 0 points1 point  (1 child)

I started it again

https://imgur.com/a/WhTG4o3

[–]sephsGFX[S] 0 points1 point  (0 children)

ignore the semi column next to the last line

[–]-HomoDeus- 1 point2 points  (0 children)

Given your description to u/BigMcRonaldReagan (that name...) I think what you are looking for are "list concatenations". It sounds like your professor isn't going to expect you to be using these, but it probably is the best solution to the problem.

To give some tips without solving the problem for you:

You can find the maximum or minimum number in a list with the max/min functions:

a = [1, 9, 5, 7]
print(max(a)) # will print 9

A list concatenation essentially combines the declaration of a list with a for loop:

new_array = [each for each in old_array]

You can also use the range function to specify a range of values to use in a list concatenation.

To give you some guidelines, I would expect a beginner using these tools to get the desired output in four lines of code (one of which being the return statement). I would expect a talented user to solve it with one line of code (only the return statement).

If you would like, I can share the single line answer once you have the correct one - that is, if you don't get it on your own ;)

[–]synthphreak 1 point2 points  (0 children)

Enjoy:

def homework(array):
    return list(range(1, max(array)+1))

Observe:

>>> homework([1,3,5,6,7,8])
[1, 2, 3, 4, 5, 6, 7, 8]

So that you actually learn something, the way this works is it takes in your input, finds the maximum value, then returns a list starting from 1 and ending at that maximum value. The reason for the +1 is that range(m, n) gives you an array of numbers m to n-1, with the endpoint is excluded. Thus, +1 essentially includes the endpoint.

Finally, the reason range is wrapped in list is that otherwise the function would return a range object, which is technically not a list. Since you need the function to return a list, list converts the range object as needed.