Illustrative Problems 1. Find Minimum in A List
Illustrative Problems 1. Find Minimum in A List
Illustrative Problems 1. Find Minimum in A List
def minimum(x):
mini = x[0]
for i in x[0:]:
if i < mini:
mini = i
else:
mini = x[0]
return (mini)
b = [1,2,3,4,5]
print(minimum(b))
guessesTaken = 0
print('Take a guess.')
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
if guess != number:
number = str(number)
3. Towers of Hanoi
if height >= 1:
moveTower(height-1,fromPole,withPole,toPole)
moveDisk(fromPole,toPole)
moveTower(height-1,withPole,toPole,fromPole)
def moveDisk(fp,tp):
moveTower(3,"A","B","C")
UNIT 2
def swa(num1,num2):
temp=num1
num1=num2
num2=temp
print(num1)
print(num2)
n1=int(input())
n2=int(input())
def dist(x1,y1,x2,y2):
d1=(x2-x1)**2
d2=(y2-y1)**2
#compute distance
distance=((d1)+(d2))**2
x1=int(input())
y1=int(input())
x2=int(input())
y2=int(input())
def rotate(l,order):
for i in range(0,order):
j=len(l)-1
while(j>0): #swapping
t=l[i]
l[i]=l[j]
l[j]=t
j=j-1
print(i,'rotation',l)
return
L=[10,20,30,40,50] #input
UNIT 3
if number < 0 :
else :
a=int(input())
b=int(input())
if a==0:
print("GCD is",b)
elif b==0:
print("GCD is",a)
elif a>b:
small=b
else:
small=a
gcd=1
for i in range(1,small+1):
gcd=i
3. Exponentiation
a=int(input("Enter a number"))
sum=1
for i in range(b):
sum*=a
arr=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
sum=0
for i in arr:
sum+=i
5. Linear Search
def search(arr, x):
for i in range(len(arr)):
if arr[i] == x:
return i
return -1
arr=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
a=search(arr,x)
if a==-1:
else:
6. Binary Search
if r >= l:
mid = (l + r)//2
if arr[mid] == x:
return mid
# If element is smaller than mid, then it can only be present in left subarray
else:
else:
# Element is not present in the array
return -1
# Test array
x = 56
# Function call
if result != -1:
else:
UNIT 4
1. Selection Sort
def selsort(A):
# Traverse through all array elements
for i in range(len(A)):
# Find the minimum element in remaining unsorted array
min_idx = i
for j in range(i+1, len(A)):
if A[min_idx] > A[j]:
min_idx = j
# Swap the found minimum element with the first element
A[i], A[min_idx] = A[min_idx], A[i]
# Main Program
A = [64, 25, 12, 22, 11]
selsort(A)
print ("Sorted array")
for i in range(len(A)):
print("%d" %A[i])
2. Insertion Sort
# Main Program
arr = [12, 11, 13, 5, 6]
insertionSort(arr)
for i in range(len(arr)):
print ("%d" %arr[i])
3. Merge Sort
def mergeSort(arr):
if len(arr) >1:
mid = len(arr)//2 #Finding the mid of the array
L = arr[:mid] # Dividing the array elements
R = arr[mid:] # into 2 halves
i = j = k = 0
# Main Program
arr = [12, 11, 13, 5, 6, 7]
print ("Given array is", end="\n")
printList(arr)
mergeSort(arr)
print("Sorted array is: ", end="\n")
printList(arr)
def histogram(A):
for x in A:
if x not in unique:
unique.append(x)
a=A.count(n)
print("Frequency of",n,"is", a )
A=[1,2,3,4,5,6,1,2,3,4,5,1,2,3,4,1,2,3,1,2,1]
histogram(A)
UNIT 5
f=open("a.txt",'r')
count=0
for i in f:
for j in i:
f=open("a.txt",'r')
wcount=0
for i in f:
words=i.split()
wcount+=len(words)
f=open("a.txt",'r')
wcount=0
for i in f.read().split():
wcount+=1
c=f.readlines()
print(c)
a=2+3j
b=3-2J
c=a+b
print('c',c)
print('z',z)
f=z-c
print('f',f)
Output:
c (5+1j)
z (5-2j)
f -3j