Member Avatar for leegeorg07

Hi again,

I am trying a competition entry (its old, purely to test myself) and I have come against a problem, this is the description for the competition:

In a word game each letter has a score based on its position in the alphabet, A scoring 1 through to Z scoring 26. The object of the game is to split the letters of a word into two groups so that the total score of the letters in each group is the same. For example, LIONHEAD can be split into LIHAD and ONE, both scoring 34.

and I currently have this code:

import cword
running=1
while running==1:
    poss=[]
    print"""
do you want to:
1)enter a word?
2)quit?
"""
    menu=int(raw_input(''))
    if menu==1:
        word=raw_input("what is the word to check?")
        if cword.check_word(word):#checks is word is a word
            for pword in cword.dicta:
                if len(pword)<len(word):
                    for letter in word:
                            if letter in pword:
                                if cword.val_word(pword)==cword.val_word(word)/2:#gets value of possible and current word, /2 bc new word value will always be half of the original.
                                    if pword in poss:
                                        continue
                                    else:
                                        poss.append(pword)
            print poss
    elif menu==2:
        running=0
    else:
        print "that was not a proper command"
        print "please enter a new one."
    
raw_input()

When running this with 'tiger' as the word, I get these possibles: ['ais', 'aria', 'awe', 'bags', 'beau', 'bind', 'bred', 'cate', 'ceil', 'cepe', 'crag', 'cue', 'dike', 'dip', 'ecu', 'ell', 'ex', 'fard', 'feme', 'fend', 'fer', 'fin', 'gabs', 'gang', 'gape', 'glee', 'glia', 'haik', 'hat', 'held', 'hep', 'it', 'jar', 'kale', 'lake', 'leak', 'lice', 'mig', 'page', 'peag', 'peh', 'pica', 'raia', 'raj', 'ref', 'rib', 'sade', 'see', 'tace', 'ted', 'ti', 'wae'] but how can I go through these words so that only the correct amount of each letter is used?

Many thanks in advance.

Recommended Answers

All 3 Replies

First you want to check that the word ends up as an even number, otherwise you can't divide it in two. Tiger = 59 so is not a good choice.

import string


def number_equiv(word):
    ## I prefer a list over string.find  (so sue me)
    letters_list = [ ltr for ltr in string.lowercase ]
    total = 0
    for ltr in word:
        num = letters_list.index(ltr) + 1
        print ltr, num
        total += num
    print total

number_equiv("tiger")
number_equiv("tigers")

Next, I would think that you would want to start with the largest number since it is the most inflexible. For "tigers" that would be t=20. Since "tigers" adds up to 78, one-half would be 39. 39 -"t"(20) = 19 which is "s" which you have, so "tigers" = "ts" and "iger". In this case, I think that is the only solution.

Member Avatar for leegeorg07

Thanks for the reply, but what I meant was how could I check that the possibilities had the correct amount of allowed letters (from the original word) and that they only used the original letters.

I thought I would need to count the letters and if the amount is more than allowed it should remove that word, would this work?

Member Avatar for leegeorg07

Dont worry, I managed to find a method of doing it, I was also able to simplify cword.py so that it is all in a single file, the code is below for anyone interested.

LETTERS={'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6, 'g':7, 'h':8, 'i':9, 'j':10, 'k':11, 'l':12, 'm':13, 'n':14, 'o':15, 'p':16, 'q':17, 'r':18, 's':19, 't':20, 'u':21, 'v':22, 'w':23, 'x':24, 'y':25, 'z':26}
running=1
def val_word(word):
    val=0
    for letter in word.lower():
        val+=LETTERS[letter]
    return val
while running==1:
    poss=[]
    ptemp=[]
    word_list=[]
    to_rem=[]
    print"""
do you want to:
1)enter a word?
2)quit?
"""
    menu=int(raw_input(''))
    if menu==1:
        word=raw_input("what is the word to check?")
        if int(val_word(word))%2:
            print "Sorry, %s cannot be split." % word
        else:
            for pword in dicta:
                if len(pword)<len(word):
                    for letter in word:
                        word_list.append(letter)
                        if letter in pword:
                            if val_word(pword)==val_word(word)/2:#gets value of possible and current word, /2 bc new word value will always be half of the original.
                                if pword in poss:
                                    continue
                                else:
                                    poss.append(pword)
            for pword in poss:
                ptemp=[]
                for better in pword:
                    ptemp.append(better)
                for petter in ptemp:
                    if word_list.count(petter) < ptemp.count(petter):
                        if pword in to_rem:
                            continue
                        else:
                            to_rem.append(pword)            
            for i in to_rem:
                poss.remove(i)
            print 'any 2 of these could be used: \n %s' %poss
                
                
    elif menu==2:
        running=0
    else:
        print "that was not a proper command"
        print "please enter a new one."
    
raw_input()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.