#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Python solver already built for this specific kind of puzzle?


    i have a puzzle i am working on writing some python to solve just as a mental exercise but i was wondering if something existed already that does this and what one would call this kind of problem.

    the problem is if you assign a unique integer to every letter in the alphabet determine what number a specific word would equal if you added all the integers of the elements of a word up. ie;

    Nate = 10
    Tony = 15
    Mira = 20

    What is the value of Tom? ( just an example, not sure if that really works out to have an answer).

    So, what is that kind of problem called? and second, is this something that numpy could solve? if not, is there anything else i could use to see how one would solve this problem?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Well I don't have a clue what numbers you are assigning to letters as I don't see how you got those examples but, it sounds like this is what you want:
    python Code:
    def string_value(some_string):
        return sum(ord(char)-96 for char in some_string.lower() if char.isalpha())

    Here every letter is just assigned 1-26 in order. I don't see why you would ever need numpy for this unless I am completely misunderstanding the stated problem.
    -Mek
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    You've got 3 equations and 9 unknowns.
    Code:
       ~.'NATE TONY MIRA'-.' '   NB. nub of the letters without the space character
    NATEOYMIR
       #~.'NATE TONY MIRA'-.' '   NB. tally is 9
    9
    This system of equations is under-determined---no unique solution.
    Code:
      N A T E O Y M I R
    
     /                 \   /   \      /    \ 
    | 1 1 1 1 0 0 0 0 0 | |  N  |    |  10  |
    | 1 0 1 0 1 1 0 0 0 | |  A  | =  |  15  |
    | 0 1 0 0 0 0 1 1 1 | |  T  |	 |  20  |
     \                 /  |  E  |	  \    / 
                          |  O  |
                          |  Y  |
                          |  M  |
                          |  I  |
                          |  R  |
    		       \   /
    With as many words as characters the system will have an unique solution or no solution at all, and with more words the system is over-determined. Then you can find a best-fit solution.

    For example, if the sum of letters in "rat" is 8, "tat" is 5, and "at" is 3 what is the sum of "ra"?
    Code:
       NB. Solve these equations
       NB. r a t
       NB. 1 1 1   8
       NB. 0 1 2   5
       NB. 0 1 1   3
    
    
       [A=:1 1 1,0 1 2   ,:0 1 1
    1 1 1
    0 1 2
    0 1 1
    
       (,.8 5 3)%.A
    5
    1
    2
       NB. thus 'r' is 5, 'a' is 1, 't' is 2, and "ra" should be 6.
    
       1 1 0 +/ .* (,.8 5 3)%.A  NB. dot product with "ra"
    6
    That's written in j, executable Iverson notation. www.jsoftware.com .

    How do we do it with numpy?
    Code:
    >>> import numpy
    >>> import numpy.linalg as LA
    >>> A = numpy.asarray(((1, 1, 1,),(0, 1, 2,),(0, 1, 1,)))
    >>> A
    array([[1, 1, 1],
           [0, 1, 2],
           [0, 1, 1]])
    >>> LA.solve(A,[8,5,3])
    array([ 5.,  1.,  2.])
    >>> LA.solve(A,[8,5,3]).dot((1,1,0))
    6.0
    >>>
    And if the system is overdetermined, look here ( http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.lstsq.html )

    Comments on this post

    • Mekire agrees : Haha. Yeah I figured out what he was trying to accomplish after I posted. The insufficiency of the sample set confused me. :D
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0
    @b49P23TIvg - thanks! that was an awesome post.

    and for the other users who see this and want to know what he's on about, see the code snippet below ( python 2.7.4 )

    Code:
    from sets import Set
    import numpy
    import numpy.linalg as LA
    
    wordValues = {}
    wordValues['rat'] = 8
    wordValues['tat'] = 5
    wordValues['at'] = 3
    wordValues['tart'] = 10
    
    targetWord = 'ra'
    
    allLetters = 'rattatattart'
    
    uniqueLetters = list(set(allLetters))
    
    instanceCountMapTuple = ()
    
    def instanceCountMap(word,wordSet):
        a = [0] * len(wordSet)
        for i in range(0,len(word)):
            if(wordSet.index(word[i]) != -1):
                a[wordSet.index(word[i])] += 1;
            else:
                a.append(0);
        return tuple(a)
    
    
    print uniqueLetters
    instanceCountMapTuple = instanceCountMap('rat',uniqueLetters), instanceCountMap('tat',uniqueLetters), instanceCountMap('at',uniqueLetters), instanceCountMap('tart',uniqueLetters)
    
    print allLetters
    print uniqueLetters
    print instanceCountMapTuple
    
    A = numpy.asarray(instanceCountMapTuple)
    print A
    
    print LA.lstsq(A,[wordValues['rat'],wordValues['tat'],wordValues['at'],wordValues['tart']])[0]
    i added the word 'tart' to make it so the use of lstsq was required since chances of a square result array are very slim. solve only works with an x by x array
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    allLetters = ''.join(wordValues.keys())
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0
    thanks for the ''.join tip.

    so here is the exact problem i have been trying to solve. it was brain teaser given to the dev group as just something fun and i have been told it is solvable but can't figure it out using the example solution you posted.

    The letters of the alphabet have been assign distinct positive integer. Below are the names of some of the members of our solar system and have the value of each if you add up the assign numbers for the letters in the names:

    pluto 40
    uranus 36
    neptune 29
    saturn 33
    jupiter 50
    mars 32
    earth 31
    moon 36
    venus 39
    mercury 33
    sun 18

    If one uses the letter values in the same way what is the value of planets?


    using the code below, i don't get an answer the matches the assertions of the problem

    Code:
    from sets import Set
    import numpy
    import numpy.linalg as LA
    
    wordValues = {}
    wordValues['PLUTO'] = 40
    wordValues['URANUS'] = 36
    wordValues['NEPTUNE'] = 29
    wordValues['SATURN'] = 33
    wordValues['JUPITER'] = 50
    wordValues['MARS'] = 32
    wordValues['EARTH'] = 31
    wordValues['MOON'] = 36
    wordValues['VENUS'] = 39
    wordValues['MERCURY'] = 33
    wordValues['SUN'] =  18
    
    targetWord = 'PLANETS'
    
    allLetters = ''.join(wordValues.keys())
    
    uniqueLetters = list(set(allLetters))
    
    instanceCountMapTuple = ()
    
    def instanceCountMap(word,wordSet):
        a = [0] * len(wordSet)
        
        for i in range(0,len(word)):
            if(wordSet.index(word[i]) != -1):
                a[wordSet.index(word[i])] += 1;
            else:
                a.append(0);
        return tuple(a)
    
    instanceCountMapTuple = instanceCountMap('PLUTO',uniqueLetters), instanceCountMap('URANUS',uniqueLetters),\
                            instanceCountMap('NEPTUNE',uniqueLetters),instanceCountMap('SATURN',uniqueLetters),\
                            instanceCountMap('JUPITER',uniqueLetters), instanceCountMap('MARS',uniqueLetters),\
                            instanceCountMap('EARTH',uniqueLetters), instanceCountMap('MOON',uniqueLetters),\
                            instanceCountMap('VENUS',uniqueLetters), instanceCountMap('MERCURY',uniqueLetters),\
                            instanceCountMap('SUN',uniqueLetters)
                                             
    
    A = numpy.asarray(instanceCountMapTuple)
    
    print LA.lstsq(A,[wordValues['PLUTO'],wordValues['URANUS'],wordValues['NEPTUNE'],wordValues['SATURN'],wordValues['JUPITER'],\
                      wordValues['MARS'],wordValues['EARTH'],wordValues['MOON'],wordValues['VENUS'],wordValues['MERCURY'],\
                      wordValues['SUN']] )[0]
    any ideas or is this really not solvable with the given information? ( also tried subset elimination to no avail )
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Lumping together "pi" and "cy" (from jupiter and mercury) brings us down to 11 equations with 15 unknowns. Maybe you'll see some other possibilities?

    If you had a set of coupled equations is there only one solution that makes good sense? No negative values, for instance.

    Good puzzle!
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo