Skip to content
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.appmodconfig
9 changes: 9 additions & 0 deletions python123/P01_hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Author: OMKAR PATHAK
# This program prints the entered message

def print_text(text):
'''This function prints the text passed as argument to this function'''
print(text)

if __name__ == '__main__':
print_text('Hello01 appmod')
15 changes: 15 additions & 0 deletions python123/P02_InstanceMethods.py
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 get_vehicle_type(self):
print(self)
print('I have a type')

car = Vehicle()
print(car)
car.type()
16 changes: 16 additions & 0 deletions python123/P02_VariableScope copy.py
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

def test():
#global x
y = 100 # Local y
x = 20
print(x + y) #prints 'Local x' and 'Local y'

if __name__ == '__main__':
test()
print(x) #prints 'Global x'
112 changes: 112 additions & 0 deletions python123/P05_Pattern.py
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):
'''
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])
18 changes: 18 additions & 0 deletions python123/P06_CharCount.py
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):
'''This fuction helps to count the char frequency in the given string '''
userInput = userInput.lower() #covert to lowercase
dict = {}
for char in userInput:
keys = dict.keys()
if char in keys:
dict[char] += 1
else:
dict[char] = 1
return dict

if __name__ == '__main__':
userInput = str(input('Enter a string: '))
print(charFrequency(userInput))
20 changes: 20 additions & 0 deletions python123/P06_Inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#Author: OMKAR PATHAK
#This program illustrates the concept of inheritance
#Python looks up for method in following order: Instance attributes, class attributes and the
#from the base class

class Data(object):
def getData(self):
print('In data!')

class Time(Data): #Inheriting from Data class
def getTime(self):
print('In Time!')

if __name__ == '__main__':
data = Data()
time = Time()

data.getData()
time.getTime()
time.getData() #Inherited Data method
26 changes: 26 additions & 0 deletions python123/P07_MoreOnInheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#Author: OMKAR PATHAK
#This program illustrates the advanced concepts of inheritance
#Python looks up for method in following order: Instance attributes, class attributes and the
#from the base class
#mro: Method Resolution order

class Data(object):
def __init__(self, data):
self.data = data

def getData(self):
print('Data:',self.data)

class Time(Data): #Inhertiting from Data class
def getTime(self):
print('Time:',self.data)

if __name__ == '__main__':
data = Data(10)
time = Time(20) #inherited Class -> Value passed to __init__of Data (Base class)

time.getTime()
data.getData()
time.getData()

print(Time.mro())
23 changes: 23 additions & 0 deletions python123/P07_PrimeNumber.py
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):
'''This function checks for prime number'''
isPrime = False
if number == 2:
print(number, 'is a Prime Number')
if number > 1:
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: '))
checkPrime(userInput)
52 changes: 52 additions & 0 deletions python123/P50_ListComprehensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Author: OMKAR PATHAK
# In this example we will see how to write list comprehensions to make our tasks easier

# Python.org says:
# List comprehensions provide a concise way to create lists.
# Common applications are to make new lists where each element is
# the result of some operations applied to each member of another sequence
# or iterable, or to create a subsequence of those elements that satisfy a certain condition.

numbers = []
for i in range(10):
numbers.append(i)
print(numbers) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# Side Effect of above operation:It creates a variable(or overwrites) named 'x'
# that still exists after the loop completes. To get rid of this Side Effect we use List comprehensions.

# List comprehension:
numbers = [i for i in range(10)]
print(numbers) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# Let us see few more examples
squares = [i * i for i in range(10)]
print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

# This is same as:
squares = []
for i in range(10):
squares.append(i * i)

# Some more:
odds = [i for i in numbers if i % 2 != 0]
print(odds) # [1, 3, 5, 7, 9]

# This is same as:
odds = []
for i in numbers:
if i % 2 != 0:
odds.append(i)

# We can also use functions in comprehensions
def isSqaure(x):
import math
sqrt = int(math.sqrt(x))
return x == sqrt * sqrt

squares = [x for x in range(100) if isSqaure(x) == True]
print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

# Some Complex comprehensions:
pairs = [[x, x * x] for x in numbers]
print(pairs) # [[0, 0], [1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81]]
25 changes: 25 additions & 0 deletions python123/P51_PythonJSON.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Author: OMKAR PATHAK
# This example shows how to use Python with JSON

import json

# For storing on json format
def storeJSON(fileName, data=None):
with open(fileName, 'w') as fd:
json.dump(data, fd, indent = 4, separators = (',', ': '))

# For loading data from a JSON file
def loadJSON(fileName):
with open(fileName) as fd:
data = json.load(fd)
print(data)
return data

if __name__ == '__main__':
data = loadJSON('example.json')
print(data['menu']['value']) # File
data['menu']['value'] = 'movie'
storeJSON('example.json', data)
print()
loadJSON('example.json')
print(data['menu']['value']) # movie
55 changes: 55 additions & 0 deletions python123/P52_BucketSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Author: OMKAR PATHAK
# This program will illustrate how to implement bucket sort algorithm

# Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works by distributing the
# elements of an array into a number of buckets. Each bucket is then sorted individually, either using
# a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a
# distribution sort, and is a cousin of radix sort in the most to least significant digit flavour.
# Bucket sort is a generalization of pigeonhole sort. Bucket sort can be implemented with comparisons
# and therefore can also be considered a comparison sort algorithm. The computational complexity estimates
# involve the number of buckets.

# Time Complexity of Solution:
# Best Case O(n); Average Case O(n); Worst Case O(n)

from P26_InsertionSort import insertionSort
import math

DEFAULT_BUCKET_SIZE = 5

def bucketSort(myList, bucketSize=DEFAULT_BUCKET_SIZE):
if(len(myList) == 0):
print('You don\'t have any elements in array!')

minValue = myList[0]
maxValue = myList[0]

# For finding minimum and maximum values
for i in range(0, len(myList)):
if myList[i] < minValue:
minValue = myList[i]
elif myList[i] > maxValue:
maxValue = myList[i]

# Initialize buckets
bucketCount = math.floor((maxValue - minValue) / bucketSize) + 1
buckets = []
for i in range(0, bucketCount):
buckets.append([])

# For putting values in buckets
for i in range(0, len(myList)):
buckets[math.floor((myList[i] - minValue) / bucketSize)].append(myList[i])

# Sort buckets and place back into input array
sortedArray = []
for i in range(0, len(buckets)):
insertionSort(buckets[i])
for j in range(0, len(buckets[i])):
sortedArray.append(buckets[i][j])

return sortedArray

if __name__ == '__main__':
sortedArray = bucketSort([12, 23, 4, 5, 3, 2, 12, 81, 56, 95])
print(sortedArray)
Loading