Question

write a python program that include a function named activity_selection() and take in two arguments, first...

write a python program that include a function named activity_selection() and take in two arguments, first one would be the number of tasks and the second argument would be a list of activities. Each activity would have an activity number, start time and finish time.

Example activity_selection input and output:

activity_selection (11, [[1, 1, 4 ], [2, 3, 5], [3, 0, 6], [4, 5, 7], [5, 3, 9], [6, 5, 9],[7, 6, 10], [ 8, 8, 11], [ 9, 8, 12], [10, 2, 14], [11, 12, 16] ]

In the above example the first activity set contains 11 activities with activity 1 starting at time 1 and finishing at time 4, activity 2 starting at time 3 and finishing at time 5, etc. The activities are not in any sorted order. Your results including the number of activities selected and their order should be outputted to the terminal. For the above example the results are: Number of activities selected = 4 Activities: 2 4 9 11

Note: There can be multiple optimal solutions.

please comments the program.

Homework Answers

Answer #1

SOLUTION-

I have solved the python code below with comments for easy understanding and given screenshot also :)


CODE-

some note-

sort the list according to their finish time
Select the first activity from the sorted list and append the activity name in res.
Do following for remaining activities in the sorted list. If the start time of this activity is greater than or equal to the finish.time of previously selected activity then select this activity and append activity name in res.

code-

#program
def activity_selection(n,activity_list):
    res=[]
    activity_list.sort(key=lambda activity_list:activity_list[2])#sort according finish time
    #print(activity_list)
    res.append(activity_list[0][0])
    i=0
    count=1
    #using the greedy approach
    for j in range(1,n):

        # If this activity has start time greater than or equal to the finish time of previously selected activity
        # then select it
        if activity_list[j][1] >= activity_list[i][2]:
            res.append(activity_list[j][0])
            i = j
            count+=1
    print("selected",count)   #print the count value
    print("Activities",res)   #print the activities

activity_selection (11, [[1, 1, 4 ], [2, 3, 5], [3, 0, 6],
                         [4, 5, 7], [5, 3, 9], [6, 5, 9],
                         [7, 6, 10], [ 8, 8, 11],[ 9, 8, 12],
                         [10, 2, 14], [11, 12, 16] ])

SCREENSHOT-

IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
This program is in C++, And please consider " sort pass #" for the output: Write...
This program is in C++, And please consider " sort pass #" for the output: Write a program that uses two identical arrays of eight integers. It should display the contents of the first array, then call a function to sort it using an ascending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using...
Flowchart + Python. Ask the user for a value. Then, ask the user for the number...
Flowchart + Python. Ask the user for a value. Then, ask the user for the number of expressions of that value. Use While Loops to make a program. So then, it should be so that 5 expressions for the value 9 would be: 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 Flowcharts and Python Code. Not just Python code.
PYTHON Ask the user for a value N (0 ≤ N < 10) Create a 2-D...
PYTHON Ask the user for a value N (0 ≤ N < 10) Create a 2-D list in an N X N structure with integers 1-(N*N) Print the list created Reverse the list such that (1) each list in the 2D list is reversed and (2) the order of each list in the outer list is reversed (see example) Print the reversed list Example Execution What size 2D list would you like to create? N> 3 The original list is:...
PYTHON: Using the function random.random_sample from numpy package write two functions: * poissonRV(seed, mean,n) that returns...
PYTHON: Using the function random.random_sample from numpy package write two functions: * poissonRV(seed, mean,n) that returns for given seed a bunch of n Poisson distributed random numbers with the provided mean. You have to numerically calculate the inverse distribution function. funnyDiceRV(seed,n) that returns for given seed a bunch of n random number which describe a biased die with distribution ℙ({1})=ℙ({2})=ℙ({3})=ℙ({4})=ℙ({5})=1/10P({1})=P({2})=P({3})=P({4})=P({5})=1/10 and ℙ({6})=1/2P({6})=1/2.
Write a program that sorts the content of a map by Values. Before we start, we...
Write a program that sorts the content of a map by Values. Before we start, we need to check the explanation of Java Map Interface and Java LinkedHashMap. This program can be implemented as follows: 1. Create a LinkedHashMap class <String (Country), String(Capital)> that stores Capitals of FIVE countries. 2. Use sortMap() for receiving content of the created class of LinkedHashMap and reorder it in a sorted Map. (sort in natural order). 3. You need to transfer the map into...
C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED ) Q]. Write a program...
C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED ) Q]. Write a program that displays all the prime numbers in the given array with the following constraint. Constraint: Only those prime numbers should be displayed whose location is a composite number. Although you may have several prime numbers in the array, only those prime numbers should be displayed which are stored at non-prime locations. Remember that the first position in an array corresponds to the location/index 0....
Given an integer named area which represents the area of a rectangle, write a function minPerimeter...
Given an integer named area which represents the area of a rectangle, write a function minPerimeter that calculates the minimum possible perimeter of the given rectangle, and prints the perimeter and the side lengths needed to achieve that minimum. The length of the sides of the rectangle are integer numbers. For example, given the integer area = 30, possible perimeters and side-lengths are: (1, 30), with a perimeter of 62 (2, 15), with a perimeter of 34 (3, 10), with...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
A program for generating random numbers on a computer is to be tested. The program is...
A program for generating random numbers on a computer is to be tested. The program is instructed to generate 100 single-digit integers between 0 and 9. The frequencies of the observed integers were as follows. At the 0.05 level of significance, is there sufficient reason to believe that the integers are not being generated uniformly? Integer 0 1 2 3 4 5 6 7 8 9 Frequency 12 8 8 5 11 11 8 11 13 13 (a) Find the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT