assignment1
October 19, 2025
[10]: # q1
def grade(marks):
if marks >= 80:
return "A+", 4.00
elif marks >= 75:
return "A", 3.75
elif marks >= 70:
return "A-", 3.50
elif marks >= 65:
return "B+", 3.25
elif marks >= 60:
return "B", 3.00
elif marks >= 55:
return "B-", 2.75
elif marks >= 50:
return "C+", 2.50
elif marks >= 45:
return "C", 2.25
elif marks >= 40:
return "D", 2.00
else:
return "F", 0.00
with open("student_marks.txt") as file:
print("Roll\tLetter Grade\tGrade Point")
for line in file:
data = line.strip().split()
if len(data)!= 5:
print("Invalid data format")
continue
roll, first, second, attendance, final = map(int, data)
total_marks = (first + second)/2 + attendance + final
letter_grade, grade_point = grade(total_marks)
print(f"{roll}\t{letter_grade}\t\t{grade_point:.2f}")
1
Roll Letter Grade Grade Point
80 A+ 4.00
81 A 3.75
82 A- 3.50
[ ]: # q2
total=0
count=0
print("The numbers in the file are:")
file= open("regex-sum-42.txt","r")
for line in file:
line=line.split()
for words in line:
if words.isdigit():
print(int(words))
total+=int(words)
count+=1
print(f"There are {count} numbers in file.")
print("Sum of all the numbers is: ",total)
The numbers in the file are:
3036
7209
4497
6702
8454
7449
3665
7936
9772
7114
956
2564
8003
1704
3816
6662
5858
7777
6482
9634
8805
7123
9703
2
4676
6373
2834
7221
2981
5415
6872
4806
5460
8533
3538
9663
8001
9795
8752
1117
5349
4509
1004
9258
4183
4034
3342
3482
8567
1052
8135
5561
517
1218
8877
8062
1720
279
2054
801
918
8687
7073
1865
7084
2923
63
8824
1079
5801
5047
5
3
2572
5616
171
3062
9552
7655
829
6096
2312
6015
7100
9548
2727
1792
8402
42
There are 87 numbers in file.
Sum of all the numbers is: 445822
[3]: # q3
def frequency(teachers_file):
teachers_name=list()
for line in teachers_file:
line=line.rstrip()
if not line.startswith("Applied Mathematics"):
names=line.split()
for name in names:
if name not in teachers_name: teachers_name.append(name)
teachers_name.sort()
return teachers_name
#print(teachers_name)
teachers_file= open("amth.txt")
a=frequency(teachers_file)
print(a)
['A', 'Abdus', 'Alam', 'B', 'Bangalee', 'Chaki', 'Chandra', 'Chapal', 'Dr.',
'Ferdows', 'Hossain', 'Iqbal', 'Islam', 'Kajal', 'Khaleque', 'Kumar', 'Litan',
'M', 'Md.', 'Mohammad', 'Muntasir', 'Nirab', 'Rakib', 'S.M.', 'Saha', 'Samad',
'Shafiqul', 'Shahadat', 'Sharmin', 'Shuchi', 'Tania', 'Zavid']
[4]: # q4
count=0
avg=0
4
total=0
with open("mbox.txt") as abc:
for line in abc:
if line.startswith("X-DSPAM-Confidence:"):
val = float(line.strip().split(":")[1])
total += val
count += 1
avg= total/count
print(f"averge of the numbers is:{avg:.2f}")
print(f"there are total of {count} floating point values in file.")
averge of the numbers is:0.89
there are total of 1797 floating point values in file.
[5]: # q5
email=[]
filename= input("enter file:")
with open(filename) as mailFile:
for line in mailFile:
if line.startswith("From "):
email.append(line.split()[1])
for mails in email:
print(mails)
count=len(email)
print(f"There were {count} lines in the file with \"From\" as the first word")
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
5
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
There were 27 lines in the file with "From" as the first word
[6]: # q6
filename=input("enter file:")
email_count = {}
with open(filename) as file:
for line in file:
if line.startswith("From "):
email = line.split()[1]
email_count[email] = email_count.get(email, 0) + 1
max_email = None
max_count = 0
for email, count in email_count.items():
if count> max_count:
max_email = email
max_count = count
print(max_email, max_count)
[email protected] 5