-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtools.py
153 lines (135 loc) · 3.79 KB
/
mtools.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
def numbercheck(input_list):
try:
sum(input_list)
return True
except:
return False
def am(input_list):
"""
Calculate the Arithmetic Mean
Input: list (list/tupple/set/etc) of numbers
"""
if numbercheck(input_list) == False:
print("I need a list of numbers as input.")
print("Your input was:", input_list)
return
return sum(input_list) / len(input_list)
def sd(input_list):
"""
Calculate the Standard Deviation
Input: list (list/tupple/set/etc) of numbers
"""
if numbercheck(input_list) == False:
print("I need a list of numbers as input.")
print("Your input was:", input_list)
return
try:
if len(input_list) < 2:
print("I need at least 2 numbers to calculate the standard deviation!")
return
except:
print("I need a list of numbers with at least 2 numbers to calculate the standard deviation!")
return
mean = am(input_list)
s = sum([(member - mean) ** 2 for member in input_list])
n = len(input_list)
return (s / (n - 1)) ** 0.5
def fiboP(length, starting=0):
"""
Generate Fibonacci sequence
Output: prints the sequence to the screen
Input: Lenght of numbers to output
Optional Input: Starting point:
- 0: 0 and 1 (default)
- 1: 1 and 1
- 2: 1 and 2 (Fibonacci's way)
"""
if starting == 0:
num1 = 0
num2 = 1
elif starting == 1:
num1 = 1
num2 = 1
elif starting == 2:
num1 = 1
num2 = 2
else:
print("Invalid starting option. Valid options are:")
print(" 0 for 0 and 1 (default)")
print(" 1 for 1 and 1")
print(" 2 for 1 and 2 (Fibonacci's way)")
if length < 1:
print("I need the series to have a non-zero length.")
return
elif length == 1:
print(num1)
return
elif length == 2:
print(num1, num2)
return
else:
next_number = num1 + num2
count = 0
print(num1, end=" ")
print(num2, end=" ")
while count <= length - 3:
print(next_number, end=" ")
count += 1
num1, num2 = num2, next_number
next_number = num1 + num2
print()
return
def fiboS(length, starting=0):
"""
Generate a Fibonacci sequence
Output: Fibonacci sequence as a list
Input: Lenght of numbers to output
Optional Input: Starting point:
- 0: 0 and 1 (default)
- 1: 1 and 1
- 2: 1 and 2 (Fibonacci's way)
"""
if starting == 0:
num1 = 0
num2 = 1
elif starting == 1:
num1 = 1
num2 = 1
elif starting == 2:
num1 = 1
num2 = 2
else:
print("Invalid starting option. Valid options are:")
print(" 0 for 0 and 1 (default)")
print(" 1 for 1 and 1")
print(" 2 for 1 and 2 (Fibonacci's way)")
if length < 1:
print("I need the series to have a non-zero length.")
return
elif length == 1:
fiboSequence = [num1]
return fiboSequence
elif length == 2:
fiboSequence = [num1, num2]
return fiboSequence
else:
next_number = num1 + num2
count = 0
fiboSequence = [num1, num2]
while count <= length - 3:
fiboSequence.append(next_number)
count += 1
num1, num2 = num2, next_number
next_number = num1 + num2
return fiboSequence
#Examples and Testing
if __name__ == '__main__':
A = [1, 2, 7]
print("For A defined as", A)
print("The mean is", am(A))
print("The standard deviation is", sd(A))
print("Example mtools.fiboP(10, 0):")
fiboP(10, 0)
print("Example mtools.fiboS(13, 1):")
B = fiboS(13, 1)
print(B)