def triangular(n): return n * (n + 1) / 2 def square(n): return n * n def pentagonal(n): return n * (3 * n - 1) / 2 def hexagonal(n): return n * (2 * n - 1) def heptagonal(n): return n * (5 * n - 3) / 2 def octagonal(n): return n * (3 * n - 2) from IPython.html.widgets import interact funcs = [triangular, square, pentagonal, hexagonal, heptagonal, octagonal] labels = list(map(lambda f: f.__name__, funcs)) labels def eval_func(func_name, n): func = funcs[labels.index(func_name)] print(func(n)) interact(eval_func, func_name=labels, n=(1, 10)) %matplotlib inline from pylab import linspace, exp, pi, plot, append, axis, xlim, ylim, figure def compute_unity_roots(n, r=1.0): thetas = linspace(0, 2 * pi, n, endpoint=False) - (pi / 2 - pi / n) roots = r * exp(1j * thetas) return roots compute_unity_roots(3) z = compute_unity_roots(3) z = append(z, z[0]) plot(z.real, z.imag, '-o') axis('equal') def plot_unity_roots(n): z = compute_unity_roots(n) z = append(z, z[0]) plot(z.real, z.imag, '-o') xlim(-1.1, 1.1) ylim(-1.1, 1.1) interact(plot_unity_roots, n=(1, 20)) def plot_unity_roots_until(n, order): for o in range(1, order + 1): z = compute_unity_roots(n, r=o) if o == 1: reference_z = z[-1] else: z -= (z[-1] - reference_z) z = append(z, z[0]) plot(z.real, z.imag, '-o') axis('equal') axis('off') plot_unity_roots_until(4, 3) interact(plot_unity_roots_until, n=(1, 20), order=(1, 20)) pentagonal(3) from collections import defaultdict four_digits = defaultdict(list) for func, label in zip(funcs, labels): too_big = False n = 1 value = func(n) while not too_big: n += 1 value = func(n) if value > 999 and value < 10000: four_digits[label].append(str(value)) if value > 10000: too_big = True four_digits [(label, len(four_digits[label])) for label in labels] def filter_can_extend_left(four_digit, candidates): left_most = four_digit[:2] return [candidate for candidate in candidates if candidate[2:4] == left_most] for label in labels: print filter_can_extend_left('4995', four_digits[label]) def filter_can_extend_right(four_digit, candidates): right_most = four_digit[2:4] return [candidate for candidate in candidates if candidate[0:2] == right_most] for label in labels: print filter_can_extend_right('4995', four_digits[label]) import networkx as nx colors = ['blue', 'green', 'red', 'yellow', 'brown', 'orange'] g = nx.Graph() for label, color in zip(labels, colors): for node_value in four_digits[label]: g.add_node(node_value, color=color) all_values = reduce(list.__add__, [four_digits[label] for label in labels]) for label in labels: for node_value in four_digits[label]: to_right = filter_can_extend_right(node_value, all_values) for other_node in to_right: g.add_edge(node_value, other_node) figure(figsize=(10, 10)) nx.draw_networkx(g) def recursive_chain(current_node, available_labels): """ returns a list of lists """ if len(available_labels) == 0: return [[current_node]] else: chains = [] for label in available_labels: next_nodes = filter_can_extend_right(current_node, four_digits[label]) for next_node in next_nodes: chains += recursive_chain(next_node, [other_label for other_label in available_labels if other_label != label]) output = [] for chain in chains: output.append([current_node] + chain) return output recursive_chain('1849', labels) labels for four_digit in four_digits['triangular']: chains = recursive_chain(four_digit, ['square', 'pentagonal']) for chain in chains: if chain[-1][2:4] == chain[0][0:2]: print chain for four_digit in four_digits['triangular']: chains = recursive_chain(four_digit, ['square', 'pentagonal', 'hexagonal', 'heptagonal', 'octagonal']) for chain in chains: if chain[-1][2:4] == chain[0][0:2]: print chain sum(map(int, ['8256', '5625', '2512', '1281', '8128', '2882'])) from collections import Counter def CountOccurencesInText(word,text): """Number of occurences of word (case insensitive) in text""" c = Counter(text.upper().replace(',', ' ')\ .replace('.', ' ')\ .replace('!', ' ')\ .replace('?', ' ')\ .replace('\n', ' ')\ .split(sep=' ')) return c[word.upper()] CountOccurencesInText("python",text) text="""Georges is my name and I like python. Oh ! your name is georges? And you like Python! Yes is is true, I like PYTHON and my name is GEORGES""" # test with a little text. assert( 3 == CountOccurencesInText("Georges",text) ) assert( 3 == CountOccurencesInText("GEORGES",text) ) assert( 3 == CountOccurencesInText("georges",text) ) assert( 0 == CountOccurencesInText("george",text) ) assert( 3 == CountOccurencesInText("python",text) ) assert( 3 == CountOccurencesInText("PYTHON",text) ) assert( 2 == CountOccurencesInText("I",text) ) assert( 0 == CountOccurencesInText("n",text) ) assert( 1 == CountOccurencesInText("true",text) ) # regard ' as text: assert ( 0 == CountOccurencesInText ( "maley", "John O'maley is my friend" ) ) # Test it but with a BIG length file. (we once had a memory error with this...) text = """The quick brown fox jump over the lazy dog.The quick brown fox jump over the lazy dog.""" * 500 text += """The quick brown fox jump over the lazy dog.The quick brown Georges jump over the lazy dog.""" text += """esrf sqfdg sfdglkj sdflgh sdflgjdsqrgl """ * 4000 text += """The quick brown fox jump over the lazy dog.The quick brown fox jump over the lazy python.""" text += """The quick brown fox jump over the lazy dog.The quick brown fox jump over the lazy dog.""" * 500 text += """The quick brown fox jump over the lazy dog.The quick brown Georges jump over the lazy dog.""" text += """esrf sqfdg sfdglkj sdflgh sdflgjdsqrgl """ * 4000 text += """The quick brown fox jump over the lazy dog.The quick brown fox jump over the lazy python.""" text += """The quick brown fox jump over the lazy dog.The quick brown fox jump over the lazy dog.""" * 500 text += """The quick brown fox jump over the lazy dog.The quick brown Georges jump over the lazy dog.""" text += """esrf sqfdg sfdglkj sdflgh sdflgjdsqrgl """ * 4000 text += """The quick brown fox jump over the lazy dog.The quick brown fox jump over the lazy python.""" text += """The quick brown fox jump over the true lazy dog.The quick brown fox jump over the lazy dog.""" text += """The quick brown fox jump over the lazy dog.The quick brown fox jump over the lazy dog.""" * 500 text += """ I vsfgsdfg sfdg sdfg sdgh sgh I sfdgsdf""" text += """The quick brown fox jump over the lazy dog.The quick brown fox jump over the lazy dog.""" * 500 assert( 3 == CountOccurencesInText("Georges",text) ) assert( 3 == CountOccurencesInText("GEORGES",text) ) assert( 3 == CountOccurencesInText("georges",text) ) assert( 0 == CountOccurencesInText("george",text) ) assert( 3 == CountOccurencesInText("python",text) ) assert( 3 == CountOccurencesInText("PYTHON",text) ) assert( 2 == CountOccurencesInText("I",text) ) assert( 0 == CountOccurencesInText("n",text) ) assert( 1 == CountOccurencesInText("true",text) ) assert( 0 == CountOccurencesInText("reflexion mirror", "I am a senior citizen and I live in the Fun-Plex 'Reflexion Mirror' in Sopchoppy, Florida") ) assert( 1 == CountOccurencesInText("'reflexion mirror'", "I am a senior citizen and I live in the Fun-Plex 'Reflexion Mirror' in Sopchoppy, Florida") ) assert( 1 == CountOccurencesInText("reflexion mirror", "I am a senior citizen and I live in the Fun-Plex (Reflexion Mirror) in Sopchoppy, Florida") ) assert( 1 == CountOccurencesInText("reflexion mirror", "Reflexion Mirror\" in Sopchoppy, Florida") ) assert( 1 == CountOccurencesInText("reflexion mirror", u"I am a senior citizen and I live in the Fun-Plex «Reflexion Mirror» in Sopchoppy, Florida") ) assert( 1 == CountOccurencesInText("reflexion mirror", u"I am a senior citizen and I live in the Fun-Plex \u201cReflexion Mirror\u201d in Sopchoppy, Florida") ) assert( 1 == CountOccurencesInText("legitimate", u"who is approved by OILS is completely legitimate: their employees are of legal working age") ) assert( 0 == CountOccurencesInText("legitimate their", u"who is approved by OILS is completely legitimate: their employees are of legal working age") ) assert( 1 == CountOccurencesInText("get back to me", u"I hope you will consider this proposal, and get back to me as soon as possible") ) assert( 1 == CountOccurencesInText("skin-care", u"enable Delavigne and its subsidiaries to create a skin-care monopoly") ) assert( 1 == CountOccurencesInText("skin-care monopoly", u"enable Delavigne and its subsidiaries to create a skin-care monopoly") ) assert( 0 == CountOccurencesInText("skin-care monopoly in the US", u"enable Delavigne and its subsidiaries to create a skin-care monopoly") ) assert( 1 == CountOccurencesInText("get back to me", u"When you know:get back to me") ) assert( 1 == CountOccurencesInText("don't be left" , """emergency alarm warning. Don't be left unprotected. Order your SSSS3000 today!""" ) ) assert( 1 == CountOccurencesInText("don" , """emergency alarm warning. Don't be left unprotected. Order your don SSSS3000 today!""" ) ) assert( 1 == CountOccurencesInText("take that as a 'yes'", "Do I have to take that as a 'yes'?") ) assert( 1 == CountOccurencesInText("don't take that as a 'yes'", "I don't take that as a 'yes'?") ) assert( 1 == CountOccurencesInText("take that as a 'yes'", "I don't take that as a 'yes'?") ) assert( 1 == CountOccurencesInText("don't", "I don't take that as a 'yes'?") ) assert( 1 == CountOccurencesInText("attaching my c.v. to this e-mail", "I am attaching my c.v. to this e-mail." )) assert ( 1 == CountOccurencesInText ( "Linguist", "'''Linguist Specialist Found Dead on Laboratory Floor'''" )) assert ( 1 == CountOccurencesInText ( "Linguist Specialist", "'''Linguist Specialist Found Dead on Laboratory Floor'''" )) assert ( 1 == CountOccurencesInText ( "Laboratory Floor", "'''Linguist Specialist Found Dead on Laboratory Floor'''" )) assert ( 1 == CountOccurencesInText ( "Floor", "'''Linguist Specialist Found Dead on Laboratory Floor'''" )) assert ( 1 == CountOccurencesInText ( "Floor", "''Linguist Specialist Found Dead on Laboratory Floor''" )) assert ( 1 == CountOccurencesInText ( "Floor", "__Linguist Specialist Found Dead on Laboratory Floor__" )) assert ( 1 == CountOccurencesInText ( "Floor", "'''''Linguist Specialist Found Dead on Laboratory Floor'''''" )) assert ( 1 == CountOccurencesInText ( "Linguist", "'''Linguist Specialist Found Dead on Laboratory Floor'''" )) assert ( 1 == CountOccurencesInText ( "Linguist", "''Linguist Specialist Found Dead on Laboratory Floor''" )) assert ( 1 == CountOccurencesInText ( "Linguist", "__Linguist Specialist Found Dead on Laboratory Floor__" )) assert ( 1 == CountOccurencesInText ( "Linguist", "'''''Linguist Specialist Found Dead on Laboratory Floor'''''" )) assert ( 1 == CountOccurencesInText ( "Floor", """Look: ''Linguist Specialist Found Dead on Laboratory Floor'' is the headline today.""")) CountOccurencesInText("'reflexion mirror'", "I am a senior citizen and I live in the Fun-Plex 'Reflexion Mirror' in Sopchoppy, Florida") "I am a senior citizen and I live in the Fun-Plex 'Reflexion Mirror' in Sopchoppy, Florida".upper().replace(',', ' ')\ .replace('.', ' ')\ .replace('!', ' ')\ .replace('?', ' ')\ .replace('\n', ' ')\ .split(sep=' ')