Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. '''
  2. Lab 06
  3. Towers
  4. Andrew Ziogas
  5.  
  6. Note to person grading this:
  7. I did this while REALLY TIRED and made it EXTREMELY COMPLEX for no reason
  8. If it's hard to follow, I'm sorry.
  9. In my brain it makes perfect sense and I tried to comment it, but it's still pretty crazy to look at
  10. '''
  11. import sys
  12.  
  13. def towers(lis):
  14. '''
  15. Calculate the values of towers for each item in the list
  16. Returns a list of lists formatted like this:
  17. [[position in the list, up (1) or down (0), total value], [4, 0, 27]]
  18. The format is weird, but it really helps when calculating the max value and creating the printout
  19. '''
  20. positions = [] #Create empty list
  21.  
  22. for i in range(2, len(lis)-2):
  23. #Calculate values for all downwards facing towers
  24. tower_value = lis[i-2] + lis[i-1] + lis[i+1] #Calculate the max value for the tower at that position
  25. positions.append([i, 0, tower_value]) #Add it to the list
  26.  
  27. for i in range(1, len(lis)-3):
  28. #Calculate values for all upwards facing towers
  29. tower_value = lis[i-1] + lis[i+1] + lis[i+2] #Calculate the max value for the tower at that position
  30. positions.append([i, 1, tower_value]) #Add it to the list
  31.  
  32. return positions
  33.  
  34. def max_towers(lis, towers):
  35. '''
  36. Create a list with the largest <towers> values
  37. Expects a list formatted with the same format that towers() returns
  38. Returns a list in the same format.
  39. '''
  40. maximum_list = [] #Create an empty list
  41. item = 0 #Don't ask why I used item for this value, because I don't know
  42. while item < towers: #While we don't have the maximum number of towers
  43. max_index = find_max(lis) #Find the maximum value
  44. duplicate = is_duplicate(maximum_list, lis[max_index]) #Check for a duplicate
  45.  
  46. if duplicate == -1: #If not duplicate
  47. maximum_list.append(lis[max_index]) #Add the item to the list
  48. lis.pop(max_index) #Remove the item from the list
  49. item += 1 #Increase the number of towers by one
  50. else:
  51. if lis[max_index][2] > maximum_list[duplicate][2]: #If the duplicate is larger than the one currently at that position
  52. maximum_list[duplicate] = lis[max_index] #Switch the item to the one that's bigger
  53. lis.pop(max_index) #Remove the duplicate item from the list
  54.  
  55.  
  56. return maximum_list
  57.  
  58. def is_duplicate(lis, current_item):
  59. '''
  60. Check to see if there's a duplicate location in the list.
  61. If there is it returns the index number of that duplicate
  62. If not, it returns -1
  63. Takes a list of tower items and a single tower item
  64. Returns either an index number or -1
  65. '''
  66. for i in range(len(lis)): #For each item in the maximum list
  67. if current_item[0] == lis[i][0]: #If there's a duplicate
  68. return i #Return the value
  69.  
  70. return -1 #Otherwise return -1
  71.  
  72. def find_max(lis):
  73. '''
  74. Find the index of the item with the highest value
  75. Expects a list in the towers() format
  76. Returns an index number
  77. '''
  78. maximum = 0
  79. index = 0
  80. for item in range(len(lis)): #For each item in the list
  81. if lis[item][2] > maximum: #If the item is greater than the max
  82. maximum = lis[item][2] #Set the maximum number
  83. index = item #Set the maximum index
  84.  
  85.  
  86. return index
  87.  
  88. def create_printout(lis):
  89. '''
  90. Create a printout of all the items
  91. '''
  92. for item in lis: #For item in list
  93. printout = "Centered at location "
  94. printout += str(item[0]) #Position
  95. if item[1] == 0: #If facing upwards
  96. printout += " facing upward scoring "
  97. else:
  98. printout += " facing downward scoring "
  99. printout += str(item[2]) #Value
  100.  
  101. print(printout)
  102.  
  103. def main():
  104. '''
  105. Main function
  106. '''
  107. if len(sys.argv) != 3: #Check if the args are not what's expected
  108. print("Usage: lasers.py <laser-file> <number-of-towers>")
  109. exit(1)
  110.  
  111. file = open(sys.argv[1], "r") #File
  112. number_of_towers = int(sys.argv[2]) #Tower numbers
  113.  
  114. for line in file:
  115. print(line)
  116. lst = line.split() #Split the line in the file
  117. for i in range(len(lst)):
  118. lst[i] = int(lst[i]) #Convert each item in the list from a string to an int
  119. print(number_of_towers, "lasers")
  120.  
  121. towers_list = towers(lst) #Calculate all possible towers
  122. maximum_towers = max_towers(towers_list, number_of_towers) #Calculate the maximum towers
  123. create_printout(maximum_towers) #Print the values
  124.  
  125. if __name__ == "__main__":
  126. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement