-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task2.py
55 lines (43 loc) · 1.73 KB
/
Task2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
Read file into texts and calls.
It's ok if you don't understand how to read files
You will learn more about reading files in future lesson
"""
import csv
with open('texts.csv', 'r') as f:
reader = csv.reader(f)
texts = list(reader)
with open('calls.csv', 'r') as f:
reader = csv.reader(f)
calls = list(reader)
#step1:create a function which build a dictionary includes phone and call duration.
call_time = {}
def check_call_time(phone,call_duration):
"""
iterable the phone and call duration from dictionary.
"""
if phone in call_time:
call_time[phone] += int(call_duration)
else:
call_time[phone] = int(call_duration)
#step2:use above function and iterable the original calls list.
for call in calls:
check_call_time(call[0],call[3])
check_call_time(call[1],call[3])
#step3:output the result as max phone# and max time duration with the function of "MAX"
longest_call = max(call_time,key=call_time.get)
print("{} spent the longest time,{} seconds,on the phone during September 2016.".format(longest_call,call_time[longest_call]))
"""
TASK 2: Which telephone number spent the longest time on the phone
during the period? Don't forget that time spent answering a call is
also time spent on the phone.
Print a message:
"<telephone number> spent the longest time, <total time> seconds, on the phone during
September 2016.".
HINT: Build a dictionary with telephone numbers as keys, and their
total time spent on the phone as the values. You might find it useful
to write a function that takes a key and a value and modifies a
dictionary. If the key is already in the dictionary, add the value to
the key's existing value. If the key does not already appear in the
dictionary, add it and set its value to be the given value.
"""