logo

scripts

A bunch of scripts, some to be moved to their own repository git clone https://hacktivis.me/git/scripts.git

morse.py (1767B)


  1. def toMorseArray(s):
  2. """Return a list of the patterns to use for s."""
  3. return [lookup[c] for c in s.upper()]
  4. def toMorseString(s):
  5. out = ' '
  6. for c in s.upper():
  7. try:
  8. out = str(out + lookup[c] + ' ')
  9. except KeyError:
  10. print('[Error] KeyError')
  11. return out
  12. def fromMorse(s):
  13. out = ' '
  14. for c in s.split(' '):
  15. try:
  16. out = str(out + inv_lookup[c])
  17. except KeyError:
  18. print('[Error] KeyError')
  19. return out
  20. lookup = {'!': '-.-.--',
  21. "'": '.----.',
  22. '"': '.-..-.',
  23. '$': '...-..-',
  24. '&': '.-...',
  25. '(': '-.--.',
  26. ')': '-.--.-',
  27. '+': '.-.-.',
  28. ',': '--..--',
  29. '-': '-....-',
  30. '.': '.-.-.-',
  31. '/': '-..-.',
  32. '0': '-----',
  33. '1': '.----',
  34. '2': '..---',
  35. '3': '...--',
  36. '4': '....-',
  37. '5': '.....',
  38. '6': '-....',
  39. '7': '--...',
  40. '8': '---..',
  41. '9': '----.',
  42. ':': '---...',
  43. ';': '-.-.-.',
  44. '=': '-...-',
  45. '?': '..--..',
  46. '@': '.--.-.',
  47. 'A': '.-',
  48. 'B': '-...',
  49. 'C': '-.-.',
  50. 'D': '-..',
  51. 'E': '.',
  52. 'F': '..-.',
  53. 'G': '--.',
  54. 'H': '....',
  55. 'I': '..',
  56. 'J': '.---',
  57. 'K': '-.-',
  58. 'L': '.-..',
  59. 'M': '--',
  60. 'N': '-.',
  61. 'O': '---',
  62. 'P': '.--.',
  63. 'Q': '--.-',
  64. 'R': '.-.',
  65. 'S': '...',
  66. 'T': '-',
  67. 'U': '..-',
  68. 'V': '...-',
  69. 'W': '.--',
  70. 'X': '-..-',
  71. 'Y': '-.--',
  72. 'Z': '--..',
  73. '_': '..--.-',
  74. ' ': ' ',
  75. }
  76. inv_lookup = {lookup[k] : k for k in lookup}