diff --git a/README.md b/README.md index 1448aea52..39cfbf751 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,23 @@ def chunk(arr, size): chunk([1,2,3,4,5],2) # [[1,2],[3,4],5] ``` +### countOccurences + +Counts the occurrences of a value in an list. + +Uses the `reduce` functin from built-in module `functools` to increment a counter each time you encounter the specific value inside the list. + +```python +def countOccurences(arr, val): + return reduce( + (lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0), + arr) + +``` + +```python +countOccurrences([1, 1, 2, 1, 2, 3], 1) # 3 +``` ### gcd Calculates the greatest common divisor between two or more numbers/lists. @@ -69,7 +86,7 @@ gcd(8,36) # 4 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. -kbc + Uses `reduce` function from the inbuilt module `functools`. Also defines a method `spread` for javascript like spreading of lists. ```python diff --git a/snippets/countOccurences.md b/snippets/countOccurences.md new file mode 100644 index 000000000..32da1fdff --- /dev/null +++ b/snippets/countOccurences.md @@ -0,0 +1,17 @@ +### countOccurences + +Counts the occurrences of a value in an list. + +Uses the `reduce` functin from built-in module `functools` to increment a counter each time you encounter the specific value inside the list. + +```python +def countOccurences(arr, val): + return reduce( + (lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0), + arr) + +``` + +```python +countOccurrences([1, 1, 2, 1, 2, 3], 1) # 3 +``` \ No newline at end of file diff --git a/snippets/lcm.md b/snippets/lcm.md index 2481707cd..f4df6bbbb 100644 --- a/snippets/lcm.md +++ b/snippets/lcm.md @@ -3,7 +3,7 @@ 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. -k + Uses `reduce` function from the inbuilt module `functools`. Also defines a method `spread` for javascript like spreading of lists. ```python