diff --git a/snippets/all_unique.md b/snippets/all_unique.md new file mode 100644 index 000000000..ea4177beb --- /dev/null +++ b/snippets/all_unique.md @@ -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 +``` \ No newline at end of file diff --git a/snippets/has_duplicates.md b/snippets/has_duplicates.md new file mode 100644 index 000000000..3a7535919 --- /dev/null +++ b/snippets/has_duplicates.md @@ -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 +``` diff --git a/snippets/keys_only.md b/snippets/keys_only.md new file mode 100644 index 000000000..f53a32107 --- /dev/null +++ b/snippets/keys_only.md @@ -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'] +``` \ No newline at end of file diff --git a/snippets/values_only.md b/snippets/values_only.md new file mode 100644 index 000000000..ddda0731d --- /dev/null +++ b/snippets/values_only.md @@ -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] +``` \ No newline at end of file diff --git a/test/all_unique/all_unique.py b/test/all_unique/all_unique.py new file mode 100644 index 000000000..b7ef483c2 --- /dev/null +++ b/test/all_unique/all_unique.py @@ -0,0 +1,2 @@ +def all_unique(lst): + return len(lst) == len(set(lst)) \ No newline at end of file diff --git a/test/all_unique/all_unique.test.py b/test/all_unique/all_unique.test.py new file mode 100644 index 000000000..70ccf5715 --- /dev/null +++ b/test/all_unique/all_unique.test.py @@ -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) \ No newline at end of file diff --git a/test/has_duplicates/has_duplicates.py b/test/has_duplicates/has_duplicates.py new file mode 100644 index 000000000..aa9e0ed66 --- /dev/null +++ b/test/has_duplicates/has_duplicates.py @@ -0,0 +1,2 @@ +def has_duplicates(lst): + return len(lst) != len(set(lst)) \ No newline at end of file diff --git a/test/has_duplicates/has_duplicates.test.py b/test/has_duplicates/has_duplicates.test.py new file mode 100644 index 000000000..9c2738eb1 --- /dev/null +++ b/test/has_duplicates/has_duplicates.test.py @@ -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) \ No newline at end of file diff --git a/test/keys_only/keys_only.py b/test/keys_only/keys_only.py new file mode 100644 index 000000000..5f97a2265 --- /dev/null +++ b/test/keys_only/keys_only.py @@ -0,0 +1,5 @@ +def keys_only(dict): + lst = [] + for k, v in dict.items(): + lst.append(k) + return lst \ No newline at end of file diff --git a/test/keys_only/keys_only.test.py b/test/keys_only/keys_only.test.py new file mode 100644 index 000000000..f2d3b2bac --- /dev/null +++ b/test/keys_only/keys_only.test.py @@ -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) diff --git a/test/values_only/values_only.py b/test/values_only/values_only.py new file mode 100644 index 000000000..fb26aa46f --- /dev/null +++ b/test/values_only/values_only.py @@ -0,0 +1,5 @@ +def values_only(dict): + lst = [] + for k, v in dict.items(): + lst.append(v) + return lst \ No newline at end of file diff --git a/test/values_only/values_only.test.py b/test/values_only/values_only.test.py new file mode 100644 index 000000000..6a2f99517 --- /dev/null +++ b/test/values_only/values_only.test.py @@ -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) \ No newline at end of file