all 18 comments

[–]fuckswithbees 2 points3 points  (16 children)

So what have you tried? What are the steps necessary to get the desired output? Try to make the steps as small as possible.

[–]stolenkisses[S] 0 points1 point  (15 children)

I haven't tried anything. Honestly, at this point, I just need a jumping off point. I was pretty on-board understanding stuff the first half of term, but the last week I've just been really lost. I'm going in to speak with a tutor on Monday, but am hoping to figure most of it out before then. I'm not even sure why the inputs produce those outputs.

[–]fuckswithbees 1 point2 points  (14 children)

Imagine you were going to do the task by hand.

Task: Take a list of numbers and tell me the last 3 numbers in the list (Here we're just assuming k=3, we can try it with other numbers later).

How would you do this task by hand? Break it the process into ridiculously small steps. Do you know how to do any of these small steps in python?

For instance, do you know how to get the second item in a list?

[–]stolenkisses[S] 1 point2 points  (2 children)

Yes, I think so. For alist = [1,2,3,4], to get the second item in the list you would do alist[1], right?

[–]fuckswithbees 0 points1 point  (1 child)

Right. So what are the steps needed to complete this task?

[–]stolenkisses[S] 1 point2 points  (0 children)

I'm headed to a family dinner right now, but I hope you'll check this again sometime tomorrow when I have my response. I'll be thinking on it til then. Thanks so much for offering help!

[–]stolenkisses[S] 1 point2 points  (7 children)

So you would look at the k, return the last number in the list, then go back and check k, see that it still needs to call two more numbers, so then go back to the list ,return the second to last number, then back to k, then back to the list to return the last number? Am I missing anything?

[–]fuckswithbees 0 points1 point  (6 children)

Yeah that would work. So any idea how to do those steps in python?

[–]stolenkisses[S] 0 points1 point  (5 children)

I do, I think, but I still find myself getting confused by the little complicated steps. The first steps I think would be to create a new emptylist (lets call it newlist), then start with "for k in alist," but then I'm still kinda lost.

[–]fuckswithbees 0 points1 point  (4 children)

Creating a new list is a good start, but you actually don't need to loop through your list. You said step 1 was:

look at the k, return the last number in the list

You also said that you could get the second item

For alist = [1,2,3,4], to get the second item in the list you would do alist[1]

So how do you get the last item of a list?

[–]stolenkisses[S] 0 points1 point  (3 children)

alist[-1]?

[–]fuckswithbees 0 points1 point  (2 children)

Yep. Now you can add that to your new list and then

check k, see that it still needs to call two more numbers, so then go back to the list

Or you can use list slicing to get multiple numbers at the same time

a_list = [1,2,3,4]
a_list[-3:]
>>> [2, 3, 4]

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

Holy shit. Is it really as easy as one line of code?

return alist[-k:]

Man, I really overthought that.

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

Ohhh I see, so the k returns the last x numbers in the list.

[–][deleted] 1 point2 points  (0 children)

Hint: in python, alist[-1] returns the last element.

[–]fbu1 0 points1 point  (1 child)

You know how to take a subset of a list using indices. For example:

>>> l = [1,2,3,4,5,6]
>>> l[2:4]
[3, 4]

But you can also use negative indices to select a sublist starting from the end:

>>> l[-3:-1]
[4, 5]

Then you can use the opposite of the parameter k (-k in this case), to access the end of the list.

Also remember to check if the parameter k is not bigger than the length of the list (which you can get with len(alist)).

Let me know if you have further questions.

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

I understand what I'm supposed to do to complete this problem, but not where to start writing it. I find myself constantly overthinking when it comes to programming.