In: Computer Science
Hello, I am having trouble getting started on my project and building these functions. How do I build a function that continuously adds new "slices" to the list if they are below/above the size limit? I didn't copy the entire problem, but just for reference, when the code is run it will take user input for size limit (L), time cost for a random slice(R), and time cost for an accurate slice(A).
Question:
In real life, a steak is a 3-dimensional object, but for this project, let’s simplify the situation so that we are only chopping along a single dimension. In other words, imagine we just want really thin slices of beef (but the slices can be as tall and long as the steak itself). We can represent the length of steak using a numeric interval, let’s say 0 to 100 (where 0 is the left-most edge of beef and 100 is the right-most edge, and 50 is right in the middle, etc.). Then we can represent the location of a “slice” numerically so that, for instance, if we wanted to slice the beef into exact quarters we would have to cut at lengths 25, 50, and 75. Indeed, let’s agree to store all of the slices we make as an ordered list of numbers, where [0,100] represents an uncut steak, [0,25,50,75,100] represents a steak cut into even quarters, and [0, 13, 46, 77, 100] represents a steak cut into random, uneven quarters.
First, write two functions called fast slices and slow slices. Each function should take two inputs, slice list and size limit. The slice list input represents the current list of slices (see previous paragraph), and size limit represents the size we’re trying to get all pieces to be smaller than or equal to. The fast slices function should take the slice list and continually add random slices until all pieces are within the size limit. The slow slices function should take the slice list and continually look for pieces larger than the size limit and (from left to right) slicing them into smaller pieces with size exactly equal to the size limit. Both fast slices and slow slices must return the new slice list. Second, write another function called ave chop time which takes six inputs: slice list, fast limit, slow limit, fast cost, slow cost, and n. The slice list once again represents a list of slices (probably just an uncut steak [0,100]), the fast limit is the size limit for the fast slices, slow limit is the size limit for the slow slices, fast cost is the time it costs to do a fast slice, and slow cost is the time it costs to do a slow slice.
The function should take the slice list, run it through the fast slices function using fast limit as the size limit, then run it through the slow slices function using slow limit as the size limit. After each function, you can calculate the number of slices performed by comparing the length of the slice list before and after the function. Once you know the number of fast slices and slow slices, multiply them by fast cost and slow cost, respectively, and add together to calculate the total time it took to chop the beef. Finally, this whole process should be repeated n times and the average total time should be returned.
Answer:
Program code screen shot:
Sample Output:
Program code to copy:
# import the random file
import random
# function to check whether the given slice_list is within the
# given size. This function returns a boolean value.
def check_Size_Limit(slice_list, size_limit):
# creat a new size list
sizes = []
# in descending order create the sizes
for i in range(len(slice_list)-1):
sizes.append(slice_list[i+1] - slice_list[i])
# check if the max of the sizes is less than
# size_limit then return the true
if max(sizes) < size_limit:
return True
# otherwise return false
return False
# function to cut the slice and return the slice list
def cut_slice(sliceAt, size_limit):
# set empty list
large = [0]
# set the smaller value
small = large[-1] + random.randint(1, size_limit)
# check and upadate the large and small values
# basing of the small value with sliceAt value(index)
while small < sliceAt:
large.append(small)
small = large[-1] + random.randint(1, size_limit)
# return the list containing the values
return(large[1:])
# function to cut the beef in fastest way to slice.
# This function uses the random value to cut the slice
# This function will return a new list of slicing list
def fast_slice_list(slice_list, size_limit):
# get the maximum value
maximum_value = max(slice_list)
# if slice list are within size limit
# then return slice_list
if check_Size_Limit(slice_list, size_limit):
return slice_list
# if not create the new slice list
new_slice_list = [0]
new_beef_slice = new_slice_list[-1]+random.randint(1, size_limit)
while new_beef_slice <= maximum_value:
new_slice_list.append(new_beef_slice)
new_beef_slice = new_slice_list[-1] + random.randint(1, size_limit)
new_slice_list.append(maximum_value)
return new_slice_list
# function to cut the beef in slowest way to slice
def slow_slice_list(slice_list, size_limit):
maximum_value = max(slice_list)
# if the size limit of te slices are within the
# limit, then return te slice list
if check_Size_Limit(slice_list, size_limit):
return slice_list
# otherwise create a new slice list
new_slice_list = []
for slice in slice_list:
if slice > size_limit:
new_slice_list.extend(cut_slice(slice, size_limit))
else:
new_slice_list.append(slice)
new_slice_list.append(maximum_value)
# return the new list
return new_slice_list
# function to generate the average chop time when executed
# for n times
def ave_chop_time(slice_list,fast_limit, slow_limit, fast_cost, slow_cost, n):
# total cost set to 0
totalCost = 0
# loop to find the total cost for n times
for i in range(0,int(n)):
# get the fast slice list
new_fast_slice_list = fast_slice_list(slice_list,fast_limit)
# get the slow slice list
new_slow_slice_list = slow_slice_list(slice_list, slow_limit)
# compute the fast and the slow slice cost
fast_slice_cost = (len(new_fast_slice_list) - len(slice_list)) * fast_cost
slow_slice_cost = (len(new_slow_slice_list) - len(slice_list)) * slow_cost
# compute the total cost
totalCost += fast_slice_cost + slow_slice_cost
# return the average cost
return totalCost/n
# main function to test
if __name__ == "__main__":
print("Welcome to my average chop time calculator!")
L = float(input("Give the overall size limit: "))
R = float(input("Give the time cost for a random slice: "))
A = float(input("Give the time cost for an accurate slice: "))
print("The average chopping times by random slice size limit: ")
print("{:18} {:18}".format("fast_limit", "ave_chop_time"))
for i in range(0, 100, 5):
print("{:<18f} {:<18f}".format(L+i, ave_chop_time([0,100], L+i, L, R, A, 100)))