How to resolve the algorithm Determine if a string has all the same characters step by step in the Nanoquery programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Determine if a string has all the same characters step by step in the Nanoquery 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 seven 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 the same characters step by step in the Nanoquery programming language

Source code in the nanoquery programming language

def analyze(s)
    s = str(s)
    println "Examining [" + s + "] which has a length of " + str(len(s)) + ":"

    if len(s) < 2
        println "\tAll characters in the string are the same."
        return
    end

    for i in range(0, len(s) - 2)
        if s[i] != s[i + 1]
            println "\tNot all characters in the string are the same."
            println "\t'" + s[i + 1] + "' " + format("(0x%x)", ord(s[i + 1])) +\
                    " is different at position " + str(i + 2)
            return
        end
    end

    println "\tAll characters in the string are the same."
end

tests = {"", "   ", "2", "333", ".55", "tttTTT", "444 444k"}
for s in tests
   analyze(s)
end

  

You may also check:How to resolve the algorithm Largest proper divisor of n step by step in the MAD programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the Déjà Vu programming language
You may also check:How to resolve the algorithm Quickselect algorithm step by step in the Ruby programming language
You may also check:How to resolve the algorithm Guess the number/With feedback step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Reverse words in a string step by step in the Ksh programming language