0% found this document useful (0 votes)
18 views5 pages

Python Notes

The document contains examples of subset sum problems solved using recursion and iteration. It also contains examples of finding if the sum of digits of a number is a palindrome, solving a cards game problem using dynamic programming, and binary search.

Uploaded by

Madhu Painkiller
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
18 views5 pages

Python Notes

The document contains examples of subset sum problems solved using recursion and iteration. It also contains examples of finding if the sum of digits of a number is a palindrome, solving a cards game problem using dynamic programming, and binary search.

Uploaded by

Madhu Painkiller
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 5

-------------------------------------------------- subset sum bounded recursion

def subsetSum(arr,ptr,n,temp,ans,target):
# base case
if ptr==n:
if sum(temp)==target and temp not in ans:
ans.append(list(temp))
return
temp.append(arr[ptr])
subsetSum(arr,ptr+1,n,temp,ans,target)
temp.pop()
subsetSum(arr,ptr+1,n,temp, ans,target)
arr = [1,1,2,3,4]
n=len(arr)
ptr=0
temp=[]
ans=[]
target=7
subsetSum(arr,ptr,n,temp,ans, target)
print(ans)

----------------------------------------------------subset sum unbounded recursion

def subsetSum(arr,ptr, n, temp, ans, target):


# base case
if sum(temp) ==target and temp not in ans:
ans.append(list(temp))
if ptr==n or sum(temp)>target:
return
temp.append(arr[ptr])
subsetSum(arr,ptr, n, temp, ans, target)
temp.pop()
subsetSum(arr,ptr+1, n, temp, ans, target)
arr = [1,2,3,4]
n=len(arr)
ptr=0
temp=[]
ans=[]
target=7
subsetSum(arr,ptr, n, temp, ans, target)
print(ans)

------------------------------------------------------subset sum bounded iteration

import itertools
l=[1,1,2,3,4]
k=[]
for i in range(1,len(l)+1):
for j in itertools.combinations(l,i):
if sum(j)==7:
k.append(j)
for i in set(k):
print(list(i))

------------------------------------------------------subset sum unbounded


iteration
import itertools
l=[1,1,2,3,4]
k=[]
for i in range(1,len(l)+1):
for j in itertools.product(l,repeat=i):
if sum(j)==7:
k.append(j)
for i in set(k):
print(list(i))

n = str(input())
sumn=0
for i in n:
sumn+=int(i)
sumn=str(sumn)
if sumn==sumn[::-1]:
print(1)
else:
print(0)

-------------------------------------------sum of a integer is palindrome or


not(extract digits by type convertion)

n = int(input())
t=n
ilen=0
while(t!=0):
t//=10
ilen+=1
sumn=0
for i in range(ilen):
sumn+=(n%10)
n//=10

t = str(sumn)
if t==t[::-1]:
print(1)
else:
print(0)

------------------------------------------------sum of a integer is palindrome or


not(extract digits by maths)

def tomcheese(m,n,bl):
dp=[[0]*n for i in range(m)]
dp[0][0]=1
for i in range(m):
if [0,i] in bl:
for j in range(i,n):
dp[0][j]=0
break
else:
dp[0][i]=1
for i in range(m):
if [i,0] in bl:
for j in range(i,n):
dp[j][0]=0
break
else:
dp[i][0]=1
for i in range(1,m):
for j in range(1,n):
if [i,j] in bl:
continue
else:
dp[i][j]=dp[i-1][j]+dp[i][j-1]
for i in dp:
print(i)
print(dp[m-1][n-1])
m,n=map(int,input().split())
bl=[[0,2],[5,2],[3,0],[2,3],[3,1]]
tomcheese(m,n,bl)

-----------------------------------------------------no of ways to tom to reach


jerry with blocks

def winner(n,m,k):
if n==m or m==0:
return 0
ncp=n/k
l=[]
j=0
if m>ncp:
l[j]=ncp
m=m-ncp
for z in range(1,k):
if z!=k-1 and m>0:
l[z]=1
m=m-1
if z==k-1 and m>0:
l[z]=m
if m==0:
l[z]=0

return max(l)

op=[]
q=int(input())
for i in range(q):
n,m,k=map(int,input().split())
op[i]=winner(n,m,k)
for i in op:
print(i)

--------------------------------------------------cards game my try not yet


completed

def numberOfWays(m,n,a):
dp=[(-1]*(n) for i in range(0,m)] # not filled
f=1
for i in range(0,n):
if a[0][i]==0:
f=0
if f==0:
dp[0][i]=0
else:
dp[0][i]=1
f=1
for i in range(0,m):
if a[i][0]==0:
f=0
if fr=0:
dp[i][0]=0
else:
dp[0][0]=1
for i in range(1,m):
for j in range(1,n):
dp[i][j]=dp[i-1][j] + dp[i][j-1]
return dp[m-1][n-1]

------------------------------------------no of ways tom to reach chease

def binarySearch(a,l,r,x):
print("l,r",l,r)
m=(l+r)//2
print("m",m)
print("a[m],x",a[m],x)
# base case
if l>r:
return -1
if a[m]==x:
return m
if a[m]<x:
return binarySearch(a,m+1,r,x)
else:
return binarySearch(a,l,m-1,x)
a=[2,3,6,7,8,9]
l=0
r=len(a)-1
x=8
print(binarySearch(a,l,r,x))

------------------------------------------------------------- BINARY SEARCH

t =int(input())
for i in range(t):
n, m, k = map(int, input().split())
l=[0 for i in range(k)]
d = n // k
if m<d:
l[0]=m
else:
l[0]=d
m=m-d
while(m!=0):
for i in range(1,k):
if m!=0:
l[i]+=1
m-=1
if m==0:
break

print(l[0]-l[1])

------------------------------------------------------------------pocker card game


done

You might also like