Category Archives: General

Awful Orange Wine

It has been said that I have my crazes.  The latest is Home Brew.

2013-10-21 10.09.08In 2000 we were kindly gifted some wine making equipment by an old family friend and made some orange wine. One year later it was filthy and it was worse when we tried it nine years after that.  Acidic and awful.  It was enough to put us off wine making for ever or so we though.  Or at least 15 years.

Fast forward to a lovely autumn walk in 2013 and glistening elderberries dripping from the bushes.  A flippant conversation about elderberry wine and a week later the demijohns were being dusted, cleaned and de-spidered and we were off again.  Only this time I have gone completely bonkers.

As usual.

The Python Challenge – 7

This was pretty easy.  The hardest thing was realising I needed the PIL image library and then installing it.  Thank you to Samson for the link to the PIL download page.  Once up and running it was pretty easy to pick the grey squares and decode their RGB value to a letter…

The code was the same for both this time since I opted to open the page directly and not use print.

Python 2.7 and 3.2.3 code

#smarty?
from PIL import Image
import re, webbrowser
im = Image.open("oxygen.png")
#get height and width
xy = im.size
height = xy[1]
width = xy[0]
#grey lines appear to go down the middle, assume that the value corresponds to a letter and boxes are 7 square
count = 3
message = ""
while count < width-21:
pix = im.getpixel((count,45))
count += 7
message += chr(pix[0])
regex = r'((?<=\[).*(?=\]))'
numbers = re.findall(regex, message)
letters = numbers[0].split(", ")
final = ""
for letter in letters:
final += chr(int(letter))
webbrowser.open("http://www.pythonchallenge.com/pc/def/" + final + ".html",new=2)













The Python Challenge – 2

This challenge is interesting in that it is confirming to me how inexperienced my programming actually is. I can get to the solution but the answer shows how limited I am. Here is my solution to 2 (I’d copied and pasted the text from the source to a document 2_message.txt.)

My Python 2.7 Solution

# find rare characters in the mess below:

maxoccurence = 1 #how many do I think is rare?

# load up the file
s = open(r"2_message.txt", "r").read()
#find the unique characters
uniqueones = set(s)

#find the rare characters (<= maxoccurence) and their position
rareones = []
for one in uniqueones:
position = []
if s.count(one) <=1:
position.append(one)
position.append(s.index(one))
rareones.append(position)


#rareones is now a multi dimensional list with the character and its position
from operator import itemgetter
solution = ""
for final in sorted(rareones, key=itemgetter(1)):
solution += final[0]
print solution

But here is the first of the suggested solutions

import collections
data = ''.join([line.rstrip() for line in open('2_message.txt')])
OCCURRENCES = collections.OrderedDict()
for c in data: OCCURRENCES[c] = OCCURRENCES.get(c, 0) + 1
avgOC = len(data) // len(OCCURRENCES)
print ''.join([c for c in OCCURRENCES if OCCURRENCES[c] < avgOC])

I want to understand this! So I spend as much time working it out as I do solving the first problem.  This is a great way of learning. I have had to learn what a Dictionary is in python (a sort of associative 2 dimensional array).  Link this to the collections library and you have got something pretty powerful.
The code appears to

  1. Load the data in to a massive string, removing the carriage returns.
  2. Make a container "Dictionary" for the characters
  3. Whiz through the massive string using the character as a key and simply adding 1 to the number attached to the key.  
  4. Getting the integer (floor) average (//) for each letter if they were distributed evenly.
  5. Concatenating all the items in the Dictionary that are less frequent than the average

This is actually not that different to what I did!!!  Just much more efficient.

[c for c in OCCURRENCES if OCCURRENCES[c] < avgOC]

Is very clever.  It is one line that only outputs a string if the occurrences are less than average.  I am still wrapping my head around it.

My Python 3.2.3 Solution

# find rare characters in the mess below:

# official version
import collections
data = ''.join([line.rstrip() for line in open('2_message.txt')])
OCCURRENCES = collections.OrderedDict()
for c in data: OCCURRENCES[c] = OCCURRENCES.get(c, 0) + 1
# avgOC = len(data) // len(OCCURRENCES)
avgOC = 1
print (''.join([c for c in OCCURRENCES if OCCURRENCES[c] == avgOC]) )

This just needed the modification to "Print" though I did find on python 3 it didn't run because there was no collections library.