You are on page 1of 14

COMPUTER

SCIENCE
PRACTICAL FILE
[TERM-II]

Divvyansh Kudesiaa
Class: XI – E
Roll no.: 09
DPS INDIRAPURAM
COMPUTER SCIENCE PRACTICAL FILE

INDEX
Q1. Write a script to read a string and convert each word of the string in Pig Latin
Format
Q2. Write a script to read a string and reverse the individual word of the string. For
example, if the string is “INDIA IS MY COUNTRY” then the output should be
“AIDNI SI YM YRTNUOC”
Q3. ROT13 is a weak form of encryption that involves “rotating” each letter in a word
by 13 places. To rotate a letter means to shift it through the alphabet, wrapping
around to the beginning if necessary, so ’A’ shifted by 3 is ’D’ and ’Z’ shifted by 1
is ’A’.
Write a script to create a new string that contains the letters from the original string
“rotated” by the given amount.
For example, “cheer” rotated by 7 is “jolly” and “melon” rotated by -10 is “cubed”.
You might want to use the built-in functions ord, which converts a character to a
numeric code, and chr, which converts numeric codes to characters.
Q4. Write a program to enter a list of values and a number x to be searched. The list
and the number x, is passed as an argument to the function search() which searches
for x in the list. If present, the function should display the position where found
else display the message “element is not present in the list”
Q5. Write a program that uses a function to input a list of integers and replace each
even element of the list with the sum of its digits and each odd element with the
product of its digits. For example, if the list is entered as: [100, 43, 20, 56, 32, 91,
80, 40, 45, 21] After executing the program, the list content should be changed as
follows: [1, 12, 2, 11, 5, 9, 8, 4, 20, 2]
Q6. Complete the following function that determines if the number of even and odd
values in an integer list is the same. The function would return true if the list
contains 5, 1, 0, 2 (two evens and two odds), but it would return false for the list
containing 5, 1, 0, 2, 11 (too many odds). The function should return true if the list
is empty, since an empty list contains the same number of evens and odds (0 for
both). The function does not affect the contents of the list.
def balanced(a):
# Add your code...
Q7. Write a program to input ‘n’ numbers and store it in a tuple and find maximum &
minimum values in the tuple.
Q8. Write a Python script to generate and print a dictionary that contains a number
(between 1 and n) in the form (x, x*x)
10
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}
Q9. Write a Python program to sum all the items in a dictionary.
Q10. Write a program that takes a list of words and creates a dictionary with frequency
(number of occurrences) of word as key and list of words for that frequency as
value. For example, if list is given as [‘the’,’of’, ‘an’, ‘is’, ‘an’, ‘the’] Then
dictionary should be {2:[‘the’,’an’],1:[‘of’,’is’]}

COMPUTER SCIENCE PRACTICAL FILE

Questions
Q1. Write a script to read a string and convert each word of the string in
Pig Latin Format

CODE:-
str=input("enter a sentence: ").lower()
new_str=''
word=str.split()
print(word)
for i in word:
if i[0] in 'aeiou':
i=i+'ay'
else:
for a in range(len(i)):
if i[a] not in 'aeiou':
continue
else:
i= i[a:] + i[0:a] + 'ay'
break
print(i)
new_str+=i
new_str+=' '
print(new_str)

OUTPUT:-
COMPUTER SCIENCE PRACTICAL FILE

Q2. Write a script to read a string and reverse the individual word of the
string. For
Example, if the string is “INDIA IS MY COUNTRY” then the output
should be
“AIDNI SI YM YRTNUOC”

CODE:-
str=input("enter a sentence: ")
new_str=''
words=str.split()
for i in words:
i=i[::-1]
new_str+=i
new_str+=' '
print(new_str)

OUTPUT:-
COMPUTER SCIENCE PRACTICAL FILE

Q3. ROT13 is a weak form of encryption that involves “rotating” each


letter in a word
by 13 places. To rotate a letter means to shift it through the alphabet,
wrapping
around to the beginning if necessary, so ’A’ shifted by 3 is ’D’ and ’Z’
shifted by 1
is ’A’.
Write a script to create a new string that contains the letters from the
original string
“rotated” by the given amount.
For example, “cheer” rotated by 7 is “jolly” and “melon” rotated by -10 is
“cubed”.
You might want to use the built-in functions ord, which converts a
character to a
numeric code, and chr, which converts numeric codes to characters.

CODE:-
str=input("enter string: ").lower()
new_str=''
rotate=int(input("rotate by? "))
for i in str:
if rotate>0:
if 97<= ord(i) + rotate <=122 :
no=ord(i)+rotate
i=chr(no)
new_str+=i
else:
no=ord(i) + rotate - 26
i=chr(no)
new_str+=i
else:
if 97<= ord(i) + rotate <=122 :
no=ord(i)+rotate
i=chr(no)
new_str+=i
else:
COMPUTER SCIENCE PRACTICAL FILE

no=ord(i) + rotate + 26
i=chr(no)
new_str+=i
print(new_str)

OUTPUT:-
COMPUTER SCIENCE PRACTICAL FILE

Q4. Write a program to enter a list of values and a number x to be


searched. The list and the number x, is passed as an argument to the
function search() which searches for x in the list. If present, the function
should display the position where found else display the message “element
is not present in the list”
CODE:-
def search(L,x):
l=len(L)
if x in L:
for i in range (l):
if L[i]==x:
print(x,'found at',i,'index')
else:
continue
else:
print('element is not present in the list')

#Driver Code
L=eval(input("enter a list: "))
x=eval(input("enter the number to be searched "))
search(L,x)

OUTPUT:-
COMPUTER SCIENCE PRACTICAL FILE

Q5. Write a program that uses a function to input a list of integers and
replace each even element of the list with the sum of its digits and each
odd element with the product of its digits. For example, if the list is
entered as: [100, 43, 20, 56, 32, 91, 80, 40, 45, 21] after executing the
program, the list content should be changed as follows: [1, 12, 2, 11, 5, 9,
8, 4, 20, 2]

CODE:-
def sum(n):
s=0
while n>0:
d=n%10
s+=d
n//=10
return s
def mul(n):
p=1
while n>0:
d=n%10
p*=d
n//=10
return p
def func(L):
l=len(L)
for i in range (l):
if L[i]%2==0:
L[i]=sum(L[i])
else:
L[i]=mul(L[i])
#Driver code
L=eval(input("enter a list: "))
print(L)
func(L)
print('New list: ',L)

OUTPUT:-

COMPUTER SCIENCE PRACTICAL FILE

Q6. Complete the following function that determines if the number of even
and odd values in an integer list is the same. The function would return
true if the list contains 5, 1, 0, 2 (two evens and two odds), but it would
return false for the list containing 5, 1, 0, 2, 11 (too many odds). The
function should return true if the list is empty, since an empty list contains
the same number of evens and odds (0 for both). The function does not
affect the contents of the list.
def balanced(a):
# Add your code...

CODE:-
def balanced (a):
count_even=0
count_odd=0
for i in a:
if i%2==0:
count_even+=1
else:
count_odd+=1
if count_even==count_odd:
print('True')
else:
print('False')

#Driver Code
a=eval(input('enter list: '))
print(a)
balanced(a)

OUTPUT:-

COMPUTER SCIENCE PRACTICAL FILE

Q7. Write a program to input ‘n’ numbers and store it in a tuple and find
maximum & minimum values in the tuple.

CODE:-

numbers = tuple()
n = int(input("enter n: "))
for i in range (n):
num = int(input('enter number: '))
numbers = numbers + (num,)
print('\nThe numbers in the tuple are: ')
print(numbers)
print("\nThe maximum number is: ")
print(max(numbers))
print("The minimum number is: ")
print(min(numbers))

OUTPUT:-
COMPUTER SCIENCE PRACTICAL FILE

Q8. Write a Python script to generate and print a dictionary that contains a
number (between 1 and n) in the form (x, x*x)

CODE:-

n=int(input('enter n: '))
d=dict()
for i in range (1,n+1):
d[i]=i*i
print(d)

OUTPUT:-
COMPUTER SCIENCE PRACTICAL FILE

Q9. Write a Python program to sum all the items in a dictionary.

CODE:-

dict=eval(input('enter a dictionary: '))


sum=sum(dict.values())
print('The sum of values is',sum)

OUTPUT:-
COMPUTER SCIENCE PRACTICAL FILE

Q10. Write a program that takes a list of words and creates a dictionary
with frequency (number of occurrences) of word as key and list of words
for that frequency as value. For example, if list is given as [‘the’,’of’, ‘an’,
‘is’, ‘an’, ‘the’] Then dictionary should be {2:[‘the’,’an’],1:[‘of’,’is’]}

CODE:-

n=int(input('enter the no. of elements: '))


L1=list()
for i in range (n):
ele=input('enter element: ')
L1+=[ele]
L2=[]
for val in L1:
if val not in L2:
L2+=[val]
else:
continue
print(L2)
dict={}
for a in range (n):
dict[a]= [x for x in L2 if L1.count(x)==a]
if not dict[a]:
del(dict[a])
print(dict)

OUTPUT:-

You might also like