Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
May 14th, 2013, 07:55 PM
 nokozanEtlich
Registered User

Join Date: May 2013
Posts: 1
Time spent in forums: 21 m 10 sec
Reputation 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
May 14th, 2013, 08:39 PM
 Mekire
Contributing User

Join Date: Oct 2012
Posts: 171
Time spent in forums: 3 Days 8 h 10 m 42 sec
Reputation 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:
 Original - 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

#3
May 14th, 2013, 09:24 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,155
Time spent in forums: 1 Month 3 Weeks 2 Days 8 h 29 m 53 sec
Reputation Power: 455
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 )
Mekire agrees: Haha. Yeah I figured out what he was trying to accomplish after I posted. The insufficiency of the
sample set confused me.
__________________
[code]Code tags[/code] are essential for python code!

#4
May 15th, 2013, 08:19 PM
 nokozanEtlich2
Registered User

Join Date: May 2013
Posts: 2
Time spent in forums: 24 m 12 sec
Reputation 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

#5
May 15th, 2013, 08:35 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,155
Time spent in forums: 1 Month 3 Weeks 2 Days 8 h 29 m 53 sec
Reputation Power: 455
allLetters = ''.join(wordValues.keys())

#6
May 16th, 2013, 08:46 PM
 nokozanEtlich2
Registered User

Join Date: May 2013
Posts: 2
Time spent in forums: 24 m 12 sec
Reputation 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 )

#7
May 16th, 2013, 11:27 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,155
Time spent in forums: 1 Month 3 Weeks 2 Days 8 h 29 m 53 sec
Reputation Power: 455
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!

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Python solver already built for this specific kind of puzzle?