countOccurences

This commit is contained in:
Rohit Tanwar
2018-01-09 12:47:55 +05:30
parent 9b697225df
commit 2295b19d96
3 changed files with 36 additions and 2 deletions

View File

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

View File

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