Last active
August 29, 2015 14:17
-
-
Save TheBB/08e35427db10814b6480 to your computer and use it in GitHub Desktop.
This file contains 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
import re | |
from math import * | |
from operator import * | |
from itertools import * | |
def max(a, b): | |
return a if a > b else b | |
def max_of_three(a, b, c): | |
return max(a, max(b, c)) | |
def len(it): | |
return sum(1 for _ in it) | |
def vowel(c): | |
return c.lower() in 'aeiou' | |
def consonant(c): | |
return c.lower() in 'bcdfghjklmnpqrstvwxz' | |
def semivowel(c): | |
return c.lower() == 'y' | |
def letter(c): | |
return any(f(c) for f in [vowel, consonant, semivowel]) | |
def translate_1(s): | |
ret = '' | |
for c in s: | |
ret += c | |
if consonant(c): | |
ret += 'o' + c | |
return ret | |
def translate_2(s): | |
return ''.join(c+'o'+c if consonant(c) else c for c in s) | |
def sum(it): | |
ret = 0 | |
for i in it: | |
ret += i | |
return ret | |
def multiply_1(it): | |
ret = 1 | |
for i in it: | |
ret *= i | |
return ret | |
# For mathematicians | |
def multiply_2(it): | |
return exp(sum(map(log, it))) | |
def reverse(s): | |
return s[::-1] | |
def is_palindrome(s): | |
return s == reverse(s) | |
def is_member(val, it): | |
return any(val == i for i in it) | |
def overlapping_1(ita, itb): | |
return any(is_member(a, itb) for a in ita) | |
def overlapping_2(ita, itb): | |
return set(ita) & set(itb) | |
def generate_n_chars(n, c): | |
return ''.join(c for _ in xrange(n)) | |
def histogram(it): | |
for i in it: | |
print generate_n_chars(i, '*') | |
def max_in_list(it): | |
ret = it[0] | |
for i in it[1:]: | |
ret = ret if ret > i else i | |
return ret | |
def find_longest_word(words): | |
return max_in_list(list(map(len, words))) | |
def filter_long_words(words, n): | |
return [w for w in words if len(w) > n] | |
def is_palindrome_better(s): | |
return is_palindrome(''.join(c for c in s if letter(c)).lower()) | |
def is_pangram(s): | |
return set(s) == set('abcdefghijklmnopqrstuvwxyz') | |
def bottles_of_beer(n): | |
for i in xrange(n, -1, -1): | |
num_bottles = str(i) if i > 1 else 'No more' | |
bottles = 'bottle' if i == 1 else 'bottles' | |
if i < n: | |
print('Take one down, pass it around, {nl} {b} of beer on the wall.' | |
.format(nl=num_bottles.lower(), b=bottles)) | |
print('{nu} {b} of beer on the wall, {nl} {b} of beer.' | |
.format(nu=num_bottles, nl=num_bottles.lower(), b=bottles)) | |
print('Go to the store, buy some more, {n} {b} of beer on the wall' | |
.format(n=n, b='bottle' if n == 1 else 'bottles')) | |
def english_to_swedish(words): | |
trans = {'merry': 'god', 'christmas': 'jul', 'and': 'och', | |
'happy': 'gott', 'new': 'nytt', 'year': 'år'} | |
ret = [] | |
for w in words: | |
capitalized = w[0].upper() == w[0]: | |
translated = trans[w.lower()] | |
if capitalized: | |
translated = translated[0].upper() + translated[1:] | |
ret.append(translated) | |
def char_freq(s, case_sensitive=True): | |
if not case_sensitive: | |
s = s.lower() | |
ret = {} | |
for c in s: | |
ret[c] = ret.get(c, 0) + 1 | |
def rot(s, n=13): | |
ranges = [(65, 90), (97, 122)] | |
ret = '' | |
for c in s: | |
try: | |
lower, upper = next((l, u) for l, u in ranges if l <= ord(c) <= u) | |
i = ord(c) + n | |
while i < lower: i += upper - lower | |
while i > upper: i -= upper - lower | |
ret += chr(i) | |
except StopIteration: | |
ret += c | |
return ret | |
def correct(s): | |
s = re.sub(r'\s+', ' ', s) | |
s = re.sub(r'\.([^\s])', r'. \1', s) | |
return s | |
def make_3sg_form(s): | |
if s.endswith('y'): | |
return s[:-1] + 'ies' | |
if s[-1] in 'osxz': | |
return s[:-1] + 'es' | |
# Condition is false if len(s) < 2 | |
if s[-2:] in ['ch', 'sh']: | |
return s[:-2] + 'es' | |
return s + 's' | |
def make_ing_form(s): | |
if s.endswith('e'): | |
return s[:-1] + 'ing' | |
if s.endswith('ie'): | |
return s[:-2] + 'ying' | |
if len(s) == 3 and consonant(s[0]) and vowel(s[1]) and consonant(s[2]): | |
return s + s[-1] + 'ing' | |
return s + 'ing' | |
def max_in_list(it): | |
return reduce(max, it) | |
def word_lengths_1(words): | |
ret = [] | |
for w in words: | |
ret.append(len(w)) | |
return w | |
def word_lengths_2(words): | |
return map(len, words) | |
def word_lengths_3(words): | |
return [len(w) for w in words] | |
def find_longest_word_2(words): | |
return reduce(lambda a, b: a if len(a) > len(b) else b, words) | |
def filter_long_words_2(words, n): | |
return filter(lambda w: len(w) > n, words) | |
def english_to_swedish_2(words): | |
trans = {'merry': 'god', 'christmas': 'jul', 'and': 'och', | |
'happy': 'gott', 'new': 'nytt', 'year': 'år'} | |
capitalized = (w[0].upper() == w[0] for w in words) | |
translated = map(lambda w: trans[w.lower()], words) | |
return [w[0].upper() + w[1:] if cap else w | |
for w, cap in zip(translated, capitalized)] | |
# Returns generator and not list | |
def map(f, it): | |
return (f(i) for i in it) | |
# Returns generator and not list | |
def filter(f, it): | |
if f is None: | |
f = lambda x: x | |
return (i for i in it if f(i)) | |
def reduce(f, it): | |
it = iter(it) | |
ret = next(it) | |
try: | |
while True: | |
ret = f(ret, next(it)) | |
except StopIteration: | |
pass | |
return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment