From 3a3c6186192b73cd4d9e76f192dfaf523d3bdf50 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 20 Aug 2019 11:08:01 +0300 Subject: [PATCH] Update some snippets --- snippets/lcm.md | 39 +++++++++++++++++---------------------- snippets/max_n.md | 13 ++++++++----- snippets/min_n.md | 16 +++++++--------- 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/snippets/lcm.md b/snippets/lcm.md index 92752479b..9acd3a5bd 100644 --- a/snippets/lcm.md +++ b/snippets/lcm.md @@ -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 diff --git a/snippets/max_n.md b/snippets/max_n.md index 76bbcdfe9..2ac09ac75 100644 --- a/snippets/max_n.md +++ b/snippets/max_n.md @@ -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 diff --git a/snippets/min_n.md b/snippets/min_n.md index d88f45847..4484c579d 100644 --- a/snippets/min_n.md +++ b/snippets/min_n.md @@ -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