JoshuaB

Alpha to WASD

Nov 15th, 2013
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. """
  2. Usage:
  3.    Type something in (using only latin characters) and then press enter to
  4.    convert it to base 4 into WASD
  5.  
  6. By: Jaxrtech (with the power of Google)
  7. """
  8.  
  9. def base10toN(num, base):
  10.     """Change ``num'' to given base
  11.    Upto base 36 is supported."""
  12.  
  13.     converted_string, modstring = "", ""
  14.     currentnum = num
  15.     if not 1 < base < 37:
  16.         raise ValueError("base must be between 2 and 36")
  17.     if not num:
  18.         return '0'
  19.     while currentnum:
  20.         mod = currentnum % base
  21.         currentnum = currentnum // base
  22.         converted_string = chr(48 + mod + 7*(mod > 10)) + converted_string
  23.     return converted_string
  24.  
  25. class Offsets:
  26.     CAPS_ALPHA = ord('A')
  27.  
  28. def base_map(base_point, mapping):
  29.     """
  30.    Returns a string from a base number point converted
  31.    with a mapping.
  32.    
  33.    >>> base_map('13', ['W', 'A', 'S', 'D'])
  34.    'WS'
  35.    """
  36.     return ''.join(mapping[int(c)-1] for c in base_point)
  37.  
  38. WASD_MAPPING = ['W', 'A', 'S', 'D']
  39. BASE = 4
  40. SHIFT_ORIGIN = Offsets.CAPS_ALPHA
  41.  
  42. message = raw_input().upper().split(' ')
  43. ascii = [map(ord, s) for s in message]
  44. shifted = [map(lambda x: x - SHIFT_ORIGIN, s) for s in ascii]
  45. base = [map(lambda x: base10toN(x, BASE), s) for s in shifted]
  46. base_map = [map(lambda x: base_map(x, WASD_MAPPING), s) for s in base]
  47.  
  48. print base_map
Advertisement
Add Comment
Please, Sign In to add comment