Question

In: Computer Science

Python Pierrette is at Legoland. She enjoys all the rides but hates to wait in lines....

Python

Pierrette is at Legoland. She enjoys all the rides but hates to wait in lines. Thanks to technology,

she has an app that tells her for each ride i a time ti when there is no line. However, she wants to

take the rides in increasing fun factor order. Pierrette assigns each ride i a fun factor fi . What is

the maximum number of rides she can take?

Make sure that your code runs in O(n2 ) time in order to pass all the test cases (n is the number of

rides)

# [last_name] [first_name], [student_ID]

from typing import List


def max_num_rides(rides: List[List[int]]) -> int:

    """Calculate the maximum number of rides one can take with given constraints.

    Pierrette is at Legoland. She enjoys all the rides but hates to wait in lines.

    Thanks to technology, she has an app that tells her for each ride a time when

    there is no line. However, she wants to take the rides in increasing fun factor

    order. Pierrette assigns each ride a fun factor. This function takes pairs of

    times and fun factors for all the rides, and returns the maximum number of

    rides Pierrette can take.

    Arguments:

        rides (list): A list of [time, fun factor] pairs. For each ride, the rides

            list contains a pair of time and fun factor. Both the time and the fun

            factor are integers. Assume that it takes no time to take a ride. You

            may further assume that all the time values are distinct and all the

            fun factor values are also distinct.

    Returns:

        int: The maximum number of rides Pierrette can take.

    Examples:

        >>> print(max_num_rides([[2, 5], [1, 3]]))

        2

        >>> print(max_num_rides([[4, 2], [42, 3], [59, 8], [12, 4], [1, 5], [5, 7]]))

        3

    """

    pass # FIXME

Solutions

Expert Solution

This problem is nothing but finding the length of longest increasing subsequence. First we extract the times in that order where the fun factor will be increasing, then we will find the LCS of the generated list.

Python3 code:

# [last_name] [first_name], [student_ID]
from typing import List

def max_num_rides(rides: List[List[int]]) -> int:
    """Calculate the maximum number of rides one can take with given constraints.
    Pierrette is at Legoland. She enjoys all the rides but hates to wait in lines.
    Thanks to technology, she has an app that tells her for each ride a time when
    there is no line. However, she wants to take the rides in increasing fun factor
    order. Pierrette assigns each ride a fun factor. This function takes pairs of
    times and fun factors for all the rides, and returns the maximum number of
    rides Pierrette can take.

    Arguments:
        rides (list): A list of [time, fun factor] pairs. For each ride, the rides
            list contains a pair of time and fun factor. Both the time and the fun
            factor are integers. Assume that it takes no time to take a ride. You
            may further assume that all the time values are distinct and all the
            fun factor values are also distinct.

    Returns:
        int: The maximum number of rides Pierrette can take.

    Examples:
        >>> print(max_num_rides([[2, 5], [1, 3]]))
        2
        >>> print(max_num_rides([[4, 2], [42, 3], [59, 8], [12, 4], [1, 5], [5, 7]]))
        3
    """

    fun_time =[]
    for pair in rides:
        fun_time.append([pair[1], pair[0]])
    
    fun_time.sort()

    times = [x[1] for x in fun_time]
    n = len(times)
    
    # Declare the list for LIS 
    lis = [1]*n 
  
    # Compute optimized LIS values in bottom up manner 
    for i in range (1, n): 
        for j in range(i): 
            if times[i] > times[j] and lis[i] < lis[j] + 1 : 
                lis[i] = lis[j] + 1 

    return max(lis)

    pass # FIXME

# Test the function
print(max_num_rides([[2, 5], [1, 3]]))
print(max_num_rides([[4, 2], [42, 3], [59, 8], [12, 4], [1, 5], [5, 7]]))

Since we have used sort once and there is one nested loop, So, the time complexity effectively will be O(n2).

Please refer to the following pictures for the source code:

Testing the above code: (It matches with the desired output)


Related Solutions

1)  Write a python program that opens a file, reads all of the lines into a list...
1)  Write a python program that opens a file, reads all of the lines into a list of strings, and closes the file. Use the Readlines() method. Test your programing using the names.txt file provided. 2) Convert the program into a function called loadFile, that receives the file name as a parameter and returns a list of strings. 3) Write a main routine that calls loadFIle three times to load the three data files given into three lists. Then choose a...
The following data comparing wait times at two rides at Disney are listed below: Position Pirates...
The following data comparing wait times at two rides at Disney are listed below: Position Pirates Splash Mountain Sample Size 32 30 Average Wait Time (In Minutes) 14.68 18.77 Population Standard Deviation 11.87 16.79 What is the 98% confidence interval for the difference in wait times between pirates and splash mountain? What is the test statistic for testing to see if there is a significant difference in wait times between pirates and splash mountain?
Missy is 3 years old. She enjoys being with mom and grandma in the kitchen. She...
Missy is 3 years old. She enjoys being with mom and grandma in the kitchen. She pulls a chair close to the stove, climbs on the chair and looks inside the pot of boiling water. The steam is hot on her face, she steps back and as she falls off the chair, the boiling water is pulled down on her. Missy is rushed to the emergency department. Grandma has put some butter on her fingers, but her face was not...
8. Analysis of a replacement project Dismiss All Please Wait . . . Please Wait... At...
8. Analysis of a replacement project Dismiss All Please Wait . . . Please Wait... At times firms will need to decide if they want to continue to use their current equipment or replace the equipment with newer equipment. The company will need to do replacement analysis to determine which option is the best financial decision for the company. Price Co. is considering replacing an existing piece of equipment. The project involves the following: • The new equipment will have...
Suppose that Lynn enjoys sugar in her coffee. She has very particular preferences, and she must...
Suppose that Lynn enjoys sugar in her coffee. She has very particular preferences, and she must have exactly three spoonfuls of sugar for each cup of coffee. Let C be the number of cups of coffee, and S be the number of spoonfuls of sugar. Also, let PC be the price of a cup of coffee. Suppose Lynn has $12 to spend on Coffee and Sugar. Also, the price of Sugar is $.20 per spoonful. Graph Lynn’s Price consumption curve...
1. The demand for labor Dismiss All Please Wait . . . Please Wait... Consider Live...
1. The demand for labor Dismiss All Please Wait . . . Please Wait... Consider Live Happley Fields, a small player in the strawberry business whose production has no individual effect on wages and prices. Live Happley’s production schedule for strawberries is given in the following table: Labor Input Total Output (Number of workers) (Pounds of strawberries) 0 0 1 18 2 34 3 48 4 60 5 70 Suppose that the market wage for strawberry pickers is $170 per...
Anna Laura rides her bike. First she travels 3.5 km due east, then she takes a...
Anna Laura rides her bike. First she travels 3.5 km due east, then she takes a left turn and travels 2.5 km due north. Draw a graph illustrating the motion; be sure to choose a valid coordinate system and indicate the length scale of your axes. Draw the displacement vector x. Write the vector x include the magnitude and the direction; the direction can be an angle relative to one of your axes.
How can a theme park eliminate waiting in long lines including rides etc. How much will...
How can a theme park eliminate waiting in long lines including rides etc. How much will they cost? What resources and expenditures will your solution entail?
Case study Barbara is a trained counsellor and thoroughly enjoys the level of human interaction she...
Case study Barbara is a trained counsellor and thoroughly enjoys the level of human interaction she encounters in the workplace. You are Barbara’s manager. You find Barbara personable, trustworthy and always available to listen to you and others when they have a work-related issue or grievance. As a result, Barbara has developed positive relationships with many of her colleagues, increasing her job satisfaction. In a meeting, Barbara puts forward the idea of implementing a mentoring program in the organisation. Troy,...
Leah realizes that she cannot wait until her appointment to receive medical care; she quickly goes...
Leah realizes that she cannot wait until her appointment to receive medical care; she quickly goes to the emergency room. After a mountain of questions about Leah’s symptoms and medical history, the doctors decide to admit her for further testing. She is transferred to the neurology unit and the doctors request she undergo a series of laboratory tests, including an MRI and a spinal tap. In the meantime, the doctors prescribe high dosages of steroids to help alleviate the symptoms....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT