Alan Turing's Legacy, part VIII

Alan Turing's Legacy, part VIII

So if you're new to this blog I am obsessed with the song off the "Imitation Game"'s soundtrack, Alan Turing's Legacy. 

It's the one off the ending and just before the closing credits.

I have named seven blog posts after this and so this is the 8th.

On Saturday I went to Bletchley Park for the 3rd time this year with my childhood best friend. It was so amazing to be there again.

To feel his energy, to feel his presence, and to feel so close to him.

I have already shared this photo on LinkedIn, but I cannot resist but share this photo:

Susanna, a woman aged 30 with white skin and long blonde hair, stands in front of a photo of Alan Turing, smiling. They are both smiling and gazing in the same direction.
A photo of me in Hut 11 at Bletchley Park. Sadly I did not make it into his office in Hut 8 but that just means I'll have to go again.

Midnight Python Problems

Please pretend you can't see what time I am doing this.

I solved a problem. 

It was so hard. I kept getting it wrong.

There's loads of weird list sorting stuff in Python that I didn't know about, that I didn't understand, oh my god. Anyway, I tried. Here is my solution.

Descending Order - this link here

This problem seemed so easy but it wasn't; it was deceptively tricky. 

Arrrgh! Thanks.

def descending_order(num: int) -> int:
    num_list = [int(digit) for digit in str(num)]
    num_list.sort(reverse=True)
    return int(''.join(map(str, num_list)))
Gross! As always.

But I am getting better so that's not fair and that's not true.

I have had two "top" solutions in the last few days.

And I am getting better at one-line fancy solutions in Python.

What does this code do?

This code:
  • firstly it creates a list num_list by calling the LIST COMPREHENSION METHOD:
  • it takes num and converts it into a string
  • for every digit in that string, it converts the digit back into an integer
  • and then it creates a list of these integers
okay so that is like the first toaster or the first line of the function body
so what does the next line of the function body or the next toaster do then please? thanks
  • it takes the num_list and it sorts it
  • sort takes a list and sorts it in order
  • the optional parameter reverse=True sorts the list in DESCENDING order
and the third line/third toaster? 
  • it returns our final value
  • but what is that final value please? thanks
  • we join a value with no spaces
  • but what is it insidexjoin() brackets please? ARRRRRRRRGH
  • ah yes it maps the list, item by item, every item, one item at a time, to a string
  • and then outside of the join() method we wrap it all in an int()
I have used map() a lot in JavaScript before but I am not sure if this is the same kind of map() at all or not! I swear map is an iterator in JS - here too I guess - but map is normally called on something in JavaScript isn't it - is it different in Python???!

What was the other top solution?

There was only one really cool one I think - here it is.
def Descending_Order(num):
    return int("".join(sorted(str(num), reverse=True)))
So what does this one do then here please? Thanks. This super cool one liner omg...
  • it ultimately converts everything back into an integer
  • but what happens in those brackets?
  • we join something - but what do we join?
  • we join the outcome of the sorted method - with reverse=True, so that the order of the sorting is DESCENDING!!!!
  • but what is it that we sort???!
  • OMG I AM AN IDIOT
  • MY CODE KEPT ERRORING BECAUSE I WOULD USE SORT() INSTEAD OF SORTED() - SORT() SORTS A LIST IN PLACE - SORTED() RETURNS A NEW LIST!!!
  • So we sort the string version of num
  • So the data type goes int -> string -> list -> string -> int
  • WOW - THIS IS SO SO COOL - WOW I LOVE IT, I LOVE IT, I LOVE IT, WOW

Comments

Popular posts from this blog

Hello World

Yosemite

Two Lines of Thought, Makes Me Wonder What I'm Missing