From b954ef672b6e894aff22d1aefc8aa269592cd602 Mon Sep 17 00:00:00 2001 From: Nitkarsh Chourasia Date: Thu, 28 Dec 2023 14:13:00 +0530 Subject: [PATCH 1/4] update: pack more features. More features packed, making it a robust practical world application, ready for deployment. --- area_of_square.py | 56 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/area_of_square.py b/area_of_square.py index 471e7c1cdb6..fe3f5d95512 100644 --- a/area_of_square.py +++ b/area_of_square.py @@ -1,5 +1,51 @@ -# Returns the area of the square with given sides -n = input("Enter the side of the square: ") # Side length should be given in input -side = float(n) -area = side * side # calculate area -print("Area of the given square is ", area) +# # Returns the area of the square with given sides +# n = input("Enter the side of the square: ") # Side length should be given in input +# side = float(n) +# area = side * side # calculate area +# print("Area of the given square is ", area) + + +class Square: + def __init__(self, side=None): + if side is None: + self.ask_side() + else: + self.side = float(side) + + self.square() + self.truncate_decimals() + + # If ask side or input directly into the square. + # That can be done? + def square(self): + self.area = self.side * self.side + return self.area + + def ask_side(self): + n = input("Enter the side of the square: ") + self.side = float(n) + # return + + def truncate_decimals(self): + return ( + f"{self.area:.10f}".rstrip("0").rstrip(".") + if "." in str(self.area) + else self.area + ) + + +# Even validation is left. +# What if string is provided in number? Then? +# What if chars are provided. Then? +# What if a negative number is provided? Then? +# What if a number is provided in alphabets characters? Then? +# Can it a single method have more object in it? + +if __name__ == "__main__": + output_one = Square() + truncated_area = output_one.truncate_decimals() + # print(output_one.truncate_decimals()) + print(truncated_area) + + +# It can use a beautiful GUI also. From 713204cdebde6b66616cd1893728ff47cf3bdf75 Mon Sep 17 00:00:00 2001 From: Nitkarsh Chourasia Date: Thu, 28 Dec 2023 16:06:31 +0530 Subject: [PATCH 2/4] update: pack more features. More features packed, making it a robust practical world application, ready for deployment. --- area_of_square.py | 51 ----------------------------------------------- 1 file changed, 51 deletions(-) delete mode 100644 area_of_square.py diff --git a/area_of_square.py b/area_of_square.py deleted file mode 100644 index fe3f5d95512..00000000000 --- a/area_of_square.py +++ /dev/null @@ -1,51 +0,0 @@ -# # Returns the area of the square with given sides -# n = input("Enter the side of the square: ") # Side length should be given in input -# side = float(n) -# area = side * side # calculate area -# print("Area of the given square is ", area) - - -class Square: - def __init__(self, side=None): - if side is None: - self.ask_side() - else: - self.side = float(side) - - self.square() - self.truncate_decimals() - - # If ask side or input directly into the square. - # That can be done? - def square(self): - self.area = self.side * self.side - return self.area - - def ask_side(self): - n = input("Enter the side of the square: ") - self.side = float(n) - # return - - def truncate_decimals(self): - return ( - f"{self.area:.10f}".rstrip("0").rstrip(".") - if "." in str(self.area) - else self.area - ) - - -# Even validation is left. -# What if string is provided in number? Then? -# What if chars are provided. Then? -# What if a negative number is provided? Then? -# What if a number is provided in alphabets characters? Then? -# Can it a single method have more object in it? - -if __name__ == "__main__": - output_one = Square() - truncated_area = output_one.truncate_decimals() - # print(output_one.truncate_decimals()) - print(truncated_area) - - -# It can use a beautiful GUI also. From b8d0760922e58532190d118cae12cdb364dd3e86 Mon Sep 17 00:00:00 2001 From: Nitkarsh Chourasia Date: Thu, 28 Dec 2023 17:33:17 +0530 Subject: [PATCH 3/4] Add word_to_number function for converting words to numbers --- word2number.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 word2number.py diff --git a/word2number.py b/word2number.py new file mode 100644 index 00000000000..f8fa5f22153 --- /dev/null +++ b/word2number.py @@ -0,0 +1,75 @@ +def word_to_number(word): + numbers_dict = { + "zero": 0, + "one": 1, + "two": 2, + "three": 3, + "four": 4, + "five": 5, + "six": 6, + "seven": 7, + "eight": 8, + "nine": 9, + "ten": 10, + "eleven": 11, + "twelve": 12, + "thirteen": 13, + "fourteen": 14, + "fifteen": 15, + "sixteen": 16, + "seventeen": 17, + "eighteen": 18, + "nineteen": 19, + "twenty": 20, + "thirty": 30, + "forty": 40, + "fifty": 50, + "sixty": 60, + "seventy": 70, + "eighty": 80, + "ninety": 90, + "hundred": 100, + "thousand": 1000, + "lakh": 100000, + "crore": 10000000, + "billion": 1000000000, + "trillion": 1000000000000, + } + + # Split the string into words + words = word.split() + + result = 0 + current_number = 0 + + # Ways I can make this more efficient: + for w in words: + if w in numbers_dict: + current_number += numbers_dict[w] + elif w == "hundred": + current_number *= 100 + elif w == "thousand": + result += current_number * 1000 + current_number = 0 + elif w == "lakh": + result += current_number * 100000 + current_number = 0 + elif w == "crore": + result += current_number * 10000000 + current_number = 0 + elif w == "billion": + result += current_number * 1000000000 + current_number = 0 + elif w == "trillion": + result += current_number * 1000000000000 + current_number = 0 + + result += current_number + + return result + + +# Example usage: +number_str = "two trillion seven billion fifty crore thirty-four lakh seven thousand nine hundred" +result = word_to_number(number_str) +print(result) From 0d2e6f7f88149581980b5097343bdca484266d3f Mon Sep 17 00:00:00 2001 From: Nitkarsh Chourasia Date: Thu, 28 Dec 2023 17:40:42 +0530 Subject: [PATCH 4/4] Add word_to_number function for converting words to numbers The number will be provided into a alphabet characters, the goal is to convert them into numbers. --- .../word2number/word2number.py | 8 ++++++++ 1 file changed, 8 insertions(+) rename word2number.py => nitkarshchourasia/word2number/word2number.py (87%) diff --git a/word2number.py b/nitkarshchourasia/word2number/word2number.py similarity index 87% rename from word2number.py rename to nitkarshchourasia/word2number/word2number.py index f8fa5f22153..6e8fed09d39 100644 --- a/word2number.py +++ b/nitkarshchourasia/word2number/word2number.py @@ -73,3 +73,11 @@ def word_to_number(word): number_str = "two trillion seven billion fifty crore thirty-four lakh seven thousand nine hundred" result = word_to_number(number_str) print(result) + + +# Will make a tkinter application out of it. +## It will have a slider to use the more efficient way or just the normal way. +## More efficient way would have a library word2num to choose from. + +# The application would be good. +# I want to make it more efficient and optimized.