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