Question

In: Computer Science

Solve by using python function. use loop Input; Crossing Times = { 10,15,20,25} 1.Firstly person '1'...

Solve by using python function. use loop

Input; Crossing Times = { 10,15,20,25}

1.Firstly person '1' and '2' cross the path 
   with total time about 15 min(maximum of 10, 15) 

2.Now the person '1' will come back with total time of '10' minutes.

3. Now, person 1 and person 3 cross the path with total of about 20 minutes .(maximum of 10,20)

4. Now the person 1 will come back in 10 mintues

5. Lastly person 1 and person 4 will cross the path together in about 25 minutes(max of 10,25)

print the total sum of all(15+10+20+10+25)

Solutions

Expert Solution

I have written required comments along with code please go through them to understand the code.

Two basic things here to observe are

  1. while going time taken is max(two people crossing bridge)
  2. while coming back the person with minimum time comes back.

So we take two list one is persons who are left side  of bridge and other list ,who are right side of the bridge.

def total_time(left_side):
    """ Time calculation for crossing over bridge
    """
    total_time_taken = 0
    right_side = []
    while True:
        right_side.extend([left_side[0], left_side[1]]) # Extend new list means first two people crossed the bridge
        # print("right-",right_side) # Incase you need to see how each lists are updated please uncomment the print statements
        total_time_taken += max(left_side[0], left_side[1]) # Now time take to cross the brdige is maximum of two people times crossing the bridge
        left_side.pop(0) # so we remove first person
        left_side.pop(0) # after removing first person the second person replaces the first person we remove that person as well from the first list
        if left_side == []: # when the first list gets empty means everybody crossed the bridge we exit the loop
            break
        # print("left-",left_side)
        # the person going back to another to pick up another has minimum time so we add minum time person to arr again. means first list
        left_side.insert(0, min(right_side)) 
        # print("left-",left_side)
        total_time_taken += min(right_side) # we add the time taken by person going back it will be minimum time from the second list
        right_side.pop(right_side.index(min(right_side))) # so we remove the person who is crossing the bridge from second list 
        # print("right-",right_side)
    return total_time_taken
        
print("Time take to everyone cross the bridge:{}".format(total_time([10,5,20,25, 1]))

Output with Print Satements with change in left and right sides:

Output without print statements :

Output with a new example :

Please leave a comment for any further queries


Related Solutions

Using Python Question 1 Write an input function. Then use it to supply the input to...
Using Python Question 1 Write an input function. Then use it to supply the input to following additional functions: i) Print multiplication table of the number from 1 to 12. ii) Print the sum of all the numbers from 1 to up the number given. iii) Print if the number supplied is odd or even. Question 2 Write function that asks for input from a user. If the user types 'end', the program exits, otherwise it just keeps going.
Important: please use python. Using while loop, write python code to print the times table (from...
Important: please use python. Using while loop, write python code to print the times table (from 0 to 20, incremented by 2) for number 5. Add asterisks (****) so the output looks exactly as shown below.   Please send the code and the output of the program. ****************************************************************** This Program Shows Times Table for Number 5 (from 0 to 20) Incremented by 2 * ****************************************************************** 0 x 5 = 0 2 x 5 = 10 4 x 5 = 20 6...
Python Write a for loop with a range function and format output as currency Use an...
Python Write a for loop with a range function and format output as currency Use an input statement to ask the user for # of iterations using the prompt: #? [space after the ?] & assign to a variable Convert the variable to an integer Use a for loop and a range function to display all whole numbers from 1 to the user entered number Display the value of the item variable on screen in each iteration in the following...
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Using Python, use the following list (Temperature = [56.2,31.8,81.7,45.6,71.3,62.9,59.0,92.5,95.0,19.2,15.0]) to: - Create a loop to iterate...
Using Python, use the following list (Temperature = [56.2,31.8,81.7,45.6,71.3,62.9,59.0,92.5,95.0,19.2,15.0]) to: - Create a loop to iterate through each of the elements in the temperature list. - Convert each element of this list to a Celsius temperature and then, for each valid temperature in the list, print out both the original Fahrenheit temperature and the Celsius equivalent in this format: "32 degrees Fahrenheit is equivalent with 0 degrees Celsius."
Pythons code using idle Write an input function. Then use it to supply the input to...
Pythons code using idle Write an input function. Then use it to supply the input to following additional functions: i) Print multiplication table of the number from 1 to 12. ii) Print the sum of all the numbers from 1 to up the number given. iii) Print if the number supplied is odd or even.
Write a python program that can solve system of linear equations in three variables using input...
Write a python program that can solve system of linear equations in three variables using input function. Paste your program in a word document or notepad. Note that I am using pycharm. please use a not really complex codes, thanks
#Python program using loop instead of numpy or pandas. Write a function to enter n student...
#Python program using loop instead of numpy or pandas. Write a function to enter n student scores from 0 to 100 to represent student score, Count how many A (100 - 90), B(89-80), C(79-70), D(69-60), F(<60) We will use numpy array OR pandas later for this problem, for now use loop Invoke the function with 10 student scores, show the result as follow: Test Enter 10 scores Enter a student score: 99 Enter a student score: 88 Enter a student...
use python to solve Write a function called word_chain that repeatedly asks the user for a...
use python to solve Write a function called word_chain that repeatedly asks the user for a word until either (1) the user has entered ten words, or (2) the user types nothing and presses enter. Each time the user enters an actual word, your program should print all the words entered so far, in one long chain. For example, if the user just typed "orange", and the user has already entered "apple" and "banana", your program should print "applebananaorange" before...
Use python write a function that translates the input string into Pig Latin. The translation should...
Use python write a function that translates the input string into Pig Latin. The translation should be done word by word, where all words will be separated by only one space. You may assume that each word must have at least one vowel (a,e,i,o,u and uppercased counterparts), and there will be no punctuation or other special characters in the input string. The Pig Latin rules are as follows: For words that begin with consonants, all letters before the initial vowel...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT