Merge pull request #27 from Rob-Rychs/master
[FEATURE] add snippets has_duplicates all_unique keys_only values_only
This commit is contained in:
17
snippets/all_unique.md
Normal file
17
snippets/all_unique.md
Normal file
@ -0,0 +1,17 @@
|
||||
### all_unique
|
||||
|
||||
Checks a flat list for all unique values. Returns True if list values are all unique and False if list values aren't all unique.
|
||||
|
||||
This function compares the length of the list with length of the set() of the list. set() removes duplicate values from the list.
|
||||
|
||||
``` python
|
||||
def all_unique(lst):
|
||||
return len(lst) == len(set(lst))
|
||||
```
|
||||
|
||||
``` python
|
||||
x = [1,2,3,4,5,6]
|
||||
y = [1,2,2,3,4,5]
|
||||
all_unique(x) # True
|
||||
all_unique(y) # False
|
||||
```
|
||||
17
snippets/has_duplicates.md
Normal file
17
snippets/has_duplicates.md
Normal file
@ -0,0 +1,17 @@
|
||||
### has_duplicates
|
||||
|
||||
Checks a flat list for duplicate values. Returns True if duplicate values exist and False if values are all unique.
|
||||
|
||||
This function compares the length of the list with length of the set() of the list. set() removes duplicate values from the list.
|
||||
|
||||
``` python
|
||||
def has_duplicates(lst):
|
||||
return len(lst) != len(set(lst))
|
||||
```
|
||||
|
||||
``` python
|
||||
x = [1,2,3,4,5,5]
|
||||
y = [1,2,3,4,5]
|
||||
has_duplicates(x) # True
|
||||
has_duplicates(y) # False
|
||||
```
|
||||
22
snippets/keys_only.md
Normal file
22
snippets/keys_only.md
Normal file
@ -0,0 +1,22 @@
|
||||
### keys_only
|
||||
|
||||
Function which accepts a dictionary of key value pairs and returns a new flat list of only the keys.
|
||||
|
||||
Uses the .items() function with a for loop on the dictionary to track both the key and value and returns a new list by appending the keys to it. Best used on 1 level-deep key:value pair dictionaries (a flat dictionary) and not nested data-structures which are also commonly used with dictionaries. (a flat dictionary resembles a json and a flat list an array for javascript people).
|
||||
|
||||
``` python
|
||||
def keys_only(flat_dict):
|
||||
lst = []
|
||||
for k, v in flat_dict.items():
|
||||
lst.append(k)
|
||||
return lst
|
||||
```
|
||||
|
||||
``` python
|
||||
ages = {
|
||||
"Peter": 10,
|
||||
"Isabel": 11,
|
||||
"Anna": 9,
|
||||
}
|
||||
keys_only(ages) # ['Peter', 'Isabel', 'Anna']
|
||||
```
|
||||
22
snippets/values_only.md
Normal file
22
snippets/values_only.md
Normal file
@ -0,0 +1,22 @@
|
||||
### values_only
|
||||
|
||||
Function which accepts a dictionary of key value pairs and returns a new flat list of only the values.
|
||||
|
||||
Uses the .items() function with a for loop on the dictionary to track both the key and value of the iteration and returns a new list by appending the values to it. Best used on 1 level-deep key:value pair dictionaries and not nested data-structures.
|
||||
|
||||
``` python
|
||||
def values_only(dict):
|
||||
lst = []
|
||||
for k, v in dict.items():
|
||||
lst.append(v)
|
||||
return lst
|
||||
```
|
||||
|
||||
``` python
|
||||
ages = {
|
||||
"Peter": 10,
|
||||
"Isabel": 11,
|
||||
"Anna": 9,
|
||||
}
|
||||
values_only(ages) # [10, 11, 9]
|
||||
```
|
||||
2
test/all_unique/all_unique.py
Normal file
2
test/all_unique/all_unique.py
Normal file
@ -0,0 +1,2 @@
|
||||
def all_unique(lst):
|
||||
return len(lst) == len(set(lst))
|
||||
6
test/all_unique/all_unique.test.py
Normal file
6
test/all_unique/all_unique.test.py
Normal file
@ -0,0 +1,6 @@
|
||||
import types,functools
|
||||
from pytape import test
|
||||
from all_unique import all_unique
|
||||
def all_unique(t):
|
||||
t.true(isinstance(all_unique, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'all_unique is a function')
|
||||
test('Testing all_unique',all_unique_test)
|
||||
2
test/has_duplicates/has_duplicates.py
Normal file
2
test/has_duplicates/has_duplicates.py
Normal file
@ -0,0 +1,2 @@
|
||||
def has_duplicates(lst):
|
||||
return len(lst) != len(set(lst))
|
||||
6
test/has_duplicates/has_duplicates.test.py
Normal file
6
test/has_duplicates/has_duplicates.test.py
Normal file
@ -0,0 +1,6 @@
|
||||
import types,functools
|
||||
from pytape import test
|
||||
from has_duplicates import has_duplicates
|
||||
def has_duplicates(t):
|
||||
t.true(isinstance(has_duplicates, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'has_duplicates is a function')
|
||||
test('Testing has_duplicates',has_duplicates_test)
|
||||
5
test/keys_only/keys_only.py
Normal file
5
test/keys_only/keys_only.py
Normal file
@ -0,0 +1,5 @@
|
||||
def keys_only(dict):
|
||||
lst = []
|
||||
for k, v in dict.items():
|
||||
lst.append(k)
|
||||
return lst
|
||||
6
test/keys_only/keys_only.test.py
Normal file
6
test/keys_only/keys_only.test.py
Normal file
@ -0,0 +1,6 @@
|
||||
import types,functools
|
||||
from pytape import test
|
||||
from keys_only import keys_only
|
||||
def keys_only_test(t):
|
||||
t.true(isinstance(keys_only, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'keys_only is a function')
|
||||
test('Testing keys_only',keys_only_test)
|
||||
5
test/values_only/values_only.py
Normal file
5
test/values_only/values_only.py
Normal file
@ -0,0 +1,5 @@
|
||||
def values_only(dict):
|
||||
lst = []
|
||||
for k, v in dict.items():
|
||||
lst.append(v)
|
||||
return lst
|
||||
6
test/values_only/values_only.test.py
Normal file
6
test/values_only/values_only.test.py
Normal file
@ -0,0 +1,6 @@
|
||||
import types,functools
|
||||
from pytape import test
|
||||
from values_only import values_only
|
||||
def values_only_test(t):
|
||||
t.true(isinstance(values_only, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'values_only is a function')
|
||||
test('Testing values_only',values_only_test)
|
||||
Reference in New Issue
Block a user