diff --git a/maths/average_welford.py b/maths/average_welford.py new file mode 100644 index 000000000000..522a34adfc06 --- /dev/null +++ b/maths/average_welford.py @@ -0,0 +1,21 @@ +def average_welford(values: list) -> float: + """ + >>> average_welford([1, 2, 3, 4, 5]) + 3.0 + >>> average_welford([1.2329435, 2.209462409, 3.230925, 4.47626462, 5.2938759204]) + 3.2886942898799996 + >>> average_welford([-57386462.2329435, 2246262.209462409, 4632463.230925, 856737354.47626462, -243664265.2938759204]) + 112513070.4779665 + """ + avg = 0.0 + """while looping through the list, + we calculate the average of the + current element and the average + of the elements before it""" + for index in range(0, len(values)): + avg += (values[index]-avg)/(index+1) + return avg + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file diff --git a/maths/sum_of_digits.py b/maths/sum_of_digits.py index 64da00d4634c..07ae94075240 100644 --- a/maths/sum_of_digits.py +++ b/maths/sum_of_digits.py @@ -54,10 +54,25 @@ def sum_of_digits_compact(n: int) -> int: """ return sum(int(c) for c in str(abs(n))) +def digital_root(n: int) -> int: + """ + finding the digital root of n + https://en.wikipedia.org/wiki/Digital_root + + >>> digital_root(394328) + 2 + >>> digital_root(-394328) + "Input wasn't valid" + >>> digital_root(123) + 6 + >>> digital_root(0) + 0 + """ + return n%9 or n and 9 if n >= 0 else "Input wasn't valid" def benchmark() -> None: """ - Benchmark code for comparing 3 functions, + Benchmark code for comparing 4 functions, with 3 different length int values. """ print("\nFor small_num = ", small_num, ":") @@ -85,6 +100,14 @@ def benchmark() -> None: timeit("z.sum_of_digits_compact(z.small_num)", setup="import __main__ as z"), "seconds", ) + print( + "> digital_root()", + "\tans =", + digital_root(small_num), + "\ttime =", + timeit("z.digital_root(z.small_num)", setup="import __main__ as z"), + "seconds", + ) print("\nFor medium_num = ", medium_num, ":") print( @@ -111,6 +134,14 @@ def benchmark() -> None: timeit("z.sum_of_digits_compact(z.medium_num)", setup="import __main__ as z"), "seconds", ) + print( + "> digital_root()", + "\t\tans =", + digital_root(medium_num), + "\ttime =", + timeit("z.digital_root(z.medium_num)", setup="import __main__ as z"), + "seconds", + ) print("\nFor large_num = ", large_num, ":") print( @@ -137,6 +168,14 @@ def benchmark() -> None: timeit("z.sum_of_digits_compact(z.large_num)", setup="import __main__ as z"), "seconds", ) + print( + "> digital_root()", + "\tans =", + digital_root(large_num), + "\ttime =", + timeit("z.digital_root(z.large_num)", setup="import __main__ as z"), + "seconds", + ) if __name__ == "__main__": diff --git a/strings/censor.py b/strings/censor.py new file mode 100644 index 000000000000..935283f5c8ef --- /dev/null +++ b/strings/censor.py @@ -0,0 +1,26 @@ +def censor(txt: str, word: str) -> str: + """ + >>> censor("one two three", "one") + *** two three + >>> censor("email, password", "password") + email, ******** + >>> censor("pin: 2536", "2536") + pin: **** + + """ + + txt_lst = txt.split() + #here we loop through a list which consists of words from the text we should censor + for index, word1 in enumerate(txt_lst): + if word1 == word: + txt_lst[index] = "*" * len(word1) + txt = "" + #here we combine all of the split words in txt_lst into one string + for word1 in txt_lst: + txt += word1 + " " + txt = txt[:-1] + return txt + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file diff --git a/strings/decrypt.py b/strings/decrypt.py new file mode 100644 index 000000000000..b5fcd1b83122 --- /dev/null +++ b/strings/decrypt.py @@ -0,0 +1,38 @@ +def decrypt(txt: str, symbol: str) -> str: + """ + >>> decrypt("o$ne", "$") + one + >>> decrypt("dec@rypt", "@") + decrypt + >>> decrypt("p#ython cod#e", "#") + python code + + """ + #here we split the string we need to decypher into a list + txt_lst = txt.split() + res_str = "" + #we create a for loop which loops through the newly created list + for word1 in txt_lst: + #here we convert the current word we got through the loop into a list of charachters + word1_lst = list(word1) + #we loop through each charachter in the newly created list of charachters form the word we are looping throuh + for char1 in word1_lst: + #we set a condition, where if the symbol the loop is currently on corresponds to the symbol we need to remove, we stop on it and examine it + if char1 == symbol: + #we find the charachters index from the word + indx = word1.index(char1) + #we remove the list (the list which contains every element and characahter from the word we are looping throuh) element with the same index from the list. + word1_lst[indx] = "" + res_wrd = "" + #we loop throuh the list containing all the charachters from the word, and append those charachters to a new string + for char1 in word1_lst: + res_wrd += char1 + #here we add the newly created, decyphered word to a new string which will contain our full result + res_str += res_wrd + " " + res_str = res_str[:-1] + #and here we return out the final result + return res_str + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file