0

I want to my program to take 25 letters as input and put them in some sort of list so that i can make rules for what letter that can connect with another until and get three words from it that is not overlapping.

What i did was this:

def matris():
    matris = [[],[],[],[],[]]
    counter = 0
    counter2 = 0
    counter3 = 0
    counter4 = 0
    counter5 = 0

    while counter !=5:
        matris[0].append(raw_input('One letter: '))
        counter+=1

    while counter2 !=5:
        matris[1].append(raw_input('One letter: '))
        counter2+=1

    while counter3 !=5:
        matris[2].append(raw_input('One letter: '))
        counter3+=1

    while counter4 !=5:
        matris[2].append(raw_input('One letter: '))
        counter4+=1

    while counter5 !=5:
        matris[4].append(raw_input('One letter: '))
        counter5+=1

    return matris

So for example, when I run this it ask me for "One letter" * 25 which could generate a matrix looking something like this:

matris = [['a', 'g', 'i', 't', 'u']
          ['s', 'r', 'g', 's', 'm']
          ['f', 'e', 'd', 'c', 't']
          ['r', 's', 'i', 'f', 'x']
          ['t', 'i', 't', 't', 'i']]

If anyone have a better way for doing this, I would be thankful if you shared it. And a way that will work with what I want my program to do, which im not sure I will be able to with my version.

I have a dictionary.txt which I have made something like: dictionary = open('dictionary.txt', 'r')

so i thought that i would try to start matching matris[0][0]+matris[0][1] and see if there is a word starting with i.e 'a'+'g', and then take the next letter and so on until you find the lets say three best (most valued) words.

I'm guessing that i would need a class. So this is how far I have come:

Class hypotes:
  def __init__ (self, usedPosiiton, word):
  self.u = usedPosition
  self.w = word
  positions = []
  bestWords = [] # There should be maximum three words in this list, 
                 # the words with highest score

I thought that I have to save the positions in an array, so that I later on can make sure that the words that are best don't use the same letters?

Guessing I'm going to need some help with this class.

letterValuePoints = {'A':50,  'B':110, 'C':190, 'D':70,  'E':50,  'F':90, 
                     'G':70,  'H':70,  'I':50,  'J':170, 'K':70,  'L':50,
                     'M':70,  'N':50,  'O':70,  'P':110, 'R':50,  'S':50, 
                     'T':50,  'U':110, 'V':90,  'X':190, 'Y':170, 'Z':210, 
                     'Å':110, 'Ä':90,  'Ö':110}

I thought later on when I would give each letter a value, I would do it like this, but I dont know if that will be a good way?

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
Bob H.
  • 1

1 Answers1

1

Don't think I get the question. Here are however some snippets to get you started.

Just read 25 letters in one go and use a partitioning generator as described here use split to send it to the generator below.

def chunks(l, n):
    """ Yield successive n-sized chunks from l.
    """
    for i in xrange(0, len(l), n):
        yield l[i:i+n]

import pprint
a=raw_input('some letters: ')
some letters: a b c d e f g h i j k l m n o p q r s t u v w x y

pprint.pprint(list(chunks(a.split(), 5)))
[['a', 'b', 'c', 'd', 'e'],
 ['f', 'g', 'h', 'i', 'j'],
 ['k', 'l', 'm', 'n', 'o'],
 ['p', 'q', 'r', 's', 't'],
 ['u', 'v', 'w', 'x', 'y']]

If you are looking to do approximate matching, have a look at difflib

>>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])
['apple', 'ape']
>>> import keyword
>>> get_close_matches('wheel', keyword.kwlist)
['while']
>>> get_close_matches('apple', keyword.kwlist)
[]
>>> get_close_matches('accept', keyword.kwlist)
['except']

[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
 [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
 [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
 [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
 [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
 [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
 [70, 71, 72, 73, 74]]

After this I think you need to clarify your question a bit :-)

Community
  • 1
  • 1
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • Hi, Thank you for fast reply! =) This was very pretty: import pprint def chunks(l, n): """ Yield successive n-sized chunks from l. """ for i in xrange(0, len(l), n): yield l[i:i+n] a=raw_input('some letters: ') pprint.pprint(list(chunks(a.split(), 5))) But there must be a way to split it and return as a list with those list in it? Now it is just printing the letters out? – Bob H. Sep 13 '11 at 14:07
  • that looks really bad, perhaps i should edit my question insted.. =/ – Bob H. Sep 13 '11 at 14:09
  • Såg att du var svensk! =) Går inte att skicka pm här va? Verkar ha lite svårt för att få fram på engelska vad jag behövde ha hjälp med kanske? Försöker få programmet att metodiskt söka igenom listan som nu består av 5 listor med 5 bokstäver i varje, och försöka hitta ord som matchas med en ordlista.txt. Så jag tänkte att man kanske ska börja leta på plats lista[0][0] och lista [0][1] och se om något ord i ordlistan börjar på de bokstäverna och om det gör det fortsätta med lista[0][2] och fortsätta tills den inte kan matcha något mer, sen göra om samma provess men börja på bokstav lista[0][1].. – Bob H. Sep 13 '11 at 14:22
  • Men först måste jag ju få den att returnera själva listan inte bara printa. :) Vet inte om man ska göra den sökalgoritmen som det blir som en egen klass? Vad som kan tänkas behöva initieras? Den måste ju t.ex. lägga till vart den har varit så att den inte kollar där igen, lägga till ord som den hittar m.m. Tacksam för ditt svar!:) /Bob – Bob H. Sep 13 '11 at 14:24
  • Själva returneringen löste jag genom att göra en ny funktion som return(list(chunks(a.split(), 5))) på det som raw_input tar in :) – Bob H. Sep 13 '11 at 14:35