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
Binary file not shown.
14 changes: 14 additions & 0 deletions algorithms/Implementation/10-BonAppetit/Solutions/python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
firstLine = list(map(int, input().rstrip().split()))
costOfFood = list(map(int, input().rstrip().split()))
charged = int(input())

deal = 0

def JudgeOfCost(firstLine,costOfFood, charged):
deal = ((sum(costOfFood) - costOfFood[firstLine[1]])/2)
if (charged - deal == 0):
print("Bon Appetit")
else:
print(int(charged - deal))

JudgeOfCost(firstLine, costOfFood, charged)
Binary file not shown.
16 changes: 16 additions & 0 deletions algorithms/Implementation/11-SockMerchant/Solutions/python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/python3

import sys
from collections import *
def sockMerchant(n, ar):
numberOfPairs = 0
c = Counter(ar)
for k in c:
numberOfPairs += c[k]//2
return(numberOfPairs)


n = int(input().strip())
ar = list(map(int, input().strip().split(' ')))
result = sockMerchant(n, ar)
print(result)
Binary file not shown.
34 changes: 34 additions & 0 deletions algorithms/Implementation/6-BirthdayChocolate/Solutions/python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/python3

import sys

def solve(n, s, d, m):
if m > n:
return 0

i = j = 0
window_sum = 0

while j < m:
window_sum += s[j]
j += 1

count = 1 if window_sum == d else 0

while j < n:
window_sum -= s[i]
window_sum += s[j]

if window_sum == d:
count += 1
i += 1
j += 1

return count

n = int(input().strip())
s = list(map(int, input().strip().split(' ')))
d, m = input().strip().split(' ')
d, m = [int(d), int(m)]
result = solve(n, s, d, m)
print(result)
Binary file not shown.
27 changes: 27 additions & 0 deletions algorithms/Implementation/7-DivisibleSumPairs/Solutions/python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/python3

import sys

def divisibleSumPairs(n, k, ar):
count = 0
arrPairs = []

for x in range(len(ar)):
j = 1
while j < len(ar):
pair = []
if x < j and ((ar[x] + ar[j]) % k == 0):
pair.append(x)
pair.append(j)
if pair not in arrPairs:
arrPairs.append(pair)
count += 1
j += 1
return count


n, k = input().strip().split(' ')
n, k = [int(n), int(k)]
ar = list(map(int, input().strip().split(' ')))
result = divisibleSumPairs( n, k, ar)
print(result)
Binary file not shown.
13 changes: 13 additions & 0 deletions algorithms/Implementation/8-MigratoryBirds/Solutions/python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/python3

import sys
from collections import *

def migratoryBirds(n, ar):
c = Counter(ar).most_common(1)
return (c[0][0])

n = int(input().strip())
ar = list(map(int, input().strip().split(' ')))
result = migratoryBirds(n, ar)
print(result)
Binary file not shown.
13 changes: 13 additions & 0 deletions algorithms/Sorting/1-IntroToTutorialChallenges/Solutions/python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/python3

import sys

def introTutorial(V, arr):
return(arr.index(V))

if __name__ == "__main__":
V = int(input().strip())
n = int(input().strip())
arr = list(map(int, input().strip().split(' ')))
result = introTutorial(V, arr)
print(result)
Binary file not shown.
30 changes: 30 additions & 0 deletions algorithms/Sorting/2-InsertionSortPart1/Solutions/python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/python3

import sys

def insertionSort1(n, arr):
last = arr[-1]
last = arr[-1]
x = 0
while arr[n-(n+2) - x] > last:
arr[-1-x] = arr[n-(n+2) - x]
print (' '.join(str(y) for y in arr))
if x+2 == len(arr):
arr[n-(n+2) - x] = last
break
else:
x += 1

if arr[n-(n+2) - x] < last:
arr[-x-1] = last
print (' '.join(str(y) for y in arr))
else:
print (' '.join(str(y) for y in arr))




if __name__ == "__main__":
n = int(input().strip())
arr = list(map(int, input().strip().split(' ')))
insertionSort1(n, arr)
Binary file not shown.
21 changes: 21 additions & 0 deletions algorithms/Sorting/3-InsertionSortPart2/Solutions/python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/python3

import sys

def insertionSort2(n, l):
for i in range(1, len(l)):
j = i-1
key = l[i]
while (j >= 0) and (l[j] > key):
l[j+1],l[j] = l[j],l[j+1]
j -= 1
l[j+1] = key
print(" ".join(map(str,l)))




if __name__ == "__main__":
n = int(input().strip())
arr = list(map(int, input().strip().split(' ')))
insertionSort2(n, arr)
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def insertion_sort(l):
for i in range(1, len(l)):
j = i-1
key = l[i]
while (j >= 0) and (l[j] > key):
l[j+1] = l[j]
j -= 1
l[j+1] = key


m = int(input().strip())
ar = [int(i) for i in input().strip().split()]
insertion_sort(ar)
print(" ".join(map(str,ar)))
Binary file not shown.
31 changes: 31 additions & 0 deletions algorithms/Sorting/5-QuickSort1-Partition/Solutions/python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/python3

import sys

def quickSort(arr):
left = []
equal = []
right = []
pivot = arr[0]
for x in arr:
if x == pivot:
equal.append(x)
elif x > pivot:
right.append(x)
else:
left.append(x)

arr[:] =[]
arr += left
arr += equal
arr += right
return arr


if __name__ == "__main__":
n = int(input().strip())
arr = list(map(int, input().strip().split(' ')))
result = quickSort(arr)
print (" ".join(map(str, result)))