modified gcd.md to fewer lines of code

modify gcd.md to fewer lines of code

modified README
This commit is contained in:
sandy9999
2019-04-19 14:39:30 +05:30
parent 5424f64e2c
commit df18a3f7b2
3 changed files with 19 additions and 60 deletions

View File

@ -175,7 +175,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
@ -384,7 +384,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()`
@ -397,7 +397,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
``` ```
@ -465,42 +465,22 @@ factorial(6) # 720
### 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>
@ -694,7 +674,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')))
@ -838,7 +818,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>

View File

@ -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)

View File

@ -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
``` ```