Add some snippet
This commit is contained in:
@ -11,12 +11,12 @@ Omit the seconds argument, `fn`, to check if all elements are `True`.
|
|||||||
```py
|
```py
|
||||||
def every(lst, fn=lambda x: not not x):
|
def every(lst, fn=lambda x: not not x):
|
||||||
for el in lst:
|
for el in lst:
|
||||||
if not fn(x):
|
if not fn(el):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
```
|
```
|
||||||
|
|
||||||
```py
|
```py
|
||||||
every([4, 2, 3], lambda x: x > 1) # true
|
every([4, 2, 3], lambda x: x > 1) # True
|
||||||
every([1, 2, 3]) # True
|
every([1, 2, 3]) # True
|
||||||
```
|
```
|
||||||
|
|||||||
22
snippets/some.md
Normal file
22
snippets/some.md
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
---
|
||||||
|
title: some
|
||||||
|
tags: list,function,intermediate
|
||||||
|
---
|
||||||
|
|
||||||
|
Returns `True` if the provided function returns `True` for at least one element in the list, `False` otherwise.
|
||||||
|
|
||||||
|
Iterate over the elements of the list to test if every element in the list returns `True` based on `fn`.
|
||||||
|
Omit the seconds argument, `fn`, to check if all elements are `True`.
|
||||||
|
|
||||||
|
```py
|
||||||
|
def some(lst, fn=lambda x: not not x):
|
||||||
|
for el in lst:
|
||||||
|
if fn(el):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
```
|
||||||
|
|
||||||
|
```py
|
||||||
|
some([0, 1, 2, 0], lambda x: x >= 2 ) # True
|
||||||
|
some([0, 0, 1, 0]) # True
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user