Cs Programs Part1
Cs Programs Part1
recursively.
def findSum(lst,num):
if num==0:
return 0
else:
return lst[num-1]+findSum(lst,num-1)
mylist = []
for i in range(num):
mylist.append(n)
sum = findSum(mylist,len(mylist))
OUTPUT
Program to find the occurence of any word in a string
def countWord(str1,word):
s = str1.split()
count=0
for w in s:
if w==word:
count+=1
return count
count = countWord(str1,word)
if count==0:
else:
OUTPUT
Program to swap two tuple elements using user defined functions. One function to
create tuple and one to swap
def swap(t1,t2):
t1,t2=t2,t1
return t1,t2
def input1(t,n):
for i in range(0,n):
t=t+(a,)
return t
T1=()
T2=()
T1=input1(T1,n1)
print(T1)
T2=input1(T2,n2)
print(T2)
T1,T2=swap(T1,T2)
OUTPUT
Program to create a list of numbers and define a function to multiple every even
elements of the list by 1 and odd elements by 5 and return the list.
def change(L1,n):
for i in range(0,n,1):
if L1[i]%2==0:
L1[i]=L1[i]*10
else:
L1[i]=L1[i]*5
return L1
L=list()
for i in range(0,n):
L.append(ele)
L2=change(L,n)
print(L2)
OUTPUT
Program to create a list and define a function to shift all the elements in the list to the
right side and print the updated list.
temp=L[n-1]
for i in range(n-1,0,-1):
L[i]=L[i-1]
L[0]=temp
print(L)
L=list()
for i in range(0,n):
L.append(ele)
print(L)
LShift(L,n)
OUTPUT
Program to Search for an Element in a list using Linear Search.
def search(L,ele):
if L[i]==ele:
pos=i
break
else:
L=[]
for i in range(0,n,1):
L.append(ele)
search(L,x)
OUTPUT