-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsgt_20220922_exer3_homework_functions.py
45 lines (38 loc) · 1.8 KB
/
sgt_20220922_exer3_homework_functions.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
# # 3. City Population
# The city has a known population p0
# A percentage of population perc is added each year
# Every year a certain number of delta also arrive (or leave)
# We need to know when (if at all) the city will reach a population of p
# Write a function get_city_year (p0, perc, delta, target_p) that returns the years (full) when target_p is reached.
# If target_p cannot be reached, then we return -1
# Example:
# get_city_year(1000,2,50,1200) -> 3
# 1000 + 1000 * 0.02 + 50 => 1070 after the 1st year
# notice that we have an integer (whole number) percentage, so the population after the 1st year is 1070, not 1070.4 or 1070.6
# 1070 + 1070 * 0.02 + 50 => 1141 after the 2nd year
# 1141 + 1141 * 0.02 + 50 => 1213 after the 3rd year
# so the function here returns 3 and is done
# PS. Note that we give perc as a percentage to be converted to a decimal number.
# More test examples:
# get_city_year(1000, 2, -50, 5000) -> -1
# get_city_year(1500, 5, 100, 5000) -> 15
# get_city_year(1500000, 2.5, 10000, 2,000,000) -> 10
# the trickiest case is something like this
# get_city_year(1000, -3, 50, 2000) -> -1 is the correct answer but how to get there?
def get_city_year(p0, perc, delta, target_p):
current_p= p0
my_counter=0
if p0 >= target_p:
return 0
while current_p < target_p:
growth = int(current_p*perc/100+delta) # humans are not floats! :)
if growth <= 0: # stagnation or decline
print(f"stagnation or decline after {my_counter} years at {current_p}")
return - 1
current_p = current_p + growth
my_counter += 1
return my_counter
print(get_city_year(1000, 2, -50, 5000)) # -> -1
print(get_city_year(1500, 5, 100, 5000)) # -> 15
print(get_city_year(1500000, 2.5, 10000, 2_000_000)) # -> 10
print(get_city_year(1000, -3, 50, 2000))