Lab Part 1
Lab Part 1
test’s marks
accepted from the user.
m1 = int (input("Enter the marks in the first test: "))
m2 = int (input("Enter the marks in second test: "))
m3 = int (input("Enter the marks in third test: "))
Avg = total / 2
print ("The average of the best two test marks is: ",Avg)
Output:
Enter the marks in the first test: 40
Enter the marks in second test: 52
Enter the marks in third test: 89
The average of the best two test marks is: 70.5
for i in range(10):
if str_val.count(str(i)) > 0:
print(str(i),"appears", str_val.count(str(i)), "times");
Output:
Enter a value : 565
Palindrome
5 appears 2 times
6 appears 1 times
def fn(n):
if n == 1:
return 0
elif n == 2:
return 1
else:
return fn(n-1) + fn(n-2)
if num > 0:
print("fn(", num, ") = ",fn(num) , sep ="")
else:
print("Error in input")
Output:
Enter a number : 5
fn(5) = 3
def bin2Dec(val):
rev=val[::-1]
dec = 0
i = 0
for dig in rev:
dec += int(dig) * 2**i
i += 1
return dec
def oct2Hex(val):
rev=val[::-1]
dec = 0
i = 0
nl=[]
for elem in list[::-1]:
if elem <= 9:
nl.append(str(elem))
else:
nl.append(chr(ord('A') + (elem -10)))
hex = "".join(nl)
return hex
Or
def decimal_into_binary(decimal_1):
decimal = int(decimal_1)
print ("The given decimal number", decimal, "in Binary number is:
", bin(decimal))
def decimal_into_octal(decimal_1):
decimal = int(decimal_1)
print ("The given decimal number", decimal, "in Octal number is:
", oct(decimal))
def decimal_into_hexadecimal(decimal_1):
decimal = int(decimal_1)
Output:
Enter the Decimal Number: 12
The given decimal number 12 in Binary number is: 0b1100
The given decimal number 12 in Octal number is: 0o14
The given decimal number 12 in Hexadecimal number is: 0xc
3 a) Write a Python program that accepts a sentence and find the number of words,
digits,
uppercase letters and lowercase letters.
w = len(l_w)
for c in s:
if c.isdigit():
d = d + 1
elif c.isupper():
u = u + 1
elif c.islower():
l = l + 1
Output:
Enter a sentence: hai how are u hdfdggxcFSfjKSJSKF
No of Words: 5
No of Digits: 0
No of Uppercase letters: 8
No of Lowercase letters: 20
3 b) Write a Python program to find the string similarity between two given
strings.
long = len(str1)
else:
short = len(str1)
long = len(str2)
matchCnt = 0
for i in range(short):
if str1[i] == str2[i]:
matchCnt += 1
Output:
Enter String 1
hai
Enter String 2
haii
Similarity between two strings:
0.75
4a) Write a python program to implement insertion sort and merge sort using lists.
def merge(arr, l, m, r):
n1 = m - l + 1
n2 = r - m
L = [0] * (n1)
R = [0] * (n2)
def insertion_sort(alist):
for i in range(1, len(alist)):
temp = alist[i]
j = i - 1
while (j >= 0 and temp < alist[j]):
alist[j + 1] = alist[j]
j = j - 1
alist[j + 1] = temp
if l < r:
mergeSort(arr, 0, n-1)
print("\n\nSorted array is")
for i in range(n):
print("%d" % arr[i],end=" ")
Output:
Enter the insertion list of numbers: 12 02 65 86 75 45
Sorted list: [2, 12, 45, 65, 75, 86]
Enter the merge list of numbers: 23 25 81 01
Sorted array is
1 23 25 81
def r2d(rs):
roman_dict ={'I': 1, 'V': 5, 'X': 10, 'L': 50,
'C': 100, 'D': 500, 'M': 1000}
romanBack = list(rs)[::-1]
value = 0
rightVal = roman_dict[romanBack[0]]
for numeral in romanBack:
leftVal = roman_dict[numeral]
else:
print("Invalid phone number")
Output:
Enter a phone number : 781-140-6868
Valid phone number
5 b) Develop a python program that could search the text in a file for phone
numbers
(+919900889977) and email addresses (sample@gmail.com).
Procedure:
At first step create a txt file in the directory where your python files are
saving in a
directory .
give the exact url for file name where actually file is situated.
import re
matches = email_regex.findall(line)
# Print any matches found