How to resolve the algorithm Determine if a string has all unique characters step by step in the Tcl programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Determine if a string has all unique characters step by step in the Tcl programming language
Table of Contents
Problem Statement
Given a character string (which may be empty, or have a length of zero characters):
Use (at least) these five test values (strings):
Show all output here on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Determine if a string has all unique characters step by step in the Tcl programming language
Source code in the tcl programming language
package require Tcl 8.6 ; # For binary encode
array set yesno {1 Yes 2 No}
set test {
{}
{.}
{abcABC}
{XYZ ZYX}
{1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ}
{hétérogénéité}
}
# Loop through test strings
foreach str $test {
set chars [dict create] ; # init dictionary
set num_chars 1 ; # In case of empty string
# Loop through characters in string
for {set i 0} {$i < [string length $str]} {incr i} {
set c [string index $str $i] ; # get char at index
dict lappend chars $c $i ; # add index to a running list for key=char
set indexes [dict get $chars $c] ; # get the whole running list
set num_chars [llength $indexes] ; # count the # of indexes
if {$num_chars > 1} {
break ; # Found a duplicate, break out of the loop
}
}
# Handle Output
puts [format "Tested: %38s (len: %2d). All unique? %3s. " \
"'$str'" [string length $str] $yesno($num_chars)]
if {$num_chars > 1} {
puts [format " --> Character '%s' (hex: 0x%s) reappears at indexes: %s." \
$c [binary encode hex $c] $indexes]
}
}
You may also check:How to resolve the algorithm Attractive numbers step by step in the 11l programming language
You may also check:How to resolve the algorithm Number reversal game step by step in the Scheme programming language
You may also check:How to resolve the algorithm Interactive programming (repl) step by step in the Ursa programming language
You may also check:How to resolve the algorithm Make directory path step by step in the J programming language
You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the Nanoquery programming language