Determine if a string has all unique characters: Difference between revisions
Content added Content deleted
Drkameleon (talk | contribs) |
ReeceGoding (talk | contribs) (Added R.) |
||
Line 3,039: | Line 3,039: | ||
"1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" (36): '0' (0x30) duplicates at 9, 24 |
"1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" (36): '0' (0x30) duplicates at 9, 24 |
||
</pre> |
</pre> |
||
=={{header|R}}== |
|||
Most of this is adapted from [[Determine if a string has all the same characters#R]]. |
|||
<lang r>isAllUnique<-function(string) |
|||
{ |
|||
strLength<-nchar(string) |
|||
if(length(strLength)>1) |
|||
{ |
|||
#R has a distinction between the length of a string and that of a character vector. It is a common source |
|||
#of problems when coming from another language. We will try to avoid the topic here. |
|||
#For our purposes, let us only say that there is a good reason why we have made |
|||
#isAllUnique(c("foo", "bar") immediately throw an error. |
|||
stop("This task is intended for character vectors with lengths of at most 1.") |
|||
} |
|||
else if(length(strLength)==0) |
|||
{ |
|||
cat("Examining a character vector of length 0.", |
|||
"It is therefore made entirely of unique characters.\n") |
|||
TRUE |
|||
} |
|||
else if(strLength==0) |
|||
{ |
|||
cat("Examining a character vector of length 1, containing an empty string.", |
|||
"It is therefore made entirely of unique characters.\n") |
|||
TRUE |
|||
} |
|||
else if(strLength==1) |
|||
{ |
|||
cat("Examining the string", paste0(sQuote(string), ","), |
|||
"which is of length", paste0(strLength, "."), |
|||
"It is therefore made entirely of unique characters.\n") |
|||
TRUE |
|||
} |
|||
else |
|||
{ |
|||
cat("Examining the string", paste0(sQuote(string), ","), |
|||
"which is of length", paste0(strLength, ":"), "\n") |
|||
#strsplit outputs a list. Its first element is the vector of characters that we desire. |
|||
characters<-strsplit(string, "")[[1]] |
|||
#Our use of match is using R's vector recycling rules. Element i is being checked |
|||
#against every other. |
|||
indexesOfDuplicates<-sapply(seq_len(strLength), function(i) match(TRUE, characters[i]==characters[-i], nomatch = -1)) + 1 |
|||
firstDuplicateElementIndex<-indexesOfDuplicates[indexesOfDuplicates != 0][1] |
|||
if(is.na(firstDuplicateElementIndex)) |
|||
{ |
|||
cat("It has no duplicates. It is therefore made entirely of unique characters.\n") |
|||
TRUE |
|||
} |
|||
else |
|||
{ |
|||
cat("It has duplicates. ") |
|||
firstDuplicatedCharacter<-characters[firstDuplicateElementIndex] |
|||
cat(sQuote(firstDuplicatedCharacter), "is the first duplicated character. It has hex value", |
|||
sprintf("0x%X", as.integer(charToRaw(firstDuplicatedCharacter))), |
|||
"and is at index", paste0(firstDuplicateElementIndex, "."), |
|||
"\nThis is a duplicate of the character at index", |
|||
paste0(match(firstDuplicateElementIndex, indexesOfDuplicates), "."), "\n") |
|||
FALSE |
|||
} |
|||
} |
|||
} |
|||
#Tests: |
|||
cat("Test: A string of length 0 (an empty string):\n") |
|||
cat("Test 1 of 2: An empty character vector:\n") |
|||
print(isAllUnique(character(0))) |
|||
cat("Test 2 of 2: A character vector containing the empty string:\n") |
|||
print(isAllUnique("")) |
|||
cat("Test: A string of length 1 which contains .:\n") |
|||
print(isAllUnique(".")) |
|||
cat("Test: A string of length 6 which contains abcABC:\n") |
|||
print(isAllUnique("abcABC")) |
|||
cat("Test: A string of length 7 which contains XYZ ZYX:\n") |
|||
print(isAllUnique("XYZ ZYX")) |
|||
cat("Test: A string of length 36 doesn't contain the letter 'oh':\n") |
|||
print(isAllUnique("1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ"))</lang> |
|||
{{out}} |
|||
<pre>Test: A string of length 0 (an empty string): |
|||
Test 1 of 2: An empty character vector: |
|||
Examining a character vector of length 0. It is therefore made entirely of unique characters. |
|||
[1] TRUE |
|||
Test 2 of 2: A character vector containing the empty string: |
|||
Examining a character vector of length 1, containing an empty string. It is therefore made entirely of unique characters. |
|||
[1] TRUE |
|||
Test: A string of length 1 which contains .: |
|||
Examining the string ‘.’, which is of length 1. It is therefore made entirely of unique characters. |
|||
[1] TRUE |
|||
Test: A string of length 6 which contains abcABC: |
|||
Examining the string ‘abcABC’, which is of length 6: |
|||
It has no duplicates. It is therefore made entirely of unique characters. |
|||
[1] TRUE |
|||
Test: A string of length 7 which contains XYZ ZYX: |
|||
Examining the string ‘XYZ ZYX’, which is of length 7: |
|||
It has duplicates. ‘X’ is the first duplicated character. It has hex value 0x58 and is at index 7. |
|||
This is a duplicate of the character at index 1. |
|||
[1] FALSE |
|||
Test: A string of length 36 doesn't contain the letter 'oh': |
|||
Examining the string ‘1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ’, which is of length 36: |
|||
It has duplicates. ‘0’ is the first duplicated character. It has hex value 0x30 and is at index 25. |
|||
This is a duplicate of the character at index 10. |
|||
[1] FALSE</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |