Determine if a string has all unique characters: Difference between revisions

added Lua version
(Realize in F#)
(added Lua version)
Line 1,798:
XYZ ZYX 7 no 'Z' 5A 3 5
1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ 36 no '0' 30 10 25</pre>
 
=={{header|Lua}}==
Using regular expressions. The '-' in the pattern's '.-' is the "lazy" or "reluctant" repetition qualifier; the usual '.*'
would caused pattern to match, in the first example below, the substring "cocc" instead of "coc".
 
<lang lua>local find, format = string.find, string.format
local function printf(fmt, ...) print(format(fmt,...)) end
 
local pattern = '(.).-%1' -- '(.)' .. '.-' .. '%1'
 
function report_dup_char(subject)
local pos1, pos2, char = find(subject, pattern)
 
local prefix = format('"%s" (%d)', subject, #subject)
if pos1 then
local byte = char:byte()
printf("%s: '%s' (0x%02x) duplicates at %d, %d", prefix, char, byte, pos1, pos2)
else
printf("%s: no duplicates", prefix)
end
end
 
local show = report_dup_char
show('coccyx')
show('')
show('.')
show('abcABC')
show('XYZ ZYX')
show('1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ')
</lang>
 
{{out}}
<pre>"coccyx" (6): 'c' (0x63) duplicates at 1, 3
"" (0): no duplicates
"." (1): no duplicates
"abcABC" (6): no duplicates
"XYZ ZYX" (7): 'X' (0x58) duplicates at 1, 7
"1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" (36): '0' (0x30) duplicates at 10, 25</pre>
 
=={{header|Nanoquery}}==
Anonymous user