Update some snippets
This commit is contained in:
@ -1,41 +1,36 @@
|
||||
---
|
||||
title: lcm
|
||||
tags: math
|
||||
tags: math,list,recursion,advanced
|
||||
---
|
||||
|
||||
Returns the least common multiple of two or more numbers.
|
||||
|
||||
Use the `greatest common divisor (GCD)` formula and the fact that `lcm(x,y) = x * y / gcd(x,y)` to determine the least common multiple. The GCD formula uses recursion.
|
||||
|
||||
Uses `reduce` function from the inbuilt module `functools`. Also defines a method `spread` for javascript like spreading of lists.
|
||||
Define a function, `spread`, that uses either `list.extend()` or `list.append()` on each element in a list to flatten it.
|
||||
Use `math.gcd()` and `lcm(x,y) = x * y / gcd(x,y)` to determine the least common multiple.
|
||||
|
||||
```py
|
||||
from functools import reduce
|
||||
|
||||
import math
|
||||
|
||||
def spread(arg):
|
||||
ret = []
|
||||
for i in arg:
|
||||
if isinstance(i, list):
|
||||
ret.extend(i)
|
||||
else:
|
||||
ret.append(i)
|
||||
return ret
|
||||
|
||||
ret = []
|
||||
for i in arg:
|
||||
if isinstance(i, list):
|
||||
ret.extend(i)
|
||||
else:
|
||||
ret.append(i)
|
||||
return ret
|
||||
|
||||
def lcm(*args):
|
||||
numbers = []
|
||||
numbers.extend(spread(list(args)))
|
||||
numbers = []
|
||||
numbers.extend(spread(list(args)))
|
||||
|
||||
def _gcd(x, y):
|
||||
return x if not y else _gcd(y, x % y)
|
||||
def _lcm(x, y):
|
||||
return int(x * y / math.gcd(x, y))
|
||||
|
||||
def _lcm(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)
|
||||
```
|
||||
|
||||
|
||||
```py
|
||||
lcm(12, 7) # 84
|
||||
lcm([1, 3, 4], 5) # 60
|
||||
|
||||
@ -1,14 +1,17 @@
|
||||
---
|
||||
title: max_n
|
||||
tags: math
|
||||
tags: list,math,beginner
|
||||
---
|
||||
Returns the `n` maximum elements from the provided list. If `n` is greater than or equal to the provided list's length, then return the original list(sorted in descending order).
|
||||
|
||||
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 list
|
||||
Returns the `n` maximum elements from the provided list.
|
||||
If `n` is greater than or equal to the provided list's length, then return the original list (sorted in descending order).
|
||||
|
||||
Use `sorted() to sort the list, `[:n]` to get the specified number of elements.
|
||||
Omit the second argument, `n`, to get a one-element list.
|
||||
|
||||
```py
|
||||
def max_n(lst, n=1, reverse=True):
|
||||
return sorted(lst, reverse=reverse)[:n]
|
||||
def max_n(lst, n=1):
|
||||
return sorted(lst, reverse=True)[:n]
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -1,19 +1,17 @@
|
||||
---
|
||||
title: min_n
|
||||
tags: math
|
||||
tags: list,math,beginner
|
||||
---
|
||||
Returns the `n` minimum elements from the provided list. If `n` is greater than or equal to the provided list's length, then return the original list(sorted in ascending order).
|
||||
|
||||
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 list
|
||||
Returns the `n` minimum elements from the provided list.
|
||||
If `n` is greater than or equal to the provided list's length, then return the original list (sorted in ascending order).
|
||||
|
||||
Use `sorted() to sort the list, `[:n]` to get the specified number of elements.
|
||||
Omit the second argument, `n`, to get a one-element list.
|
||||
|
||||
```py
|
||||
from copy import deepcopy
|
||||
|
||||
|
||||
def min_n(lst, n=1):
|
||||
numbers = deepcopy(lst)
|
||||
numbers.sort()
|
||||
return numbers[:n]
|
||||
return sorted(lst, reverse=False)[:n]
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
Reference in New Issue
Block a user