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...
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 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)...
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).
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?
Please solve the following problems in their entirety. That means you will calculate a) all appropriate...
Please solve the following problems in their entirety. That means you will calculate a) all appropriate sums of squares, b) all appropriate mean squares, c) F statistics, d) critical value, and e) η2. Additionally, you are going to f) explain what happens to the null(s), g) relate the conclusion back to the problem, and h) perform any required post hoc tests with explanations 1. There is a study where the dependent variable is movie ratings. One independent variable is sex...
what is the code to solve and graph the following function in matlab dv/dt=80.5(1-e^(.4t))
what is the code to solve and graph the following function in matlab dv/dt=80.5(1-e^(.4t))
PLEASE SOLVE THIS QUESTION WITHOUT CHANGING THE SOURCE CODE POSTED BELOW. (ONLY ADDING) #include <iostream> using...
PLEASE SOLVE THIS QUESTION WITHOUT CHANGING THE SOURCE CODE POSTED BELOW. (ONLY ADDING) #include <iostream> using namespace std; const int arrSize = 3; // QUESTION ii. COMPLETE A displayMatrix(...) FUNCTION HERE // QUESTION iii. COMPLETE A transposedMatrix(...) FUNCTION HERE // QUESTION iv. COMPLETE A calculateTotal(...) FUNCTION HERE int main(){ // variable declaration int mat[arrSize][arrSize], transMat[arrSize][arrSize] = {0}, total = 0; // QUESTION i. Prompt user to enter 9 integer numbers and fill in into the matrix cout << "\nThe original...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT