0% found this document useful (0 votes)
9 views

Cs Programs Part1

Uploaded by

sleepyguy1609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Cs Programs Part1

Uploaded by

sleepyguy1609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Write a program to find the sum of elements of a list, defining a separate function,

recursively.

def findSum(lst,num):

if num==0:

return 0

else:

return lst[num-1]+findSum(lst,num-1)

mylist = []

num = int(input("Enter how many number :"))

for i in range(num):

n = int(input("Enter Element :"))

mylist.append(n)

sum = findSum(mylist,len(mylist))

print("Sum of List items ",mylist, " is :",sum)

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

str1 = input("Enter any sentence :")

word = input("Enter word to search in sentence :")

count = countWord(str1,word)

if count==0:

print("## Sorry! ",word," not present ")

else:

print("## ",word," occurs ",count," times ## ")

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):

a=int(input("Enter the element"))

t=t+(a,)

return t

T1=()

T2=()

n1=int(input("Enter the size of first tuple"))

n2=int(input("Enter the size of second tuple"))

T1=input1(T1,n1)

print(T1)

T2=input1(T2,n2)

print(T2)

T1,T2=swap(T1,T2)

print("The First Tuple is:",T1)

print("The Second tuple is:",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()

n=int(input("enter the size"))

for i in range(0,n):

ele=int(input("enter the element"))

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.

def LShift (L,n):

temp=L[n-1]

for i in range(n-1,0,-1):

L[i]=L[i-1]

L[0]=temp

print("The List after Shifting is :")

print(L)

L=list()

n=int(input("Enter the size"))

for i in range(0,n):

ele=int(input("Enter the element"))

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):

for i in range (0,len(L),1):

if L[i]==ele:

pos=i

print("Element found at",pos+1 ,"th position")

break

else:

print("Element not found")

L=[]

n=int(input("Enter the Size"))

for i in range(0,n,1):

ele=int(input("Enter the element:"))

L.append(ele)

x=int(input("Enter the element that Needs to be searched in the list"))

search(L,x)

OUTPUT

You might also like