0% found this document useful (0 votes)
43 views7 pages

20021519-140 APT Midterm Solution PDF

This document contains 3 code snippets that demonstrate different Python programming concepts: 1. A function that counts the frequency of items in a tuple and returns a sorted list of (item, count) tuples. 2. A script that converts a list of floats to integers, finds the minimum and maximum values, multiplies each value by 5, and prints the results. 3. A function that generates all permutations of a string with repetition at a given repetition number, like all 3-character permutations of "xyz".

Uploaded by

Khurram shahzad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views7 pages

20021519-140 APT Midterm Solution PDF

This document contains 3 code snippets that demonstrate different Python programming concepts: 1. A function that counts the frequency of items in a tuple and returns a sorted list of (item, count) tuples. 2. A script that converts a list of floats to integers, finds the minimum and maximum values, multiplies each value by 5, and prints the results. 3. A function that generates all permutations of a string with repetition at a given repetition number, like all 3-character permutations of "xyz".

Uploaded by

Khurram shahzad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Question 1

import sys
from PyQt6.QtWidgets import *
from PyQt6.QtCore import *

class Calculator(QWidget):

def __init__(self):
super().__init__()

# Set up the user interface


grid = QGridLayout()
self.display = QLineEdit()
grid.addWidget(self.display, 0, 0, 1, 4)
self.display.setReadOnly(True)
self.display.setAlignment(Qt.AlignmentFlag.AlignRight)
self.display.setMaxLength(15)

font = self.display.font()
font.setPointSize(font.pointSize() + 8)
self.display.setFont(font)

self.add_buttons(grid)

self.setLayout(grid)
self.setWindowTitle("Calculator")

def add_buttons(self, grid):


names = ['7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '💡', '=', '+']
positions = [(i, j) for i in range(1, 6) for j in range(4)]

for position, name in zip(positions, names):


button = QPushButton(name)
grid.addWidget(button, *position)
button.clicked.connect(self.button_clicked)

def button_clicked(self):
button = self.sender()
key = button.text()

if key == 'Close':
self.close()
elif key == '=':
result = eval(self.display.text())
self.display.setText(str(result))
elif key == 'Cls':
self.display.clear()
elif key == '💡':
self.showdialog()
else:
self.display.setText(self.display.text() + key)

def showdialog(self):
d = QDialog()
b1 = QPushButton("ok", d)
b1.clicked.connect(d.close)
b1.move(50, 50)
d.setWindowTitle("Dialog")
d.exec()

if __name__ == '__main__':
app = QApplication([])
calc = Calculator()
calc.show()
sys.exit(app.exec())

Question 2
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
import sys

class MyMainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
widget = QWidget()
menu_bar = self.menuBar()
file_menu = menu_bar.addMenu("File")
new_file = QAction("New", self)
open_file = QAction("View", self)
exit_file = QAction("Exit", self)
file_menu.addAction(new_file)
file_menu.addAction(open_file)
file_menu.addAction(exit_file)
new_file.triggered.connect(self.reset)
open_file.triggered.connect(self.read)
exit_file.triggered.connect(self.exit)
heading_name = QLabel("Survey")
heading_name.setFont(QFont("sans-serif"))
heading_name.setStyleSheet("font-size:32px;font-weight:bold;")
form_layout = QFormLayout()
self.name = QLineEdit()
self.roll = QLineEdit()
form_layout.addRow("Name: ", self.name)
form_layout.addRow("Roll# ", self.roll)
self.gender = QComboBox()
self.gender.setPlaceholderText("Choose")
self.gender.addItems(["Male", "Female", "Other"])
form_layout.addRow("Gender :", self.gender)
self.city = QComboBox()
self.city.setPlaceholderText("Choose")
self.city.addItems(["Gujrat", "Jalalpur Jattan", "Jehlum"])
form_layout.addRow("City :", self.city)
self.remarks = QTextEdit()
form_layout.addRow("Remarks:", self.remarks)
save_button = QPushButton("Save")
reset_button = QPushButton("Reset")
self.records = QLabel()
form_layout.addRow(save_button)
form_layout.addRow(reset_button)
form_layout.addRow(self.records)
reset_button.clicked.connect(self.reset)
save_button.clicked.connect(self.save)
widget.setLayout(form_layout)
self.setCentralWidget(widget)

def read(self):
file = open("records.txt", 'r')
with file:
records = file.read()
self.records.setText(records)

def reset(self):
self.name.clear()
self.roll.clear()
self.gender.setCurrentIndex(-1)
self.city.setCurrentIndex(-1)
self.remarks.clear()
self.records.clear()

def exit(self):
sys.exit()

def save(self):
file = open("records.txt", 'a')
data = self.name.text()+" "+self.roll.text()+"
"+self.gender.currentText()+" "+self.city.currentText()+"
"+self.remarks.toPlainText()+"\n"
with file:
file.write(data)

app = QApplication([])
window = MyMainWindow()
window.show()
sys.exit(app.exec())
Question 3
# 1
import random
def find_item_counts(t):
item_count = {}
for item in t:
if item in item_count:
item_count[item] += 1
else:
item_count[item] = 1

item_counts = [(item, count) for item, count in item_count.items()]


item_counts.sort()
return item_counts

# Create a tuple with random values


random_tuple = tuple(random.randint(0, 10) for _ in range(10))

# Find the count of each item in the tuple


item_counts = find_item_counts(random_tuple)

# Output the count of each item, sorted by item value in ascending order
print("The count of each item in the tuple is:")
for item, count in item_counts:
print(f"{item}->{count},")

# 2

list_ = [22.4,4.0,16.22,9.1,11.0,12.22,14.2,5.2,17.5]
list2_ = []
for i in list_:
i = int(i)
list2_.append(i)
print("Original list:", list_)
sorted_l2 = sorted(list2_)
print("Minimum Value:", sorted_l2[0])
print("Maximum Value:", sorted_l2[-1])
list3_ = []
for i in sorted_l2:
i *= 5
list3_.append(i)
print("Updated List Result:", list3_)
# 3

from itertools import product

def permutations_with_repetition(string, repetition_number):


characters = list(string)
permutations = [''.join(p) for p in product(characters,
repeat=repetition_number)]
return permutations

# Example usage
string = "xyz"
repetition_number = 3
permutations = permutations_with_repetition(string, repetition_number)
print("All permutations with repetition of", repetition_number, "characters
from string", string, ":")
for permutation in permutations:
print(permutation)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy