Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Choice Function: Replaced sample with choice for selecting a single code #154

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 46 additions & 51 deletions MaxHeap.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,55 @@
# Python MaxHeap
# public functions: push, peek, pop
# private functions: __swap, __floatUp, __bubbleDown

class MaxHeap:
def __init__(self, items=[]):
super().__init__()
self.heap = [0]
for i in items:
self.heap.append(i)
self.__floatUp(len(self.heap) - 1)
def __init__(self, items=[]):
self.heap = [0] # Initialize heap with a dummy element at index 0
for i in items:
self.push(i)

def push(self, data):
self.heap.append(data)
self.__float_up(len(self.heap) - 1) # Renamed to follow Python naming conventions

def push(self, data):
self.heap.append(data)
self.__floatUp(len(self.heap) - 1)
def peek(self):
if len(self.heap) > 1:
return self.heap[1]
else:
return None # Return None instead of False for clarity

def peek(self):
if self.heap[1]:
return self.heap[1]
else:
return False

def pop(self):
if len(self.heap) > 2:
self.__swap(1, len(self.heap) - 1)
max = self.heap.pop()
self.__bubbleDown(1)
elif len(self.heap) == 2:
max = self.heap.pop()
else:
max = False
return max
def pop(self):
if len(self.heap) > 2:
self.__swap(1, len(self.heap) - 1)
max_value = self.heap.pop()
self.__bubble_down(1)
elif len(self.heap) == 2:
max_value = self.heap.pop()
else:
max_value = None # Return None instead of False for clarity
return max_value

def __swap(self, i, j):
self.heap[i], self.heap[j] = self.heap[j], self.heap[i]
def __swap(self, i, j):
self.heap[i], self.heap[j] = self.heap[j], self.heap[i]

def __floatUp(self, index):
parent = index//2
if index <= 1:
return
elif self.heap[index] > self.heap[parent]:
self.__swap(index, parent)
self.__floatUp(parent)
def __float_up(self, index):
parent = index // 2
if index <= 1:
return
elif self.heap[index] > self.heap[parent]:
self.__swap(index, parent)
self.__float_up(parent)

def __bubbleDown(self, index):
left = index * 2
right = index * 2 + 1
largest = index
if len(self.heap) > left and self.heap[largest] < self.heap[left]:
largest = left
if len(self.heap) > right and self.heap[largest] < self.heap[right]:
largest = right
if largest != index:
self.__swap(index, largest)
self.__bubbleDown(largest)
def __bubble_down(self, index):
left = index * 2
right = index * 2 + 1
largest = index
if len(self.heap) > left and self.heap[largest] < self.heap[left]:
largest = left
if len(self.heap) > right and self.heap[largest] < self.heap[right]:
largest = right
if largest != index:
self.__swap(index, largest)
self.__bubble_down(largest)

# Test the MaxHeap class
m = MaxHeap([95, 3, 21])
m.push(10)
print(str(m.heap[0:len(m.heap)]))
print(str(m.pop()))
print(m.heap[1:]) # Print heap elements excluding the dummy element
print(m.pop())
53 changes: 27 additions & 26 deletions TempConversion.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
# Temperature Conversion Program

def menu():
print("\n1. Celsius to Fahrenheit")
print("2. Fahrenheit to Celsius")
print("3. Exit")
choice = int(input("Enter a choice: "))
return choice

def toCelsius(f):
return int((f - 32) / 1.8)
print("\n1. Celsius to Fahrenheit")
print("2. Fahrenheit to Celsius")
print("3. Exit")
choice = input("Enter a choice: ").strip() # Added .strip() to remove leading/trailing whitespace
return choice

def to_celsius(f):
return (f - 32) / 1.8 # Removed unnecessary conversion to int

def to_fahrenheit(c):
return c * 1.8 + 32 # Removed unnecessary conversion to int

def toFahrenheit(c):
return int(c * 1.8 + 32)

def main():
choice = menu()
while choice != 3:
if choice == 1:
c = eval(input("Enter degrees Celsius: "))
print(str(c) + "C = " + str(toFahrenheit(c)) + "F")
elif choice == 2:
f = eval(input("Enter degrees Fahrenheit: "))
print(str(f) + "F = " + str(toCelsius(f)) + "C")
else:
print("Invalid choice.")
choice = menu()

main()
choice = menu()
while choice != '3': # Changed 3 to '3' to compare with string
if choice == '1':
c = float(input("Enter degrees Celsius: ")) # Changed eval to float for safer input parsing
fahrenheit = to_fahrenheit(c)
print(f"{c}C = {fahrenheit}F") # Using f-strings for cleaner output formatting
elif choice == '2':
f = float(input("Enter degrees Fahrenheit: ")) # Changed eval to float for safer input parsing
celsius = to_celsius(f)
print(f"{f}F = {celsius}C") # Using f-strings for cleaner output formatting
else:
print("Invalid choice.")
choice = menu()

if __name__ == "__main__": # Added guard to ensure main() runs only if the script is executed directly
main()
57 changes: 28 additions & 29 deletions turtle_graphics.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
import turtle as tt
from random import randint, sample
from random import randint, choice

def draw():
size = randint(40, 300)
angles = (144, 150, 157.5, 160, 165)
angle = sample(angles, 1)[0]

colors = [
('#922B21', '#E6B0AA'), ('#76448A', '#D2B4DE'), ('#1F618D', '#AED6F1'), ('#515A5A', '#EAEDED'),
('#148F77', '#D1F2EB'), ('#B7950B', '#F7DC6F'), ('#F39C12', '#FDEBD0'), ('#BA4A00', '#F6DDCC')]
color = sample(colors, 1)[0]
tt.color(color[0], color[1])

x_pos = randint(-200,200)
y_pos = randint(-200,200)
tt.pu()
tt.setpos(x_pos, y_pos)
start_position = tt.pos()
tt.pd()

tt.begin_fill()
while True:
tt.forward(size)
tt.left(angle)
if abs(tt.pos() - start_position) < 1:
break
tt.end_fill()

size = randint(40, 300)
angles = (144, 150, 157.5, 160, 165)
angle = choice(angles) # Using choice instead of sample for single random choice

colors = [
('#922B21', '#E6B0AA'), ('#76448A', '#D2B4DE'), ('#1F618D', '#AED6F1'), ('#515A5A', '#EAEDED'),
('#148F77', '#D1F2EB'), ('#B7950B', '#F7DC6F'), ('#F39C12', '#FDEBD0'), ('#BA4A00', '#F6DDCC')]
color = choice(colors) # Using choice instead of sample for single random choice

x_pos = randint(-200, 200)
y_pos = randint(-200, 200)
tt.penup() # Correcting function name to penup
tt.setpos(x_pos, y_pos)
start_position = tt.pos().copy() # Using copy to avoid modifying original position
tt.pendown() # Correcting function name to pendown

tt.begin_fill()
while True:
tt.forward(size)
tt.left(angle)
if tt.distance(start_position) < 1: # Using distance function for distance calculation
break
tt.end_fill()

tt.circle(100)
for i in range(3):
tt.pensize(i%3)
draw()
tt.pensize(i % 3)
draw()

tt.done()
tt.done()