From 7f25b4de802868a072b0091ad007dc920ed592f7 Mon Sep 17 00:00:00 2001 From: Rob-Rychs Date: Sun, 1 Apr 2018 01:03:09 -0700 Subject: [PATCH 1/6] add has_duplicates and all_unique --- snippets/all_unique.md | 16 ++++++++++++++++ snippets/has_duplicates.md | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 snippets/all_unique.md create mode 100644 snippets/has_duplicates.md diff --git a/snippets/all_unique.md b/snippets/all_unique.md new file mode 100644 index 000000000..020e65f2e --- /dev/null +++ b/snippets/all_unique.md @@ -0,0 +1,16 @@ +### 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] +all_unique(x) # result +True +``` \ No newline at end of file diff --git a/snippets/has_duplicates.md b/snippets/has_duplicates.md new file mode 100644 index 000000000..2c0f65f5e --- /dev/null +++ b/snippets/has_duplicates.md @@ -0,0 +1,16 @@ +### 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] +has_duplicates(x) # result +True +``` From 7a296dafa2409f60323bb8293746e6a8bd712c7f Mon Sep 17 00:00:00 2001 From: Rob-Rychs Date: Sun, 1 Apr 2018 01:16:14 -0700 Subject: [PATCH 2/6] add test boilerplate --- test/all_unique/all_unique.py | 2 ++ test/all_unique/all_unique.test.py | 6 ++++++ test/has_duplicates/has_duplicates.py | 2 ++ test/has_duplicates/has_duplicates.test.py | 6 ++++++ 4 files changed, 16 insertions(+) create mode 100644 test/all_unique/all_unique.py create mode 100644 test/all_unique/all_unique.test.py create mode 100644 test/has_duplicates/has_duplicates.py create mode 100644 test/has_duplicates/has_duplicates.test.py 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 From 57c3f5f1ab934c60655f4bae431c5673cb630687 Mon Sep 17 00:00:00 2001 From: Rob-Rychs Date: Sun, 1 Apr 2018 13:56:31 -0700 Subject: [PATCH 3/6] add snippets keys_only + values_only + test bp --- snippets/keys_only.md | 23 +++++++++++++++++++++++ snippets/values_only.md | 23 +++++++++++++++++++++++ test/keys_only/Keys_only.test.py | 6 ++++++ test/keys_only/keys_only.py | 0 test/values_only/values_only.py | 5 +++++ test/values_only/values_only.test.py | 6 ++++++ 6 files changed, 63 insertions(+) create mode 100644 snippets/keys_only.md create mode 100644 snippets/values_only.md create mode 100644 test/keys_only/Keys_only.test.py create mode 100644 test/keys_only/keys_only.py create mode 100644 test/values_only/values_only.py create mode 100644 test/values_only/values_only.test.py diff --git a/snippets/keys_only.md b/snippets/keys_only.md new file mode 100644 index 000000000..7c2a3e478 --- /dev/null +++ b/snippets/keys_only.md @@ -0,0 +1,23 @@ +### 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 and not nested data-structures. + +``` python +def keys_only(dict): + lst = [] + for k, v in dict.items(): + lst.append(k) + return lst +``` + +``` python +ages = { + "Peter": 10, + "Isabel": 11, + "Anna": 9, +} +keys_only(ages) # result +['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..e326dd9a9 --- /dev/null +++ b/snippets/values_only.md @@ -0,0 +1,23 @@ +### 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) # result +[10, 11, 9] +``` \ 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..6ba12d5cd --- /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) \ 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..e69de29bb 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 From aa31f4cc7470896d5b9e1a4d9918a3f468a979ce Mon Sep 17 00:00:00 2001 From: Rob-Rychs Date: Sun, 15 Apr 2018 10:10:41 -0700 Subject: [PATCH 4/6] fix #results in examples, added test file for keys_only --- snippets/all_unique.md | 5 +++-- snippets/has_duplicates.md | 5 +++-- snippets/keys_only.md | 3 +-- snippets/values_only.md | 3 +-- test/keys_only/keys_only.py | 5 +++++ 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/snippets/all_unique.md b/snippets/all_unique.md index 020e65f2e..ea4177beb 100644 --- a/snippets/all_unique.md +++ b/snippets/all_unique.md @@ -11,6 +11,7 @@ def all_unique(lst): ``` python x = [1,2,3,4,5,6] -all_unique(x) # result -True +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 index 2c0f65f5e..3a7535919 100644 --- a/snippets/has_duplicates.md +++ b/snippets/has_duplicates.md @@ -11,6 +11,7 @@ def has_duplicates(lst): ``` python x = [1,2,3,4,5,5] -has_duplicates(x) # result -True +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 index 7c2a3e478..1dc5a4d0d 100644 --- a/snippets/keys_only.md +++ b/snippets/keys_only.md @@ -18,6 +18,5 @@ ages = { "Isabel": 11, "Anna": 9, } -keys_only(ages) # result -['Peter', 'Isabel', 'Anna'] +keys_only(ages) # ['Peter', 'Isabel', 'Anna'] ``` \ No newline at end of file diff --git a/snippets/values_only.md b/snippets/values_only.md index e326dd9a9..ddda0731d 100644 --- a/snippets/values_only.md +++ b/snippets/values_only.md @@ -18,6 +18,5 @@ ages = { "Isabel": 11, "Anna": 9, } -values_only(ages) # result -[10, 11, 9] +values_only(ages) # [10, 11, 9] ``` \ No newline at end of file diff --git a/test/keys_only/keys_only.py b/test/keys_only/keys_only.py index e69de29bb..5f97a2265 100644 --- a/test/keys_only/keys_only.py +++ 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 From 9f0bd80219d4a94471a82fc3921ec46b03e3a3ea Mon Sep 17 00:00:00 2001 From: Rob-Rychs Date: Sun, 15 Apr 2018 10:22:54 -0700 Subject: [PATCH 5/6] update keys_only snippet discription, changed func param to flat_dict --- snippets/keys_only.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/keys_only.md b/snippets/keys_only.md index 1dc5a4d0d..f53a32107 100644 --- a/snippets/keys_only.md +++ b/snippets/keys_only.md @@ -2,12 +2,12 @@ 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 and not nested data-structures. +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(dict): +def keys_only(flat_dict): lst = [] - for k, v in dict.items(): + for k, v in flat_dict.items(): lst.append(k) return lst ``` From 3b8896c5cbf59a78c33d58eb6ae3b3857ace3ad4 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Mon, 16 Apr 2018 18:42:38 +0530 Subject: [PATCH 6/6] Rename Keys_only.test.py to keys_only.test.py --- test/keys_only/{Keys_only.test.py => keys_only.test.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename test/keys_only/{Keys_only.test.py => keys_only.test.py} (84%) diff --git a/test/keys_only/Keys_only.test.py b/test/keys_only/keys_only.test.py similarity index 84% rename from test/keys_only/Keys_only.test.py rename to test/keys_only/keys_only.test.py index 6ba12d5cd..f2d3b2bac 100644 --- a/test/keys_only/Keys_only.test.py +++ b/test/keys_only/keys_only.test.py @@ -3,4 +3,4 @@ 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) \ No newline at end of file +test('Testing keys_only',keys_only_test)