fork(11) download
  1. #ACSL
  2. #Junior Division
  3. #Cells
  4.  
  5.  
  6. #PROBLEM: The ACSL cell always has 8-character bits. The bits are some combination of A, B, C, D, E, F,
  7. #G, and H. The cell performs operations as listed below:
  8.  
  9. #DIVIDE - The cell divides into two cells with one cell taking the first four bits and the second cell taking the
  10. #last four bits. Then each partial cell replicates and concatenates to get back to 8 bits each.
  11.  
  12. #e.g. DIVIDE ABCDEFGH becomes ABCDABCD and EFGHEFGH
  13.  
  14. #ADDn - The first n (0 <= n <= 4) bits replicate and are concatenated to the first n bits. The last n bits are deleted.
  15.  
  16. #e.g. ADD3 ABCDEFGH becomes ABCABCDE
  17.  
  18. #SUBTRACTn - The first n (0 <= n <= 4) bits are deleted and the last n bits replicate and are concatenated on the
  19. #right.
  20.  
  21. #e.g. SUBTRACT3 ABCDEFGH becomes DEFGHFGH
  22.  
  23.  
  24. #INPUT: There will be 5 lines of input. Each line will contain an operation followed by a string representing the
  25. #8 bit cell.
  26.  
  27.  
  28. #OUTPUT: Print the outcome of the operation on the cell.
  29.  
  30. def getFunction(input):
  31. function = ""
  32. x = 1
  33. if input[:6] == "DIVIDE":
  34. function = "divide"
  35. elif input[:3] == "ADD":
  36. function = "add"
  37. while True:
  38. number = input[3:(x+3)]
  39. comma = (len(number)-1)
  40. if number[comma] == ",":
  41. function += number[:comma]
  42. break
  43. x += 1
  44. elif input[:8] == "SUBTRACT":
  45. function = "subtract"
  46. while True:
  47. number = input[8:(x+8)]
  48. comma = (len(number)-1)
  49. if number[comma] == ",":
  50. function += number[:comma]
  51. break
  52. x += 1
  53. else:
  54. function = "invalid"
  55. return function
  56.  
  57. def getData(input, function):
  58. length = len(function)
  59. return input[(length + 2):]
  60.  
  61. def divide(data):
  62. part1 = data[:4]
  63. part2 = data[4:8]
  64. part1 *= 2
  65. part2 *= 2
  66. return (part1 + " and " + part2)
  67.  
  68. def add(data, number):
  69. replicate = data[:int(number)]
  70. data = replicate + data
  71. data = data[:8]
  72. return data
  73.  
  74. def subtract(data, number):
  75. replicate = data[(8-int(number)):8]
  76. data = data + replicate
  77. data = data[int(number):]
  78. return data
  79.  
  80. def main():
  81. x = 5
  82. inputs = []
  83. functions = []
  84. data = []
  85. for i in range(x):
  86. inputs.append(raw_input())
  87. for i in range(x):
  88. functions.append(getFunction(inputs[i]))
  89. data.append(getData(inputs[i], functions[i]))
  90. if functions[i] == "divide":
  91. print divide(data[i])
  92. elif functions[i] != "invalid":
  93. y = functions[i]
  94. number = y[len(y)-1]
  95. function = y[:(len(y)-1)]
  96. if function == "add":
  97. print add(data[i], number)
  98. elif function == "subtract":
  99. print subtract(data[i], number)
  100. else:
  101. print "INVALID FUNCTION"
  102. else:
  103. print "INVALID FUNCTION"
  104.  
  105. main()
Success #stdin #stdout 0.01s 7856KB
stdin
DIVIDE, ABGHBEBC
ADD1, BCBCFDFD
SUBTRACT2, ABEECDAB
ADD4, ADDFHFBE
SUBTRACT0, ABCDEFGH
stdout
ABGHABGH and BEBCBEBC
BBCBCFDF
EECDABAB
ADDFADDF
ABCDEFGH