-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.py
More file actions
44 lines (38 loc) · 1.7 KB
/
utility.py
File metadata and controls
44 lines (38 loc) · 1.7 KB
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
import warnings
# helper method to check the formatting of a input message
def check_input_message_formatting(message: str, message_type):
warnings.filterwarnings('always', '.*info.*', ) # always display warnings
if type(message) is not str:
raise TypeError(message_type + " must be a string")
# make the input string upper case
message_temp = message.upper()
# remove all special characters
message_temp = [char for char in message_temp if 65 <= ord(char) <= 90]
message_formatted = "".join(message_temp)
# check if characters have been removed from the input string
if len(message_formatted) != len(message):
warnings.warn(message_type + " info: Special characters and/or white spaces have been removed, lowercase characters has been formatted to UPPERCASE")
return message_formatted
# check if the output sting has been modified respect to the input string
if message_formatted != message:
warnings.warn(message_type + "info : Lowercase characters has been formatted to UPPERCASE")
return message_formatted
# Helper method to calculate double factorial on a number n, i.e. n * (n-2) * (n-4) * (n-6) * ....
def doubleFactorial(n):
if n <= 1:
return 1
return n * doubleFactorial(n-2)
# Helper method to calculate the moving average on on input list of numbers given a window width ww
def moving_average(in_list, ww):
out_list = []
for idx, el in enumerate(in_list):
if idx < ww:
s = sum(in_list[0: idx+1])
avg = s / (idx+1)
else:
s = sum(in_list[idx+1-ww: idx+1])
avg = s / ww
out_list.append(avg)
return out_list
if __name__ == '__main__':
pass