-
Notifications
You must be signed in to change notification settings - Fork 0
Add various Python mathematical and string utility programs #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,17 @@ | ||||||||||||||||||||||||||||
| #Author: OMKAR PATHAK | ||||||||||||||||||||||||||||
| #This program converts the given binary number to its decimal equivalent | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def binaryToDecimal(binary): | ||||||||||||||||||||||||||||
| '''This function calculates the decimal equivalent to given binary number''' | ||||||||||||||||||||||||||||
| binary1 = binary | ||||||||||||||||||||||||||||
| decimal, i, n = 0, 0, 0 | ||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JAS - Just a suggestion The variable names 'i' and 'n' are non-descriptive. While 'i' is often used as a loop counter, 'n' appears unused in the provided logic. Use descriptive names like 'power_index'.
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||||||||||||||||||||
| while(binary != 0): | ||||||||||||||||||||||||||||
| dec = binary % 10 | ||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JAS - Just a suggestion The variable name 'dec' is a non-standard abbreviation. Expanding it to 'remainder' or 'last_digit' would make the mathematical operation more explicit and readable.
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||||||||||||||||||||
| decimal = decimal + dec * pow(2, i) | ||||||||||||||||||||||||||||
| binary = binary//10 | ||||||||||||||||||||||||||||
| i += 1 | ||||||||||||||||||||||||||||
|
Comment on lines
+8
to
+12
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing Binary Input Validation Currently, the code accepts any integer (like 123) and treats it as binary, which leads to mathematically incorrect decimal conversions. We should ensure the input only contains digits 0 and 1 to maintain functional correctness.
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||||||||||||||||||||
| print('Decimal equivalent of {} is {}'.format(binary1, decimal)) | ||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect Output Message I noticed that the print statement uses the variable
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if __name__ == '__main__': | ||||||||||||||||||||||||||||
| userInput = int(input('Enter the binary number to check its decimal equivalent: ')) | ||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JAS - Just a suggestion The variable
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||||||||||||||||||||
| binaryToDecimal(userInput) | ||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,18 @@ | ||||||||||
| #Author: OMKAR PATHAK | ||||||||||
| #This program checks for the character frequency in the given string | ||||||||||
|
|
||||||||||
| def charFrequency(userInput): | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JAS - Just a suggestion The function
Suggested change
Reasons & GapsReasons
Gaps
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JAS - Just a suggestion The parameter
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||
| '''This fuction helps to count the char frequency in the given string ''' | ||||||||||
| userInput = userInput.lower() #covert to lowercase | ||||||||||
| dict = {} | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||
| for char in userInput: | ||||||||||
| keys = dict.keys() | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JAS - Just a suggestion The variable name
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||
| if char in keys: | ||||||||||
|
Comment on lines
+9
to
+10
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JAS - Just a suggestion I notice we're calling
Suggested change
|
||||||||||
| dict[char] += 1 | ||||||||||
| else: | ||||||||||
| dict[char] = 1 | ||||||||||
| return dict | ||||||||||
|
|
||||||||||
| if __name__ == '__main__': | ||||||||||
| userInput = str(input('Enter a string: ')) | ||||||||||
| print(charFrequency(userInput)) | ||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,22 @@ | ||||||||||||||||||||||
| #Author: OMKAR PATHAK | ||||||||||||||||||||||
| #This program calculates the factorial of a given number | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def factorial(number): | ||||||||||||||||||||||
| '''This function calculates the factorial of a number''' | ||||||||||||||||||||||
| if number == 1 or number == 0: | ||||||||||||||||||||||
| return 1 | ||||||||||||||||||||||
|
Comment on lines
+6
to
+7
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential Recursion Depth Error The recursive
Suggested change
|
||||||||||||||||||||||
| else: | ||||||||||||||||||||||
| return number * factorial(number - 1) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def factorial_without_recursion(number): | ||||||||||||||||||||||
| fact = 1 | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JAS - Just a suggestion The variable name 'fact' is a common abbreviation. Expanding it to 'factorial_result' or 'result' improves semantic clarity and aligns with descriptive naming standards.
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||||||||||||||
| while(number > 0): | ||||||||||||||||||||||
| fact = fact * number | ||||||||||||||||||||||
| number = number - 1 | ||||||||||||||||||||||
| print('Factorial of', number,'is: ') | ||||||||||||||||||||||
| print(fact) | ||||||||||||||||||||||
|
Comment on lines
+13
to
+17
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logical Error in Iterative Factorial Output I noticed a small logic issue in the
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if __name__ == '__main__': | ||||||||||||||||||||||
| userInput = int(input('Enter the number to find its factorial: ')) | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing Input Validation for Non-Integers We're currently casting
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JAS - Just a suggestion The variable 'userInput' uses camelCase, which is inconsistent with the snake_case convention used elsewhere in the file and standard Python practices.
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||||||||||||||
| print('Factorial of', userInput, 'is:', factorial(userInput)) | ||||||||||||||||||||||
| factorial_without_recursion(userInput) | ||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,24 @@ | ||||||||||||||
| #Author: OMKAR PATHAK | ||||||||||||||
| #This program calculates the fibonacci series till the n-th term | ||||||||||||||
|
|
||||||||||||||
| def fibonacci(number): | ||||||||||||||
| '''This function calculates the fibonacci series till the n-th term''' | ||||||||||||||
| if number <= 1: | ||||||||||||||
| return number | ||||||||||||||
| else: | ||||||||||||||
| return (fibonacci(number - 1) + fibonacci(number - 2)) | ||||||||||||||
|
|
||||||||||||||
| def fibonacci_without_recursion(number): | ||||||||||||||
| if number == 0: return 0 | ||||||||||||||
| fibonacci0, fibonacci1 = 0, 1 | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JAS - Just a suggestion The variable names
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||||||
| for i in range(2, number + 1): | ||||||||||||||
| fibonacci1, fibonacci0 = fibonacci0 + fibonacci1, fibonacci1 | ||||||||||||||
| return fibonacci1 | ||||||||||||||
|
Comment on lines
+14
to
+16
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JAS - Just a suggestion In the
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||||||
|
|
||||||||||||||
| if __name__ == '__main__': | ||||||||||||||
| userInput = int(input('Enter the number upto which you wish to calculate fibonnaci series: ')) | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JAS - Just a suggestion The variable
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||||||
| for i in range(userInput + 1): | ||||||||||||||
| print(fibonacci(i),end=' ') | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance Issue: Exponential Time Complexity in Recursion The current recursive implementation of
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| print("\nUsing LOOP:") | ||||||||||||||
| print(fibonacci_without_recursion(userInput)) | ||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,19 @@ | ||||||||||
| #Author: OMKAR PATHAK | ||||||||||
| #This program calculates the LCM of the two numbers entered by the user | ||||||||||
|
|
||||||||||
| def LCM(number1, number2): | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JAS - Just a suggestion In Python, function names should follow the
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||
| '''This function calculates LCM of two numbers inputed by the user''' | ||||||||||
| maximum = max(number1, number2) | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Infinite Loop Risk with Non-Positive Inputs I notice that if a user enters 0 or a negative number, the current logic will result in an infinite loop because the increment
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||
| i = maximum | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JAS - Just a suggestion The variable name 'i' is used here as a tracking value for the multiple being checked. While common in loops, a more descriptive name like 'current_multiple' would enhance clarity.
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||
| while True: | ||||||||||
| if (i % number1 == 0 and i % number2 == 0): | ||||||||||
| lcm = i | ||||||||||
| break | ||||||||||
| i += maximum | ||||||||||
|
|
||||||||||
| return lcm | ||||||||||
|
|
||||||||||
| if __name__ == '__main__': | ||||||||||
| userInput1 = int(input('Enter first number: ')) | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JAS - Just a suggestion The variable
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||
| userInput2 = int(input('Enter second number: ')) | ||||||||||
| print('LCM of {} and {} is {}'.format( userInput1, userInput2, LCM(userInput1, userInput2))) | ||||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,23 @@ | ||||||||
| #Author: OMKAR PATHAK | ||||||||
| #This program checks whether the entered number is prime or not | ||||||||
|
|
||||||||
| def checkPrime(number): | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JAS - Just a suggestion The function returns a boolean value but lacks a standard boolean prefix like 'is_' or 'has_'. Renaming it to 'is_prime' follows Python naming conventions for boolean functions.
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||
| '''This function checks for prime number''' | ||||||||
| isPrime = False | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JAS - Just a suggestion In Python, variable names should follow the
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||
| if number == 2: | ||||||||
| print(number, 'is a Prime Number') | ||||||||
|
Comment on lines
+7
to
+8
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant Logic for Number 2 I noticed that we're checking if
Suggested change
|
||||||||
| if number > 1: | ||||||||
| for i in range(2, number): | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance Issue: Inefficient Prime Check Currently, the loop runs up to
Suggested change
|
||||||||
| if number % i == 0: | ||||||||
| print(number, 'is not a Prime Number') | ||||||||
| isPrime = False | ||||||||
| break | ||||||||
| else: | ||||||||
| isPrime = True | ||||||||
|
|
||||||||
| if isPrime: | ||||||||
| print(number, 'is a Prime Number') | ||||||||
|
|
||||||||
| if __name__ == '__main__': | ||||||||
| userInput = int(input('Enter a number to check: ')) | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JAS - Just a suggestion Variable names in Python should use
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||
| checkPrime(userInput) | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JAS - Just a suggestion
Incorrect Function Naming Convention
In Python, function names should follow the
snake_caseconvention. The current namebinaryToDecimalusescamelCase, which is inconsistent with PEP 8 standards.Reasons & Gaps
Reasons
Gaps