Skip to content

Instantly share code, notes, and snippets.

@hufman
Last active April 27, 2018 20:20
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 hufman/ea01877155baebcc6821e7ab811c237c to your computer and use it in GitHub Desktop.
Save hufman/ea01877155baebcc6821e7ab811c237c to your computer and use it in GitHub Desktop.
Python puzzles
1
2 3
4 5 6
7 8 9 10
http://rextester.com/OUC7480
import itertools
r = itertools.chain(range(1,11)); print('\n'.join([' '.join([str(n) for n in itertools.islice(r, l)]) for l in range(1,5)]))
http://rextester.com/DPXLL55930
r = range(1,11); print('\n'.join([' '.join([str(r.pop(0)) for i in range(0, l)]) for l in range(1,5)]))
Input: 'An apple a day keeps the doctor away.'
Output: 'A an day the away apple keeps doctor.' # sorted words by length
' '.join(sorted(str.lower().strip('.').split(), key=lambda s:len(s))).capitalize()+'.'
test string, valid if each character exists the same count as each other character, or no string has the same count as any other
counts=reduce(lambda acc, i: acc.update({i:acc.get(i,0)+1}) or acc, teststr, {}); len(set(counts.values())) in [1, len(counts)]
test string, valid if each character found exists twice only
all(v==2 for v in reduce(lambda acc, i: acc.update({i:acc.get(i,0)+1}) or acc, teststr, {}).values())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment