From b5a1fa18ba681e4af5b4906edb1acb955534d44e Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Sat, 27 Jan 2018 11:03:02 +0530 Subject: [PATCH] factorial --- README.md | 60 +++++++++--------------------------- scripts/lint.py | 2 +- snippets/average.md | 4 +-- snippets/chunk.md | 4 +-- snippets/compact.md | 4 +-- snippets/count_occurences.md | 4 +-- snippets/count_vowels.md | 4 +-- snippets/deep_flatten.md | 4 +-- snippets/difference.md | 4 +-- snippets/factorial.md | 4 +-- snippets/gcd.md | 4 +-- snippets/lcm.md | 4 +-- snippets/max_n.md | 4 +-- snippets/min_n.md | 4 +-- snippets/shuffle.md | 4 +-- snippets/spread.md | 4 +-- snippets/zip.md | 4 +-- 17 files changed, 31 insertions(+), 91 deletions(-) diff --git a/README.md b/README.md index f2c4a8ccf..8a3002fdb 100644 --- a/README.md +++ b/README.md @@ -48,12 +48,10 @@ Returns the average of two or more numbers. Takes the sum of all the `args` and divides it by `len(args)`. The secind argument `0.0` in sum is to handle floating point division in `python2`. -```py - +```py def average(*args): return sum(args, 0.0) / len(args) - ``` @@ -75,15 +73,13 @@ Calculates the factorial of a number. Use recursion. If `num` is less than or equal to `1`, return `1`. Otherwise, return the product of `num` and the factorial of `num - 1`. Throws an exception if `num` is a negative or a floating point number. -```py - +```py def factorial(num): if not ((num >= 0) & (num % 1 == 0)): raise Exception( f"Number( {num} ) can't be floating point or negative ") return 1 if num == 0 else num * factorial(num - 1) - ```
View Examples @@ -107,8 +103,7 @@ The `helperGcdfunction` uses recursion. Base case is when `y` equals `0`. In thi Uses the reduce function from the inbuilt module `functools`. Also defines a method `spread` for javascript like spreading of lists. -```py - +```py from functools import reduce @@ -131,7 +126,6 @@ def gcd(*args): return x if not y else gcd(y, x % y) return reduce((lambda x, y: _gcd(x, y)), numbers) - ``` @@ -155,8 +149,7 @@ Use the `greatest common divisor (GCD)` formula and the fact that `lcm(x,y) = x Uses `reduce` function from the inbuilt module `functools`. Also defines a method `spread` for javascript like spreading of lists. -```py - +```py from functools import reduce @@ -182,7 +175,6 @@ def lcm(*args): return x * y / _gcd(x, y) return reduce((lambda x, y: _lcm(x, y)), numbers) - ``` @@ -205,8 +197,7 @@ Returns the `n` maximum elements from the provided list. If `n` is greater than Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy` module to create a shallow clone of the list and sort it in ascending order and then use `list.reverse()` reverse it to make it descending order. Use `[:n]` to get the specified number of elements. Omit the second argument, `n`, to get a one-element array -```py - +```py from copy import deepcopy @@ -216,7 +207,6 @@ def max_n(arr, n=1): numbers.sort() numbers.reverse() return numbers[:n] - ``` @@ -238,8 +228,7 @@ Returns the `n` minimum elements from the provided list. If `n` is greater than Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy` module to create a shallow clone of the list and sort it in ascending order. Use `[:n]` to get the specified number of elements. Omit the second argument, `n`, to get a one-element array -```py - +```py from copy import deepcopy @@ -248,7 +237,6 @@ def min_n(arr, n=1): numbers = deepcopy(arr) numbers.sort() return numbers[:n] - ``` @@ -272,8 +260,7 @@ Chunks an array into smaller lists of a specified size. Uses `range` to create a list of desired size. Then use `map` on this list and fill it with splices of `arr`. -```py - +```py from math import ceil @@ -282,7 +269,6 @@ def chunk(arr, size): return list( map(lambda x: arr[x * size:x * size + size], list(range(0, ceil(len(arr) / size))))) - ``` @@ -303,12 +289,10 @@ Removes falsey values from a list. Use `filter()` to filter out falsey values (False, None, 0, and ""). -```py - +```py def compact(arr): return list(filter(lambda x: bool(x), arr)) - ``` @@ -331,14 +315,12 @@ Counts the occurrences of a value in an list. Uses the `reduce` functin from built-in module `functools` to increment a counter each time you encounter the specific value inside the list. -```py - +```py def count_occurences(arr, val): return reduce( (lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0), arr) - ``` @@ -359,8 +341,7 @@ Deep flattens a list. Use recursion. Use `list.extend()` with an empty array (`result`) and the spread function to flatten a list. Recursively flatten each element that is a list. -```py - +```py def spread(arg): ret = [] @@ -377,7 +358,6 @@ def deep_flatten(arr): result.extend( spread(list(map(lambda x: deep(x) if type(x) == list else x, arr)))) return result - ``` @@ -398,13 +378,11 @@ Returns the difference between two arrays. Create a `set` from `b`, then use list comprehension to only keep values not contained in `b` -```py - +```py def difference(a, b): b = set(b) return [item for item in a if item not in b] - ```
View Examples @@ -426,8 +404,7 @@ Randomizes the order of the values of an list, returning a new list. Uses the [Fisher-Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) to reorder the elements of the list. -```py - +```py from copy import deepcopy from random import randint @@ -441,7 +418,6 @@ def shuffle(arr): i = randint(0, m) temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m] return temp_arr - ``` @@ -461,8 +437,7 @@ shuffle(foo) # [2,3,1] , foo = [1,2,3] Implements javascript's spread syntax as a function. Flattens the list(non-deep) and returns an list. -```py - +```py def spread(arg): ret = [] @@ -472,7 +447,6 @@ def spread(arg): else: ret.append(i) return ret - ``` @@ -496,8 +470,7 @@ Creates a list of elements, grouped based on the position in the original lists. Use `max` combined with `list comprehension` to get the length of the longest list in the arguments. Loops for `max_length` times grouping elements. If lengths of `lists` vary `fill_value` is used. By default `fill_value` is `None`. -```py - +```py def zip(*args, fillvalue=None): max_length = max([len(arr) for arr in args]) @@ -507,7 +480,6 @@ def zip(*args, fillvalue=None): args[k][i] if i < len(args[k]) else None for k in range(len(args)) ]) return result - ``` @@ -532,15 +504,13 @@ Retuns `number` of vowels in provided `string`. Use a regular expression to count the number of vowels `(A, E, I, O, U)` in a string. -```py - +```py import re def count_vowels(str): return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE))) - ``` diff --git a/scripts/lint.py b/scripts/lint.py index f01eadc82..2324adfe3 100644 --- a/scripts/lint.py +++ b/scripts/lint.py @@ -11,7 +11,7 @@ for file in files: originalCode = re.search(codeRe,fileData).group(0) #print(re.split(codeRe,fileData)[0]) formatedCode = autopep8.fix_code(re.split(codeRe,fileData)[1]) - fileToSave = fileData.replace(originalCode,('```python \n'+formatedCode+'\n```')) + fileToSave = fileData.replace(originalCode,('```python'+formatedCode+'```')) someFile = open("snippets/"+file,'w') someFile.write(fileToSave) someFile.close() \ No newline at end of file diff --git a/snippets/average.md b/snippets/average.md index d818556a0..33a2480c3 100644 --- a/snippets/average.md +++ b/snippets/average.md @@ -6,12 +6,10 @@ Returns the average of two or more numbers. Takes the sum of all the `args` and divides it by `len(args)`. The secind argument `0.0` in sum is to handle floating point division in `python2`. -```python - +```python def average(*args): return sum(args, 0.0) / len(args) - ``` ``` python diff --git a/snippets/chunk.md b/snippets/chunk.md index f6f180acb..5b8d500f9 100644 --- a/snippets/chunk.md +++ b/snippets/chunk.md @@ -4,8 +4,7 @@ Chunks an array into smaller lists of a specified size. Uses `range` to create a list of desired size. Then use `map` on this list and fill it with splices of `arr`. -```python - +```python from math import ceil @@ -14,7 +13,6 @@ def chunk(arr, size): return list( map(lambda x: arr[x * size:x * size + size], list(range(0, ceil(len(arr) / size))))) - ``` ``` python diff --git a/snippets/compact.md b/snippets/compact.md index 12b5a7ba8..188e9a3fb 100644 --- a/snippets/compact.md +++ b/snippets/compact.md @@ -4,12 +4,10 @@ Removes falsey values from a list. Use `filter()` to filter out falsey values (False, None, 0, and ""). -```python - +```python def compact(arr): return list(filter(lambda x: bool(x), arr)) - ``` ``` python diff --git a/snippets/count_occurences.md b/snippets/count_occurences.md index c08bfad80..f580d5fb8 100644 --- a/snippets/count_occurences.md +++ b/snippets/count_occurences.md @@ -6,14 +6,12 @@ Counts the occurrences of a value in an list. Uses the `reduce` functin from built-in module `functools` to increment a counter each time you encounter the specific value inside the list. -```python - +```python def count_occurences(arr, val): return reduce( (lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0), arr) - ``` ```python diff --git a/snippets/count_vowels.md b/snippets/count_vowels.md index 87b0fbb47..69a35f4d0 100644 --- a/snippets/count_vowels.md +++ b/snippets/count_vowels.md @@ -4,15 +4,13 @@ Retuns `number` of vowels in provided `string`. Use a regular expression to count the number of vowels `(A, E, I, O, U)` in a string. -```python - +```python import re def count_vowels(str): return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE))) - ``` ``` python diff --git a/snippets/deep_flatten.md b/snippets/deep_flatten.md index 0e52e1c15..877f0ef78 100644 --- a/snippets/deep_flatten.md +++ b/snippets/deep_flatten.md @@ -4,8 +4,7 @@ Deep flattens a list. Use recursion. Use `list.extend()` with an empty array (`result`) and the spread function to flatten a list. Recursively flatten each element that is a list. -```python - +```python def spread(arg): ret = [] @@ -22,7 +21,6 @@ def deep_flatten(arr): result.extend( spread(list(map(lambda x: deep(x) if type(x) == list else x, arr)))) return result - ``` ```python diff --git a/snippets/difference.md b/snippets/difference.md index 1dd327304..189b74902 100644 --- a/snippets/difference.md +++ b/snippets/difference.md @@ -4,13 +4,11 @@ Returns the difference between two arrays. Create a `set` from `b`, then use list comprehension to only keep values not contained in `b` -```python - +```python def difference(a, b): b = set(b) return [item for item in a if item not in b] - ``` ``` python difference([1, 2, 3], [1, 2, 4]) # [3] diff --git a/snippets/factorial.md b/snippets/factorial.md index a22f2c67f..19afdf942 100644 --- a/snippets/factorial.md +++ b/snippets/factorial.md @@ -4,15 +4,13 @@ Calculates the factorial of a number. Use recursion. If `num` is less than or equal to `1`, return `1`. Otherwise, return the product of `num` and the factorial of `num - 1`. Throws an exception if `num` is a negative or a floating point number. -```python - +```python def factorial(num): if not ((num >= 0) & (num % 1 == 0)): raise Exception( f"Number( {num} ) can't be floating point or negative ") return 1 if num == 0 else num * factorial(num - 1) - ``` ``` python factorial(6) # 720 diff --git a/snippets/gcd.md b/snippets/gcd.md index 1df3b60f8..e66f371e4 100644 --- a/snippets/gcd.md +++ b/snippets/gcd.md @@ -8,8 +8,7 @@ The `helperGcdfunction` uses recursion. Base case is when `y` equals `0`. In thi Uses the reduce function from the inbuilt module `functools`. Also defines a method `spread` for javascript like spreading of lists. -```python - +```python from functools import reduce @@ -32,7 +31,6 @@ def gcd(*args): return x if not y else gcd(y, x % y) return reduce((lambda x, y: _gcd(x, y)), numbers) - ``` diff --git a/snippets/lcm.md b/snippets/lcm.md index d0d7c0703..1085433d3 100644 --- a/snippets/lcm.md +++ b/snippets/lcm.md @@ -6,8 +6,7 @@ Use the `greatest common divisor (GCD)` formula and the fact that `lcm(x,y) = x Uses `reduce` function from the inbuilt module `functools`. Also defines a method `spread` for javascript like spreading of lists. -```python - +```python from functools import reduce @@ -33,7 +32,6 @@ def lcm(*args): return x * y / _gcd(x, y) return reduce((lambda x, y: _lcm(x, y)), numbers) - ``` diff --git a/snippets/max_n.md b/snippets/max_n.md index 9fdb99b34..c1827aaf3 100644 --- a/snippets/max_n.md +++ b/snippets/max_n.md @@ -4,8 +4,7 @@ Returns the `n` maximum elements from the provided list. If `n` is greater than Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy` module to create a shallow clone of the list and sort it in ascending order and then use `list.reverse()` reverse it to make it descending order. Use `[:n]` to get the specified number of elements. Omit the second argument, `n`, to get a one-element array -```python - +```python from copy import deepcopy @@ -15,7 +14,6 @@ def max_n(arr, n=1): numbers.sort() numbers.reverse() return numbers[:n] - ``` ```python diff --git a/snippets/min_n.md b/snippets/min_n.md index e854b06aa..69de68e37 100644 --- a/snippets/min_n.md +++ b/snippets/min_n.md @@ -4,8 +4,7 @@ Returns the `n` minimum elements from the provided list. If `n` is greater than Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy` module to create a shallow clone of the list and sort it in ascending order. Use `[:n]` to get the specified number of elements. Omit the second argument, `n`, to get a one-element array -```python - +```python from copy import deepcopy @@ -14,7 +13,6 @@ def min_n(arr, n=1): numbers = deepcopy(arr) numbers.sort() return numbers[:n] - ``` ```python diff --git a/snippets/shuffle.md b/snippets/shuffle.md index 856695ec8..6fce8b819 100644 --- a/snippets/shuffle.md +++ b/snippets/shuffle.md @@ -6,8 +6,7 @@ Randomizes the order of the values of an list, returning a new list. Uses the [Fisher-Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) to reorder the elements of the list. -```python - +```python from copy import deepcopy from random import randint @@ -21,7 +20,6 @@ def shuffle(arr): i = randint(0, m) temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m] return temp_arr - ``` ``` python diff --git a/snippets/spread.md b/snippets/spread.md index cc33d47e9..2eaf23f81 100644 --- a/snippets/spread.md +++ b/snippets/spread.md @@ -2,8 +2,7 @@ Implements javascript's spread syntax as a function. Flattens the list(non-deep) and returns an list. -```python - +```python def spread(arg): ret = [] @@ -13,7 +12,6 @@ def spread(arg): else: ret.append(i) return ret - ``` diff --git a/snippets/zip.md b/snippets/zip.md index d49a02f33..9f168989f 100644 --- a/snippets/zip.md +++ b/snippets/zip.md @@ -6,8 +6,7 @@ Creates a list of elements, grouped based on the position in the original lists. Use `max` combined with `list comprehension` to get the length of the longest list in the arguments. Loops for `max_length` times grouping elements. If lengths of `lists` vary `fill_value` is used. By default `fill_value` is `None`. -```python - +```python def zip(*args, fillvalue=None): max_length = max([len(arr) for arr in args]) @@ -17,7 +16,6 @@ def zip(*args, fillvalue=None): args[k][i] if i < len(args[k]) else None for k in range(len(args)) ]) return result - ``` ``` python