Advertisement
acclivity

pyAlicePrimeChallenge

Feb 7th, 2022
1,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. # Python Challenge
  2. # Get the encoded text of "Alice In Wonderland" from https://pastebin.com/sHqwZBAz
  3. # Decode it (translate any digits "123456" to "aeiouy" respectively)
  4. # Compute the sum of the ASCII values of the letters in each word
  5. # Print the longest words where the ASCII sum is a prime number
  6. # e.g. "and" has ASCII values 97, 110, 100 totalling 307 which is prime.
  7.  
  8. def checkPrime(number):
  9.     if number < 4:
  10.         return number > 1
  11.     if number % 6 not in (1, 5):
  12.         return False
  13.     limit = 1 + int(number ** 0.5)
  14.     for divisor in range(5, limit, 2):
  15.         if not number % divisor:
  16.             return False
  17.     return True
  18.  
  19. wdict = {}
  20. longlen = 0
  21. fin = open("AliceCoded.txt")
  22. for line in fin:
  23.     outword = ""
  24.     for c in line:
  25.         if c in "123456":
  26.             n = int(c)
  27.             c = " aeiouy"[n]
  28.         if c.isalpha():
  29.             outword += c
  30.             continue
  31.         if outword:
  32.             if outword not in wdict:
  33.                 tot = sum([ord(c) for c in outword])
  34.                 if checkPrime(tot):
  35.                     lgth = len(outword)
  36.                     wdict[outword] = tot
  37.                     if lgth > longlen:
  38.                         longlen = lgth
  39.             outword = ""
  40.  
  41. for word, tot in wdict.items():
  42.     if len(word) == longlen:
  43.         print(word, ":", tot)
  44.  
  45. # Results:
  46. # inquisitively : 1439
  47. # straightening : 1399
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement