Skip to content

Instantly share code, notes, and snippets.

@Aluriak
Last active May 7, 2019 13:35
Show Gist options
  • Save Aluriak/ac055c0a0612edbe5cd228dfa86bcde5 to your computer and use it in GitHub Desktop.
Save Aluriak/ac055c0a0612edbe5cd228dfa86bcde5 to your computer and use it in GitHub Desktop.
"""What is the adress i should use ?"""
import itertools, random
ANSWER = 'Univ Rennes, Inria, CNRS, IRISA F-35000 Rennes'
NEEDED = {'Inria', 'Univ', 'CNRS'}
MAX_PROPOSITIONS = 6
PARTICLES = (
('Rennes 1', 'Rennes', 'Univ Rennes', 'Univ Rennes 1'),
('Inria', 'IRISA', ''),
('CNRS', 'Inria', ''),
('CNRS F-35000 Rennes', 'Inria F-35000 Rennes', 'F-35000 Rennes 1', 'IRISA F-35000 Rennes', 'IRISA F-35000 Rennes 1'),
)
def single_in(word, text) -> bool:
if word in text:
return word not in text[text.find(word)+1:]
return False
assert single_in('a', 'ab')
assert single_in('hello', 'hellollo')
assert not single_in('hello', 'hellohello')
def is_ok(addr) -> bool:
return all(single_in(word, addr) for word in NEEDED)
addr_from_particles = lambda x: ', '.join(filter(bool, x))
all_addrs = list(filter(is_ok, map(addr_from_particles, itertools.product(*PARTICLES))))
assert ANSWER in all_addrs
if MAX_PROPOSITIONS:
all_addrs = list(random.sample(all_addrs, MAX_PROPOSITIONS))
assert len(all_addrs) == MAX_PROPOSITIONS
if ANSWER not in all_addrs:
all_addrs[0] = ANSWER # NB: random.sample returns objects in random order
random.shuffle(all_addrs)
for addr in all_addrs:
print(addr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment