Advertisement
Guest User

Untitled

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