summaryrefslogtreecommitdiffstats
path: root/upg2_2.py
blob: 6a0b84968a1789620bf887ee5a6f270cfe716e35 (plain) (blame)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def create_ten_list_while():
    l = []
    i = 0
    while i <= 10:
        l.append(i)
        i += 1
    return l

def create_ten_list_for():
    l = []
    for i in range(0, 10+1):
        l.append(i)
    return l

def create_zero_to_number_list_while(number):
    l = []
    i = 0
    while i <= number:
        l.append(i)
        i += 1
    return l

def create_zero_to_number_list_for(number):
    l = []
    for i in range(0, number+1):
        l.append(i)
    return l

def create_number_to_number_list_while(num1, num2):
    l = []
    i = num1
    while i <= num2:
        l.append(i)
        i += 1
    return l

def create_number_to_number_list_for(num1, num2):
    l = []
    for i in range(num1, num2+1):
        l.append(i)
    return l

import functools
def get_max_inline(integer_list):
    while True:
        return functools.reduce(lambda x,y: x if x > y else y, integer_list)

def get_max_while(integer_list):
    best = integer_list[0]
    i = 1
    while i < len(integer_list):
        best = integer_list[i] if integer_list[i] > best else best
        i += 1
    return best

def get_max_for(integer_list):
    best = integer_list[0]
    for num in integer_list[1:]:
        best = num if num > best else best
    return best

def get_min(integer_list):
    return functools.reduce(lambda x, y: x if x < y else y, integer_list)

def word_in_list_while(words, word):
    i = 0
    while i < len(words):
        if word == words[i]:
            return True
        i += 1
    return False

def word_in_list_for(words, word):
    for w in words:
        if word == w:
            return True
    return False

def count_integers_while(value_list):
    amount = 0
    i = 0
    while i < len(value_list):
        if type(value_list[i]) == int:
            amount += 1
        i += 1
    return amount

def count_integers_for(value_list):
    amount = 0
    for val in value_list:
        if type(val) == int:
            amount += 1
    return amount

def count_integers_inline(value_list):
    return functools.reduce(lambda x,y: x + 1 if type(y) == int else x, value_list, 0)

def average_while(values):
    s = 0
    i = 0
    while i < len(values):
        s += values[i]
        i += 1
    return s / len(values)

def average_for(values):
    s = 0
    for val in values:
        s += val
    return s / len(values)

def population(pop_a, rate_a, pop_b, rate_b):
    if pop_a == pop_b:
        return -1
    if rate_a <= rate_b:
        return -1
    years = 0
    while not pop_a > pop_b:
        years += 1
        pop_a *= 1 + (rate_a / 100)
        pop_b *= 1 + (rate_b / 100)
    return years

def population_2(pop_a, rate_a, pop_b, rate_b):
    if pop_a > pop_b:
        return population(pop_b, rate_b, pop_a, rate_a)
    return population(pop_a, rate_a, pop_b, rate_b)

def birthday(n, cur=365):
    if cur == (365-n):
        return 1
    if cur == 365:
        return 1 - (cur/365 * birthday(n, cur-1))
    return cur/365 * birthday(n, cur-1)