From ecf1b36d9884d8f54ae814acc18f0342fc62f286 Mon Sep 17 00:00:00 2001 From: Asif A Fasih Date: Fri, 1 Oct 2021 16:09:27 -0500 Subject: [PATCH 1/7] instructions and a possible solution for String to Integer (Atoi) problem. --- string-to-integer/README.md | 22 +++++++++++++ string-to-integer/string_to_integer.py | 43 ++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 string-to-integer/README.md create mode 100644 string-to-integer/string_to_integer.py diff --git a/string-to-integer/README.md b/string-to-integer/README.md new file mode 100644 index 0000000..d1db16e --- /dev/null +++ b/string-to-integer/README.md @@ -0,0 +1,22 @@ +## String to Integer + +* https://leetcode.com/problems/string-to-integer-atoi/description/ + +* Implement atoi to convert a string to an integer. + +*Hint:* Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. + +*Notes:* +* It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front. + + + +### Requirements for atoi: +* The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. +* Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. +* The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. +* If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. +* If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, `INT_MAX (2147483647)` or `INT_MIN (-2147483648)` is returned. + +## Possible Solution +You can examine a possible solution in `string_to_integer.py` \ No newline at end of file diff --git a/string-to-integer/string_to_integer.py b/string-to-integer/string_to_integer.py new file mode 100644 index 0000000..9d30d7c --- /dev/null +++ b/string-to-integer/string_to_integer.py @@ -0,0 +1,43 @@ +def main(): + string_value = input('Enter a value: ') + solution = Solution() + print(solution.convert_string_to_int(string_value)) + + +class Solution(object): + INT_MAX = 2147483647 + INT_MIN = -2147483648 + + def convert_string_to_int(self, string_input_value): + length = len(string_input_value) + + for i in range(length): + if string_input_value[0] == ' ': + string_input_value = string_input_value[1:] + else: + break + + num = 0 + pos = True + for i in range(len(string_input_value)): + if i == 0 and string_input_value[i] == '+': + pos = True + elif i == 0 and string_input_value[i] == '-': + pos = False + elif not ('0' <= string_input_value[i] <= '9'): + if pos: + return num + else: + (-1) * num + else: + num = num * 10 + int(string_input_value[i]) + if num > self.INT_MAX and pos: + return self.INT_MAX + elif num > self.INT_MIN and not pos: + return self.INT_MIN + + if pos: + return num + else: + return (-1) * num + From a28a7c9ca8ca96ff9e1a9e24f402b29c1d906f13 Mon Sep 17 00:00:00 2001 From: Asif A Fasih Date: Fri, 1 Oct 2021 16:14:04 -0500 Subject: [PATCH 2/7] removing main method for final solution --- string-to-integer/string_to_integer.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/string-to-integer/string_to_integer.py b/string-to-integer/string_to_integer.py index 9d30d7c..fa0d2f9 100644 --- a/string-to-integer/string_to_integer.py +++ b/string-to-integer/string_to_integer.py @@ -1,9 +1,3 @@ -def main(): - string_value = input('Enter a value: ') - solution = Solution() - print(solution.convert_string_to_int(string_value)) - - class Solution(object): INT_MAX = 2147483647 INT_MIN = -2147483648 From 7f7ae1f1fcb7050035aa356640aeb1b07618debe Mon Sep 17 00:00:00 2001 From: Asif A Fasih Date: Fri, 1 Oct 2021 16:15:37 -0500 Subject: [PATCH 3/7] chore: removing blank line for flake8 linter. --- string-to-integer/string_to_integer.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/string-to-integer/string_to_integer.py b/string-to-integer/string_to_integer.py index fa0d2f9..931f195 100644 --- a/string-to-integer/string_to_integer.py +++ b/string-to-integer/string_to_integer.py @@ -33,5 +33,4 @@ def convert_string_to_int(self, string_input_value): if pos: return num else: - return (-1) * num - + return (-1) * num \ No newline at end of file From 281c93d1922f13dda88cf8ea4bc39ea91ce5edb4 Mon Sep 17 00:00:00 2001 From: Asif A Fasih Date: Fri, 1 Oct 2021 16:22:12 -0500 Subject: [PATCH 4/7] chore: addressing PEP-8 standards. --- string-to-integer/string_to_integer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/string-to-integer/string_to_integer.py b/string-to-integer/string_to_integer.py index 931f195..44abad1 100644 --- a/string-to-integer/string_to_integer.py +++ b/string-to-integer/string_to_integer.py @@ -33,4 +33,4 @@ def convert_string_to_int(self, string_input_value): if pos: return num else: - return (-1) * num \ No newline at end of file + return (-1) * num From fd430ad2a7fc5028a26ac2fe080565093f29313e Mon Sep 17 00:00:00 2001 From: Asif A Fasih Date: Sun, 3 Oct 2021 14:37:37 -0500 Subject: [PATCH 5/7] renaming folder to 0008 --- 0008/README.md | 22 ++++++++++++++++++++++ 0008/string_to_integer.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 0008/README.md create mode 100644 0008/string_to_integer.py diff --git a/0008/README.md b/0008/README.md new file mode 100644 index 0000000..d1db16e --- /dev/null +++ b/0008/README.md @@ -0,0 +1,22 @@ +## String to Integer + +* https://leetcode.com/problems/string-to-integer-atoi/description/ + +* Implement atoi to convert a string to an integer. + +*Hint:* Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. + +*Notes:* +* It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front. + + + +### Requirements for atoi: +* The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. +* Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. +* The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. +* If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. +* If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, `INT_MAX (2147483647)` or `INT_MIN (-2147483648)` is returned. + +## Possible Solution +You can examine a possible solution in `string_to_integer.py` \ No newline at end of file diff --git a/0008/string_to_integer.py b/0008/string_to_integer.py new file mode 100644 index 0000000..44abad1 --- /dev/null +++ b/0008/string_to_integer.py @@ -0,0 +1,36 @@ +class Solution(object): + INT_MAX = 2147483647 + INT_MIN = -2147483648 + + def convert_string_to_int(self, string_input_value): + length = len(string_input_value) + + for i in range(length): + if string_input_value[0] == ' ': + string_input_value = string_input_value[1:] + else: + break + + num = 0 + pos = True + for i in range(len(string_input_value)): + if i == 0 and string_input_value[i] == '+': + pos = True + elif i == 0 and string_input_value[i] == '-': + pos = False + elif not ('0' <= string_input_value[i] <= '9'): + if pos: + return num + else: + (-1) * num + else: + num = num * 10 + int(string_input_value[i]) + if num > self.INT_MAX and pos: + return self.INT_MAX + elif num > self.INT_MIN and not pos: + return self.INT_MIN + + if pos: + return num + else: + return (-1) * num From 93aaf7791a993156adbaf130eb90d12ae510469c Mon Sep 17 00:00:00 2001 From: Pawan Jain <42181691+pawangeek@users.noreply.github.com> Date: Mon, 4 Oct 2021 18:03:52 +0530 Subject: [PATCH 6/7] Delete README.md --- string-to-integer/README.md | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 string-to-integer/README.md diff --git a/string-to-integer/README.md b/string-to-integer/README.md deleted file mode 100644 index d1db16e..0000000 --- a/string-to-integer/README.md +++ /dev/null @@ -1,22 +0,0 @@ -## String to Integer - -* https://leetcode.com/problems/string-to-integer-atoi/description/ - -* Implement atoi to convert a string to an integer. - -*Hint:* Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. - -*Notes:* -* It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front. - - - -### Requirements for atoi: -* The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. -* Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. -* The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. -* If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. -* If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, `INT_MAX (2147483647)` or `INT_MIN (-2147483648)` is returned. - -## Possible Solution -You can examine a possible solution in `string_to_integer.py` \ No newline at end of file From 4943132556b6023580085344d82c2eff691c2f9c Mon Sep 17 00:00:00 2001 From: Pawan Jain <42181691+pawangeek@users.noreply.github.com> Date: Mon, 4 Oct 2021 18:04:11 +0530 Subject: [PATCH 7/7] Delete string_to_integer.py --- string-to-integer/string_to_integer.py | 36 -------------------------- 1 file changed, 36 deletions(-) delete mode 100644 string-to-integer/string_to_integer.py diff --git a/string-to-integer/string_to_integer.py b/string-to-integer/string_to_integer.py deleted file mode 100644 index 44abad1..0000000 --- a/string-to-integer/string_to_integer.py +++ /dev/null @@ -1,36 +0,0 @@ -class Solution(object): - INT_MAX = 2147483647 - INT_MIN = -2147483648 - - def convert_string_to_int(self, string_input_value): - length = len(string_input_value) - - for i in range(length): - if string_input_value[0] == ' ': - string_input_value = string_input_value[1:] - else: - break - - num = 0 - pos = True - for i in range(len(string_input_value)): - if i == 0 and string_input_value[i] == '+': - pos = True - elif i == 0 and string_input_value[i] == '-': - pos = False - elif not ('0' <= string_input_value[i] <= '9'): - if pos: - return num - else: - (-1) * num - else: - num = num * 10 + int(string_input_value[i]) - if num > self.INT_MAX and pos: - return self.INT_MAX - elif num > self.INT_MIN and not pos: - return self.INT_MIN - - if pos: - return num - else: - return (-1) * num