-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrain.py
79 lines (58 loc) · 1.18 KB
/
rain.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import sys
import os
from collections import Counter
filename = input('Which data file do you want to use? ')
if not os.path.exists(filename):
print(f'There is no file named {filename} in the working directory, giving up...')
sys.exit()
try:
number = int(input('How many decilitres of water do you want to pour down? '))
if number<0:
raise Exception('')
except:
print("Only nonnegative integer accepted.")
rainfile= open(filename,'r')
list = []
rows=0
columns=0
for line in rainfile:
rows=rows+1
for i in line:
try:
if (int(i)):
list.append(int(i))
if rows==1:
columns=columns+1
except:
pass
#print(columns*rows)
count=Counter(list)
count_l=dict(count)
count_l=(dict(sorted(count.items())))
#print(count_l)
total=0
no=number
if no!=0:
count=1
#print(no)
for i in count_l.keys():
if(no==0):
break
val=count_l[i]
# print(val)
total=total+val
if((no-total)<0):
# print("IFFFFF")
count=count+(no/total)
no=0
#print(count)
else:
#print("ELSEEEEE")
no=no-total
count=count+1
if(no!=0):
no=no/(columns*rows)
count=count+no
else:
count=0
print(f'The water rises to {count:.2f} centimetres.')