Jump to content

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

RPL: add section
(→‎{{header|jq}}: simplify)
(RPL: add section)
Line 3,655:
Input = '1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ', length = 36
First duplicate at positions 10 and 25, character = '0'
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ → string
≪ "All chars unique" ""
1 string SIZE '''FOR''' j
string j DUP SUB
'''IF''' DUP2 POS
'''THEN''' DUP " duplicated at " +
string ROT POS →STR + " and " + j →STR +
ROT DROP SWAP
string SIZE 'j' STO
'''ELSE''' + '''END NEXT'''
DROP
≫ ≫ ''''UNICH?'''' STO
|
'''UNICH?''' ''( "string" -- "report" )''
initialize stack
scan string
extract jth character
if already seen
generate report
.
.
exit loop
else add the char to already seen list
clean stack
.
|}
{{in}}
<pre>
≪ { "" "." "abcABC" "XYZ ZYX" "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" } → cases
≪ 1 cases SIZE FOR n cases n GET UNICH? NEXT
≫ ≫ ´TASK’ STO
</pre>
{{out}}
<pre>
5: "All chars unique"
4: "All chars unique"
3: "All chars unique"
2: "Z duplicated at 3 and 5"
1: "0 duplicated at 10 and 25"
</pre>
 
1,151

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.