How to resolve the algorithm Determine if a string has all unique characters step by step in the Mathematica / Wolfram Language programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Determine if a string has all unique characters step by step in the Mathematica / Wolfram Language 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 Mathematica / Wolfram Language programming language

The Wolfram Programming Language (formerly Mathematica) is a powerful computer algebra system that can be used for a wide variety of tasks, including symbolic computation, numerical computation, data analysis, and visualization.

The UniqueCharacters function takes a string as input and prints out whether or not all of the characters in the string are unique. If there are any repeated characters, the function prints out the character and the positions at which it is repeated.

Here is a breakdown of the code:

ClearAll[UniqueCharacters]

This line clears all previously defined values for the UniqueCharacters function. This is done to ensure that the function will always start with a clean slate, regardless of what other code has been run sebelumnya.

UniqueCharacters[s_String] := Module[{c, len, good = True},

This line defines the UniqueCharacters function. The function takes one argument, s, which must be a string. The Module construct creates a new scope for the variables c, len, and good.

c = Characters[s];

This line converts the string s into a list of characters. The Characters function takes a string as input and returns a list of the individual characters in the string.

len = Length[c];

This line calculates the length of the list of characters.

Print[s, " with length ", len];

This line prints out the string s and its length.

Do[
If[c[[i]] == c[[j]],
 Print["Character ", c[[i]], " is repeated at positions ", i, 
  " and ", j];
 good = False
 ]
,
{i, len - 1},
{j, i + 1, len}
];

This loop iterates over the list of characters and checks for repeated characters. The loop index i ranges from 1 to len - 1, and the loop index j ranges from i + 1 to len. If the character at position i is equal to the character at position j, then the function prints out the character and the positions at which it is repeated. The good variable is set to False to indicate that there are repeated characters in the string.

If[good,
Print["No repeats"];
True
,
False
]

After the loop has finished, the good variable is checked. If good is True, then there are no repeated characters in the string and the function prints out "No repeats". If good is False, then there are repeated characters in the string and the function returns False.

UniqueCharacters[""]

This line calls the UniqueCharacters function with an empty string as input. The function prints out "No repeats" because there are no characters in the string to repeat.

UniqueCharacters["."]

This line calls the UniqueCharacters function with a string containing a single period as input. The function prints out "No repeats" because there are no repeated characters in the string.

UniqueCharacters["abcABC"]

This line calls the UniqueCharacters function with a string containing the letters "abc" in both upper and lower case. The function prints out the following:

abcABC with length 6
Character a is repeated at positions 1 and 4
Character b is repeated at positions 2 and 5
Character c is repeated at positions 3 and 6

The function identifies three repeated characters: "a", "b", and "c".

UniqueCharacters["XYZ ZYX"]

This line calls the UniqueCharacters function with a string containing the letters "XYZ" in both upper and lower case. The function prints out the following:

XYZ ZYX with length 6
Character X is repeated at positions 1 and 4
Character Y is repeated at positions 2 and 5
Character Z is repeated at positions 3 and 6

The function identifies three repeated characters: "X", "Y", and "Z".

UniqueCharacters["1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ"]

This line calls the UniqueCharacters function with a string containing all of the digits and uppercase letters. The function prints out "No repeats" because there are no repeated characters in the string.

Source code in the wolfram programming language

ClearAll[UniqueCharacters]
UniqueCharacters[s_String] := Module[{c, len, good = True},
  c = Characters[s];
  len = Length[c];
  Print[s, " with length ", len];
  Do[
   If[c[[i]] == c[[j]],
    Print["Character ", c[[i]], " is repeated at positions ", i, 
     " and ", j];
    good = False
    ]
   ,
   {i, len - 1},
   {j, i + 1, len}
   ];
  If[good,
   Print["No repeats"];
   True
   ,
   False
   ]
  ]
UniqueCharacters[""]
UniqueCharacters["."]
UniqueCharacters["abcABC"]
UniqueCharacters["XYZ ZYX"]
UniqueCharacters["1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ"]


  

You may also check:How to resolve the algorithm Arbitrary-precision integers (included) step by step in the GAP programming language
You may also check:How to resolve the algorithm Least common multiple step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Verify distribution uniformity/Chi-squared test step by step in the Racket programming language
You may also check:How to resolve the algorithm Guess the number/With feedback step by step in the Sidef programming language
You may also check:How to resolve the algorithm Empty program step by step in the Bracmat programming language