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...
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...
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...
Python: Working with CSV file --> How would I write a function that will return a...
Python: Working with CSV file --> How would I write a function that will return a list of lists, with the frequency and the zip code. Organize the list in decreasing order of frequency (Print the number of suppliers and the zip code for the 10 most common zip codes in the file. ) An example of three items from the 720 zip codes results in: [ ... [9, '65616'], [8, '94573'], [8, '63103'] ...]. This tells us that Branson,...
(Python Programming) Write a program that prompts a user for a positive integer and then uses...
(Python Programming) Write a program that prompts a user for a positive integer and then uses a loop to calculate and display the sum of specific fractions as follows: Let's say the user enters 5, then your program will compute: 1/5 + 2/4 + 3/3 + 4/2 + 5/1 which is 8.7.
Write a Python program that reads in the month, day, and year of a date and...
Write a Python program that reads in the month, day, and year of a date and prints it in the dd/mm/yyyy format or mm/dd/yyyy format, the format depending on the user’s preference. The program also prints the message It is a magic date If the product of month and day is equal to the last two digits of the year. For example, April 20 1980 is a magic date because April (numeric value 4) multiplied by 20 is equal to...
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
(In Python) Complete the isPrime() function to take an int and return True if it is...
(In Python) Complete the isPrime() function to take an int and return True if it is a prime number and False if it is not. Note: A prime number is not evenly divisible (meaning, the remainder is not 0) for all numbers smaller than itself (down to 2). For example, if num is 7, then you could test: if 7 remainder 6 is not 0, then if 7 remainder 5 is not 0, then if 7 remainder 4 is not...
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:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT