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

Practical Exam_85 minutes_Python 3rd

The document provides instructions for a programming assignment requiring the use of Thonny IDE to complete three tasks involving Python programming. Students must submit their work as a zip file named with their roll number and name, and any deviation from the specified requirements will render the work invalid. The tasks include printing a rhombus, analyzing prime factors from a text file, and managing a list of books using object-oriented programming techniques.

Uploaded by

tranqm2006
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Practical Exam_85 minutes_Python 3rd

The document provides instructions for a programming assignment requiring the use of Thonny IDE to complete three tasks involving Python programming. Students must submit their work as a zip file named with their roll number and name, and any deviation from the specified requirements will render the work invalid. The tasks include printing a rhombus, analyzing prime factors from a text file, and managing a list of books using object-oriented programming techniques.

Uploaded by

tranqm2006
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

INSTRUCTIONS

Please read the instructions carefully before doing the


questions.
● You can use materials on your computer, notebook, and
textbook.
● You are NOT allowed to use any device to share data
with others.
Besides the above conditions, students must follow the
following requirements:
1.The work must be completed by using Thonny IDE.
2.The result is three files .py, compressed into a
RollNo_Name_PFP191.zip (1), e.g.,
HE123456_DatNTT_PFP191.zip.
3.THIS PART IS VERY IMPORTANT, PLEASE READ IT CAREFULLY
AND FOLLOW THE INSTRUCTIONS.
● You are given a data text file.
● You are not allowed to use other IDEs such as VS
Code, PyCharm, etc.
● Just one of the above requirements is violated,
your work will be considered invalid.
On completion, submit the zip file (1).
85 minutes
Question 1. (3 points)
Write a python program that uses a for loop to print a
rhombus (diamond) with asterisks as shown below. The
input is the side length of the rhombus and will be
entered by the user.

Question 2. (3 points)
Write a program in Python that reads a text file
"data_number.txt" (Attached with exam paper) containing
many numbers into a list. Read each number in the list
in turn and analyze it into prime factors. eg: 99 =
3x3x11. Print the results to the screen after the
program finishes processing.
3. Write a program in the python programming language
using object-oriented programming techniques. The
program enters a list of books, each with information
on name, year and DOI number. Sort books by name and
order in the alphabet table. Print the list of books
before and after the sorting. Search for the books that
have the highest year.
(4 points)
Solution:
1. Code
# Program to print a rhombus
n = input('Please enter the edge of the rhombus: ')
n = int(n)
# Print the balance triangle
for i in range(1, n):
s = n - i
k = s * " "
print(k, end='')
print((2 * i - 1) * '*')
# Print the reverse balance triangle
for i in range(n, 0, -1):
s = n - i
k = s * ' '
print(k, end='')
print((2 * i - 1) * '*')

2. Code
def phanTichSoNguyen(n):
i = 2
listNumbers = []
# Analize number
while (n > 1):
if (n % i == 0):
n = int(n / i)
listNumbers.append(i)
else:
i = i + 1
# If listNumbers is empty then add n to listNumbers
if (len(listNumbers) == 0):
listNumbers.append(n)
return listNumbers
name = input('Enter file: ')
handle = open(name)
for i in handle:
numbers = i.split()
for n in numbers:
# Analyze the unsigned integer n
listNumbers = phanTichSoNguyen(int(n))
size = len(listNumbers);
sb = ""
for i in range(0, size - 1):
sb = sb + str(listNumbers[i]) + " x "
sb = sb + str(listNumbers[size-1])
# print result
print("Result:", n, "=", sb)
3. Code
class book:
name = 0
year = 0
DOI = ''
def set_year(self):
self.year = input('Enter year: ')
def get_year(self):
return self.year
def set_DOI(self):
self.DOI = input('Enter DOI: ')
def get_DOI(self):
return self.DOI
def set_name(self):
self.name = input('Enter name: ')
def get_name(self):
return self.name
lst=[]
for i in range(5):
print('Enter books :{}'.format(i+1))
tmp_st = book()
tmp_st.set_name()
tmp_st.set_year()
tmp_st.set_DOI()
lst.append(tmp_st)
print('before sorting:')
for i in range(5):
print('Book {}'.format(i+1))
print('Name : {}'.format(lst[i].get_name()))
print('DOI : {}'.format(lst[i].get_DOI()))
print('Year : {}'.format(lst[i].get_year()))

lst.sort(key=lambda x: x.name, reverse=False)

print('after sorting:')
for i in range(5):
print('Books {}'.format(i+1))
print('Name : {}'.format(lst[i].get_name()))
print('DOI : {}'.format(lst[i].get_DOI()))
print('Year : {}'.format(lst[i].get_year()))
class Employee:
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary

def __repr__(self):
return f"{self.name}\n{self.age}\
n{self.salary}"

def main():
print("1. Test f1 (1 mark)")
print("2. Test f2 (1 mark)")
print("3. Test f3 (1 mark)")
print("4. Test f4 (1 mark)")
selection = input("Your selection (1 -> 4): ")

if selection == "2":
num_employees = int(input("Enter the number of
employees: "))
employees = []

for i in range(num_employees):
print(f"Enter employee {i + 1}")
name = input("Enter name : ")
age = int(input("Enter age : "))
salary = int(input("Enter salary : "))
employee = Employee(name, age, salary)
employees.append(employee)

# Sort employees by name in alphabetical order


employees.sort(key=lambda x: x.name)

print("OUTPUT")
for idx, employee in enumerate(employees,
start=1):
print(f"Employee {idx}")
print(employee)

print("FINISH")

elif selection == "3":


num_employees = int(input("Enter the number of
employees: "))
employees = []

for i in range(num_employees):
print(f"Enter employee {i + 1}")
name = input("Enter name : ")
age = int(input("Enter age : "))
salary = int(input("Enter salary : "))
employee = Employee(name, age, salary)
employees.append(employee)

# Calculate the average salary


total_salary = sum(employee.salary for employee
in employees)
average_salary = total_salary / num_employees

# Filter employees with salary >= average


salary
filtered_employees = [employee for employee in
employees if employee.salary >= average_salary]

# Sort the filtered employees by salary in


descending order
filtered_employees.sort(key=lambda x: x.salary,
reverse=True)

print(f"\nAverage salary: {average_salary}")


print("Employees with salary >= average salary
(in descending order):")
for employee in filtered_employees:
print(employee)

if __name__ == "__main__":
main()

You might also like