Created
August 26, 2015 15:33
-
-
Save abele/227685a3647715a453af to your computer and use it in GitHub Desktop.
idiomatic python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import random | |
def getRandomPerson(ppl): | |
return ppl[random.randint(0,len(ppl)-1)] | |
def good_pair(ignore_ppl, chosen1, chosen2): | |
for pair in ignore_ppl: | |
if chosen1 in pair and chosen2 in pair: | |
return False | |
return True | |
ppl = ['x', 'y', 'z', 'a', 'b', 'c'] | |
ignore_ppl = [['x', 'y'],['x', 'c']] | |
#Ja lasa failu no stdin | |
#ppl = [line.strip() for line in open(str(sys.argv[1]))] | |
#ignore_ppl = [line.split() for line in open(str(sys.argv[2]))] | |
result = [] | |
while len(ppl) > 0: | |
chosen = getRandomPerson(ppl) | |
while result and not good_pair(ignore_ppl, chosen, result[len(result) - 1]): | |
chosen = getRandomPerson(ppl) | |
result.append(chosen) | |
ppl.remove(chosen) | |
for i in range(0, len(result)): | |
if i is len(result) - 1: | |
print(result[i] + " dāvina dāvanu " + result[0]) | |
else: | |
print(result[i] + " dāvina dāvanu " + result[i+1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import random | |
import itertools | |
import argparse | |
import logging | |
import sys | |
def main(seed, log): | |
logging.basicConfig(level=getattr(logging, str(log).upper())) | |
LOGGER = logging.getLogger(__name__) | |
random.seed(seed) | |
people_list = ['x', 'y', 'z', 'a', 'b', 'c'] | |
ilegal_pair_list = [['x', 'y'],['x', 'c']] | |
LOGGER.debug('People=%s', people_list) | |
LOGGER.debug('Ignore pairs=%s', ilegal_pair_list) | |
# Ja lasa failu no stdin | |
# people_list = [line.strip() for line in open(str(sys.argv[1]))] | |
# ilegal_pair_list = [line.split() for line in open(str(sys.argv[2]))] | |
result = [] | |
while people_list: | |
chosen = random.choice(people_list) | |
while result and not good_pair(ilegal_pair_list, chosen, result[-1]): | |
chosen = random.choice(people_list) | |
result.append(chosen) | |
people_list.remove(chosen) | |
for giver, receiver in pairwise(result + [result[0]]): | |
print('{} dāvina dāvanu {}'.format(giver, receiver)) | |
def pairwise(iterable): | |
"s -> (s0,s1), (s1,s2), (s2, s3), ..." | |
a, b = itertools.tee(iterable) | |
next(b, None) | |
return itertools.izip(a, b) | |
def good_pair(ilegal_pair_list, giver, receiver): | |
for pair in ilegal_pair_list: | |
if set([giver, receiver]) == set(pair): | |
return False | |
return True | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Secret Santa Generator') | |
parser.add_argument('--log', type=str, default='info') | |
parser.add_argument('--seed', type=int, default=1) | |
args = parser.parse_args() | |
sys.exit(main(args.seed, args.log)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from secret_santa import main | |
def test_returns_same_result(capsys): | |
main(1, 'info') | |
stdout, _ = capsys.readouterr() | |
assert stdout == u"""x dāvina dāvanu b | |
b dāvina dāvanu z | |
z dāvina dāvanu a | |
a dāvina dāvanu y | |
y dāvina dāvanu c | |
c dāvina dāvanu x | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment