티스토리 뷰

python lecture/algorism

[edu] Word sums

burningrizen 2018. 10. 8. 20:16

If we value each letter corresponding to its position in the alphabet, we may compute the value of a word as the sum of the values of its individual letters. We can use this procedure to hunt for word sums. A word sum is a sequence of (usually two) words whose sum of word values equals the word value of another word that can be associated with the sequence of words.

KING + CHAIR = THRONE

Checking whether two words are a word sum when combined with a third word is fairly straightforward. However, it is far more difficult to find good word sums, especially since the associations determine their quality. Here we provide you with a couple of examples, where we give the word value of each word between a pair of brackets.

term 1term 2result
ARM (32)BEND (25)ELBOW (57)
WHITE (65)HOUSE (68)GOVERNMENT (133)
MONA (43)LISA (41)LEONARDO (84)
PETER (64)PAN (31)NEVERLAND (95)
FAMILY (66)TREE (48)ANCESTORS (114)
RED (27)BULL (47)COCKTAIL (74)
EGG (19)PLANT (63)AUBERGINE (82)
ANT (35)LION (50)DOODLEBUG (85)
VISUAL (84)BASIC (34)MICROSOFT (118)
BLACK (29)JACK (25)VEGAS (54)

Along the same lines we can also make associations with names of famous people.

term 1term 2result
JOHN (47)CLEESE (49)HUMOUR (96)
TOM (48)HANKS (53)FORREST (101)
BOB (19)MARLEY (74)RASTAFARI (93)
KURT (70)COBAIN (44)NOVOSELIC (114)
NELSON (79)MANDELA (50)HUMANITARIAN (129)
EMMA (32)WATSON (92)VOLDEMORT (124)
JAMES (48)BOND (35)DANIEL CRAIG (83)
GEORGE (57)LUCAS (56)JAR JAR BINKS (113)
STEPHEN (87)HAWKING (73)TEXT TO SPEECH (160)
CLOCKWORK (111)ORANGE (60)STANLEY KUBRICK (171)

With the last four examples in the above table, we only take into account the letters to determine the value of a word. This allowed us to come up with multi-word results.

Assignment

  • Write a function lettervalue that takes a string containing a single character. The function must return the value of the character. Letters have a value that corresponds to their position in the alphabet. No distinction is made between uppercase and lowercase letters. All other characters have a zero value.

  • Write a function wordvalue that takes a string argument. The function must return the sum of the values of all characters in the given string.

  • Write a function wordsum that takes three strings. The function must return a Boolean value that indicates whether or not the sum of the word values of the first two arguments equals the word value of the third argument.

Example

>>> lettervalue('A')
1
>>> lettervalue('j')
10
>>> lettervalue('!')
0

>>> wordvalue('arm')
32
>>> wordvalue('BEND')
25
>>> wordvalue('elbow')
57

>>> wordsum('arm', 'BEND', 'elbow')
True
>>> wordsum('KING', 'chair', 'THRONE')
True
>>> wordsum('Monty', 'Python', 'SHRUBBERY')
False



















def lettervalue(letter):
code = ord(letter.lower())
START_ALPHA_LOW = 97
STOP_ALPHA_LOW = 123
if code >= START_ALPHA_LOW and code < STOP_ALPHA_LOW:
return code - START_ALPHA_LOW + 1
return 0


def wordvalue(chars):
sum = 0
for char in chars:
sum += lettervalue(char)
return sum


def wordsum(a, b, c):
return True if wordvalue(a) + wordvalue(b) == wordvalue(c) else False


print(wordsum('Monty', 'Python', 'SHRUBBERY'))


'python lecture > algorism' 카테고리의 다른 글

[edu] Kaprekar series  (0) 2018.10.14
[edu] Transitions and transversions  (0) 2018.10.08
[edu] Penney Ante  (0) 2018.10.06
[edu] Corkscrew  (0) 2018.10.04
[eud] Cowsay  (0) 2018.10.02
댓글