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
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...
Question: Write a C++ program to ask the user to give you the number of rows...
Question: Write a C++ program to ask the user to give you the number of rows and number of columns, then make the multiplication table by using nested loop. For example, if the user enters 3 for rows and 4 for columns then the multiplication table would be like the following: 1    2   3     4 1   1 2 3 4 2   2    4      6    8 3    3 6 9 12
Write a program that asks the user for a number in the range 1 through 12....
Write a program that asks the user for a number in the range 1 through 12. The program should display the corresponding month of the year, where 1 = January, 2 = February, 3 = March, 4 = April, 5 = May, 6 = June, 7 = July, 8 = August, 9 = September, 10 = October, 11 = November, and 12 = December. The program should display an error message if the user enters a number that is outside...
Write a Python program that prompts a user for two integers and stores the two integers...
Write a Python program that prompts a user for two integers and stores the two integers the user types into the shell. Print the product, float division, integer division, remainder, sum, and difference of the two integers to the console. The entire equation must be printed (see below for formatting). Only display 2 digits after the decimal place for results that are floats. Include a module docstring that summarizes the program.. Sample Run 1: Enter an integer: 5 Enter another...
Please use Python 3 4). Write a program that asks the user to enter 10 numbers....
Please use Python 3 4). Write a program that asks the user to enter 10 numbers. The program should store the numbers in a list and then display the following data: • The lowest number in the list • The highest number in the list •The total of the numbers in the list • The average of the numbers in the list   Sample input & output: (Prompt) Enter Number 1: (User enter) 4 (Prompt) Enter Number 2: (User enter) 7...
WRITE it in Python! how to Write a method named smallest largest that asks the user...
WRITE it in Python! how to Write a method named smallest largest that asks the user to enter numbers, then prints the smallest and largest of all the numbers typed in by the user. You may assume the user enters a valid number greater than 0 for the number of numbers to read. Here is an example dialogue: How many numbers do you want to enter? 4 Number 1: 5 Number 2: 11 Number 3: -2 Number 4: 3 Smallest...
make a box plot explaining the Variations that affect the lead time of serving a hot...
make a box plot explaining the Variations that affect the lead time of serving a hot coffee. Number of Customer Order Time (minutes) Preparation time (minutes) 1 3 11 2 5 11 3 4 12 4 5 13 5 5 12 6 4 9 7 3 7 8 3 9 9 4 10 10 5 11 11 3 7 12 4 14 13 5 13 14 2 8 15 3 10 16 5 12 17 4 10 18 3 9...
Intro to JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers...
Intro to JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by space as input and prints the sum of all the integers between them, including the two given numbers. Note that the numbers can appear in either order. You may assume that both numbers are between –10, 000 and 10, 000. For example, if the input is as follows: 10 4 the output should be 49, since 10+9+8+7+6+5+4=49. Similarly, if the input is...
Write a C++ program to demonstrate thread synchronization. Your main function should first create a file...
Write a C++ program to demonstrate thread synchronization. Your main function should first create a file called synch.txt. Then it will create two separate threads, Thread-A and Thread-B. Both threads will open synch.txt and write to it. Thread-A will write the numbers 1 - 26 twenty times in nested for loops then exit. In other words, print 1 - 26 over and over again on separate lines for at least 20 times. Thread-B will write the letters A - Z...
Write a python program that will perform text analysis on an input text using all of...
Write a python program that will perform text analysis on an input text using all of the following steps: 1. Take the name of an input file as a command line argument. For example, your program might be called using python3 freq.py example1 to direct you to process a plain text file called example. More information is given on how to do this below. 2. Read the contents of the file into your program and divide the text into a...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT