Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Lab 06
- Towers
- Note to person grading this:
- I did this while REALLY TIRED and made it EXTREMELY COMPLEX for no reason
- If it's hard to follow, I'm sorry.
- In my brain it makes perfect sense and I tried to comment it, but it's still pretty crazy to look at
- '''
- import sys
- def towers(lis):
- '''
- Calculate the values of towers for each item in the list
- Returns a list of lists formatted like this:
- [[position in the list, up (1) or down (0), total value], [4, 0, 27]]
- The format is weird, but it really helps when calculating the max value and creating the printout
- '''
- positions = [] #Create empty list
- for i in range(2, len(lis)-2):
- #Calculate values for all downwards facing towers
- tower_value = lis[i-2] + lis[i-1] + lis[i+1] #Calculate the max value for the tower at that position
- positions.append([i, 0, tower_value]) #Add it to the list
- for i in range(1, len(lis)-3):
- #Calculate values for all upwards facing towers
- tower_value = lis[i-1] + lis[i+1] + lis[i+2] #Calculate the max value for the tower at that position
- positions.append([i, 1, tower_value]) #Add it to the list
- return positions
- def max_towers(lis, towers):
- '''
- Create a list with the largest <towers> values
- Expects a list formatted with the same format that towers() returns
- Returns a list in the same format.
- '''
- maximum_list = [] #Create an empty list
- item = 0 #Don't ask why I used item for this value, because I don't know
- while item < towers: #While we don't have the maximum number of towers
- max_index = find_max(lis) #Find the maximum value
- duplicate = is_duplicate(maximum_list, lis[max_index]) #Check for a duplicate
- if duplicate == -1: #If not duplicate
- maximum_list.append(lis[max_index]) #Add the item to the list
- lis.pop(max_index) #Remove the item from the list
- item += 1 #Increase the number of towers by one
- else:
- if lis[max_index][2] > maximum_list[duplicate][2]: #If the duplicate is larger than the one currently at that position
- maximum_list[duplicate] = lis[max_index] #Switch the item to the one that's bigger
- lis.pop(max_index) #Remove the duplicate item from the list
- return maximum_list
- def is_duplicate(lis, current_item):
- '''
- Check to see if there's a duplicate location in the list.
- If there is it returns the index number of that duplicate
- If not, it returns -1
- Takes a list of tower items and a single tower item
- Returns either an index number or -1
- '''
- for i in range(len(lis)): #For each item in the maximum list
- if current_item[0] == lis[i][0]: #If there's a duplicate
- return i #Return the value
- return -1 #Otherwise return -1
- def find_max(lis):
- '''
- Find the index of the item with the highest value
- Expects a list in the towers() format
- Returns an index number
- '''
- maximum = 0
- index = 0
- for item in range(len(lis)): #For each item in the list
- if lis[item][2] > maximum: #If the item is greater than the max
- maximum = lis[item][2] #Set the maximum number
- index = item #Set the maximum index
- return index
- def create_printout(lis):
- '''
- Create a printout of all the items
- '''
- for item in lis: #For item in list
- printout = "Centered at location "
- printout += str(item[0]) #Position
- if item[1] == 0: #If facing upwards
- printout += " facing upward scoring "
- else:
- printout += " facing downward scoring "
- printout += str(item[2]) #Value
- print(printout)
- def main():
- '''
- Main function
- '''
- if len(sys.argv) != 3: #Check if the args are not what's expected
- print("Usage: lasers.py <laser-file> <number-of-towers>")
- exit(1)
- file = open(sys.argv[1], "r") #File
- number_of_towers = int(sys.argv[2]) #Tower numbers
- for line in file:
- print(line)
- lst = line.split() #Split the line in the file
- for i in range(len(lst)):
- lst[i] = int(lst[i]) #Convert each item in the list from a string to an int
- print(number_of_towers, "lasers")
- towers_list = towers(lst) #Calculate all possible towers
- maximum_towers = max_towers(towers_list, number_of_towers) #Calculate the maximum towers
- create_printout(maximum_towers) #Print the values
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement