-
Notifications
You must be signed in to change notification settings - Fork 0
Sindhu python #12
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
Sindhu python #12
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) | ||||||||||||
| a=input("Enter a number: ") | ||||||||||||
| b=input("Enter another 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. |
||||||||||||
| base_value = 10 | ||||||||||||
| increment_value=20 | ||||||||||||
| 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. Potential Runtime Error: Unhandled Input Type I notice we're directly casting the inputs
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,16 @@ | ||||||||
| #Author: OMKAR PATHAK | ||||||||
| #This programs shows the rules for variable scope | ||||||||
|
|
||||||||
| # LEGB Rule: Local, Enclosing, Global, Built-in | ||||||||
|
|
||||||||
| x = 80 # Global x | ||||||||
|
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 Single-Character Variable Name The variable name 'x' is non-descriptive. Using single-character names for global state reduces code readability and maintainability. Consider using a more descriptive name like 'global_count' or 'base_value'.
Suggested change
|
||||||||
|
|
||||||||
| def test(): | ||||||||
| #global x | ||||||||
| y = 100 # Local y | ||||||||
|
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 Single-Character Variable Name The variable name 'y' is a single character, which is discouraged for local variables that are not loop counters. A more descriptive name like 'local_offset' or 'increment_value' would improve clarity.
Suggested change
|
||||||||
| x = 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. Shadowing Global Variable JAS - Just a suggestion
Suggested change
|
||||||||
| print(x + y) #prints 'Local x' and 'Local y' | ||||||||
|
|
||||||||
| if __name__ == '__main__': | ||||||||
| test() | ||||||||
| print(x) #prints 'Global x' | ||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,48 @@ | ||||||||
| #Author: OMKAR PATHAK | ||||||||
| #This program gives examples about various list operations | ||||||||
| # User story id : Prod - PYTH-003 | ||||||||
|
|
||||||||
| #Syntax: list[start: end: step] | ||||||||
|
|
||||||||
| myList = [1, 2, 3, 4, 5, 6, 7, 8, 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 Non-Descriptive Variable Name The variable name 'myList' is generic and does not describe the content or purpose of the data. Using a more descriptive name like 'numbers' or 'integer_list' improves code maintainability.
Suggested change
|
||||||||
| #index 0 1 2 3 4 5 6 7 8 | ||||||||
| # -9 -8 -7 -6 -5 -4 -3 -2 -1 | ||||||||
|
|
||||||||
| #List Slicing | ||||||||
| print('Original List:',myList) | ||||||||
| print('First Element:',myList[0]) #Prints the first element of the list or 0th element of the list | ||||||||
| print('Element at 2nd Index position:',myList[2]) #Prints the 2nd element of the list | ||||||||
| print('Elements from 0th Index to 4th Index:',myList[0: 5]) #Prints elements of the list from 0th index to 4th index. IT DOESN'T INCLUDE THE LAST INDEX | ||||||||
| print('Element at -7th Index:',myList[-7]) #Prints the -7th or 3rd element of the list | ||||||||
|
|
||||||||
| #To append an element to a list | ||||||||
| myList.append(10) | ||||||||
| print('Append:',myList) | ||||||||
|
|
||||||||
| #To find the index of a particular element | ||||||||
| print('Index of element \'6\':',myList.index(6)) #returns index of element '6' | ||||||||
|
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 ValueError in List Index Lookup JAS - Just a suggestion
Suggested change
|
||||||||
|
|
||||||||
| #To sort the list | ||||||||
| myList.sort() | ||||||||
|
|
||||||||
| #To pop last element | ||||||||
| print('Poped Element:',myList.pop()) | ||||||||
|
|
||||||||
| #To remove a particular element from the lsit BY NAME | ||||||||
| myList.remove(6) | ||||||||
| print('After removing \'6\':',myList) | ||||||||
|
|
||||||||
| #To insert an element at a specified Index | ||||||||
| myList.insert(5, 6) | ||||||||
| print('Inserting \'6\' at 5th index:',myList) | ||||||||
|
|
||||||||
| #To count number of occurences of a element in the list | ||||||||
| print('No of Occurences of \'1\':',myList.count(1)) | ||||||||
|
|
||||||||
| #To extend a list that is insert multiple elemets at once at the end of the list | ||||||||
| myList.extend([11,0]) | ||||||||
| print('Extending list:',myList) | ||||||||
|
|
||||||||
| #To reverse a list | ||||||||
| myList.reverse() | ||||||||
| print('Reversed list:',myList) | ||||||||
| 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): | ||||||||||||
| '''This function finds the factorial of the number passed as argument''' | ||||||||||||
| if number < 0: | ||||||||||||
| print('Invalid entry! Cannot find factorial of a negative 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. Incorrect Logic for Negative Input I notice that when a negative number is entered, we print an error message but then continue execution into the next
Suggested change
|
||||||||||||
| if number == 0 or number == 1: | ||||||||||||
| print("Hello") | ||||||||||||
|
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. Unnecessary Debug Print in Base Case JAS - Just a suggestion
Suggested change
|
||||||||||||
| 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 Variable Naming Violation The variable name 'userInput' uses camelCase, which violates Python's snake_case convention. Renaming it to 'user_input' improves consistency with PEP 8 and the rest of the codebase.
Suggested change
|
||||||||||||
| 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): | ||||||
| #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 Non-Standard Variable Naming The variable name 'userInput' uses camelCase, which is non-standard for Python. Additionally, 'userInput' is somewhat generic; a more descriptive name like 'pattern_level' would improve clarity.
Suggested change
|
||||||
| pattern1(userInput) | ||||||
| print() | ||||||
| pattern2(userInput) | ||||||
| print() | ||||||
| pattern3(userInput) | ||||||
| print() | ||||||
| pattern4(userInput) | ||||||
| print() | ||||||
| pattern5(userInput) | ||||||
| print() | ||||||
|
|
||||||
| def pattern6(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. Unreachable and Redundant Nested Function I noticed that Reasons & GapsReasons
Gaps
|
||||||
| ''' | ||||||
| following is the another approach to solve pattern problems with reduced time complexity | ||||||
|
|
||||||
| for | ||||||
|
|
||||||
| * | ||||||
| ** | ||||||
| *** | ||||||
| **** | ||||||
| ***** | ||||||
| ''' | ||||||
|
|
||||||
| num = int(input('Enter number for pattern')) | ||||||
| pattern = '*' | ||||||
| string = pattern * num | ||||||
| x = 0 | ||||||
|
|
||||||
| for i in string: | ||||||
| x = x + 1 | ||||||
| print(string[0:x]) | ||||||
| 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 Function Naming Convention In Python, function names should follow the
Suggested change
|
||||||||
| '''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() | ||||||||
| 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. Inefficient Dictionary Key Lookup JAS - Just a suggestion
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 Boolean-Returning Function Without Prefix The function 'checkPrime' returns a boolean value (via the 'isPrime' flag logic) but lacks a standard boolean prefix like 'is_' or 'has_'. Renaming it to 'is_prime' improves clarity and follows Pythonic conventions.
Suggested change
|
||||||||||||||
| '''This function checks for prime number''' | ||||||||||||||
| isPrime = False | ||||||||||||||
| if number == 2: | ||||||||||||||
| print(number, 'is a Prime Number') | ||||||||||||||
| if number > 1: | ||||||||||||||
|
Comment on lines
+7
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. Redundant and Conflicting Prime Logic I notice that when the input is 2, the code prints that it is a prime number twice. This happens because the first
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 Non-Standard Abbreviation and Casing The variable 'userInput' uses camelCase, which is non-standard for Python variables. Additionally, while 'userInput' is understandable, 'input_number' would be more expressive in the context of a prime number checker.
Suggested change
|
||||||||||||||
| checkPrime(userInput) | ||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,45 @@ | ||||||||||||||||||||||
| #Author: OMKAR PATHAK | ||||||||||||||||||||||
| #This program illustrates a stopwatch | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import time | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| print('Press ENTER to begin, Press Ctrl + C to stop') | ||||||||||||||||||||||
| while True: | ||||||||||||||||||||||
| try: | ||||||||||||||||||||||
| input() #For ENTER | ||||||||||||||||||||||
| starttime = time.time() | ||||||||||||||||||||||
| print('Started') | ||||||||||||||||||||||
|
Comment on lines
+8
to
+11
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. Stopwatch Logic Error: Start Time Reset I notice that the
Suggested change
|
||||||||||||||||||||||
| except KeyboardInterrupt: | ||||||||||||||||||||||
| print('Stopped') | ||||||||||||||||||||||
| endtime = time.time() | ||||||||||||||||||||||
| print('Total Time:', round(endtime - starttime, 2),'secs') | ||||||||||||||||||||||
| break | ||||||||||||||||||||||
| # Press enter to start and stop the watch | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
| import time | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| print('Press Enter to begin, Press Enter again to stop') | ||||||||||||||||||||||
| if input()=='': | ||||||||||||||||||||||
| starttime = time.time() | ||||||||||||||||||||||
| print('Started') | ||||||||||||||||||||||
| while True: | ||||||||||||||||||||||
| val=input() #For ENTER | ||||||||||||||||||||||
|
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. |
||||||||||||||||||||||
| if val=='': | ||||||||||||||||||||||
| print('Stopped') | ||||||||||||||||||||||
| endtime = time.time() | ||||||||||||||||||||||
| print('Total Time:', round(endtime - starttime, 2),'secs') | ||||||||||||||||||||||
| break | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| """ | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| """ | ||||||||||||||||||||||
| Output: | ||||||||||||||||||||||
| Press Enter to begin, Press Enter again to stop | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Started | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Stopped | ||||||||||||||||||||||
| Total Time: 1.05 secs | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| """ | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| 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') | ||||||||
| 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. |
||||||||
| print('Here i is',i) | ||||||||
| os.rename('newDir' + str(i),'newDir' + str(i + 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. Directory Renaming Logic Error I noticed a logic issue in the loop. We're creating 'newDir1' outside the loop, but then trying to rename 'newDir1' to 'newDir2', then 'newDir2' to 'newDir3', and so on. However, the loop starts at i=1 and goes to 9. After the first iteration, 'newDir1' no longer exists, but the code doesn't create any new directories to rename in subsequent steps. This will cause a
Suggested change
|
||||||||
| 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(): | ||||||||||||||
| ''' 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 notice a small discrepancy between the game's instructions and the actual logic. The prompt tells users to guess between 0 and 20, but
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 Variable Naming Improvement The variable name 'randomNumber' uses camelCase, which is inconsistent with Python's snake_case convention. Renaming it to 'random_number' improves consistency and readability.
Suggested change
|
||||||||||||||
| 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. Missing Input Validation for Non-Integers The current implementation uses
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.
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. |
||||||
| '''This function returns the position of the target if found else returns -1''' | ||||||
| position = 0 | ||||||
| global iterations | ||||||
| iterations = 0 | ||||||
|
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. Unnecessary Global Variable Usage I notice we're using a global variable Reasons & GapsReasons
Gaps
|
||||||
| while position < len(List): | ||||||
| iterations += 1 | ||||||
| if target == List[position]: | ||||||
| return position | ||||||
| position += 1 | ||||||
| return -1 | ||||||
|
|
||||||
| 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 Vague Generic Name The variable name
Suggested change
|
||||||
| if ans != -1: | ||||||
| print('Target found at position:',ans,'in',iterations,'iterations') | ||||||
| else: | ||||||
| print('Target not found in the 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
Single-Character Variable Name
The variable name 'a' is non-descriptive. Use a name that reflects the purpose of the input, such as 'first_number' or 'input_value_a', to improve code readability.