"Produce the Second Largest Item From a List"
- 4 minutes read - 688 wordsSo, I’ve just discovered that my employer requires people in my role to be able to code. Now, I know that I can, obviously, although sometimes my computer disagrees.
One interesting technique I saw to help one maintain proficiency at coding is a technique called “Kata”. That is, practicing forms alone as a form of intense training. So, I thought, I’d have a go.
I picked an interview question to solve, which is “Given a list of integers, write a function to return the second largest integer in the list”. Not the most bonkers of challenges, but a fun one nonetheless. And so, I picked up a handy Python3 interpreter, and had at it.
First, create a list of (say) 10 integers:
>>> import random
>>> l = []
>>> for i in range(0,10):
l.append(randint(1,100))That gives me a list of 10 integers between 1 and 100. Awesome. And we’ve got an easy way to create another list should we need it. Now, my first thought was to write a for-next loop that iterated through the list and picked out the largest and second-largest numbers, but that didn’t seem very Pythonic. My second thoughts were that I could do whatever I wanted, so the easiest thing to do was to find the largest number in the list, which is easy, then delete it from the list, then find the new largest number in the list, and return that. That code looks like this:
def secondlargest(l):
l.remove(max(l))
return(max(l))But does it work? Let’s see:
>>> l
[77, 54,6, 87, 71, 58, 19, 97, 69, 30, 53]
>>> secondlargest(l)
87Awesome. Job done, Kata practiced. Look, it still works:
>>> secondlargest(l)
77WHAAAAAAT?
Oh heck. This isn’t great. What is going on here?
Well, OK. This is one of those weird things that I wish I’d thought about. Basically, because I’m passing a complex type, a list, Python3 passes it by value. Inside my function, I modify the list, but because the parameter is passed by value, I’m modifying the actual one that existed outside of my function, not a local copy. Hence I am changing the value passed in. Technically my function works, but I am trashing data that I do not own, which a function should not do.
So, what do I need to do? I had assumed that I was working with a copy of the list. But I’m not. I would have been if I’d passed in a simple type, like an integer. Instead, I need to explicitly make a copy of the list passed in. This isn’t as simple as an assignment:
>>> l2 = lThis will just create another variable pointing at the same memory.
Anything I do to l2 will happen to l as well. I need to use the
built-in copy() operator to make an actual copy.
So, my updated function looks like this:
def secondlargestb(l):
l2 = l.copy()
l2.remove(max(l2))
return(max(l2))But does this work?
So, I reset the list. I should see 87 as the result again:
>>> secondlargestb(l)
87
>>> secondlargestb(l)
87
>>> secondlargestb(l)
87 Excellent. I’m no longer clobbering the list, and this works. Woo hoo.
Of course, then I realised that I don’t need to actually delete an
element from the list. Instead, I could sort the list, using the
built-in sort() method, and then return the second to last element
from the list:
def secondlargest2(l):
l2 = l.copy()
l2.sort()
return(l2[-2])That code also works.
For me, this demonstrates that the Kata approach works. Of course I knew about passing by value and passing by reference, but I didn’t know how Python3 did it. So I’ve learned something I didn’t know before, and I’ve also thought about how to explain it.
One small detail — the copy() method only copies one level down. So if
I had a list of lists, only the outer list gets copied. If you need to
duplicate the whole thing, you need to do a deep copy instead. There’s
excellent documentation available on the Python3 docs
site. I
recommend it.
[Edit: 5/10/2020 — Typo]