Question

In: Computer Science

Hello, I am having trouble getting started on my project and building these functions. How do...

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.

Solutions

Expert Solution

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)))


Related Solutions

I am having a hard time getting started on how to do this assignment. I would...
I am having a hard time getting started on how to do this assignment. I would like some ideas on how to start the MEMO. and maybe some ideas on what you would include. But I don't necessarily need the assignment completed for me. just need ideas!!! One routine negative message in the form of an internal memo. 400-500 word message. Single-spaced, with 1-inch margins on all sides. Objective: To announce organizational restructuring, one routine negative message addressed to employees....
Hello, I am having trouble with my Advanced Accounting Ch 18 homework. Accounting for nonprofts. Here...
Hello, I am having trouble with my Advanced Accounting Ch 18 homework. Accounting for nonprofts. Here are a few questions: At the beginning of the year, Nutrition Now, a health and welfare not-for-profit entity, had the following equity balances: Net assets without donor restriction $400,000 Net assets with donor restriction $290,000 1. Unrestricted contributions (pledges) of $250,000, to be collected in cash in thirty days, before the end of the year, are received. 2. Salaries of $27,000 are paid with...
Hello! I am having trouble starting this program in Java. the objective is as follows: "...
Hello! I am having trouble starting this program in Java. the objective is as follows: " I will include a text file with this assignment. It is a text version of this assignment. Write a program that will read the file line by line, and break each line into an array of words using the tokenize method in the String class. Count how many words are in the file and print out that number. " my question is, how do...
Hello, I am having difficulty with this assignment. I chose Amazon as my stock, but I...
Hello, I am having difficulty with this assignment. I chose Amazon as my stock, but I am confused on what they're asking :             Choose a stock that interests you. Utilizing Bloomberg (or other financial websites) as a source of data, collect the following      information:                         a. The stock’s Beta                         b. The rate of return on the market (S&P 500 Index)                         c. The risk-free rate (rRF)                         d. The last dividend paid (D0)...
I'm having trouble with my ZeroDenominatorException. How do I implement it to where if the denominator...
I'm having trouble with my ZeroDenominatorException. How do I implement it to where if the denominator is 0 it throws the ZeroDenominatorException and the catch catches to guarantee that the denominator is never 0. /** * The main class is the driver for my Rational project. */ public class Main { /** * Main method is the entry point to my code. * @param args The command line arguments. */ public static void main(String[] args) { int numerator, denominator =...
hello, I am having an issue with a question in my highway engineering course. the question...
hello, I am having an issue with a question in my highway engineering course. the question is: An equal tangent sag vertical curve has an initial grade of –2.5%. It is known that the final grade is positive and that the low point is at elevation 82 m and station 1 + 410.000. The PVT of the curve is at elevation 83.5 m and the design speed of the curve is 60 km/h. Determine the station and elevation of the...
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
Getting an error with my for loop.  How do I correct it?  I am supposed to: Continuously prompt...
Getting an error with my for loop.  How do I correct it?  I am supposed to: Continuously prompt for the number of minutes of each Rental until the value falls between 60 and 7,200 inclusive. For one of the Rental objects, create a loop that displays Coupon good for 10percent off next rental as many times as there are full hours in the Rental. ///////////////////////////////RentalDemo package java1; import java.util.Scanner; public class RentalDemo { public static void main(String[] args) {    Rental object1 =...
Needs to be coded in Python. Hello i am working on a project for my computer...
Needs to be coded in Python. Hello i am working on a project for my computer programming course and i am having trouble with one part. The code needs to be able to produce two versions of an output given inputs by the user. for instance. Here is the code i have to produce the following output. The input from the user are num1 num2 and asc which is just asking if they want the output to be ascending or...
I am working on these study questions and am having trouble understanding how it all works...
I am working on these study questions and am having trouble understanding how it all works together. Any help would be greatly appreciated!! An all equity firm is expected to generate perpetual EBIT of $50 million per year forever. The corporate tax rate is 0% in a fantasy no tax world. The firm has an unlevered (asset or EV) Beta of 1.0. The risk-free rate is 5% and the market risk premium is 6%. The number of outstanding shares is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT