Katas in python (I)

Recently I found this interesting website about solving interesting programming quizzes. By the way, I’d like to congratulate the authors of the website for the idea and the result: a website pretty similar to StackOverFlow where you can post your solutions, vote for your favorites and so forth, but aimed to coders that want to improve or demonstrate their programming skills to the community.

My objective was to practice with python, as I’ve been learning this language for a few months and lately I stopped my training, as I moved on to other things (Android, mainly).

So, I’ll publish here the solutions to the problems that I’m solving, so that other people can check them, comment about it and probably suggest better approaches. I would really appreciate that. Thanks in advance!!

Kata I: Phonewords – from phone numbers to words

Modern phones have keyboards with letter-to-number assignments as per the ITU-T E.161 standard:


Using that mapping, people can create phonewords, which are words used to remember part of a phone number.

Write a function that takes as input a number and a wordlist (e.g: an array slurped from /usr/share/dict/words) and returns a list of candidate phonewords for that number. For example, the number 43556 may yield the list/array (Gekko, hello), and 2663742 may yield (amnesia, amnesic, anoesia).

The solution

from sys import argv

def translator(figure):
    if figure == "2":
    return "ABC"
    if figure == "3":
        return "DEF"
    if figure == "4":
        return "GHI"
    if figure == "5":
        return "JKL"
    if figure == "6":
        return  "MNO"
    if figure == "7":
        return "PQRS"
    if figure == "8":
        return "TUV"
    if figure == "9":
        return "WXYZ"
    return ""

def get_words():
    dictionary = open("/usr/share/dict/words")

    words = []
    for line in dictionary:

    return words

script, number = argv

words = get_words()

pos = 0
for figure in number:
    aux = []
    for word in words:
    pattern = translator(figure)

    if pattern == "":
            print "Cannot find a match"

    for letter in pattern:
            if word[pos] == letter:
    words = aux
    pos = pos + 1

print "The matches found are: %r" % words

This reads the dictionary placed in /usr/share/dict/words and puts it in a list. Then iterates over the figures that compose the number and translates to every possibility of the dictionary, saving the matches for the next iteration over the next figure.

That’s not a bad solution (well, I definitely hate the translator function), IMHO, but this one is much better and shorter, and it’s not mine ;):

import re

def numbertoreg(num):
    regexp = "\\b(" #only completed words is best
    for n in num:
        regexp=regexp + letras[int(n)-2]
    return regexp   + ")\\b" #word boundaries

def phonewords(phone):
    regexp = numbertoreg(phone)
    f = open("/usr/share/dict/words","r")
    r = re.compile(str(regexp))
    print r.findall(f.read())


Owner: arraclan

This uses spreadly regular expressions (which I neither know how to use them in Python, nor came to my mind the idea of using regular expressions :() to map the numbers to its corresponding letters. Then build the regular expression resulting of the number given and search for matches in the /usr/share/dict/words dictionary. Basically, it delegates the hard work to the library 🙂

I hope you’ll find this interesting, at least I hope you’ve learnt how to use regular expressions in Python, as I did. See you soon.



Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )


Conectando a %s