store recent function calls into a dictionary
check if the value is in our dictionary
- return that value if it is
 
if that fails you will need to compute the value
| """ | |
| Compress String | |
| input: | |
| i j | |
| 'a 1 2 c 9 b 5 6 c 1' | |
| output: | |
| 'a12b56c10' | |
| def island_count(grid): | |
| if not grid: | |
| return 0 | |
| nr = len(grid) | |
| nc = len(grid[0]) | |
| num_islands = 0 | |
| for r in range(nr): | 
| class Node(object): | |
| def __init__(self, val, neighbors): | |
| self.val = val | |
| self.neighbors = neighbors | |
| def clone_Graph(node): | |
| if not node: | |
| return | 
| import os | |
| from collections import Counter | |
| from operator import itemgetter | |
| def most_freq(input_string, k): | |
| words = get_clean_words_list(input_string) | |
| words = Counter(words) | |
| result = [] | |
| for word, freq in words.items(): | 
| def find_middle(ll): | |
| even = ll.size % 2 == 0 | |
| node = ll.head | |
| if even: | |
| for i in range(ll.size // 2 + 1): | |
| if i == ll.size / 2 - 1: | |
| return node.data, node.next.data | |
| node = node.next | |
| else: | |
| for i in range(ll.size // 2 + 2): | 
| import unittest | |
| from fibonacci_resursive import fibonacci | |
| class FiboTest(unittest.TestCase): | |
| def test_fibonacci(self): | |
| assert fibonacci(9) == 34 | |
| assert fibonacci(1) == 1 | |
| assert fibonacci(2) == 1 | |
| assert fibonacci(5) == 3 | |
| import time | |
| # store recent function calls into a dictionary | |
| cache_values = {} | |
| def fibonacci(n): | |
| # check if the given number is less than 0 | |
| if n < 0: | |
| # output an error message stating that the number given is incorrect | |
| raise ValueError("Has to be a positive integer!") | |
| # check if the value is in our dictionary | 
| import time | |
| def fibonacci(n): | |
| # check if the given number is less than 0 | |
| if n < 0: | |
| # output an error message stating that the number given is incorrect | |
| raise ValueError("Has to be a positive integer!") | |
| # check if the given number is equal to 1 or 2 return 1 if it is | |
| elif n == 0: | |
| return 0 |