Advertisement
kingbode

Untitled

Nov 13th, 2023
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. # convert word to integer and vice versa
  2. def word_to_int(word):
  3.     x = [ord(i) for i in word]
  4.     print(f'letter of {word} are represented as a list of integers as below:\n{x}')
  5.     result = 0
  6.  
  7.     for i in x:
  8.         # keep each number in the same order
  9.         result = result * 1000 + i
  10.  
  11.     return result
  12. def int_to_word(num):
  13.     word = ''
  14.     while num > 0:
  15.         word = chr(num % 1000) + word
  16.         num //= 1000
  17.     return word
  18.  
  19.  
  20.  
  21. result = word_to_int('Mohammed')
  22. print(result)
  23.  
  24. result = int_to_word(result)
  25. print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement