Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys # helps in terminating the program
- # a = input()
- # letters = []
- # for character in a:
- # letters += character
- # Follow naming conventions.
- # You can also convert an input string to a list of characters by doing list(input())
- # but for this scenario it's more efficient to work with strings
- str_in = input()
- # s1 = []
- step1 = ''
- i = 0
- # while i < len(letters):
- while i < len(str_in):
- # if letters[i] == '1' and letters[i + 1] == '2':
- # s1 += letters[i] + letters[i + 1]
- # i += 1
- # elif letters[i].isalpha() == True or letters[i] == ' ':
- # s1 += letters[i]
- # i += 1
- # else:
- # i += 1
- # Best to start with adding alphas
- if str_in[i].isalpha():
- step1 += str_in[i]
- else:
- # Avoid IR with this
- if i + 1 < len(str_in):
- if str_in[i] == '1' and str_in[i+1] == '2':
- step1 += str_in[i] + str_in[i + 1]
- i += 1
- i += 1
- # s1 = ''.join(s1)
- # if s1 == '' or '12' not in s1:
- if len(step1.strip()) == 0:
- print("COMPRESSION FAILED")
- sys.exit()
- step1_up = step1.upper()
- step2 = '' # more efficient to work with strings
- # for letter in s1_up:
- # step2 += letter
- # vowels = ['A', 'E', 'I', 'O', 'U']
- vowels = 'AEIOU'
- # conso = ['B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z']
- # if a letter ain't a vowel, it's either a consonant or 1 or 2.
- # uppercasing a number yields no effect.
- # s2 = []
- # for y in step2:
- for y in step1_up:
- # if y in vowels:
- if y not in vowels:
- # y = y.upper()
- y = y.lower()
- # step2.append(y)
- step2 += y
- # elif y in conso:
- # y = y.lower()
- # s2.append(y)
- else:
- # step2.append(y)
- step2 += y # no need to upper since you upper'd the whole string initially
- # s2 = ''.join(s2)
- # s2_2 = s2
- # step2 is complete and a string, prior two lines unnecessary
- # if s2 == '' or '12' not in s2:
- if len(step2.strip()) == 0:
- print("COMPRESSION FAILED")
- sys.exit()
- step2 = step2.split()
- odd = []
- even = []
- j = 0
- while j < len(step2):
- word = step2[j]
- if word == '':
- del step2[j]
- # no need to increment, len(step2) will decrease and list will shift left
- else:
- if j % 2 == 0:
- # edit it now so all you have to do is join lists
- n_word = ''
- for k in len(word):
- if k % 2 == 0:
- n_word += word[k]
- odd.append(n_word)
- else:
- even.append(word[len(word)//2:])
- j += 1 # bring out increment
- # first_part = []
- # second_part = []
- # m = 0
- # while m < len(odd):
- # n = 0
- # while n < len(odd[m]):
- # if n % 2 == 0:
- # first_part.append(odd[m][n])
- # if n == len(odd[m]) - 1 or n == len(odd[m]) - 2:
- # first_part.append(' ')
- # n += 1
- # m += 1
- # first_half = ''.join(first_part)
- # first_half = first_half.rstrip()
- #
- # for x in even:
- # upper_half = (x[(len(x) // 2):])
- # second_part.append(upper_half)
- # second_part.append(' ')
- # second_half = ''.join(second_part)
- # hashtag = "#"
- # s3 = first_half + hashtag + second_half
- # no need for this part since we processed it while sorting the odds and evens
- step3 = '{}#{}'.format(' '.join(odd), ' '.join(even))
- if step3.strip() == '#':
- print("COMPRESSION FAILED")
- sys.exit()
- print('STEP 3: {}'.format(step3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement