Skip to content
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
62 changes: 62 additions & 0 deletions a1_w10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'''
#############################################################################################
#*******************************************************************************************#
# Copyright (c) 2021 pyCoder|semih Corporation;) All rights reserved. ##
# [Timestamp:20210322] ##
#*******************************************************************************************#
#############################################################################################
'''
# Question 1:
# Given n ropes of different lengths, connect them into a single rope with minimum cost.
# Assume that the cost to connect two ropes is the same as the sum of their lengths.(Hint: Use a priority queue implemented using min-heap)
# Input: [5,4,2,8]
# Output : The minimum cost is 36
# For example,
# [5, 4, 2, 8] –> First, connect ropes of lengths 4 and 2 that will cost 6.
# [5, 6, 8] –> Next, connect ropes of lengths 5 and 6 that will cost 11.
# [11, 8] –> Finally, connect the remaining two ropes that will cost 19.
# Therefore, the total cost for connecting all ropes is 6 + 11 + 19 = 36.

import heapq
from heapq import heappush, heappop

"""
2 min-heap
/ \
4 5
/
8
"""
# Function to calculate the minimum cost to join `n` ropes into a single rope
def findMinCost(prices):

# In-place transform list `prices` into a min-heap in linear time
heapq.heapify(prices)

# keep track of the minimum cost so far
cost = 0

# repeat till heap size is reduced to one
while len(prices) > 1:

# Extract the top two elements from the min-heap
x = heappop(prices)
y = heappop(prices)

# calculate the cost of the extracted values
sum = x + y

# insert the cost back to the min-heap
heappush(prices, sum)

# update the minimum cost
cost += sum

return cost


if __name__ == '__main__':

prices = [5, 4, 2, 8]
print("The minimum cost is:", findMinCost(prices))

69 changes: 69 additions & 0 deletions a2_w10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'''
#############################################################################################
#*******************************************************************************************#
# Copyright (c) 2021 pyCoder|semih Corporation;) All rights reserved. ##
# [Timestamp:20210322] ##
#*******************************************************************************************#
#############################################################################################
'''
# Question 2:
# Given a binary array, sort it in linear time and
# constant space by modifying the partitioning logic of the Quicksort algorithm.
# The output should print all zeroes, followed by all ones. For example,
# Input: {1,0,1,0,1,0,0,1}
# Output : {0,0,0,0,1,1,1,1}

"""
Python program for implementation of Quicksort Sort:
===================================================
This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right of pivot.
"""
def partition(arr, low, high):
i = (low-1) # index of smaller element
pivot = arr[high] # pivot

for j in range(low, high):

# If current element is smaller than or
# equal to pivot
if arr[j] <= pivot:

# increment index of smaller element
i = i+1
arr[i], arr[j] = arr[j], arr[i]

arr[i+1], arr[high] = arr[high], arr[i+1]
return (i+1)

# The main function that implements QuickSort
# arr[] --> Array to be sorted,
# low --> Starting index,
# high --> Ending index

# Function to do Quick sort

def quickSort(arr, low, high):
if len(arr) == 1:
return arr
if low < high:

# pi is partitioning index, arr[p] is now
# at right place
pi = partition(arr, low, high)

# Separately sort elements before
# partition and after partition
quickSort(arr, low, pi-1)
quickSort(arr, pi+1, high)

# Driver code to test above
arr = [1,0,1,0,1,0,0,1]
n = len(arr)
quickSort(arr, 0, n-1)
print("Sorted array is:",end=" ")
for i in range(n):
print("%d" % arr[i], end=' ')

68 changes: 68 additions & 0 deletions a3_w10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'''
#############################################################################################
#*******************************************************************************************#
# Copyright (c) 2021 pyCoder|semih Corporation;) All rights reserved. ##
# [Timestamp:20210323] ##
#*******************************************************************************************#
#############################################################################################
'''
# Question 3:
# Construct a following tree.

# 1 <-- 0.level
# / \
# / \
# 2 3 <-- 1.level
# / / \
# / / \
# 4 5 6 <-- 2.level
# / \
# / \
# 7 8 <-- 3.level
# Take this binary tree, calculate the difference between the sum of all nodes present at odd levels and the sum of all nodes present at even level.
# You should get the required difference as output is: (1+4+5+6) - (2+3+7+8) = -4

# A class to store a binary tree node.
class Node:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right


# Function to calculate the difference between the sum of all nodes present
# at odd levels and the sum of all nodes present at even level
def findDiff(root, diff=0, level=1):

# base case
if root is None:
return diff

# if the current level is odd
if level % 2 == 1:
diff = diff + root.data

# if the current level is even
else:
diff = diff - root.data

# recur for the left and right subtree
diff = findDiff(root.left, diff, level + 1)
diff = findDiff(root.right, diff, level + 1)

return diff


if __name__ == '__main__':

root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.right.left = Node(5)
root.right.right = Node(6)
root.right.left.left = Node(7)
root.right.left.right = Node(8)

print(findDiff(root))