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

2mid-exam

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

# -*- coding: utf-8 -*-

"""
Created on Sat Dec 7 10:22:31 2024

@author: Dr. salma


"""
#### Q1/1
print("solution of Q1/1 \n")
W = []
def O_item(W):
if not W:
print("Storage is empty! No item to remove.")
else:
removed_item = W.pop()
print(f"Item '{removed_item}' removed from storage.")
W = [ "box", "chair", "table", "lamb", "fan"]
O_item(W)

#### Q1/2
from collections import deque

print("solution of Q1/2 \n")


W = deque() # Initialize the queue
def O_item(W):
if not W:
print("Storage is empty! No item to remove.")
else:
removed_item = W.popleft()
print(f"Item '{removed_item}' removed from storage.")

W = deque([ "box", "chair", "table", "lamb", "fan"])


O_item(W)

#### Q1/3
#### write Python program find difference between each number in the ar
print("solution of Q1/3 \n")
def dfa(w):
a = sum(w) / len(w)

1
d = [ww - a for ww in w]
return d

# Example usage
N = [10, 20, 30, 40, 50]
ds = dfa(N)
print("Numbers:", N)
print("the output :", ds)
"""
def differences_from_average(numbers):
# Calculate the average of the numbers
average = sum(numbers) / len(numbers)

# Calculate the difference for each number


differences = [num - average for num in numbers]

return differences

# Example usage
numbers = [10, 20, 30, 40, 50]
differences = differences_from_average(numbers)
print("Array:", numbers)
print("Differences from average:", differences)
"""
#### Q1/4
#### write Python program to frequency of any number in inputted array.
print("solution of Q1/4 \n")
def FC(N):
fr = {}
for n in N:
if n in fr:
fr[n] += 1
else:
fr[n] = 1
return fr

N = list(map(int, input("Enter the numbers in the array (separated by s


F = FC(N)
print("The output is :")

2
for n, f in F.items():
print(f"{n}: {f}")
"""
from collections import Counter

def calculate_frequency(numbers):
# Use Counter to count the frequency of each number
frequency = Counter(numbers)
return frequency

# Input the array from the user


numbers = list(map(int, input("Enter the numbers in the array (separate

# Get the frequency of each number


frequency = calculate_frequency(numbers)

# Print the frequencies


print("Frequencies of numbers:")
for num, freq in frequency.items():
print(f"{num}: {freq}")
"""
#### Q1/5
#### what are the outputs for these repeatations.
print("solution of Q1/5 \n")
n = ["box", "doll", "book", "ball", "table"]

for i in n:
print(i)

for i in range(5):
print(i)

for id, it in enumerate(n):


print(id, " ", it)

#######################################################################
#### Q2/A
print("solution of Q2/A \n")

3
class Flower:
def __init__(self, name, color, price):
self.name = name
self.color = color
self.price = price
self.next = None # Pointer to the next flower in the list

class FlowerLinkedList:
def __init__(self):
self.head = None

def append(self, name, color, price):


new_flower = Flower(name, color, price)
if not self.head:
self.head = new_flower
return
last_flower = self.head
while last_flower.next:
last_flower = last_flower.next
last_flower.next = new_flower

def display(self):
current_flower = self.head
while current_flower:
print(f'Name: {current_flower.name}, Color: {current_flower
current_flower = current_flower.next

def reverse(self):
prev = None
current = self.head
while current:
next_node = current.next # Store next node
current.next = prev # Reverse the link
prev = current # Move prev to current
current = next_node # Proceed to next node
self.head = prev # Update head to new first eleme

# Example Usage

4
flower_list = FlowerLinkedList()
flower_list.append("Rose", "Red", 2.5)
flower_list.append("Tulip", "Yellow", 1.5)
flower_list.append("Lily", "White", 3.0)
flower_list.display()
flower_list.reverse()
flower_list.display()

#### Q2/B
print("solution of Q2/B \n")
class Node:
def __init__(self, name, body_cover, movement):
self.name = name
self.body_cover = body_cover
self.movement = movement
self.next = None

class AnimalLinkedList:
def __init__(self):
self.head = None

def append(self, name, body_cover, movement):


new_node = Node(name, body_cover, movement)
if not self.head:
self.head = new_node
return
last = self.head
while last.next:
last = last.next
last.next = new_node

def display(self):
current = self.head
while current:
print(f"Name: {current.name}, Body Cover: {current.body_cov
current = current.next

# Example usage
animal_list = AnimalLinkedList()

5
animal_list.append("Dog", "Fur", "Walks")
animal_list.append("Cat", "Fur", "Walks")
animal_list.append("Fish", "Scales", "Swims")
animal_list.append("Bird", "Feathers", "Flies")
animal_list.append("Penguin", "feathers", "swims")
animal_list.append("Cockroach", "exoskeleton", "flies")
animal_list.append("monkey", "fur", "jumps")

animal_list.display()

#######################################################################
#### solutions
"""
solution of Q1/1

Item 'fan' removed from storage.


solution of Q1/2

Item 'box' removed from storage.


solution of Q1/3

Numbers: [10, 20, 30, 40, 50]


the output : [-20.0, -10.0, 0.0, 10.0, 20.0]
solution of Q1/4

Enter the numbers in the array (separated by spaces): 2 1 1 1 2 3 3 3 3


The output is :
2: 2
1: 3
3: 4
4: 1
5: 3
solution of Q1/5

box
doll
book
ball
table

6
0
1
2
3
4
0 box
1 doll
2 book
3 ball
4 table
solution of Q2/A

Name: Rose, Color: Red, Price: 2.5


Name: Tulip, Color: Yellow, Price: 1.5
Name: Lily, Color: White, Price: 3.0
Name: Lily, Color: White, Price: 3.0
Name: Tulip, Color: Yellow, Price: 1.5
Name: Rose, Color: Red, Price: 2.5
solution of Q2/B

Name: Dog, Body Cover: Fur, Movement: Walks


Name: Cat, Body Cover: Fur, Movement: Walks
Name: Fish, Body Cover: Scales, Movement: Swims
Name: Bird, Body Cover: Feathers, Movement: Flies
Name: Penguin, Body Cover: feathers, Movement: swims
Name: Cockroach, Body Cover: exoskeleton, Movement: flies
Name: monkey, Body Cover: fur, Movement: jumps
"""

You might also like