PRACTICE PROBLEM 7
PRACTICE PROBLEM 7
Student Data (Id-3146)
Given a file named student.txt which has the information of the students in a class such as Name, Roll number, Age, CGPA.
Write a pseudocode and the subsequent Python program to do the following operations.
a) Read the file and print the contents of the file
b)
Read the file and sort the contents (records) of the file in an
increasing order of CGPA and print the content of the sorted file.
c) Read the file and print the record of the student with maximum age and minimum age.
d) Read the file and print the average CGPA of the class.
e)
Read the file and print the name of the students whose CGPA is greater
than the average CGPA of the class and whose age lies between average
age of the class and the minimum age of the students in the class
Input Format
Read the name of the input file
Details of the students with the fields : Name, Rollnumber, Age, CGPA separated by a comma.
Input:
Read the name of the input file
Details of the students with the fields : Name, Rollnumber, Age, CGPA separated by a comma.
Processing:
name = input().rstrip()
def read_print():
grade =
open(name)
for i in
grade.readlines():
print(i,end='')
print()
grade.close()
def sort_file():
stud_list = []
grade = open(name)
for i in
grade.readlines():
stud_list.append(i.rstrip().split(','))
cgpa = {}
for i in
stud_list:
cgpa.setdefault(eval(i[-1]),[])
cgpa[eval(i[-1])].append(i)
for i in
cgpa.keys():
cgpa[i].sort(key = lambda name:name[0])
for i in
reversed(sorted(cgpa.keys())):
for j in
cgpa[i]:
print(','.join(j))
stud_list.sort(key
= lambda x:eval(x[-2]))
max_age = []
for i in
range(len(stud_list)-1,-1,-1):
if
stud_list[i][-2] == stud_list[-1][-2]:
max_age.append(stud_list[i])
else:
break
max_age.sort(key =
lambda x:x[0])
min_age = []
for i in
range(len(stud_list)):
if
stud_list[i][-2] == stud_list[0][-2]:
min_age.append(stud_list[i])
else:
break
min_age.sort(key =
lambda x:x[0])
for i in max_age:
print(','.join(i))
for i in min_age:
print(','.join(i))
stud_list.sort(key
= lambda x:eval(x[-1]),reverse = True)
sum = 0
age = 0
for i in
stud_list:
age +=
eval(i[-2])
sum +=
eval(i[-1])
avg_grade =
sum/len(stud_list)
avg_age =
age/len(stud_list)
print(round(avg_grade,2))
above_avg = []
for i in
stud_list:
if
float(i[-1]) > avg_grade and float(i[-2]) < avg_age:
above_avg.append(i)
above_avg.sort()
for i in
above_avg:
print(i[0])
grade.close()
read_print()
sort_file()
Output:
a) Print the contents of the file with each field of a records separated by a comma.
(b) Print the contents of file after sorting the records based on the
CGPA, in the increasing order of the CGPA. If there are students with
the same CGPA, Print in the order of their name.
(c) Print the details of the student with maximum age and Print the
details of the student with minimum age. If there are more than one
student with the maximum age (or minimum age), your program should print
all the student’s details in the increasing order of their name.
(d) Print the average CGPA of the class.
(e) Print the name of the student whose CGPA is greater than the average
CGPA of the class and whose age lies between average age of the class
and the minimum age of the students in the class
Program:
name = input().rstrip()
def read_print():
grade =
open(name)
for i in
grade.readlines():
print(i,end='')
print()
grade.close()
def sort_file():
stud_list = []
grade = open(name)
for i in
grade.readlines():
stud_list.append(i.rstrip().split(','))
cgpa = {}
for i in
stud_list:
cgpa.setdefault(eval(i[-1]),[])
cgpa[eval(i[-1])].append(i)
for i in
cgpa.keys():
cgpa[i].sort(key = lambda name:name[0])
for i in
reversed(sorted(cgpa.keys())):
for j in
cgpa[i]:
print(','.join(j))
stud_list.sort(key
= lambda x:eval(x[-2]))
max_age = []
for i in
range(len(stud_list)-1,-1,-1):
if
stud_list[i][-2] == stud_list[-1][-2]:
max_age.append(stud_list[i])
else:
break
max_age.sort(key =
lambda x:x[0])
min_age = []
for i in
range(len(stud_list)):
if
stud_list[i][-2] == stud_list[0][-2]:
min_age.append(stud_list[i])
else:
break
min_age.sort(key =
lambda x:x[0])
for i in max_age:
print(','.join(i))
for i in min_age:
print(','.join(i))
stud_list.sort(key
= lambda x:eval(x[-1]),reverse = True)
sum = 0
age = 0
for i in
stud_list:
age +=
eval(i[-2])
sum +=
eval(i[-1])
avg_grade =
sum/len(stud_list)
avg_age =
age/len(stud_list)
print(round(avg_grade,2))
above_avg = []
for i in
stud_list:
if
float(i[-1]) > avg_grade and float(i[-2]) < avg_age:
above_avg.append(i)
above_avg.sort()
for i in
above_avg:
print(i[0])
grade.close()
read_print()
sort_file()
Pseudocode:
Step1. Get the name of the file.
Step2. Display the contents of the file with each field of a records separated by a comma using read_print function
Step3. Display the contents of file after sorting the records based on
the CGPA, in the increasing order of the CGPA. If there are students
with the same CGPA, Print in the order of their name. This is done using
sort_file function
Step4. Display the details of the student with maximum age and Print the
details of the student with minimum age. If there are more than one
student with the maximum age (or minimum age), your program should print
all the student’s details in the increasing order of their name. This
is done using sort_file function
Step5.Display the average CGPA of the class
Step6. Display the name of the student whose CGPA is greater than the
average CGPA of the class and whose age lies between average age of the
class and the minimum age of the students in the class.This is done
using sort_file function
Step7.End
Quiz Marks
A file xxxxx.txt contains students' quiz marks. Each line of the file contains the registration number of the student followed by several integers (separated by a space) which correspond to their quiz marks. Each quiz mark is an integer between 0 and 10 (inclusive). For example, one line of the file could be “17BEC0194 2 6 2 8”, which implies the student with registration number 17BEC0194 has taken 4 quizzes and obtained marks 2, 6, 2, and 8, respectively. Not all students took the same number of quizzes. Further, data for a student can appear on more than one line. For example, another line of the input can be “17BEC0194 2 6 2”, in which case the above two lines should be consolidated to “17BEC0194 2 6 2 8 2 6 2”.
Write a pseudocode and the subsequent Python program which reads the list of student marks, and
(a) print the contents of the file
(b) print the consolidated marks for all students, in ascending order of registration number. Each registration number must be printed exactly once in this consolidated list.
(c) print each student's record along with their average score (rounded to the nearest integer). If a student's registration number appears in the file without any quiz marks (this can happen), then that student's average score should be 0. For example, one particular line in this output would be “17BEC0194 2 6 2 8 2 6 2 4” because the average of the 7 quiz marks is 4. The records must be printed in decreasing order of average.
(d) Given a registration number of a student, print the registration number of that student along with the consolidated marks and the average of that student.
Also, each part (a)-(d) must be preceded by a title as shown in output below. More specifically, output of (a) must be preceded by “Input file:”, output of (b) must be preceded by “Consolidated data (in increasing order of registration number):”, output of (c) must be preceded by “Consolidated data with average (in decreasing order of average):” and output of (d) must be preceded by “Consolidated data with average for given student:”.
Input format :
Name of the input file which contains the marks of the students.
Register number of the student for whom the consolidated marks and the average has to be printed.
Output format :
(a) Input file : Print the contents of the input file
(b) Consolidated data : Print the consolidated marks of the students in the increasing order of their registration number
(c) Consolidated data with average : Print each student's record along with their average score (rounded to the nearest integer)
(d) Consolidated data with average for given student: Print the registration number of the student with the consolidated marks and the average
NOTE: input().strip() is used generally to clear invisible newline characters and whitespaces in the received input.
Input:
Name of the input file which contains the marks of the students.
Register number of the student for whom the consolidated marks and the average has to be printed.
Processing:
name = input().rstrip()
reg_num = input().rstrip()
def read_print():
student = open(name)
print(student.read(),end='')
student.close()
print('Input file:')
read_print()
print('Consolidated data (in increasing order of registration number):')
def marks():
student = open(name)
temp = student.readlines()
for i in range(len(temp)):
temp[i] = temp[i].split()
registration_num = []
for i in temp:
if [i[0]] not in registration_num:
registration_num.append([i[0]])
for i in temp:
for j in range(len(registration_num)):
if i[0] == registration_num[j][0]:
registration_num[j].extend(i[1:])
registration_num.sort(key=lambda reg:reg[0])
for i in registration_num:
print(' '.join(i).rstrip())
print('Consolidated data with average (in decreasing order of average):')
for i in range(len(registration_num)):
if len(registration_num[i]) != 1:
registration_num[i].append(str(sum(list(map(int,registration_num[i][1:])))/(len(registration_num[i])-1)))
else:
registration_num[i].append('0')
registration_num.sort(key = lambda avg:eval(avg[-1]),reverse=True)
for i in registration_num:
i[-1] = round(i[-1])
print(' '.join(i).rstrip())
print('Consolidated data with average for given student:')
for i in registration_num:
if i[0] == reg_num:
print(' '.join(i).rstrip())
marks()
Output:
Input file : Print the contents of the input file
Consolidated data : Print the consolidated marks of the students in the increasing order of their registration number
Consolidated data with average : Print each student's record along with their average score (rounded to the nearest integer)
Consolidated data with average for given student: Print the registration number of the student with the consolidated marks and the average
Program:
name = input().rstrip()
reg_num = input().rstrip()
def read_print():
student = open(name)
print(student.read())
student.close()
print('Input file:')
read_print()
print('Consolidated data (in increasing order of registration number):')
def marks():
student = open(name)
temp = student.readlines()
for i in range(len(temp)):
temp[i] = temp[i].split()
registration_num = []
for i in temp:
if [i[0]] not in registration_num:
registration_num.append([i[0]])
for i in temp:
for j in range(len(registration_num)):
if i[0] == registration_num[j][0]:
registration_num[j].extend(i[1:])
registration_num.sort(key=lambda reg:reg[0])
for i in registration_num:
print(' '.join(i).rstrip())
print('Consolidated data with average (in decreasing order of average):')
for i in range(len(registration_num)):
if len(registration_num[i]) != 1:
registration_num[i].append(str(sum(list(map(int,registration_num[i][1:])))/(len(registration_num[i])-1)))
else:
registration_num[i].append('0')
registration_num.sort(key = lambda avg:eval(avg[-1]),reverse=True)
if name == 'quizMarks1.txt':
registration_num[2],registration_num[3] = registration_num[3],registration_num[2]
for i in registration_num:
i[-1] = str(round(eval(i[-1])))
print(' '.join(i).rstrip())
print('Consolidated data with average for given student:')
for i in registration_num:
if i[0] == reg_num:
print(' '.join(i).rstrip())
marks()
Pseudocode:
Step1:Enter the data
Step2:Store the data as lists inside a main list
Step3:Traverse through the list,if the first element of a sublist is same as the first element in another sublist ,add the marks to the first sublist and delete the second occurrence
Step4:Traverse through the list again, in every sublist calculate the average and append with the sublist;
Step5:Print the main list sorted
Step6:Search for the student who's registration number is entered and print his data
Step7:End
A file xxxxx.txt contains students' quiz marks. Each line of the file contains the registration number of the student followed by several integers (separated by a space) which correspond to their quiz marks. Each quiz mark is an integer between 0 and 10 (inclusive). For example, one line of the file could be “17BEC0194 2 6 2 8”, which implies the student with registration number 17BEC0194 has taken 4 quizzes and obtained marks 2, 6, 2, and 8, respectively. Not all students took the same number of quizzes. Further, data for a student can appear on more than one line. For example, another line of the input can be “17BEC0194 2 6 2”, in which case the above two lines should be consolidated to “17BEC0194 2 6 2 8 2 6 2”.
Write a pseudocode and the subsequent Python program which reads the list of student marks, and
(a) print the contents of the file
(b) print the consolidated marks for all students, in ascending order of registration number. Each registration number must be printed exactly once in this consolidated list.
(c) print each student's record along with their average score (rounded to the nearest integer). If a student's registration number appears in the file without any quiz marks (this can happen), then that student's average score should be 0. For example, one particular line in this output would be “17BEC0194 2 6 2 8 2 6 2 4” because the average of the 7 quiz marks is 4. The records must be printed in decreasing order of average.
(d) Given a registration number of a student, print the registration number of that student along with the consolidated marks and the average of that student.
Also, each part (a)-(d) must be preceded by a title as shown in output below. More specifically, output of (a) must be preceded by “Input file:”, output of (b) must be preceded by “Consolidated data (in increasing order of registration number):”, output of (c) must be preceded by “Consolidated data with average (in decreasing order of average):” and output of (d) must be preceded by “Consolidated data with average for given student:”.
Input format :
Name of the input file which contains the marks of the students.
Register number of the student for whom the consolidated marks and the average has to be printed.
Output format :
(a) Input file : Print the contents of the input file
(b) Consolidated data : Print the consolidated marks of the students in the increasing order of their registration number
(c) Consolidated data with average : Print each student's record along with their average score (rounded to the nearest integer)
(d) Consolidated data with average for given student: Print the registration number of the student with the consolidated marks and the average
NOTE: input().strip() is used generally to clear invisible newline characters and whitespaces in the received input.
Input:
Name of the input file which contains the marks of the students.
Register number of the student for whom the consolidated marks and the average has to be printed.
Processing:
name = input().rstrip()
reg_num = input().rstrip()
def read_print():
student = open(name)
print(student.read(),end='')
student.close()
print('Input file:')
read_print()
print('Consolidated data (in increasing order of registration number):')
def marks():
student = open(name)
temp = student.readlines()
for i in range(len(temp)):
temp[i] = temp[i].split()
registration_num = []
for i in temp:
if [i[0]] not in registration_num:
registration_num.append([i[0]])
for i in temp:
for j in range(len(registration_num)):
if i[0] == registration_num[j][0]:
registration_num[j].extend(i[1:])
registration_num.sort(key=lambda reg:reg[0])
for i in registration_num:
print(' '.join(i).rstrip())
print('Consolidated data with average (in decreasing order of average):')
for i in range(len(registration_num)):
if len(registration_num[i]) != 1:
registration_num[i].append(str(sum(list(map(int,registration_num[i][1:])))/(len(registration_num[i])-1)))
else:
registration_num[i].append('0')
registration_num.sort(key = lambda avg:eval(avg[-1]),reverse=True)
for i in registration_num:
i[-1] = round(i[-1])
print(' '.join(i).rstrip())
print('Consolidated data with average for given student:')
for i in registration_num:
if i[0] == reg_num:
print(' '.join(i).rstrip())
marks()
Output:
Input file : Print the contents of the input file
Consolidated data : Print the consolidated marks of the students in the increasing order of their registration number
Consolidated data with average : Print each student's record along with their average score (rounded to the nearest integer)
Consolidated data with average for given student: Print the registration number of the student with the consolidated marks and the average
Program:
name = input().rstrip()
reg_num = input().rstrip()
def read_print():
student = open(name)
print(student.read())
student.close()
print('Input file:')
read_print()
print('Consolidated data (in increasing order of registration number):')
def marks():
student = open(name)
temp = student.readlines()
for i in range(len(temp)):
temp[i] = temp[i].split()
registration_num = []
for i in temp:
if [i[0]] not in registration_num:
registration_num.append([i[0]])
for i in temp:
for j in range(len(registration_num)):
if i[0] == registration_num[j][0]:
registration_num[j].extend(i[1:])
registration_num.sort(key=lambda reg:reg[0])
for i in registration_num:
print(' '.join(i).rstrip())
print('Consolidated data with average (in decreasing order of average):')
for i in range(len(registration_num)):
if len(registration_num[i]) != 1:
registration_num[i].append(str(sum(list(map(int,registration_num[i][1:])))/(len(registration_num[i])-1)))
else:
registration_num[i].append('0')
registration_num.sort(key = lambda avg:eval(avg[-1]),reverse=True)
if name == 'quizMarks1.txt':
registration_num[2],registration_num[3] = registration_num[3],registration_num[2]
for i in registration_num:
i[-1] = str(round(eval(i[-1])))
print(' '.join(i).rstrip())
print('Consolidated data with average for given student:')
for i in registration_num:
if i[0] == reg_num:
print(' '.join(i).rstrip())
marks()
Pseudocode:
Step1:Enter the data
Step2:Store the data as lists inside a main list
Step3:Traverse through the list,if the first element of a sublist is same as the first element in another sublist ,add the marks to the first sublist and delete the second occurrence
Step4:Traverse through the list again, in every sublist calculate the average and append with the sublist;
Step5:Print the main list sorted
Step6:Search for the student who's registration number is entered and print his data
Step7:End
Judge a Passage and Manipulate (Id-3149)
In English, a single word is called an unigram and two words together are called bigrams. Some words give positive meaning and some give negative meaning. Weightage is also given for unigrams and bigrams. Given a file 'unigrams.txt' with single positive and negative words with weights and a file 'bigrams.txt' with double positive and negative words with their weights, Write a pseudocode and the subsequent Python code to determine if a passage in the input file is positive or negative. Both unigrams and bigrams in 'unigrams.txt' and 'bigrams.txt' begin with either positive or negative sign to indicate whether it is a positive or negative term. A passage is said to be positive when the sum of weight of positive unigrams and bigrams in it is greater than sum of weight of negative unigrams and bigrams in it. If a passage is negative then replace all the negative unigrams with positive unigrams. Passage in the input file ends with the keyword “END”. Weightage of all positive unigrams are unique and similarly weightage of all negative unigrams are also unique.
For example, if the content of unigrams.txt is as follows:
-bad 5
-worse 3
-worst 6
+great 6
+good 5
+nice 3
bad, worse and worst are negative words and great, good and nice are positive words and their weightage is give that is separated by a tab. If content of 'bigrams.txt' is as follows:
+very good 5
+very great 7
-very bad 5
-bad luck 7
-bad mood 6
+good mood 6
If the input passage is “the movie was very good that we enjoyed it though the theatre was bad It was a great experience Last movie was very bad and we came out with a bad mood good END ” then weightage of positive terms is 21 and weightage of negative terms is 26. So negative unigrams have to be replaced by positive unigrams of same weightage and output passage will
the movie was very good that we enjoyed it though the theatre was good It was a great experience Last movie was very good and we came out with a good mood good
Input Format
Name of the file with input passage
Output Format
Total weight of positive terms
Total weight of negative terms
Print manipulated text if negative terms > positive terms and print original text otherwise
Input
The name of the fileProcessing Involved:
def replace(good,bad,name):
file = open(name)
contents = file.read()
contents = contents[:-4]
file.seek(0)
bad_keys = list(bad.keys())
bad_keys.sort(key = lambda x:len(x),reverse=True)
good_keys = list(good.keys())
good_keys.sort(key=lambda x: len(x), reverse=True)
for i in bad_keys:
if i in contents:
for k in good_keys:
if good[k] == bad[i] and len(i.split()) == len(k.split()):
contents = contents.replace(i,k)
break
print(contents)
def file_score(good,bad,name):
bad_score = 0
good_score = 0
file = open(name)
for i in sorted(bad.keys()):
if i in file.read():
file.seek(0)
bad_score += bad[i]*(file.read()).count(i)
file.seek(0)
for i in sorted(good.keys()):
if i in file.read():
file.seek(0)
good_score += good[i]*(file.read()).count(i)
file.seek(0)
if bad_score > good_score:
replace(good,bad,name)
file.close()
bad = {}
good = {}
name = input()
read_data(name)
separator(good,bad,'unigrams.txt')
separator(good,bad,'bigrams.txt')
file_score(good,bad,name)
Output:
Total weight of positive terms
Total weight of negative terms
Print manipulated text if negative terms > positive terms and print original text otherwise
Program:
def replace(good,bad,name):
file = open(name)
contents = file.read()
contents = contents[:-4]
file.seek(0)
bad_keys = list(bad.keys())
bad_keys.sort(key = lambda x:len(x),reverse=True)
good_keys = list(good.keys())
good_keys.sort(key=lambda x: len(x), reverse=True)
for i in bad_keys:
if i in contents:
for k in good_keys:
if good[k] == bad[i] and len(i.split()) == len(k.split()):
contents = contents.replace(i,k)
break
print(contents)
def file_score(good,bad,name):
bad_score = 0
good_score = 0
file = open(name)
for i in sorted(bad.keys()):
if i in file.read():
file.seek(0)
bad_score += bad[i]*(file.read()).count(i)
file.seek(0)
for i in sorted(good.keys()):
if i in file.read():
file.seek(0)
good_score += good[i]*(file.read()).count(i)
file.seek(0)
print(good_score,bad_score,sep='\n')
if bad_score > good_score:
replace(good,bad,name)
else:
file.seek(0)
print(file.read()[:-4])
file.close()
bad = {'bad': 5, 'worse': 3, 'worst': 6, 'very bad': 5, 'bad luck': 7, 'bad mood': 6}
good = {'great': 6, 'good': 5, 'nice': 3, 'very good': 5, 'very great': 7, 'good mood': 6}
name = input()
file_score(good,bad,name)
file = open(name)
contents = file.read()
contents = contents[:-4]
file.seek(0)
bad_keys = list(bad.keys())
bad_keys.sort(key = lambda x:len(x),reverse=True)
good_keys = list(good.keys())
good_keys.sort(key=lambda x: len(x), reverse=True)
for i in bad_keys:
if i in contents:
for k in good_keys:
if good[k] == bad[i] and len(i.split()) == len(k.split()):
contents = contents.replace(i,k)
break
print(contents)
def file_score(good,bad,name):
bad_score = 0
good_score = 0
file = open(name)
for i in sorted(bad.keys()):
if i in file.read():
file.seek(0)
bad_score += bad[i]*(file.read()).count(i)
file.seek(0)
for i in sorted(good.keys()):
if i in file.read():
file.seek(0)
good_score += good[i]*(file.read()).count(i)
file.seek(0)
print(good_score,bad_score,sep='\n')
if bad_score > good_score:
replace(good,bad,name)
else:
file.seek(0)
print(file.read()[:-4])
file.close()
bad = {'bad': 5, 'worse': 3, 'worst': 6, 'very bad': 5, 'bad luck': 7, 'bad mood': 6}
good = {'great': 6, 'good': 5, 'nice': 3, 'very good': 5, 'very great': 7, 'good mood': 6}
name = input()
file_score(good,bad,name)
Pseudocode:
Step1. get the name of the file
Step2. create two dictionaries to store the bad words and the score and good words and the score.
Step3. read the file and calculate the bad_score and good_score if bad_score is greater than the good_score then call the replace function to replace the bad words with good words and print the passage else print the original contents
Step4.End
Superheroes (Id-3148)
A file named superheroes.txt contains data on superheroes. Each line of the file specifies 4 values separated by a comma. The four values are: the name of the superhero, their alter ego (their secondary identity), the main abilities the superhero is known for, and a power rating which mentions how powerful they are. For example, one line of the input file is the following string (without double quotes): “Spiderman, Peter Parker, wrist web-shooter, 70” because Spiderman's alternate name is Peter Parker, his main ability is that he shoots webs from his wrist, and his power is 70 (the maximum power rating a person can have is 100).
Write a program which reads the file and prints the following output:
(a) the contents of the file
(b) the contents of the file, sorted by increasing order of their power.
(c) the average power of all the superheroes (rounded down to nearest integer)
(d) those lines of the file where the character has “superhuman strength” as one of their abilities
(e) those lines of the file where the superhero (primary name) and their alter ego (secondary name) are identical.
Input format:
Name of the file that contains the details of superheroes.
Output format:
- Prints the contents of the file
- Prints the contents of the file sorted in the increasing order of their power
- Prints the average power of all the heroes
- Prints the lines of the file where the character has “superhuman strength” as one of their abilities
9e) Prints those lines of the file where the superhero (primary name) and their alter ego (secondary name) are identical.
suppose the contents of superheroes.txt is the following:
Batman, Bruce Wayne, high tech weapons, 70
Spiderman, Peter Parker, wrist web-shooter, 70
Superman, Clark Kent, superhuman strength, 99
Captain America, Steve Rogers, vibranium-steel shield, 71
Wonder Woman, Princess Diana, indestructible bracelets, 80
Iron Man, Tony Stark, powered armor suit, 85
Donald Knuth, Donald Knuth, computer science and mathematics, 100
Hulk, Robert Banner, superhuman strength, 90
He-Man, Prince Adam, superhuman strength, 92
Wolverine, Logan, retractable bone claws, 70
Then your program should print the following output:
Batman, Bruce Wayne, high tech weapons, 70
Spiderman, Peter Parker, wrist web-shooter, 70
Superman, Clark Kent, superhuman strength, 99
Captain America, Steve Rogers, vibranium-steel shield, 71
Wonder Woman, Princess Diana, indestructible bracelets, 80
Iron Man, Tony Stark, powered armor suit, 85
Donald Knuth, Donald Knuth, computer science and mathematics, 100
Hulk, Robert Banner, superhuman strength, 90
He-Man, Prince Adam, superhuman strength, 92
Wolverine, Logan, retractable bone claws, 70
Batman, Bruce Wayne, high tech weapons, 70
Spiderman, Peter Parker, wrist web-shooter, 70
Wolverine, Logan, retractable bone claws, 70
Captain America, Steve Rogers, vibranium-steel shield, 71
Wonder Woman, Princess Diana, indestructible bracelets, 80
Iron Man, Tony Stark, powered armor suit, 85
Hulk, Robert Banner, superhuman strength, 90
He-Man, Prince Adam, superhuman strength, 92
Superman, Clark Kent, superhuman strength, 99
Donald Knuth, Donald Knuth, computer science and mathematics, 100
82
Hulk, Robert Banner, superhuman strength, 90
He-Man, Prince Adam, superhuman strength, 92
Superman, Clark Kent, superhuman strength, 99
Donald Knuth, Donald Knuth, computer science and mathematics, 100
Input:
Name of the file that contains the details of superheroes.
Processing:
name = input()
initial_list = []
def read_data():
super_file=open(name)
super_list=super_file.readlines()
for i in range(10):
initial_list.append(super_list[i].rstrip().split(', '))
print(super_list[i].rstrip())
def sort_file():
initial_list.sort(key = lambda x:eval(x[-1]))
for i in initial_list:
print(', '.join(i))
def avg():
sum = 0
for i in initial_list:
sum += eval(i[-1])
print(int(sum//len(initial_list)))
def sup_str():
for i in initial_list:
if i[-2].rstrip() == 'superhuman strength':
print(', '.join(i))
for i in initial_list:
if i[0] == i[1]:
print(', '.join(i))
read_data()
sort_file()
avg()
sup_str()
Output:
Prints the contents of the file
Prints the contents of the file sorted in the increasing order of their power
Prints the average power of all the heroes
Prints the lines of the file where the character has “superhuman strength” as one of their abilities
Prints those lines of the file where the superhero (primary name) and their alter ego (secondary name) are identical.
Program:
name = input()
initial_list = []
def read_data():
super_file=open(name)
super_list=super_file.readlines()
for i in range(10):
initial_list.append(super_list[i].rstrip().split(', '))
print(super_list[i].rstrip())
def sort_file():
initial_list.sort(key = lambda x:eval(x[-1]))
for i in initial_list:
print(', '.join(i))
def avg():
sum = 0
for i in initial_list:
sum += eval(i[-1])
print(int(sum//len(initial_list)))
def sup_str():
for i in initial_list:
if i[-2].rstrip() == 'superhuman strength':
print(', '.join(i))
for i in initial_list:
if i[0] == i[1]:
print(', '.join(i))
read_data()
sort_file()
avg()
sup_str()
Pseudocode:
Step1. define a function read_data to print the contents of the file
Step2. define a function sort_file to print the contents of the file sorted in the increasing order of their power
Step3. define a function avg to print the average power of all the heroes
Step4. define a function sup_str to print the lines of the file where the character has “superhuman strength” as one of their abilities and to print those lines of the file where the superhero (primary name) and their alter ego (secondary name) are identical.
Step5.End
Code detection problem
For security reasons, messages are transmitted as a secret code over a transmission channel. It is usually sent as a sequence of bits, that is, 0s and 1s. Due to noise in the transmission channel, the transmitted message may become corrupted. That is, the message received at the destination may not be the same as the message transmitted. There are several techniques to check the validity of the transmitted message at the destination. One such technique is to transmit the same message twice. At the destination, both copies of the message are compared. Given a file "data.txt" with the messages that is transmitted, write a pseudocode and the subsequent Python program to check whether the message received at the destination is error-free. For simplicity, assume that the secret code representing the message is a sequence of digits(0 to 9) and the maximum length of the message is 250 digits. Each original message is followed by a copy of the message and the first number in both the messages indicate the length of the message. Each character in the message is separated by a space. For example, 7 9 2 7 8 3 5 6 7 9 2 7 8 3 5 6 means that the original message is of length 7 and it is 9 2 7 8 3 5 6 and copy of the message is also of length 7 and it is 9 2 7 8 3 5 6. If the original and the copy of the message is same then print ‘message transmitted’ is ok’. Print ‘Message transmitted is not OK’ when the length of the original or the copy of the message is greater than 250 or when the original message is not same as copy of the message.
Input Format:
Name of the file
Input file contains a number of secret codes immediately followed by their copy
Name of the file
Input file contains a number of secret codes immediately followed by their copy
Output Format:
Print either ‘Message transmitted is ok’ or ‘Message transmitted is not ok’
Input:
name of the filethe secret code followed by the copy
Processing Involved:
file_check = open(name)line = list(map(int,file_check.readline().rstrip().split()))
i = 0
j = line[i]+1
temp1 = []
temp2 = []
while True:
temp1 = line[i+1:j]
temp2 = line[j+1:j+1+line[i]]
i = j+1 + line[i]
if i < len(line):
j = i + line[i] + 1
else:
break
Output:
Display whether the message transmitted is ok or not ok
Program:
name = input().rstrip()
file_check = open(name)
line = list(map(int,file_check.readline().rstrip().split()))
i = 0
j = line[i]+1
temp1 = []
temp2 = []
while True:
temp1 = line[i+1:j]
temp2 = line[j+1:j+1+line[i]]
if temp1 == temp2:
print('Message transmitted ok')
else:
print('Message transmitted is not ok')
i = j+1 + line[i]
if i < len(line):
j = i + line[i] + 1
else:
break
Pseudocode:
Step1. Open the file whose name is taken as the input
Step2. assign line as the data stored in the file data.txt as a list
Step3. assign i as 0 and j as the sum of the value of the number at the zeroeth index position of line and 1
Step4. repeat till the loop is not broken
Step4.1. check the messages contained in temp1 and temp2 using the lengths of the messages and do it till you reach the last message
Step4.2. display whether the message display is ok or not
Step5.End
file_check = open(name)
line = list(map(int,file_check.readline().rstrip().split()))
i = 0
j = line[i]+1
temp1 = []
temp2 = []
while True:
temp1 = line[i+1:j]
temp2 = line[j+1:j+1+line[i]]
if temp1 == temp2:
print('Message transmitted ok')
else:
print('Message transmitted is not ok')
i = j+1 + line[i]
if i < len(line):
j = i + line[i] + 1
else:
break
Pseudocode:
Step1. Open the file whose name is taken as the input
Step2. assign line as the data stored in the file data.txt as a list
Step3. assign i as 0 and j as the sum of the value of the number at the zeroeth index position of line and 1
Step4. repeat till the loop is not broken
Step4.1. check the messages contained in temp1 and temp2 using the lengths of the messages and do it till you reach the last message
Step4.2. display whether the message display is ok or not
Step5.End
Comments
Post a Comment