-
Notifications
You must be signed in to change notification settings - Fork 3
[PYTH-002] Add Python list operations and foundational programs #13
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
base: master
Are you sure you want to change the base?
Changes from all commits
d7952f2
17cf43a
57e3eba
b3156cd
3b0c036
3575e23
93c94ad
08e64c5
b77abac
ded422e
a46d053
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 |
|---|---|---|
|
|
@@ -21,3 +21,4 @@ | |
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
| .appmodconfig | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # 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) | ||
|
|
||
| if __name__ == '__main__': | ||
| justPrint('Hello01 appmod') |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,15 @@ | ||||||
| #Author: OMKAR PATHAK | ||||||
| #In this example we will be seeing how instance methods are used | ||||||
| #Instance methods are accessed by: instance.method() | ||||||
|
|
||||||
| class Vehicle(): | ||||||
| #Class Methods/ Attributes | ||||||
|
|
||||||
| #Here self is passed as an argument because instance is passed as first argument | ||||||
| def type(self): #Without self it throws an error | ||||||
|
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 Built-in Function 'type' I notice we're naming this method
Suggested change
|
||||||
| print(self) | ||||||
| print('I have a type') | ||||||
|
|
||||||
| car = Vehicle() | ||||||
| print(car) | ||||||
| car.type() | ||||||
| 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 | ||
|
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. |
||
|
|
||
| def test(): | ||
| #global x | ||
| y = 100 # Local y | ||
|
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. |
||
| x = 20 | ||
|
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(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,16 @@ | ||
| #Author: OMKAR PATHAK | ||
| #This programs shows the rules for variable scope | ||
|
|
||
| # LEGB Rule: Local, Enclosing, Global, Built-in | ||
|
|
||
| x = 'Global x' | ||
|
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. |
||
|
|
||
| def test(): | ||
| #global x | ||
| y = 'Local y' | ||
|
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. |
||
| x = 'Local x' | ||
|
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(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,47 @@ | ||||||||
| #Author: OMKAR PATHAK | ||||||||
| #This program gives examples about various list operations | ||||||||
|
|
||||||||
| #Syntax: list[start: end: step] | ||||||||
|
|
||||||||
| myList = [1, 2, 3, 4, 5, 6, 7, 8, 9] | ||||||||
|
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. |
||||||||
| #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' | ||||||||
|
|
||||||||
| #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) | ||||||||
|
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.remove() I noticed we're using
Suggested change
|
||||||||
| 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,16 @@ | ||||||||||||
| #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
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. Logic Error: Missing Return for Negative Input I notice that when a negative number is entered, we print an error message but don't return a value. This causes the function to continue executing and eventually crash or return
Suggested change
|
||||||||||||
| if number == 0 or number == 1: | ||||||||||||
| return 1 | ||||||||||||
| else: | ||||||||||||
| return number * factorial(number - 1) | ||||||||||||
|
|
||||||||||||
| if __name__ == '__main__': | ||||||||||||
| userInput = int(input('Enter the Number to find the factorial of: ')) | ||||||||||||
|
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. Variable Naming Convention The variable name 'userInput' uses camelCase, which is not the standard naming convention for Python variables. Following PEP 8, variables should use snake_case to maintain consistency across the codebase.
Suggested change
|
||||||||||||
| print(factorial(userInput)) | ||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,16 @@ | ||||||||||||
| #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): | ||||||||||||
|
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 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
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. Logic Error: Missing Return for Negative Input I notice that when a negative number is entered, we print an error message but the function continues to execute. This leads to a
Suggested change
|
||||||||||||
| if number == 0 or number == 1: | ||||||||||||
| return 1 | ||||||||||||
| else: | ||||||||||||
| return number * factorial(number - 1) | ||||||||||||
|
|
||||||||||||
| if __name__ == '__main__': | ||||||||||||
| userInput = int(input('Enter the Number to find the factorial of: ')) | ||||||||||||
|
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 deviates from the standard Python snake_case convention. Renaming it to 'user_input' ensures consistency with PEP 8 and project standards.
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: ')) | ||||||
|
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
|
||||||
| 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 = int(input('Enter number for pattern')) | ||||||
|
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 Input Prompt in Nested Function I noticed that
Suggested change
|
||||||
| pattern = '*' | ||||||
| string = pattern * num | ||||||
| x = 0 | ||||||
|
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 i in string: | ||||||
| x = x + 1 | ||||||
| print(string[0:x]) | ||||||
|
Comment on lines
+92
to
+112
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 It looks like |
||||||
| 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: ')) | ||||||
| pattern1(userInput) | ||||||
| print() | ||||||
| pattern2(userInput) | ||||||
| print() | ||||||
| pattern3(userInput) | ||||||
| print() | ||||||
| pattern4(userInput) | ||||||
| print() | ||||||
| pattern5(userInput) | ||||||
| print() | ||||||
|
|
||||||
| def pattern6(userInput): | ||||||
|
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 It looks like |
||||||
| ''' | ||||||
| following is the another approach to solve pattern problems with reduced time complexity | ||||||
|
|
||||||
| for | ||||||
|
|
||||||
| * | ||||||
| ** | ||||||
| *** | ||||||
| **** | ||||||
| ***** | ||||||
| ''' | ||||||
|
|
||||||
| num = int(input('Enter number for pattern')) | ||||||
|
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 Input Prompt in pattern6 I noticed that
Suggested change
|
||||||
| 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): | ||
|
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. 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 fuction helps to count the char frequency in the given string ''' | ||
| userInput = userInput.lower() #covert to lowercase | ||
| dict = {} | ||
|
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: | ||
| if char in dict: | ||
| if char in keys: | ||
|
Comment on lines
+9
to
+10
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. |
||
| dict[char] += 1 | ||
| else: | ||
| dict[char] = 1 | ||
| return dict | ||
|
|
||
| if __name__ == '__main__': | ||
| userInput = str(input('Enter a string: ')) | ||
|
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(charFrequency(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
Clarification on .gitignore Entry
I see the
.appmodconfigentry is still present from the last review. If this file is intended to hold local, user-specific, or secret configurations, then this is the correct approach. However, if it's a default or template configuration file required for the application to run, it should be checked into version control. Could you please confirm the purpose of this file to ensure it's being handled correctly?