Skip to content

Instantly share code, notes, and snippets.

View imedadel's full-sized avatar
🏗️
Building something

Imed Adel imedadel

🏗️
Building something
View GitHub Profile
def pickingNumbers(arr):
maximum = 0
for number in arr:
freq = arr.count(number) + arr.count(number-1)
if freq > maximum:
maximum = freq
return maximum
def getTotalX(a, b):
maxN = max(b)
minN = min(a)
sumN = 0
for number in range(minN, maxN+1):
factorsOfNumber = all([number%x == 0 for x in a])
multsOfNumber = all([x%number == 0 for x in b])
sumN += factorsOfNumber * multsOfNumber
return sumN
toys, budget = [int(x) for x in input().strip().split()]
spent = 0
bought = 0
prices = [int(x) for x in input().strip().split()]
prices.sort()
for price in prices:
if spent + price <= budget:
q = int(input())
while(q>0):
q -= 1
letters = [c for c in input().strip()]
cleaned = []
cleaned.append(letters[0])
for letter in letters[1:]:
if letter != cleaned[-1]:
cleaned.append(letter)
def countSwaps(a):
swaps = 0
n = len(a)
for i in range(n):
for j in range(n-1):
# Swap adjacent elements if they are in decreasing order
if a[j] > a[j + 1]:
a[j], a[j + 1] = a[j+1], a[j]
swaps += 1
print("Array is sorted in", swaps, "swaps.")
#!/bin/python3
import math
import os
import random
import re
import sys
first_multiple_input = input().rstrip().split()
from collections import Counter
def freqQuery(queries):
items = Counter()
checks = Counter()
res = []
for couple in queries:
query, item = couple
if query == 1:
checks[items[item]] -= 1
items[item] += 1
@imedadel
imedadel / grokking-algorithms-binary-search.py
Created October 28, 2019 18:02
Grokking Algorithms: Binary Search in Python
def binary_search(list, item):
low = 0
high = len(list)-1
while low <= high:
mid = (low + high) // 2
guess = list[mid]
if guess == item:
return mid
elif guess > item:
pairs = int(input())
from collections import Counter
def checkPairs(first, second):
firstCount = Counter(first)
secondCount = Counter(second)
commonLettersExist = firstCount - secondCount != firstCount
if commonLettersExist:
return "YES"
# Using Counter
# see https://www.journaldev.com/20806/python-counter-python-collections-counter#python-counter-arithmetic-operations
# (Counter(note) - Counter(magazine)) returns only the elements with a positive count
# That is, if there are more less in `note` than `magazine`, we would get an empty dictionary.
from collections import Counter
def checkMagazine(magazine, note):
if (Counter(note) - Counter(magazine)) == {}:
print("Yes")
else:
print("No")