Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python Challenge
- # Get the encoded text of "Alice In Wonderland" from https://pastebin.com/sHqwZBAz
- # Decode it (translate any digits "123456" to "aeiouy" respectively)
- # Compute the sum of the ASCII values of the letters in each word
- # Print the longest words where the ASCII sum is a prime number
- # e.g. "and" has ASCII values 97, 110, 100 totalling 307 which is prime.
- def checkPrime(number):
- if number < 4:
- return number > 1
- if number % 6 not in (1, 5):
- return False
- limit = 1 + int(number ** 0.5)
- for divisor in range(5, limit, 2):
- if not number % divisor:
- return False
- return True
- wdict = {}
- longlen = 0
- fin = open("AliceCoded.txt")
- for line in fin:
- outword = ""
- for c in line:
- if c in "123456":
- n = int(c)
- c = " aeiouy"[n]
- if c.isalpha():
- outword += c
- continue
- if outword:
- if outword not in wdict:
- tot = sum([ord(c) for c in outword])
- if checkPrime(tot):
- lgth = len(outword)
- wdict[outword] = tot
- if lgth > longlen:
- longlen = lgth
- outword = ""
- for word, tot in wdict.items():
- if len(word) == longlen:
- print(word, ":", tot)
- # Results:
- # inquisitively : 1439
- # straightening : 1399
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement