Section A:
The circular nature of the this replacement scheme makes the str
.replace()
method unworkable. For example, seat (a replaced by e) \(\rightarrow\) seet (e replaced by i) \(\rightarrow\) siit instead of siet.
The code below uses an accumulator pattern that works because it replaces characters one at a time as it traverses the string, so no former replacements are replaced in a subsequent step.
def cipher(s_in):
if len(s_in) == 1:
return(s_in)
n_s_0 = s_in[-1] + s_in[1:-1] + s_in[0]
n_s_1 = ''
for c in n_s_0:
if c == 'a':
n_s_1 += 'e'
elif c == 'e':
n_s_1 += 'i'
elif c == 'i':
n_s_1 += 'o'
elif c == 'o':
n_s_1 += 'u'
elif c == 'u':
n_s_1 += 'a'
else:
n_s_1 += c
return(n_s_1)
Sections B and C:
def order_add_cut(s1, s2):
if s1 < s2:
return (s1+s2)[1:-1]
else:
return (s2+s1)[1:-1]
def make_word(n):
a_set = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
word = ''
last_c = ''
while len(word) < n:
valid = False
if last_c != '':
char = input('Input any single letter except ' + last_c + ': ')
else:
char = input('Input a letter: ')
if char != last_c and char in a_set:
valid = True
else:
print('Must be any single letter except', last_c,'!')
if valid:
word += char
last_c = char
return word
def main():
my_word = make_word(3)
print(my_word)
main()