diff --git a/MaxHeap.py b/MaxHeap.py index 0168aae5..9d0547e7 100644 --- a/MaxHeap.py +++ b/MaxHeap.py @@ -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())) \ No newline at end of file +print(m.heap[1:]) # Print heap elements excluding the dummy element +print(m.pop()) diff --git a/TempConversion.py b/TempConversion.py index b8c71e54..000764df 100644 --- a/TempConversion.py +++ b/TempConversion.py @@ -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() \ No newline at end of file + 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() diff --git a/turtle_graphics.py b/turtle_graphics.py index da671ef3..15c8f583 100644 --- a/turtle_graphics.py +++ b/turtle_graphics.py @@ -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() \ No newline at end of file +tt.done()