Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@EkremDincel
Last active March 1, 2020 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EkremDincel/a53f0f09efec9507bbf15a2310d02fec to your computer and use it in GitHub Desktop.
Save EkremDincel/a53f0f09efec9507bbf15a2310d02fec to your computer and use it in GitHub Desktop.
İlginç soruları çözmek için kullanılabilecek fonksiyonlar.
import math
##############
def ebob(x,y):
while y:
r, x = x, y
y=r%y
return x
def ekok(x,y):
return x*y/ebob(x,y)
def eboblar(args):
f, s, loop = args[0], args[1], args[2:]
l = ebob(f, s)
for i in loop:
l = ebob(l, i)
return l
def ekoklar(args):
f, s, loop = args[0], args[1], args[2:]
l = ekok(f, s)
for i in loop:
l = ekok(l, i)
return l
#################
def map(x,a,b,c,d):
y=(x-a)/(b-a)*(d-c)+c
return y
F = math.factorial
def C(x, y):
"""Kombinasyon"""
return F(x) / (F(x-y) * F(y))
def P(x, y):
"""Permütasyon"""
return F(x) / F(x-y)
from itertools import combinations, permutations, product, chain
#################
def ayır(iterable, ayıraç_sayısı):
"""ayır("abcd", 2) -> [['a', 'b', 'cd'], ['a', 'bc', 'd'], ['ab', 'c', 'd']]"""
ayıraçlar = combinations(range(1, len(iterable)), ayıraç_sayısı)
for ayıraç in ayıraçlar:
liste = []
start = 0
for i in ayıraç:
if not (e := iterable[start:i]): continue
liste.append(e)
start = i
if e := iterable[start:]: liste.append(e)
yield liste
def hepsini_ayır(iterable):
for i in range(len(iterable) - 1):
yield from ayır(iterable, i)
###############
def altkümeler(iterable):
"altkümeler([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
for r in range(len(iterable)+1):
yield from combinations(iterable, r)
############### @aib
import functools
import itertools
import operator
def groupings(sum_):
if sum_ == 0:
return [()]
else:
return [(i,) + subg for i in range(1, sum_ + 1) for subg in groupings(sum_ - i)]
groupings_with_length = lambda s, l: list(filter(lambda g: len(g) == l, groupings(s)))
def assign_elements(grouping, elements, fold_op=operator.add):
element_gen = iter(elements)
return tuple(functools.reduce(fold_op, itertools.islice(element_gen, g)) for g in grouping)
assign_digits = functools.partial(assign_elements, fold_op=lambda x, y: x*10 + y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment