Python Forum
Tuple help! - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Tuple help! (/thread-31076.html)

Pages: 1 2


Tuple help! - chiron - Nov-21-2020

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?


RE: Tuple help! - bowlofred - Nov-21-2020

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')



RE: Tuple help! - buran - Nov-21-2020

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.


RE: Tuple help! - chiron - Nov-21-2020

@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.


RE: Tuple help! - bowlofred - Nov-21-2020

(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.


RE: Tuple help! - buran - Nov-21-2020

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.


RE: Tuple help! - chiron - Nov-21-2020

@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?


RE: Tuple help! - buran - Nov-22-2020

(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.


RE: Tuple help! - chiron - Nov-22-2020

@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.


RE: Tuple help! - bowlofred - Nov-23-2020

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.