#Matthew Ramina,
Grade 9
#High Technology
HS, Lincroft, NJ
#Senior 5
Division
#2012-13 Contest
2 "Cells"
def sort(string):
#alphabetizes a string (prob a better way to do this)
L = list(string)
M = []
for i in sorted(L):
M.append(i)
final = "".join(M)
return final
while True:
#take input, upper case it, remove spaces,
split it
orig = input("Enter the operation,
followed by the cell(s), separated by commas: ")
orig = orig.upper()
orig = orig.replace(" ",
"")
L = orig.split(',')
#get command and each cell
try:
command = L[0]
cell1 = L[1]
except:
print("Invalid input!")
continue
try:
cell2 = L[2]
except:
pass
#declare final vars
fcell1 = ""
fcell2 = ""
if "DIVIDE" in command:
bit1 = cell1[:4] #get the first 4
bit2 = cell1[4:8] #get the second 4
fcell1 = sort(bit1) #sort it
fcell2 = sort(bit2)
fcell1 *= 2 #concatenate it
fcell2 *= 2
elif "ADD" in command:
try:
n = int(command[-1]) #get the
number
except:
print("Invalid input!")
continue
bit = cell1[:n] #get the part
bit = sort(bit) #sort it
lstr = list(cell1) #make the original
cell a list
lstr.insert(n, bit) #put it in the cell
for i in range(n): #get rid of the last
n letters
lstr.pop()
fcell1 = "".join(lstr)
elif "SUBTRACT" in command:
try:
n = int(command[-1]) #get the
number
except:
print("Invalid input!")
continue
lstr = list(cell1) #make the original
cell a list
for i in range(0, n): #get rid of the
first n letters
lstr.pop(0)
bit = cell1[-n:] #get the part
bit = sort(bit) #sort it
lstr.insert(len(lstr), bit) #put it in
the cell
fcell1 = "".join(lstr)
elif "UNION" in command:
bit1 = cell1[4:8] #get rid of first 4
bit2 = cell2[:4] #get rid of second 4
bit1 = sort(bit1) #sort them
bit2 = sort(bit2)
fcell1 = bit1+bit2 #concatenate them
elif "INTERSECT" in command:
bit1 = cell1[:2]+cell1[6:8] #get the
end bits
bit2 = cell2[:2]+cell2[6:8]
bit1 = sort(bit1) #sort them
bit2 = sort(bit2)
fcell1 = bit1 + bit2 #concatenate them
else:
print("Invalid input!")
continue
if fcell2 == "": #output
print(fcell1)
else:
print(fcell1, "and", fcell2)