Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tuple help!
#1
Complete beginner to all things programming, let alone python. I'm looking to numerous sources to help me out learning but I'm struggling big time. I think part of the reason is that I'm not reading/interpreting the questions correctly. Here is a tuple exercise I'm struggling with from the learning game CheckiO:

def easy_unpack(elements: tuple) -> tuple:

returns a tuple with 3 elements - first, third and second to the last

When I read this, based on what I've learned thus far, my thinking is:

"Three elements of the 1st, 3rd, and 2nd to last means -1, -3, -2."

Therefore my code is:

# easy_unpack((1, 2, 3, 4, 5, 6, 7, 9)) == (-1, -3, -2)
return (9, 6, 7)

This is wrong, however. Why?
Reply
#2
Elements are numbered starting from zero. So the first element of a tuple is element 0.

>>> t = ("first", "second", "third", "fourth", "fifth")
>>> t[0]
'first'
Then once you decide which ones are pulled out, you can put them in a new tuple and return that.

>>> new_tuple = (t[0], t[1], t[0])
>>> new_tuple
('first', 'second', 'first')
chiron likes this post
Reply
#3
Not a native English speaker, but
(Nov-21-2020, 04:24 AM)chiron Wrote: first, third and second to the last

would mean
  • first
  • third
  • second to the last
so, from (1, 2, 3, 4, 5, 6, 7, 9) I would expect (1, 3, 7)
It's up to you to figure out the correct indexes to us.
chiron likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
@bowlofred So we have to define each element separately that we want an output of?

@buran Embarrassing on my part Doh I read that as all three numbers from the last, hence why my original post contained an output of negative numbers.
Reply
#5
(Nov-21-2020, 05:12 PM)chiron Wrote: @bowlofred So we have to define each element separately that we want an output of?

That's one way to do it. You can also use slices if you need multiple elements that are defined in some repetitive way such aas "all between third and ninth" or "every third element after the seventh". But for 3 particular elements, yes a separate definition of each seems the way to go.
chiron likes this post
Reply
#6
I think the idea is to teach you that (i) indexes ate 0-based and (ii) [how] you can use negative indexes.
def easy_unpack(elements):
    return (elements[0], elements[2], elements[-1])

print(easy_unpack((1, 2, 3, 4, 5)))
Note that you may need to handle special cases like empty tuple or less than 3 elements in it, etc.
chiron likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
@buran I understood the index part with respect to starting at 0 if we're starting from the beginning of the index or at -1 if we're starting from the end.

But this is the second time I'm misreading/misunderstanding the question. The code you provided is the correct answer but I read it as them wanting us to provide a full index of integers and then deriving a specific output. So I guess I'm reading into it too much since the solution was much simpler than that.

Am I missing something with respect to how one thinks of programming questions? Is there a specific way I should be thinking/looking at problems and issues? Or is something like CheckiO just a bad way of learning?
Reply
#8
(Nov-21-2020, 10:19 PM)chiron Wrote: I read it as them wanting us to provide a full index of integers
I really don't understand what you mean by this.

the assignment is clear - they provide you with signature of a function easy_unpack that has parameter elements which is a tuple. It returns new tuple with just the first, third and second to last element of the tuple you pass as argument when you call that function. You need to implement that function.

For a more versatile function in may take second argument - tuple of indexes you want to get, in the order you want them. This way it will not have indexes it returns hard-coded. But this is not required for this assignment.

In my opinion check.io and similar are nice games/sites/riddles for practicing.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
@buran The way you break down the assignment is not at all how I read it which is why I think I'm not understanding/correctly reading the code in the instructions.

When I see this (the instructions):

def easy_unpack(elements: tuple) -> tuple:

returns a tuple with 3 elements - first, third and second to the last

I think:

# easy_unpack((1, 2, 3, 4, 5, 6, 7, 9)) == (1, 3, -2)

returns (2, 4, 7)

Which is apparently wrong. So as a complete beginner to anything/everything programming, is there a specific way that I'm supposed to read the code to properly understand the instructions? Your explanation of it and isolating the function and parameter made sense but not how my mind interpreted the instructions and this keeps happening.
Reply
#10
The first item in a list is not the item with index 1, it's the item with index 0. Index 1 is the second item.
chiron likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,727 Nov-04-2020, 11:26 AM
Last Post: Aggam
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 3,119 May-21-2019, 11:39 AM
Last Post: avorane

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020