From 078e64e3d9630f941775069e0300b5412d4efec1 Mon Sep 17 00:00:00 2001 From: NipunRautela Date: Mon, 12 Oct 2020 19:17:28 +0530 Subject: [PATCH] Added a folder called Python Algos for ALgorithms in Python language. Added some algos to the folder --- Python Algos/is_prime.py | 15 +++++++++++++++ Python Algos/pyramid.py | 7 +++++++ Python Algos/reverse_individual_words.py | 24 ++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 Python Algos/is_prime.py create mode 100644 Python Algos/pyramid.py create mode 100644 Python Algos/reverse_individual_words.py diff --git a/Python Algos/is_prime.py b/Python Algos/is_prime.py new file mode 100644 index 0000000..b8374ba --- /dev/null +++ b/Python Algos/is_prime.py @@ -0,0 +1,15 @@ +def is_prime(num: int) -> bool: + """ + Returns True if the num is prime else false + """ + for i in range(2, num/2): + if num%i == 0: + return False + + return True + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/Python Algos/pyramid.py b/Python Algos/pyramid.py new file mode 100644 index 0000000..706182a --- /dev/null +++ b/Python Algos/pyramid.py @@ -0,0 +1,7 @@ +n = int(input("Enter the number of rows: ")) + +for i in range(1, n+1): + for j in range(1, i+1): + print(j, end="") + print() + diff --git a/Python Algos/reverse_individual_words.py b/Python Algos/reverse_individual_words.py new file mode 100644 index 0000000..2901988 --- /dev/null +++ b/Python Algos/reverse_individual_words.py @@ -0,0 +1,24 @@ +def reverse_individual_words(input_str: str) -> str: + """ + Reverses words in a given string + >>> reverse_individual_words("This is a text") + 'sihT si a txet' + >>> reverse_individual_words("Another 1234 text") + 'rehtonA 4321 txet' + >>> reverse_individual_words("A sentence with full stop.") + 'A ecnetnes htiw lluf pots.' + """ + individual_reverse_string = [] + for word in input_str.split(" "): + if word[-1] in [".", "!", "?"]: + individual_reverse_string.append(word[-2::-1] + word[-1]) + else: + individual_reverse_string.append(word[::-1]) + + return " ".join(individual_reverse_string) + + +if __name__ == "__main__": + import doctest + + doctest.testmod()