#ACSL
#Junior Division
#Cells
#PROBLEM: The ACSL cell always has 8-character bits. The bits are some combination of A, B, C, D, E, F,
#G, and H. The cell performs operations as listed below:
#DIVIDE - The cell divides into two cells with one cell taking the first four bits and the second cell taking the
#last four bits. Then each partial cell replicates and concatenates to get back to 8 bits each.
#e.g. DIVIDE ABCDEFGH becomes ABCDABCD and EFGHEFGH
#ADDn - The first n (0 <= n <= 4) bits replicate and are concatenated to the first n bits. The last n bits are deleted.
#e.g. ADD3 ABCDEFGH becomes ABCABCDE
#SUBTRACTn - The first n (0 <= n <= 4) bits are deleted and the last n bits replicate and are concatenated on the
#right.
#e.g. SUBTRACT3 ABCDEFGH becomes DEFGHFGH
#INPUT: There will be 5 lines of input. Each line will contain an operation followed by a string representing the
#8 bit cell.
#OUTPUT: Print the outcome of the operation on the cell.
def getFunction(input):
function = ""
x = 1
if input[:6] == "DIVIDE":
function = "divide"
elif input[:3] == "ADD":
function = "add"
while True:
number = input[3:(x+3)]
comma = (len(number)-1)
if number[comma] == ",":
function += number[:comma]
break
x += 1
elif input[:8] == "SUBTRACT":
function = "subtract"
while True:
number = input[8:(x+8)]
comma = (len(number)-1)
if number[comma] == ",":
function += number[:comma]
break
x += 1
else:
function = "invalid"
return function
def getData(input, function):
length = len(function)
return input[(length + 2):]
def divide(data):
part1 = data[:4]
part2 = data[4:8]
part1 *= 2
part2 *= 2
return (part1 + " and " + part2)
def add(data, number):
replicate = data[:int(number)]
data = replicate + data
data = data[:8]
return data
def subtract(data, number):
replicate = data[(8-int(number)):8]
data = data + replicate
data = data[int(number):]
return data
def main():
x = 5
inputs = []
functions = []
data = []
for i in range(x):
inputs.append(raw_input())
for i in range(x):
functions.append(getFunction(inputs[i]))
data.append(getData(inputs[i], functions[i]))
if functions[i] == "divide":
print divide(data[i])
elif functions[i] != "invalid":
y = functions[i]
number = y[len(y)-1]
function = y[:(len(y)-1)]
if function == "add":
print add(data[i], number)
elif function == "subtract":
print subtract(data[i], number)
else:
print "INVALID FUNCTION"
else:
print "INVALID FUNCTION"
main()