Advertisement
Aikiro42

hgs

Jan 26th, 2020
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. import sys
  2.  
  3.  
  4. def validate_step(step, i):
  5.     if step in ['', '#']:
  6.         print('COMPRESSION FAILED')
  7.         sys.exit()
  8.     else:
  9.         print('STEP {}: {}'.format(i, step))
  10.  
  11.  
  12. str_words = input().split()
  13.  
  14. if len(str_words) <= 0:
  15.     print('COMPRESSION FAILED')
  16.     sys.exit()
  17.  
  18. # Word Elimination
  19. for i in range(len(str_words)):
  20.     word = str_words[i]
  21.     new_word = ''
  22.     # add all alpha chars (and 12) to new word
  23.     j = 0
  24.     while j < len(word):
  25.         char = word[j]
  26.         if char.isalpha() or char == ' ':
  27.             new_word += char
  28.         else:
  29.             if j+1 < len(word):
  30.                 if char == '1' and word[j+1] == '2':
  31.                     new_word += char + word[j+1]
  32.                     j += 1
  33.         j += 1
  34.     str_words[i] = new_word
  35. step = (' '.join(str_words)).strip()
  36. validate_step(step, 1)
  37.  
  38. # Letter Conversion
  39. for i in range(len(str_words)):
  40.     new_word = ''
  41.     for char in str_words[i]:
  42.         if char.lower() in ['a', 'e', 'i', 'o', 'u']:
  43.             new_word += char.upper()
  44.         else:
  45.             new_word += char.lower()
  46.     str_words[i] = new_word
  47.  
  48. step = (' '.join(str_words)).strip()
  49. validate_step(step, 2)
  50.  
  51. # Word Compression
  52. i = 0
  53. while i < len(str_words):
  54.     if str_words[i] == '':
  55.         del str_words[i]
  56.     else:
  57.         if i % 2 == 0:  # odd
  58.             str_words[i] = str_words[i][::2]
  59.         else:  # even
  60.             str_words[i] = str_words[i][len(str_words[i]) // 2:]
  61.         i += 1
  62.  
  63. step = (' '.join(str_words[::2]) + '#' + ' '.join(str_words[1::2])).strip()
  64. validate_step(step, 3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement