Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set-operations concept #266

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions concepts/set-operations/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"authors": ["colinleach"],
"contributors": [],
"blurb": "Core R provides several functions that perform set-like operations on vectors."
}
39 changes: 39 additions & 0 deletions concepts/set-operations/about.md
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 13 additions & 0 deletions concepts/set-operations/introduction.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions concepts/set-operations/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"url": "https://search.r-project.org/CRAN/refmans/generics/html/setops.html",
"description": "Set Operations"
}
]
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,11 @@
"slug": "basics",
"name": "Basics"
},
{
"uuid": "85db3d8c-dfec-424c-9682-caa8611db8f8",
"slug": "set-operations",
"name": "Set Operations"
},
{
"uuid": "2751b6f2-7d71-4397-b063-9bf927a57756",
"slug": "booleans",
Expand Down