## The Midlife Geek

### Ramblings of a middle aged engineer, runner and open source enthusiast

What a minefield. If you haven’t seen it, there’s a great talk from David Beazley on YouTube discussing threads, event loops and co-routines. It’s worthwhile just to see someone give a presentation on coding live with Emacs.

I’ve been spending time with Python recently and am beginning to really like some of the language’s features.

List comprehension (listcomp) creates a list by evaluating an expression on each item in a given list, from left to right.

It combines and expression and a loop:

>>> [ord(letter) for letter in 'example']
[101, 120, 97, 109, 112, 108, 101]

Apply a condition:

>>> [ord(letter) for letter in 'example' if ord(letter) < 112]
[101, 97, 109, 108, 101]

It’s useful for combining lists:

>>> [(letter, number) for letter in 'ab' for number in '12']
[('a', '1'), ('a', '2'), ('b', '1'), ('b', '2')]

Pimoroni’s Rainbow Hat introduction has an example to cycle colours on the LED rainbow:

for i in range(101):
h = i / 100.0
r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(h, 1.0, 1.0)]
rh.rainbow.set_all(r, g, b)
rh.rainbow.show()

This is a little hard to follow but we’ll break it down. Hue, Saturation and Value (HSV) represents colour using three values between 0.0 and 1.0 creating a colour “wheel” that is easy to cycle on the LED. Unfortunately the LED combines red, green and blue. The Colorsys library can convert between the two.

The expression is int(c*255). The loop is for c in colorsys.hsv_to_rgb(h, 1.0, 1.0)

The h=i/100 is giving a range of values from 0.0 to 1.0 in 0.01 steps (we could use a list comprehension too [i/100 for i in range(101)]).

So let’s look at a snapshot, where h=1.3:

>>> colorsys.hsv_to_rgb(0.13,1.0,1.0)
(1.0, 0.78, 0.0)

Which the list comprehension converts to:

>>> [int(c*255) for c in colorsys.hsv_to_rgb(0.13, 1.0, 1.0)]
[255, 198, 0]

Giving us the RGB value needed.

With a bachelors completed, a sense of “what now” quickly settles in and a masters degree is a logical next step. After graduation, the Open University (OU) was quick to respond to tweets:

Struggled to get my head around Euler problem 15:

Starting at the top left of a 20×20 grid and only moving right and down, how many routes are there?

I figured that this was a combinatorics problem but couldn’t see what to count or choose from.

I recently gave up Facebook and wanted to remove Facebook’s app. For me Facebook has become too much of a trade for privacy. Given that change in trust, it annoys me not having the option to remove the app on Samsung.

Enable Android debugging and install Android Debug Bridge (ADB) tools:

ADB lets you issue commands to the device’s shell, such as pm the package manager. You need to find the app you want to remove:

 12 pm list packages | grep [packagename] pm uninstall -k --user 0 [packagename]

Success!

Project Euler again, problem 19. How many Sundays fell on the first of the month in the twentieth century?

Brute force solution in R but it let me play around with the way R handles dates. Seems more straight forward than in many languages (looking at you Java).

Project Euler problem 25 is to find the index of the first Fibonacci number of 1000 characters. I used the gmp library – I couldn’t find a way to get the length of a bigz data type so converted it to a string:

Project Euler problem 17:

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?

Project Euler again, this time Python. The problem is to sort a list of 5000 names alphabetically then give them a value. For example “COLIN” is 3 + 15 + 12 + 9 + 14 = 53 and is the 938th item – so its value is 49714 (53*938).

Project Euler problem 21 is to find the sum of all amicable numbers under 10000. An amicable number is:

Let $$d(n)$$ be the sum of proper divisors of $$n$$ then $$d(a)=b$$ and $$d(b)=a$$ if $$a!=b$$ then $$a$$ and $$b$$ are amicable numbers.