Determine if a string has all unique characters: Difference between revisions
(created a new (draft) task, added the computer programming language REXX.) Β |
Thundergnat (talk | contribs) (ββ{{header|Perl 6}}: Add a Perl 6 example) |
||
Line 34: | Line 34: | ||
Show all output here on this page. |
Show all output here on this page. |
||
<br><br> |
<br><br> |
||
=={{header|Perl 6}}== |
|||
{{works with|Rakudo|2019.07.1}} |
|||
<lang perl6> -> $str { |
|||
if my $match = $str.match( / (.).*?$0 /, :exhaustive ) { |
|||
say "\n{$str.perl} has duplicated characters:"; |
|||
say "'{.values.Str}' ({.values.Str.uniname}; hex ordinal: {(.values.Str.ord).fmt: "0x%X"})" ~ |
|||
" in positions: {join ', ', flat 1 + .from, .pos}" for $match.list |
|||
} else { |
|||
say "\n{$str.perl} has no duplicated characters." |
|||
} |
|||
} for flat |
|||
'', |
|||
'.', |
|||
'abcABC', |
|||
'XYZ ZYX', |
|||
'1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ', |
|||
'π¦πππ¦π'</lang> |
|||
{{out}} |
|||
<pre>"" has no duplicated characters. |
|||
"." has no duplicated characters. |
|||
"abcABC" has no duplicated characters. |
|||
"XYZ ZYX" has duplicated characters: |
|||
'X' (LATIN CAPITAL LETTER X; hex ordinal: 0x58) in positions: 1, 7 |
|||
'Y' (LATIN CAPITAL LETTER Y; hex ordinal: 0x59) in positions: 2, 6 |
|||
'Z' (LATIN CAPITAL LETTER Z; hex ordinal: 0x5A) in positions: 3, 5 |
|||
"1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" has duplicated characters: |
|||
'0' (DIGIT ZERO; hex ordinal: 0x30) in positions: 10, 25 |
|||
"π¦πππ¦π" has duplicated characters: |
|||
'π¦' (BUTTERFLY; hex ordinal: 0x1F98B) in positions: 1, 4</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
Revision as of 00:11, 30 October 2019
- Task
Given a character string (which may be empty, or have a length of zero characters):
- create a function/procedure/routine to:
- determine if all the characters in the string are unique
- indicate if or which character is duplicated and where
- display each string and it's length (as the strings are being examined)
- a zeroβlength (empty) string shall be considered as unique
- process the strings from leftβtoβright
- if unique, display a message saying such
- if not unique, then:
- display a message saying such
- display what character is duplicated
- only the 1st nonβunique character need be displayed
- display where "both" duplicated characters are in the string
- the above messages can be part of a single message
- display the hexadecimal value of the duplicated character
Use (at least) these five test values (strings):
- a string of length 0 (an empty string)
- a string of length 1 which is a single period (.)
- a string of length 6 which contains: abcABC
- a string of length 7 which contains a blank in the middle: XYZ ZYX
- a string of length 36 which doesn't contain the letter "oh":
- 1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ
Show all output here on this page.
Perl 6
<lang perl6> -> $str {
if my $match = $str.match( / (.).*?$0 /, :exhaustive ) { say "\n{$str.perl} has duplicated characters:"; say "'{.values.Str}' ({.values.Str.uniname}; hex ordinal: {(.values.Str.ord).fmt: "0x%X"})" ~ " in positions: {join ', ', flat 1 + .from, .pos}" for $match.list } else { say "\n{$str.perl} has no duplicated characters." }
} for flat
, '.', 'abcABC', 'XYZ ZYX', '1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ', 'π¦πππ¦π'</lang>
- Output:
"" has no duplicated characters. "." has no duplicated characters. "abcABC" has no duplicated characters. "XYZ ZYX" has duplicated characters: 'X' (LATIN CAPITAL LETTER X; hex ordinal: 0x58) in positions: 1, 7 'Y' (LATIN CAPITAL LETTER Y; hex ordinal: 0x59) in positions: 2, 6 'Z' (LATIN CAPITAL LETTER Z; hex ordinal: 0x5A) in positions: 3, 5 "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" has duplicated characters: '0' (DIGIT ZERO; hex ordinal: 0x30) in positions: 10, 25 "π¦πππ¦π" has duplicated characters: 'π¦' (BUTTERFLY; hex ordinal: 0x1F98B) in positions: 1, 4
REXX
<lang rexx>/*REXX pgm determines if a string is comprised of all unique characters (no duplicates).*/ @.= /*assign a default for the @. array. */ parse arg @.1 /*obtain optional argument from the CL.*/ if @.1= then do; @.1= /*Not specified? Then assume defaults.*/
@.2= . @.3= 'abcABC' @.4= 'XYZ ZYX' @.5= '1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ' end
do j=1; if j\==1 & @.j== then leave /*String is null & not j=1? We're done*/ say copies('β', 79) /*display a separator line (a fence). */ say 'Testing for the string (length' length(@.j)"): " @.j say dup= isUnique(@.j) say 'The characters in the string' word("are aren't", 1 + (dup>0) ) 'all unique.' if dup==0 then iterate ?= substr(@.j, dup, 1) say 'The character ' ? " ('"c2x(?)"'x) at position " dup , ' is repeated at position ' pos(?, @.j, dup+1) end /*j*/
exit /*stick a fork in it, we're all done. */ /*ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ*/ isUnique: procedure; parse arg x /*obtain the character string.*/
do k=1 to length(x) - 1 /*examine all but the last. */ p= pos( substr(x, k, 1), x, k + 1) /*see if the Kth char is a dup*/ if p\==0 then return k /*Find a dup? Return location.*/ end /*k*/ return 0 /*indicate all chars unique. */</lang>
- output when using the internal defaults
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ Testing for the string (length 0): The characters in the string are all unique. βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ Testing for the string (length 1): . The characters in the string are all unique. βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ Testing for the string (length 6): abcABC The characters in the string are all unique. βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ Testing for the string (length 7): XYZ ZYX The characters in the string aren't all unique. The character X ('58'x) at position 1 is repeated at position 7 βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ Testing for the string (length 36): 1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ The characters in the string aren't all unique. The character 0 ('30'x) at position 10 is repeated at position 25