factorial

This commit is contained in:
Rohit Tanwar
2018-01-27 11:03:02 +05:30
parent 94a4c53868
commit b5a1fa18ba
17 changed files with 31 additions and 91 deletions

View File

@ -50,11 +50,9 @@ Takes the sum of all the `args` and divides it by `len(args)`. The secind argume
```py ```py
def average(*args): def average(*args):
return sum(args, 0.0) / len(args) return sum(args, 0.0) / len(args)
``` ```
<details><summary>View Examples</summary> <details><summary>View Examples</summary>
@ -77,14 +75,12 @@ Use recursion. If `num` is less than or equal to `1`, return `1`. Otherwise, ret
```py ```py
def factorial(num): def factorial(num):
if not ((num >= 0) & (num % 1 == 0)): if not ((num >= 0) & (num % 1 == 0)):
raise Exception( raise Exception(
f"Number( {num} ) can't be floating point or negative ") f"Number( {num} ) can't be floating point or negative ")
return 1 if num == 0 else num * factorial(num - 1) return 1 if num == 0 else num * factorial(num - 1)
``` ```
<details><summary>View Examples</summary> <details><summary>View Examples</summary>
@ -109,7 +105,6 @@ Uses the reduce function from the inbuilt module `functools`. Also defines a met
```py ```py
from functools import reduce from functools import reduce
@ -132,7 +127,6 @@ def gcd(*args):
return reduce((lambda x, y: _gcd(x, y)), numbers) return reduce((lambda x, y: _gcd(x, y)), numbers)
``` ```
@ -157,7 +151,6 @@ Uses `reduce` function from the inbuilt module `functools`. Also defines a metho
```py ```py
from functools import reduce from functools import reduce
@ -183,7 +176,6 @@ def lcm(*args):
return reduce((lambda x, y: _lcm(x, y)), numbers) return reduce((lambda x, y: _lcm(x, y)), numbers)
``` ```
@ -207,7 +199,6 @@ Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy`
```py ```py
from copy import deepcopy from copy import deepcopy
@ -217,7 +208,6 @@ def max_n(arr, n=1):
numbers.reverse() numbers.reverse()
return numbers[:n] return numbers[:n]
``` ```
<details><summary>View Examples</summary> <details><summary>View Examples</summary>
@ -240,7 +230,6 @@ Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy`
```py ```py
from copy import deepcopy from copy import deepcopy
@ -249,7 +238,6 @@ def min_n(arr, n=1):
numbers.sort() numbers.sort()
return numbers[:n] return numbers[:n]
``` ```
<details><summary>View Examples</summary> <details><summary>View Examples</summary>
@ -274,7 +262,6 @@ Uses `range` to create a list of desired size. Then use `map` on this list and f
```py ```py
from math import ceil from math import ceil
@ -283,7 +270,6 @@ def chunk(arr, size):
map(lambda x: arr[x * size:x * size + size], map(lambda x: arr[x * size:x * size + size],
list(range(0, ceil(len(arr) / size))))) list(range(0, ceil(len(arr) / size)))))
``` ```
<details><summary>View Examples</summary> <details><summary>View Examples</summary>
@ -305,11 +291,9 @@ Use `filter()` to filter out falsey values (False, None, 0, and "").
```py ```py
def compact(arr): def compact(arr):
return list(filter(lambda x: bool(x), arr)) return list(filter(lambda x: bool(x), arr))
``` ```
<details><summary>View Examples</summary> <details><summary>View Examples</summary>
@ -333,13 +317,11 @@ Uses the `reduce` functin from built-in module `functools` to increment a counte
```py ```py
def count_occurences(arr, val): def count_occurences(arr, val):
return reduce( return reduce(
(lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0), (lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0),
arr) arr)
``` ```
<details><summary>View Examples</summary> <details><summary>View Examples</summary>
@ -361,7 +343,6 @@ Use recursion. Use `list.extend()` with an empty array (`result`) and the spread
```py ```py
def spread(arg): def spread(arg):
ret = [] ret = []
for i in arg: for i in arg:
@ -378,7 +359,6 @@ def deep_flatten(arr):
spread(list(map(lambda x: deep(x) if type(x) == list else x, arr)))) spread(list(map(lambda x: deep(x) if type(x) == list else x, arr))))
return result return result
``` ```
<details><summary>View Examples</summary> <details><summary>View Examples</summary>
@ -400,12 +380,10 @@ Create a `set` from `b`, then use list comprehension to only keep values not con
```py ```py
def difference(a, b): def difference(a, b):
b = set(b) b = set(b)
return [item for item in a if item not in b] return [item for item in a if item not in b]
``` ```
<details><summary>View Examples</summary> <details><summary>View Examples</summary>
@ -428,7 +406,6 @@ Uses the [Fisher-Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Y
```py ```py
from copy import deepcopy from copy import deepcopy
from random import randint from random import randint
@ -442,7 +419,6 @@ def shuffle(arr):
temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m] temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m]
return temp_arr return temp_arr
``` ```
<details><summary>View Examples</summary> <details><summary>View Examples</summary>
@ -463,7 +439,6 @@ Implements javascript's spread syntax as a function. Flattens the list(non-deep)
```py ```py
def spread(arg): def spread(arg):
ret = [] ret = []
for i in arg: for i in arg:
@ -473,7 +448,6 @@ def spread(arg):
ret.append(i) ret.append(i)
return ret return ret
``` ```
@ -498,7 +472,6 @@ Use `max` combined with `list comprehension` to get the length of the longest li
```py ```py
def zip(*args, fillvalue=None): def zip(*args, fillvalue=None):
max_length = max([len(arr) for arr in args]) max_length = max([len(arr) for arr in args])
result = [] result = []
@ -508,7 +481,6 @@ def zip(*args, fillvalue=None):
]) ])
return result return result
``` ```
<details><summary>View Examples</summary> <details><summary>View Examples</summary>
@ -534,14 +506,12 @@ Use a regular expression to count the number of vowels `(A, E, I, O, U)` in a st
```py ```py
import re import re
def count_vowels(str): def count_vowels(str):
return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE))) return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE)))
``` ```
<details><summary>View Examples</summary> <details><summary>View Examples</summary>

View File

@ -11,7 +11,7 @@ for file in files:
originalCode = re.search(codeRe,fileData).group(0) originalCode = re.search(codeRe,fileData).group(0)
#print(re.split(codeRe,fileData)[0]) #print(re.split(codeRe,fileData)[0])
formatedCode = autopep8.fix_code(re.split(codeRe,fileData)[1]) 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 = open("snippets/"+file,'w')
someFile.write(fileToSave) someFile.write(fileToSave)
someFile.close() someFile.close()

View File

@ -8,10 +8,8 @@ Takes the sum of all the `args` and divides it by `len(args)`. The secind argume
```python ```python
def average(*args): def average(*args):
return sum(args, 0.0) / len(args) return sum(args, 0.0) / len(args)
``` ```
``` python ``` python

View File

@ -6,7 +6,6 @@ Uses `range` to create a list of desired size. Then use `map` on this list and f
```python ```python
from math import ceil from math import ceil
@ -14,7 +13,6 @@ def chunk(arr, size):
return list( return list(
map(lambda x: arr[x * size:x * size + size], map(lambda x: arr[x * size:x * size + size],
list(range(0, ceil(len(arr) / size))))) list(range(0, ceil(len(arr) / size)))))
``` ```
``` python ``` python

View File

@ -6,10 +6,8 @@ Use `filter()` to filter out falsey values (False, None, 0, and "").
```python ```python
def compact(arr): def compact(arr):
return list(filter(lambda x: bool(x), arr)) return list(filter(lambda x: bool(x), arr))
``` ```
``` python ``` python

View File

@ -8,12 +8,10 @@ Uses the `reduce` functin from built-in module `functools` to increment a counte
```python ```python
def count_occurences(arr, val): def count_occurences(arr, val):
return reduce( return reduce(
(lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0), (lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0),
arr) arr)
``` ```
```python ```python

View File

@ -6,13 +6,11 @@ Use a regular expression to count the number of vowels `(A, E, I, O, U)` in a st
```python ```python
import re import re
def count_vowels(str): def count_vowels(str):
return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE))) return len(len(re.findall(r'[aeiou]', str, re.IGNORECASE)))
``` ```
``` python ``` python

View File

@ -6,7 +6,6 @@ Use recursion. Use `list.extend()` with an empty array (`result`) and the spread
```python ```python
def spread(arg): def spread(arg):
ret = [] ret = []
for i in arg: for i in arg:
@ -22,7 +21,6 @@ def deep_flatten(arr):
result.extend( result.extend(
spread(list(map(lambda x: deep(x) if type(x) == list else x, arr)))) spread(list(map(lambda x: deep(x) if type(x) == list else x, arr))))
return result return result
``` ```
```python ```python

View File

@ -6,11 +6,9 @@ Create a `set` from `b`, then use list comprehension to only keep values not con
```python ```python
def difference(a, b): def difference(a, b):
b = set(b) b = set(b)
return [item for item in a if item not in b] return [item for item in a if item not in b]
``` ```
``` python ``` python
difference([1, 2, 3], [1, 2, 4]) # [3] difference([1, 2, 3], [1, 2, 4]) # [3]

View File

@ -6,13 +6,11 @@ Use recursion. If `num` is less than or equal to `1`, return `1`. Otherwise, ret
```python ```python
def factorial(num): def factorial(num):
if not ((num >= 0) & (num % 1 == 0)): if not ((num >= 0) & (num % 1 == 0)):
raise Exception( raise Exception(
f"Number( {num} ) can't be floating point or negative ") f"Number( {num} ) can't be floating point or negative ")
return 1 if num == 0 else num * factorial(num - 1) return 1 if num == 0 else num * factorial(num - 1)
``` ```
``` python ``` python
factorial(6) # 720 factorial(6) # 720

View File

@ -10,7 +10,6 @@ Uses the reduce function from the inbuilt module `functools`. Also defines a met
```python ```python
from functools import reduce from functools import reduce
@ -32,7 +31,6 @@ def gcd(*args):
return x if not y else gcd(y, x % y) return x if not y else gcd(y, x % y)
return reduce((lambda x, y: _gcd(x, y)), numbers) return reduce((lambda x, y: _gcd(x, y)), numbers)
``` ```

View File

@ -8,7 +8,6 @@ Uses `reduce` function from the inbuilt module `functools`. Also defines a metho
```python ```python
from functools import reduce from functools import reduce
@ -33,7 +32,6 @@ def lcm(*args):
return x * y / _gcd(x, y) return x * y / _gcd(x, y)
return reduce((lambda x, y: _lcm(x, y)), numbers) return reduce((lambda x, y: _lcm(x, y)), numbers)
``` ```

View File

@ -6,7 +6,6 @@ Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy`
```python ```python
from copy import deepcopy from copy import deepcopy
@ -15,7 +14,6 @@ def max_n(arr, n=1):
numbers.sort() numbers.sort()
numbers.reverse() numbers.reverse()
return numbers[:n] return numbers[:n]
``` ```
```python ```python

View File

@ -6,7 +6,6 @@ Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy`
```python ```python
from copy import deepcopy from copy import deepcopy
@ -14,7 +13,6 @@ def min_n(arr, n=1):
numbers = deepcopy(arr) numbers = deepcopy(arr)
numbers.sort() numbers.sort()
return numbers[:n] return numbers[:n]
``` ```
```python ```python

View File

@ -8,7 +8,6 @@ Uses the [Fisher-Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Y
```python ```python
from copy import deepcopy from copy import deepcopy
from random import randint from random import randint
@ -21,7 +20,6 @@ def shuffle(arr):
i = randint(0, m) i = randint(0, m)
temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m] temp_arr[m], temp_arr[i] = temp_arr[i], temp_arr[m]
return temp_arr return temp_arr
``` ```
``` python ``` python

View File

@ -4,7 +4,6 @@ Implements javascript's spread syntax as a function. Flattens the list(non-deep)
```python ```python
def spread(arg): def spread(arg):
ret = [] ret = []
for i in arg: for i in arg:
@ -13,7 +12,6 @@ def spread(arg):
else: else:
ret.append(i) ret.append(i)
return ret return ret
``` ```

View File

@ -8,7 +8,6 @@ Use `max` combined with `list comprehension` to get the length of the longest li
```python ```python
def zip(*args, fillvalue=None): def zip(*args, fillvalue=None):
max_length = max([len(arr) for arr in args]) max_length = max([len(arr) for arr in args])
result = [] result = []
@ -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)) args[k][i] if i < len(args[k]) else None for k in range(len(args))
]) ])
return result return result
``` ```
``` python ``` python