-
Notifications
You must be signed in to change notification settings - Fork 0
Patch 2 #11
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
Patch 2 #11
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,28 @@ | ||||||
| # Author: OMKAR PATHAK | ||||||
| # This program prints the entered message | ||||||
|
|
||||||
| def justPrint(text): | ||||||
| '''This function prints the text passed as argument to this function''' | ||||||
| print(text) | ||||||
| first_number = input("Enter a number: ") | ||||||
| second_number = input("Enter another number: ") | ||||||
| base_value = 10 | ||||||
| increment_value=20 | ||||||
|
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. |
||||||
| difference = increment_value - base_value | ||||||
| divide_value = increment_value / base_value | ||||||
| multiply_value = increment_value * base_value | ||||||
| floor_division = increment_value // base_value # // -> integer division | ||||||
|
|
||||||
| print("Floor Division:", floor_division) | ||||||
| # print("Difference is:", increment_value - base_value) | ||||||
| print("Divide value is:", divide_value) | ||||||
| print("Multiply value is:", multiply_value) | ||||||
| print("Modulus:", increment_value % base_value ) # % -> remainder | ||||||
| print('Addition is:', int(a) + int(b)) | ||||||
|
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. Undefined Variable Reference I noticed that we're trying to use variables
Suggested change
|
||||||
|
|
||||||
| if __name__ == '__main__': | ||||||
| justPrint('Hello Sindhuja') | ||||||
| justPrint('Hello Sindhuja') | ||||||
| justPrint('Hello Sindhuja') | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,17 @@ | ||||||
| #Author: OMKAR PATHAK | ||||||
| #This program finds the favtorial of the specified numbers | ||||||
| #For example, factorial of 5 = 5*4*3*2*1 = 120 | ||||||
|
|
||||||
| def factorial(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 parameter name 'number' is explicitly listed as a forbidden generic name. Using a more descriptive name like 'input_value' or 'n' (if following mathematical conventions) is preferred.
Suggested change
Reasons & GapsReasons
Gaps
|
||||||
| '''This function finds the factorial of the number passed as argument''' | ||||||
| if number < 0: | ||||||
| print('Invalid entry! Cannot find factorial of a negative 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. Infinite Recursion on Negative Input I noticed that when a negative number is entered, the function prints an error message but continues execution. Since it doesn't return early, it proceeds to the recursive call, leading to a
Suggested change
|
||||||
| if number == 0 or number == 1: | ||||||
| print("Hello") | ||||||
|
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. |
||||||
| return 1 | ||||||
| else: | ||||||
| return number * factorial(number - 1) | ||||||
|
|
||||||
| if __name__ == '__main__': | ||||||
| userInput = int(input('Enter the Number to find the factorial of: ')) | ||||||
|
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
|
||||||
| print(factorial(userInput)) | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,112 @@ | ||||||
| #Author: OMKAR PATHAK | ||||||
| #This program prints various patterns | ||||||
|
|
||||||
| def pattern1(level): | ||||||
| '''This function prints the following pattern: | ||||||
|
|
||||||
| * | ||||||
| ** | ||||||
| *** | ||||||
| **** | ||||||
|
|
||||||
| ''' | ||||||
| for i in range(1, level + 1): | ||||||
| print() | ||||||
| for j in range(i): | ||||||
| print('*', end = '') | ||||||
|
|
||||||
| def pattern2(level): | ||||||
| '''This function prints the following pattern: | ||||||
|
|
||||||
| **** | ||||||
| *** | ||||||
| ** | ||||||
| * | ||||||
|
|
||||||
| ''' | ||||||
| for i in range(level, 0, -1): | ||||||
| print() | ||||||
| for j in range(i): | ||||||
| print('*', end = '') | ||||||
|
|
||||||
| def pattern3(level): | ||||||
| '''This function prints the following pattern: | ||||||
|
|
||||||
| * | ||||||
| ** | ||||||
| *** | ||||||
| **** | ||||||
|
|
||||||
| ''' | ||||||
| counter = level | ||||||
| for i in range(level + 1): | ||||||
| print(' ' * counter + '*' * i) | ||||||
| counter -= 1 | ||||||
|
|
||||||
| def pattern4(level): | ||||||
| '''This function prints the following pattern: | ||||||
|
|
||||||
| **** | ||||||
| *** | ||||||
| ** | ||||||
| * | ||||||
|
|
||||||
| ''' | ||||||
| counter = 0 | ||||||
| for i in range(level, 0 ,-1): | ||||||
| print(' ' * counter + '*' * i) | ||||||
| counter += 1 | ||||||
|
|
||||||
| def pattern5(level): | ||||||
| '''This function prints the following pattern: | ||||||
|
|
||||||
| * | ||||||
| *** | ||||||
| ***** | ||||||
|
|
||||||
| ''' | ||||||
| # first loop for number of lines | ||||||
| for i in range(level + 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. Redundant Empty Line in Pattern 5 I noticed that
Suggested change
|
||||||
| #second loop for spaces | ||||||
| for j in range(level - i): | ||||||
| print (" ",end='') | ||||||
| # this loop is for printing stars | ||||||
| for k in range(2 * i - 1): | ||||||
| print("*", end='') | ||||||
| print() | ||||||
|
|
||||||
|
|
||||||
| if __name__ == '__main__': | ||||||
| userInput = int(input('Enter the level: ')) | ||||||
|
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
|
||||||
| pattern1(userInput) | ||||||
| print() | ||||||
| pattern2(userInput) | ||||||
| print() | ||||||
| pattern3(userInput) | ||||||
| print() | ||||||
| pattern4(userInput) | ||||||
| print() | ||||||
| pattern5(userInput) | ||||||
| print() | ||||||
|
|
||||||
| def pattern6(userInput): | ||||||
| ''' | ||||||
| following is the another approach to solve pattern problems with reduced time complexity | ||||||
|
|
||||||
| for | ||||||
|
|
||||||
| * | ||||||
| ** | ||||||
| *** | ||||||
| **** | ||||||
| ***** | ||||||
| ''' | ||||||
|
|
||||||
| num = userInput | ||||||
| pattern = '*' | ||||||
| string = pattern * num | ||||||
| x = 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 name
Suggested change
Reasons & GapsReasons
Gaps
|
||||||
|
|
||||||
| for i in string: | ||||||
| x = x + 1 | ||||||
| print(string[0:x]) | ||||||
|
Comment on lines
+92
to
+112
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. |
||||||
| 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 In Python, function names should follow the
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. JAS - Just a suggestion The variable name
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||
| for char in userInput: | ||||||||||
| keys = dict.keys() | ||||||||||
| if char in 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 I noticed 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,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 boolean values (via
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 The variable
Suggested change
Reasons & GapsReasons
Gaps
|
||||||
| if number == 2: | ||||||
| print(number, 'is a Prime Number') | ||||||
| if number > 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. Redundant Output for Number 2 I noticed that when the input is 2, the program prints that it is a prime number twice. This happens because both the specific check for 2 and the general loop logic (which doesn't execute for 2 but triggers the
Suggested change
|
||||||
| for i in range(2, number): | ||||||
| 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 The variable
Suggested change
Reasons & GapsReasons
Gaps
|
||||||
| checkPrime(userInput) | ||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,13 @@ | ||||||||
| #Author: OMKAR PATHAK | ||||||||
| #This program illustrates the example for os module in short | ||||||||
|
|
||||||||
| import os | ||||||||
| import time | ||||||||
|
|
||||||||
| print(os.getcwd()) #Prints the current working directory | ||||||||
|
|
||||||||
| os.mkdir('newDir1') | ||||||||
|
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 FileExistsError I notice we're calling
Suggested change
|
||||||||
| for i in range(1,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 While 'i' is common for loop counters, using a more descriptive name like 'directory_index' or 'folder_number' can improve readability in scripts performing file system operations.
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||
| print('Here i is',i) | ||||||||
| os.rename('newDir' + str(i),'newDir' + str(i + 1)) | ||||||||
| time.sleep(2) | ||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,24 @@ | ||||||||||||||
| #Author: OMKAR PATHAK | ||||||||||||||
| #This program guesses the randomnly generated number | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| import random | ||||||||||||||
|
|
||||||||||||||
| def guess(): | ||||||||||||||
|
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 name
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||||||
| ''' This function guesses the randomnly generated number ''' | ||||||||||||||
| randomNumber = random.randint(0, 21) | ||||||||||||||
|
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. Off-by-One Error in Random Range I noticed that the random number is generated between 0 and 21, but the user prompt suggests the range is 0 to 20. This means a user could never guess the number 21 if it's generated, leading to a frustrating experience. We should align the generation range with the user instructions.
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 In Python, variable names should follow the
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||||||
| count = 0 | ||||||||||||||
|
|
||||||||||||||
| while True: | ||||||||||||||
| count += 1 | ||||||||||||||
| number = int(input('Enter the number between 0 to 20: ')) | ||||||||||||||
|
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 Runtime Crash on Invalid Input Currently, if a user enters something that isn't a number (like a letter or a blank space), the program will crash with a
Suggested change
|
||||||||||||||
| if number < randomNumber: | ||||||||||||||
| print('Too small') | ||||||||||||||
| elif number > randomNumber: | ||||||||||||||
| print('Too large') | ||||||||||||||
| else: | ||||||||||||||
| print('You have got it in', count, 'tries') | ||||||||||||||
| break | ||||||||||||||
|
|
||||||||||||||
| if __name__ == '__main__': | ||||||||||||||
| guess() | ||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,23 @@ | ||||||||||||||||||||||||||||||||||||||||||||
| #Author: OMKAR PATHAK | ||||||||||||||||||||||||||||||||||||||||||||
| #This program is an example for sequential search | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| def sequentialSearch(target, List): | ||||||||||||||||||||||||||||||||||||||||||||
|
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 name 'List' uses a type indicator as a name, which is a form of Hungarian notation. In Python, it is also a built-in type name. Use a descriptive name like 'items' or 'search_list' instead.
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||||||||||||||||||||||||||||||||||||
| '''This function returns the position of the target if found else returns -1''' | ||||||||||||||||||||||||||||||||||||||||||||
| position = 0 | ||||||||||||||||||||||||||||||||||||||||||||
| global iterations | ||||||||||||||||||||||||||||||||||||||||||||
| iterations = 0 | ||||||||||||||||||||||||||||||||||||||||||||
| while position < len(List): | ||||||||||||||||||||||||||||||||||||||||||||
| iterations += 1 | ||||||||||||||||||||||||||||||||||||||||||||
| if target == List[position]: | ||||||||||||||||||||||||||||||||||||||||||||
| return position | ||||||||||||||||||||||||||||||||||||||||||||
| position += 1 | ||||||||||||||||||||||||||||||||||||||||||||
| return -1 | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+4
to
+14
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. Global State for Iteration Counting Using a global variable
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| if __name__ == '__main__': | ||||||||||||||||||||||||||||||||||||||||||||
| List = [1, 2, 3, 4, 5, 6, 7, 8] | ||||||||||||||||||||||||||||||||||||||||||||
| target = 3 | ||||||||||||||||||||||||||||||||||||||||||||
| ans = sequentialSearch(target, List) | ||||||||||||||||||||||||||||||||||||||||||||
|
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 'ans' is a common but non-standard abbreviation for 'answer'. Expanding it to 'result' or 'position' would improve semantic clarity.
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||||||||||||||||||||||||||||||||||||
| if ans != -1: | ||||||||||||||||||||||||||||||||||||||||||||
| print('Target found at position:',ans,'in',iterations,'iterations') | ||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||
| print('Target not found in the list') | ||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,29 @@ | ||||||
| #Author: OMKAR PATHAK | ||||||
| #This programs give an example of binary search algorithm | ||||||
|
|
||||||
| def binarySearch(target, List): | ||||||
|
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 name 'binarySearch' uses camelCase, which violates the Python snake_case convention. Additionally, 'List' as a parameter name is a built-in type name and should be more descriptive.
Suggested change
Reasons & GapsReasons
Gaps
|
||||||
| '''This function performs a binary search on a sorted list and returns the position if successful else returns -1''' | ||||||
| left = 0 #First position of the list | ||||||
| right = len(List) - 1 #Last position of the list | ||||||
| global iterations | ||||||
| iterations = 0 | ||||||
|
Comment on lines
+8
to
+9
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. State Management Issue: Global Variable Usage Using Reasons & GapsReasons
Gaps
|
||||||
|
|
||||||
| while left <= right: #U can also write while True condition | ||||||
| iterations += 1 | ||||||
| mid = (left + right) // 2 | ||||||
| if target == List[mid]: | ||||||
| return mid | ||||||
| elif target < List[mid]: | ||||||
| right = mid - 1 | ||||||
| else: | ||||||
| left = mid + 1 | ||||||
| return -1 | ||||||
|
|
||||||
| if __name__ == '__main__': | ||||||
| List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14] | ||||||
|
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 'List' shadows the built-in 'list' type and is capitalized, which usually denotes a class. A name like 'sorted_numbers' would be more expressive.
Suggested change
Reasons & GapsReasons
Gaps
|
||||||
| target = 2 | ||||||
| ans = binarySearch(target, List) | ||||||
|
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 'ans' is a cryptic abbreviation for 'answer' or 'result'. Using a more descriptive name like 'result_index' or 'target_position' improves code clarity.
Suggested change
Reasons & GapsReasons
Gaps
|
||||||
| if(ans != -1): | ||||||
| print('Target found at position:',ans,'in',iterations,'iterations') | ||||||
| else: | ||||||
| print('Target not found') | ||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,21 @@ | ||||||||||
| #Author: OMKAR PATHAK | ||||||||||
| #This program shows an example of selection sort | ||||||||||
|
|
||||||||||
| #Selection sort iterates all the elements and if the smallest element in the list is found then that number | ||||||||||
| #is swapped with the first | ||||||||||
|
|
||||||||||
| #Best O(n^2); Average O(n^2); Worst O(n^2) | ||||||||||
|
|
||||||||||
| def selectionSort(List): | ||||||||||
|
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
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 'List' is a generic type name and shadows the built-in Python
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||
| for i in range(len(List) - 1): #For iterating n - 1 times | ||||||||||
| minimum = i | ||||||||||
| for j in range( i + 1, len(List)): # Compare i and i + 1 element | ||||||||||
| if(List[j] < List[minimum]): | ||||||||||
| minimum = j | ||||||||||
| if(minimum != i): | ||||||||||
| List[i], List[minimum] = List[minimum], List[i] | ||||||||||
| return List | ||||||||||
|
|
||||||||||
| if __name__ == '__main__': | ||||||||||
| List = [3, 4, 2, 6, 5, 7, 1, 9] | ||||||||||
| print('Sorted List:',selectionSort(List)) | ||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,24 @@ | ||||||||||||||||||||||||||||||||||
| #Author: OMKAR PATHAK | ||||||||||||||||||||||||||||||||||
| #This program shows an example of bubble sort using Python | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Bubblesort is an elementary sorting algorithm. The idea is to | ||||||||||||||||||||||||||||||||||
| # imagine bubbling the smallest elements of a (vertical) array to the | ||||||||||||||||||||||||||||||||||
| # top; then bubble the next smallest; then so on until the entire | ||||||||||||||||||||||||||||||||||
| # array is sorted. Bubble sort is worse than both insertion sort and | ||||||||||||||||||||||||||||||||||
| # selection sort. It moves elements as many times as insertion sort | ||||||||||||||||||||||||||||||||||
| # (bad) and it takes as long as selection sort (bad). On the positive | ||||||||||||||||||||||||||||||||||
| # side, bubble sort is easy to understand. Also there are highly | ||||||||||||||||||||||||||||||||||
| # improved variants of bubble sort. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Best O(n^2); Average O(n^2); Worst O(n^2) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def bubbleSort(List): | ||||||||||||||||||||||||||||||||||
|
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 name 'List' is a generic type name and does not describe the content or purpose of the data. Using descriptive names like 'numbers' or 'input_elements' improves code clarity.
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||||||||||||||||||||||||||
| for i in range(len(List)): | ||||||||||||||||||||||||||||||||||
| for j in range(len(List) - 1, i, -1): | ||||||||||||||||||||||||||||||||||
| if List[j] < List[j - 1]: | ||||||||||||||||||||||||||||||||||
| List[j], List[j - 1] = List[j - 1], List[j] | ||||||||||||||||||||||||||||||||||
| return List | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+15
to
+20
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 that this implementation of Bubble Sort always runs in O(n²) time, even if the list becomes sorted early. We can optimize this by adding a flag to check if any swaps occurred during a pass. If no swaps happen, the list is already sorted, and we can exit early. This improves the best-case time complexity to O(n).
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if __name__ == '__main__': | ||||||||||||||||||||||||||||||||||
| List = [3, 4, 2, 6, 5, 7, 1, 9] | ||||||||||||||||||||||||||||||||||
|
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 'List' is too generic and shadows the built-in Python type. A more descriptive name like 'unsorted_numbers' or 'sample_data' would be more expressive.
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||||||||||||||||||||||||||
| print('Sorted List:',bubbleSort(List)) | ||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,25 @@ | ||||||||||||||||||||||||
| #Author: OMKAR PATHAK | ||||||||||||||||||||||||
| #This program shows an example of insertion sort using Python | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| # Insertion sort is good for collections that are very small | ||||||||||||||||||||||||
| # or nearly sorted. Otherwise it's not a good sorting algorithm: | ||||||||||||||||||||||||
| # it moves data around too much. Each time an insertion is made, | ||||||||||||||||||||||||
| # all elements in a greater position are shifted. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| # Best O(n); Average O(n^2); Worst O(n^2) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def insertionSort(List): | ||||||||||||||||||||||||
|
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
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 'List' is a forbidden generic name and also conflicts with the built-in type
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||||||||||||||||
| for i in range(1, len(List)): | ||||||||||||||||||||||||
| currentNumber = List[i] | ||||||||||||||||||||||||
| for j in range(i - 1, -1, -1): | ||||||||||||||||||||||||
| if List[j] > currentNumber : | ||||||||||||||||||||||||
| List[j], List[j + 1] = List[j + 1], List[j] | ||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||
| List[j + 1] = currentNumber | ||||||||||||||||||||||||
| break | ||||||||||||||||||||||||
|
Comment on lines
+14
to
+19
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 Insertion Sort Implementation I notice a potential logic issue in how the
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return List | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if __name__ == '__main__': | ||||||||||||||||||||||||
| List = [3, 4, 2, 6, 5, 7, 1, 9] | ||||||||||||||||||||||||
| print('Sorted List:',insertionSort(List)) | ||||||||||||||||||||||||
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
Misleading or Vague Function Name
The function name 'justPrint' uses camelCase, which violates Python's snake_case convention. Additionally, the name is vague and doesn't reflect that the function also performs arithmetic and user input operations.
Reasons & Gaps
Reasons
Gaps