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

solved the updation of MaxHeap.py #156 #157

Open
wants to merge 2 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
98 changes: 47 additions & 51 deletions MaxHeap.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,56 @@
# 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=[]):
super().__init__()
self.heap = [0] # Used a dummy element at index 0
for i in items:
self.heap.append(i)
self._float_up(len(self.heap) - 1)

def push(self, data):
self.heap.append(data)
self.__floatUp(len(self.heap) - 1)
def push(self, data):
self.heap.append(data)
self._float_up(len(self.heap) - 1)

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 peek(self):
if len(self.heap) > 1:
return self.heap[1]
else:
return None
def pop(self):
if len(self.heap) > 2:
self._swap(1, len(self.heap) - 1)
max_val = self.heap.pop()
self._bubble_down(1)
elif len(self.heap) == 2:
max_val = self.heap.pop()
else:
max_val = None
return max_val

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)

m = MaxHeap([95, 3, 21])
m.push(10)
print(str(m.heap[0:len(m.heap)]))
print(str(m.pop()))
print(str(m.heap[1:])) # Excluding the dummy element when printing heap elements
print(str(m.pop())) # Output of pop operation
27 changes: 14 additions & 13 deletions TempConversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@ def menu():
print("\n1. Celsius to Fahrenheit")
print("2. Fahrenheit to Celsius")
print("3. Exit")
choice = int(input("Enter a choice: "))
choice = input("Enter a choice: ")
return choice

def toCelsius(f):
return int((f - 32) / 1.8)
def to_celsius(fahrenheit):
return (fahrenheit - 32) / 1.8

def toFahrenheit(c):
return int(c * 1.8 + 32)
def to_fahrenheit(celsius):
return celsius * 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")
while choice != '3':
if choice == '1':
celsius = float(input("Enter degrees Celsius: "))
print(f"{celsius}C = {to_fahrenheit(celsius)}F")
elif choice == '2':
fahrenheit = float(input("Enter degrees Fahrenheit: "))
print(f"{fahrenheit}F = {to_celsius(fahrenheit)}C")
else:
print("Invalid choice.")
choice = menu()

main()
if __name__ == "__main__":
main()