Determine if a string has all unique characters: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) (ββ{{header|Perl}}: works better with Unicode combining characters) |
(julia example) |
||
Line 160: | Line 160: | ||
'π‘' (0x1f421) is duplicated at positions 3 and 8. |
'π‘' (0x1f421) is duplicated at positions 3 and 8. |
||
</pre> |
</pre> |
||
=={{header|Julia}}== |
|||
<lang julia>arr(s) = [c for c in s] |
|||
alldup(a) = filter(x -> length(x) > 1, [findall(x -> x == a[i], a) for i in 1:length(a)]) |
|||
firstduplicate(s) = (a = arr(s); d = alldup(a); isempty(d) ? nothing : first(d)) |
|||
function testfunction(strings) |
|||
println("String | Length | All Unique | First Duplicate | Positions\n" * |
|||
"-------------------------------------------------------------------------------------") |
|||
for s in strings |
|||
n = firstduplicate(s) |
|||
a = arr(s) |
|||
println(rpad(s, 38), rpad(length(s), 11), n == nothing ? "yes" : |
|||
rpad("no $(a[n[1]])", 26) * rpad(n[1], 4) * "$(n[2])") |
|||
end |
|||
end |
|||
testfunction([ |
|||
"", |
|||
".", |
|||
"abcABC", |
|||
"XYZ ZYX", |
|||
"1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ", |
|||
"hΓ©tΓ©rogΓ©nΓ©itΓ©", |
|||
"ππππ", |
|||
"ππππππ", |
|||
"π ππ‘π¦π¬π³ππ‘", |
|||
]) |
|||
</lang>{{out}} |
|||
<pre> |
|||
String | Length | All Unique | First Duplicate | Positions |
|||
------------------------------------------------------------------------------------- |
|||
0 yes |
|||
. 1 yes |
|||
abcABC 6 yes |
|||
XYZ ZYX 7 no X 1 7 |
|||
1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ 36 no 0 10 25 |
|||
hΓ©tΓ©rogΓ©nΓ©itΓ© 13 no Γ© 2 4 |
|||
ππππ 4 yes |
|||
ππππππ 6 no π 1 5 |
|||
π ππ‘π¦π¬π³ππ‘ 8 no π‘ 3 8 |
|||
</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |