From aff5cbd546f68d6c400cdf042d20d295a695741e Mon Sep 17 00:00:00 2001 From: colinleach Date: Sat, 8 Jul 2023 15:18:14 -0700 Subject: [PATCH 1/2] set-operations concept --- concepts/set-operations/.meta/config.json | 5 +++ concepts/set-operations/about.md | 39 +++++++++++++++++++++++ concepts/set-operations/introduction.md | 13 ++++++++ concepts/set-operations/links.json | 6 ++++ config.json | 5 +++ 5 files changed, 68 insertions(+) create mode 100644 concepts/set-operations/.meta/config.json create mode 100644 concepts/set-operations/about.md create mode 100644 concepts/set-operations/introduction.md create mode 100644 concepts/set-operations/links.json diff --git a/concepts/set-operations/.meta/config.json b/concepts/set-operations/.meta/config.json new file mode 100644 index 00000000..15b9f30e --- /dev/null +++ b/concepts/set-operations/.meta/config.json @@ -0,0 +1,5 @@ +{ + "authors": ["colinleach"], + "contributors": [], + "blurb": "Core R provides several functions that perform set-like operations on vectors." +} \ No newline at end of file diff --git a/concepts/set-operations/about.md b/concepts/set-operations/about.md new file mode 100644 index 00000000..8c37efa9 --- /dev/null +++ b/concepts/set-operations/about.md @@ -0,0 +1,39 @@ +# Introduction + +R has no separate Set datatype, instead using a variety of functions to perform similar operations on vectors. + +We have already seen `%in%` to test for set membership: + +```R +2 %in% 1:10 # TRUE +12 %in% 1:10 # FALSE +``` + +Relevant functions include `unique` to remove duplicates, plus `union()`, `intersect()` and `setdiff()` to operate on pairs of sets. + +```R +> set_1 <- c("a", "b", "c", "b", "a") +> unique(set_1) # deduplicate +[1] "a" "b" "c" + +> set_2 <- c('a', "c", "d") +> union(set_1, set_2) # values in either set +[1] "a" "b" "c" "d" + +> intersect(set_1, set_2) # values in both sets +[1] "a" "c" + +> setdiff(set_1, set_2) # values in set_1 but not set_2 +[1] "b" + +> setdiff(set_2, set_1) +[1] "d" +``` + +## The `hashtable` package + +The vector operations described above are part of core R. +These are useful for small problems but become slow for large sets. + +The `hashtable` package provides implementations of `hashmap` and `hashset` which scale better. +Like most external packages, this is not available within Exercism, but is worth investigating for real-world problems. diff --git a/concepts/set-operations/introduction.md b/concepts/set-operations/introduction.md new file mode 100644 index 00000000..6b59684a --- /dev/null +++ b/concepts/set-operations/introduction.md @@ -0,0 +1,13 @@ +# Introduction + +R has no separate Set datatype, instead using a variety of functions to perform similar operations on vectors. + +We have already seen `%in%` to test for set membership: + +```R +2 %in% 1:10 # TRUE +12 %in% 1:10 # FALSE +``` + +Relevant functions include `unique` (to remove duplicates), `union()`, `intersect()` and `setdiff()` to operate on pairs of sets. +Details are available online. diff --git a/concepts/set-operations/links.json b/concepts/set-operations/links.json new file mode 100644 index 00000000..88ed67cd --- /dev/null +++ b/concepts/set-operations/links.json @@ -0,0 +1,6 @@ +[ + { + "url": "https://search.r-project.org/CRAN/refmans/generics/html/setops.html", + "description": "Set Operations" + } + ] \ No newline at end of file diff --git a/config.json b/config.json index 312e341d..9bd21030 100644 --- a/config.json +++ b/config.json @@ -540,6 +540,11 @@ "uuid": "d75c1a77-9733-45b0-ae21-2b4f0f313ef4", "slug": "basics", "name": "Basics" + }, + { + "uuid": "85db3d8c-dfec-424c-9682-caa8611db8f8", + "slug": "set-operations", + "name": "Set Operations" } ], "key_features": [ From 3a957792e77e454e1ac0564a3e924260c5294dd4 Mon Sep 17 00:00:00 2001 From: colinleach Date: Sat, 8 Jul 2023 15:37:50 -0700 Subject: [PATCH 2/2] small fixes for setoperations --- concepts/set-operations/.meta/config.json | 2 +- concepts/set-operations/links.json | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/concepts/set-operations/.meta/config.json b/concepts/set-operations/.meta/config.json index 15b9f30e..d9226c05 100644 --- a/concepts/set-operations/.meta/config.json +++ b/concepts/set-operations/.meta/config.json @@ -2,4 +2,4 @@ "authors": ["colinleach"], "contributors": [], "blurb": "Core R provides several functions that perform set-like operations on vectors." -} \ No newline at end of file +} diff --git a/concepts/set-operations/links.json b/concepts/set-operations/links.json index 88ed67cd..24e5d70f 100644 --- a/concepts/set-operations/links.json +++ b/concepts/set-operations/links.json @@ -1,6 +1,6 @@ [ - { - "url": "https://search.r-project.org/CRAN/refmans/generics/html/setops.html", - "description": "Set Operations" - } - ] \ No newline at end of file + { + "url": "https://search.r-project.org/CRAN/refmans/generics/html/setops.html", + "description": "Set Operations" + } +]