Question

In: Computer Science

Please solve the following question by Python3. Code the function calc_tot_playtime to calculate the total playtime...

Please solve the following question by Python3.

Code the function calc_tot_playtime to calculate the total playtime by user and game.

Each element in input is of the format

"user_id, game_id, total_playtime_in_minute".

Input is a list of strings of the above format.

Output should be sorted by user_id, game_id in ascending order.

Example:

Input: ["1,10,20", "1,15,10m", "1,10,10m", "2,10,40m", "2, 20, 10m"]

output: [('1', '10', 30), ('1', '15', 10), ('2', '10', 40), ('2', '20', 10)]

Functions are defined below:

def calc_total_playtime(input_data=None):

"""

your code here

:param: input_data: input list of strings

return output of tuples

index 0 -user_id

index 1 - game_id

index 2- total play time in minutes

"""

Solutions

Expert Solution

Please follow the comments to better understand the code.

Check the screenshot below to better understand the code in indentation form.

=====================================================================

def calc_total_playtime(input_data=None):
#STEP-1 Pre-process the string.
#Convert it into a list of string, using a for loop
temp=[]
for s in input_data:
user_id, game_id, playtime=s.split(',')
playtime=int(playtime[:-1]) #Remove 'm' from the string
temp.append([user_id,game_id,playtime])
  
  
#print(temp) #Check the temp data
  
#This is very important.
#We can sort the list using sorted() function and using a Lambda Function.
#Here we need to sort the data based on (user_id, game_id)
# i.e, they are 0th and 1st indexes of each of the temp list.
  
output = sorted(temp, key = lambda x: (x[0], x[1]))
  
#print(output)
#Now, if two or more users have same (user_id, game_id), then add their times
final_output=[]
sum=output[0][2]
i=0
while i<len(output)-1:
if output[i][0]==output[i+1][0] and output[i][1]==output[i+1][1]:
sum+=output[i+1][2]
else:
final_output.append([output[i][0],output[i][1],sum])
sum=output[i+1][2]
i+=1
final_output.append([output[i][0],output[i][1],sum])
#Return the output
return final_output
  

#Test the function here
#TEST-1
input_data=["1,10,20m", "1,15,10m", "1,10,10m", "2,10,40m", "2,20,10m","1,15,40m","2,20,50m","3,45,60m"]
print(calc_total_playtime(input_data))

#TEST-2
input_data=["1,10,20", "1,15,10m", "1,10,10m", "2,10,40m", "2,20,10m"]
print(calc_total_playtime(input_data))

=====================================================================================

==========================

Please feel free to reach out in comments if you have any doubts.

Please give a THUMBS UP...!!!

Happy Learning..!!

===========================


Related Solutions

Please use python3 Create the function team_average which has the following header def team_average(filename): This function...
Please use python3 Create the function team_average which has the following header def team_average(filename): This function will return the average number of games won by the Red Sox from a text file with entries like this 2011-07-02 Red Sox @ Astros Win 7-5 2011-07-03 Red Sox @ Astros Win 2-1 2011-07-04 Red Sox vs Blue Jays Loss 7-9 This function should create a file object for the file whose name is given by the parameter filename. If the file cannot...
Please solve the following game: Assume that a total $100 grant will be shared by the...
Please solve the following game: Assume that a total $100 grant will be shared by the three researchers, X, Y, and Z. Each person is rational and selfish. There are six proposals with different shares of (X, Y, Z) for choices as the following. Proposal I: (X, Y, Z) = (50, 40, 10) Proposal II: (X, Y, Z) = (60, 10, 30) Proposal III: (X, Y, Z) = (40, 20, 40) Proposal IV: (X, Y, Z) = (20, 30, 50)...
Please solve the following game: Assume that a total $100 grant will be shared by the...
Please solve the following game: Assume that a total $100 grant will be shared by the three researchers, X, Y, and Z. Each person is rational and selfish. There are six proposals with different shares of (X, Y, Z) for choices as the following. Proposal I: (X, Y, Z) = (50, 40, 10) Proposal II: (X, Y, Z) = (60, 10, 30) Proposal III: (X, Y, Z) = (40, 20, 40) Proposal IV: (X, Y, Z) = (20, 30, 50)...
Please solve the following game: Assume that a total $100 grant will be shared by the...
Please solve the following game: Assume that a total $100 grant will be shared by the three researchers, X, Y, and Z. Each person is rational and selfish. There are six proposals with different shares of (X, Y, Z) for choices as the following. Proposal I: (X, Y, Z) = (50, 40, 10) Proposal II: (X, Y, Z) = (60, 10, 30) Proposal III: (X, Y, Z) = (40, 20, 40) Proposal IV: (X, Y, Z) = (20, 30, 50)...
I need to modify the following code (using Python3), where Groceries.csv is of following form (Item...
I need to modify the following code (using Python3), where Groceries.csv is of following form (Item on 1st column, price on 2nd) Stewing beef,15.45 Ground beef,11.29 Pork chops,11.72 Chicken,7.29 Bacon,7.12 Wieners,4.33 Canned salmon,5.68 Homogenized milk,5.79 Partly skimmed milk,5.20 Butter,4.99 Processed cheese slices,2.53 Evaporated milk,1.89 Eggs,3.11 Bread,2.74 Soda crackers,3.27 Macaroni,1.45 Flour,4.54 Corn flakes,5.72 Apples,4.71 Bananas,1.56 Oranges,3.70 ... a. In the function createPricesDict(), create a dictionary of each product mapped to its price. b. Suppose we have another dictionary for our cart...
Please use C programming to write the code to solve the following problem. Also, please use...
Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...
PLEASE SOLVE I WILL RATE AND THUMBS UP Assembly code question Write an LC3 program to...
PLEASE SOLVE I WILL RATE AND THUMBS UP Assembly code question Write an LC3 program to compute the XOR (exclusive OR) operation. The program computes the XOR of two numbers stored in registers R0 and R1 and returns the result in register R3. To test your program, before computing the XOR load the two values from memory locations x4000 (into R0) and x4001 (into R1).
PLEASE DONT FORGET TO SOLVE THE ASSIGNMENT QUESTION MOST IMP: Ex1) Download the code from the...
PLEASE DONT FORGET TO SOLVE THE ASSIGNMENT QUESTION MOST IMP: Ex1) Download the code from the theory section, you will find zipped file contains the code of Singly linked list and Doubly linked list, in addition to a class Student. In Class SignlyLinkedList, 1. There is a method display, what does this method do? 2. In class Test, and in main method, create a singly linked list objet, test the methods of the list. 3. To class Singly linked list,...
The following VHDL code has errors and is not working in Umhdl. please solve the errors...
The following VHDL code has errors and is not working in Umhdl. please solve the errors for the following code and rewrite the code here and show the output. The code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.std_logic_unsigned.all; entity Car_Parking_System_VHDL is port ( clk,reset_n: in std_logic; -- clock and reset of the car parking system front_sensor, back_sensor: in std_logic; -- two sensor in front and behind the gate of the car parking system password_1, password_2: in std_logic_vector(1 downto 0); -- input...
a) Please calculate the Total Factor Productivity of a countryfor a year with the following...
a) Please calculate the Total Factor Productivity of a country for a year with the following data on Real GDP of the country along with other required information:Y   = $ 2,000 billionL   =     40 billion hoursK   = $ 10, 000 billionα   = 0.7b) If the labour force increases by 20%, how it will affect the total factor productivity?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT