Categories
Programming What I’m Up To

A more Pythonic approach to one of my “Capture the Flag” solutions

In my article about the Capture the Flag at The Undercroft in which I recently participated, I wrote about my solution to this particular challenge:

Your answer lies in the 1’s and 0’s…

0010111 00001111 00010111, 00011001 00001111 10101 00000001 00010010 00000101 00010010 00001001 00000111 00001000 00010100

(Make sure to use the comma, and spaces correctly)

The first part of my solution was turning those numbers into a list. Copy the numbers into a text editor, stick 0b in front of each one, and then turn the sequence into a Python list:

Paste the list into a Python REPL and then display its contents to see the numbers in decimal:

The next step is to convert those numbers into letters. Once again, the Unicode/ASCII value for “A” is 65, so the trick is to add 64 to each number and convert the resulting number into a character.

Here’s how I did that:

I could’ve gone super-functional and done it in one line:

Tap to find out more about lambda functions in Python.

Between lambda and map(), there’s a whole lot of functional programming concepts to solve a relatively simple problem.

I could write a whole article — and I probably should — based on just that single line of code, but in the meantime, I thought I’d post an easier, more Pythonic solution.

This simpler solution uses good ol’ list comprehensions:

Most programming languages don’t have list comprehensions. In those languages, if you want to perform some operation on every item in an array, you use a mapping function, typically named map(), but sometimes collect() or select().

Hence my original solution with lambda and map() — it’s force of habit from working in JavaScript, Kotlin, Ruby, and Swift, which don’t have Python’s nifty list comprehensions.

Leave a Reply

Your email address will not be published. Required fields are marked *