Merge pull request #76 from sandy9999/gcd_edit
modified gcd.md to fewer lines of code
This commit is contained in:
44
README.md
44
README.md
@ -174,7 +174,7 @@ count_by(['one', 'two', 'three'], len) # {3: 2, 5: 1}
|
|||||||
|
|
||||||
:information_source: Already implemented via `list.count()`.
|
:information_source: Already implemented via `list.count()`.
|
||||||
|
|
||||||
Counts the occurrences of a value in an list.
|
Counts the occurrences of a value in a list.
|
||||||
|
|
||||||
Uses the list comprehension to increment a counter each time you encounter the specific value inside the list.
|
Uses the list comprehension to increment a counter each time you encounter the specific value inside the list.
|
||||||
```py
|
```py
|
||||||
@ -383,7 +383,7 @@ spread([1,2,3,[4,5,6],[7],8,9]) # [1,2,3,4,5,6,7,8,9]
|
|||||||
### zip
|
### zip
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin), [Leonardo Galdino](https://www.github.com/LeonardoGaldino)
|
||||||
|
|
||||||
:information_source: Already implemented via `itertools.zip_longest()`
|
:information_source: Already implemented via `itertools.zip_longest()`
|
||||||
|
|
||||||
@ -396,7 +396,7 @@ def zip(*args, fillvalue=None):
|
|||||||
result = []
|
result = []
|
||||||
for i in range(max_length):
|
for i in range(max_length):
|
||||||
result.append([
|
result.append([
|
||||||
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 fillvalue for k in range(len(args))
|
||||||
])
|
])
|
||||||
return result
|
return result
|
||||||
```
|
```
|
||||||
@ -489,42 +489,22 @@ fibonnaci_until_num(15) # 610
|
|||||||
### gcd
|
### gcd
|
||||||
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
<span style="color:grey">Author:-</span> [Rohit Tanwar](https://www.github.com/kriadmin)
|
||||||
|
|
||||||
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin), [cclauss](https://www.github.com/cclauss)
|
<span style="color:grey">Contributors:-</span>[Rohit Tanwar](https://www.github.com/kriadmin), [cclauss](https://www.github.com/cclauss), [Sandhya Saravanan](https://www.github.com/sandy9999)
|
||||||
|
|
||||||
:information_source: `math.gcd` works with only two numbers
|
Calculates the greatest common divisor of a list of numbers.
|
||||||
|
|
||||||
Calculates the greatest common divisor between two or more numbers/lists.
|
Uses the reduce function from the inbuilt module `functools`. Also uses the `math.gcd` function over a list.
|
||||||
|
|
||||||
The `helperGcdfunction` uses recursion. Base case is when `y` equals `0`. In this case, return `x`. Otherwise, return the GCD of `y` and the remainder of the division `x/y`.
|
|
||||||
|
|
||||||
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
|
from functools import reduce
|
||||||
|
import math
|
||||||
|
|
||||||
|
def gcd(numbers):
|
||||||
def spread(arg):
|
return reduce(math.gcd, numbers)
|
||||||
ret = []
|
|
||||||
for i in arg:
|
|
||||||
if isinstance(i, list):
|
|
||||||
ret.extend(i)
|
|
||||||
else:
|
|
||||||
ret.append(i)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
|
|
||||||
def gcd(*args):
|
|
||||||
numbers = []
|
|
||||||
numbers.extend(spread(list(args)))
|
|
||||||
|
|
||||||
def _gcd(x, y):
|
|
||||||
return x if not y else gcd(y, x % y)
|
|
||||||
|
|
||||||
return reduce((lambda x, y: _gcd(x, y)), numbers)
|
|
||||||
```
|
```
|
||||||
<details><summary>View Examples</summary>
|
<details><summary>View Examples</summary>
|
||||||
|
|
||||||
```py
|
```py
|
||||||
gcd(8,36) # 4
|
gcd([8,36,28]) # 4
|
||||||
```
|
```
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
@ -718,7 +698,7 @@ values_only(ages) # [10, 11, 9]
|
|||||||
|
|
||||||
Returns the length of a string in bytes.
|
Returns the length of a string in bytes.
|
||||||
|
|
||||||
`utf-8` encodes a given string and find its length.
|
`utf-8` encodes a given string, then `len` finds the length of the encoded string.
|
||||||
```py
|
```py
|
||||||
def byte_size(string):
|
def byte_size(string):
|
||||||
return(len(string.encode('utf-8')))
|
return(len(string.encode('utf-8')))
|
||||||
@ -862,7 +842,7 @@ def is_upper_case(string):
|
|||||||
|
|
||||||
```py
|
```py
|
||||||
is_upper_case('ABC') # True
|
is_upper_case('ABC') # True
|
||||||
is_upper_case('a3@$') # True
|
is_upper_case('a3@$') # False
|
||||||
is_upper_case('aB4') # False
|
is_upper_case('aB4') # False
|
||||||
```
|
```
|
||||||
</details>
|
</details>
|
||||||
|
|||||||
@ -6,7 +6,7 @@ count_vowels:[Rohit Tanwar](@kriadmin)
|
|||||||
deep_flatten:[Rohit Tanwar](@kriadmin),[Meet Zaveri](@meetzaveri)
|
deep_flatten:[Rohit Tanwar](@kriadmin),[Meet Zaveri](@meetzaveri)
|
||||||
difference:[Rohit Tanwar](@kriadmin)
|
difference:[Rohit Tanwar](@kriadmin)
|
||||||
factorial:[Rohit Tanwar](@kriadmin)
|
factorial:[Rohit Tanwar](@kriadmin)
|
||||||
gcd:[Rohit Tanwar](@kriadmin),[cclauss](@cclauss)
|
gcd:[Rohit Tanwar](@kriadmin),[cclauss](@cclauss),[Sandhya Saravanan](@sandy9999)
|
||||||
lcm:[Rohit Tanwar](@kriadmin)
|
lcm:[Rohit Tanwar](@kriadmin)
|
||||||
max_n:[Rohit Tanwar](@kriadmin)
|
max_n:[Rohit Tanwar](@kriadmin)
|
||||||
min_n:[Rohit Tanwar](@kriadmin)
|
min_n:[Rohit Tanwar](@kriadmin)
|
||||||
|
|||||||
@ -13,5 +13,6 @@ MarkupSafe==1.0
|
|||||||
misaka==2.1.0
|
misaka==2.1.0
|
||||||
mistune==0.8.3
|
mistune==0.8.3
|
||||||
oauth2==1.9.0.post1
|
oauth2==1.9.0.post1
|
||||||
|
pycodestyle==2.3.1
|
||||||
pycparser==2.18
|
pycparser==2.18
|
||||||
Werkzeug==0.14.1
|
Werkzeug==0.14.1
|
||||||
|
|||||||
@ -1,38 +1,17 @@
|
|||||||
### gcd
|
### gcd
|
||||||
|
|
||||||
:information_source: `math.gcd` works with only two numbers
|
Calculates the greatest common divisor of a list of numbers.
|
||||||
|
|
||||||
Calculates the greatest common divisor between two or more numbers/lists.
|
Uses the reduce function from the inbuilt module `functools`. Also uses the `math.gcd` function over a list.
|
||||||
|
|
||||||
The `helperGcdfunction` uses recursion. Base case is when `y` equals `0`. In this case, return `x`. Otherwise, return the GCD of `y` and the remainder of the division `x/y`.
|
|
||||||
|
|
||||||
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
|
from functools import reduce
|
||||||
|
import math
|
||||||
|
|
||||||
|
def gcd(numbers):
|
||||||
def spread(arg):
|
return reduce(math.gcd, numbers)
|
||||||
ret = []
|
|
||||||
for i in arg:
|
|
||||||
if isinstance(i, list):
|
|
||||||
ret.extend(i)
|
|
||||||
else:
|
|
||||||
ret.append(i)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
|
|
||||||
def gcd(*args):
|
|
||||||
numbers = []
|
|
||||||
numbers.extend(spread(list(args)))
|
|
||||||
|
|
||||||
def _gcd(x, y):
|
|
||||||
return x if not y else gcd(y, x % y)
|
|
||||||
|
|
||||||
return reduce((lambda x, y: _gcd(x, y)), numbers)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
``` python
|
``` python
|
||||||
gcd(8,36) # 4
|
gcd([8,36,28]) # 4
|
||||||
```
|
```
|
||||||
Reference in New Issue
Block a user