Skip to content

Commit 77ad7ff

Browse files
committed
assignment solutions
1 parent 81c1043 commit 77ad7ff

File tree

11 files changed

+183
-0
lines changed

11 files changed

+183
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class BankAccount:
2+
def __init__(self, account_number, balance):
3+
self.account_number = account_number
4+
self.balance = balance
5+
6+
def deposit(self, amount):
7+
if amount > 0:
8+
self.balance += amount
9+
print(f"Deposited {amount}. New balance: {self.balance}")
10+
else:
11+
print("Invalid deposit amount.")
12+
13+
def withdraw(self, amount):
14+
if amount > 0 and amount <= self.balance:
15+
self.balance -= amount
16+
print(f"Withdrew {amount}. New balance: {self.balance}")
17+
elif amount > self.balance:
18+
print("Insufficient funds.")
19+
else:
20+
print("Invalid withdrawal amount.")
21+
22+
def check_balance(self):
23+
print(f"Your current balance is: {self.balance}")
24+
25+
account1 = BankAccount("1234567890", 1000)
26+
account1.deposit(500)
27+
account1.withdraw(200)
28+
account1.check_balance()
29+
account1.withdraw(2000)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Library:
2+
def __init__(self, listofbooks):
3+
self.availablebooks = listofbooks
4+
5+
def displayAvailablebooks(self):
6+
print("The books we have in our library are as follows:")
7+
for book in self.availablebooks:
8+
print(book)
9+
10+
def removeBook(self, requestedBook):
11+
if requestedBook in self.availablebooks:
12+
print("The book you requested has now been Removed")
13+
self.availablebooks.remove(requestedBook)
14+
else:
15+
print("Sorry the book you have requested is currently not in the library")
16+
17+
def addBook(self, returnedBook):
18+
self.availablebooks.append(returnedBook)
19+
print("Book added successfully!")
20+
21+
myLibrary = Library(['book1', 'book2', 'book3'])
22+
myLibrary.addBook('book4')
23+
myLibrary.displayAvailablebooks()
24+
myLibrary.removeBook('book2')
25+
myLibrary.displayAvailablebooks()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
original_list = [1, 2, 3, 4, 5]
2+
print("List before reverse : ",original_list)
3+
4+
reversed_list = []
5+
6+
for value in original_list:
7+
reversed_list = [value] + reversed_list
8+
9+
print("List after reverse : ", reversed_list)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
num = int(input("Enter a number: "))
2+
sum = 0
3+
4+
temp = num
5+
6+
while temp > 0:
7+
digit = temp % 10
8+
sum += digit ** 3
9+
temp //= 10
10+
11+
if num == sum:
12+
print(num,"is an Armstrong number")
13+
else:
14+
print(num,"is not an Armstrong number")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
n = int(input("Enter a number: "))
2+
if n < 0:
3+
print("Factorial of a negative number does not exist! ")
4+
else:
5+
factorial = 1
6+
7+
while n >= 1:
8+
factorial *= n
9+
n -= 1
10+
11+
print("Factorial: ", factorial)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
print("Fibonacci series with 10 elements: ")
2+
first, last = 0, 1
3+
for _ in range(10):
4+
print(first)
5+
first, last = last, first + last
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class MyQueue:
2+
def __init__(self):
3+
self.items = []
4+
5+
def enqueue(self, item):
6+
self.items.append(item)
7+
print("Enqueud Element: ", item)
8+
9+
def dequeue(self):
10+
if not self.is_empty():
11+
print("Dequeued Element: ", self.items.pop(0))
12+
else:
13+
print("Cannot dequeue from an empty queue.")
14+
15+
def is_empty(self):
16+
return len(self.items) == 0
17+
18+
queue = MyQueue()
19+
queue.enqueue(10)
20+
queue.enqueue(20)
21+
queue.enqueue(30)
22+
print("Current Queue:", queue.items)
23+
queue.dequeue()
24+
print("Current Queue:", queue.items)

Data Structure Assignment/num.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def square():
2+
for i in range(0,5):
3+
for j in range(0,5):
4+
print("*", end=' ')
5+
print('')
6+
7+
def triangle():
8+
for i in range(0,5):
9+
print("Triangle pattern to be shown here!!", i)
10+
11+
square()
12+
triangle();

Data Structure Assignment/stack.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Stack:
2+
def __init__(self):
3+
self.stack = []
4+
5+
def isEmpty(self):
6+
return len(self.stack) == 0
7+
8+
def push(self, element):
9+
self.stack.append(element)
10+
11+
def pop(self):
12+
if not self.isEmpty():
13+
print("Popped Element: ", self.stack.pop())
14+
else:
15+
print("Stack is empty.")
16+
17+
def peek(self):
18+
if not self.isEmpty():
19+
print("Peeked Element: ", self.stack[-1])
20+
else:
21+
print("Stack is empty.")
22+
23+
myStack = Stack()
24+
myStack.push(23)
25+
myStack.push(32)
26+
myStack.push(42)
27+
28+
myStack.peek()
29+
myStack.pop()
30+
myStack.peek()
31+
myStack.pop()
32+
myStack.peek()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def celciusToFahrenheit(temp):
2+
return (9/5)*temp+32
3+
4+
def fahrenheitToCelcius(temp):
5+
return (temp-32)*5/9
6+
7+
print("Enter desired conversion: \n \'a\' for Celsius to Fahrenheit \n \'b\' for Fahrenheit to Celsius")
8+
choice = input()
9+
10+
if choice == 'a' or choice == 'A':
11+
temp = float(input("Enter temperature in Celcius: "))
12+
print("Fahrenheit: ", celciusToFahrenheit(temp))
13+
14+
elif choice == 'b' or choice == 'B':
15+
temp = float(input("Enter temperature in Fahrenheit: "))
16+
print("Celsius: ", fahrenheitToCelcius(temp))
17+
else:
18+
print("Enter a valid choice!")

0 commit comments

Comments
 (0)