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

Published on 12 May 2024 09:40 PM
#Jq

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

Source code in the jq programming language

# Emit null if there is no duplicate, else [c, [ix1, ix2]]
def firstDuplicate:
  label $out 
  | foreach explode[] as $i ({ix: -1};
    .ix += 1
    | .ix as $ix
    | .iu = ([$i] | implode)
    | .[.iu] += [ $ix] ;
    if .[.iu]|length == 2 then [.iu, .[.iu]], break $out else empty end )
    // null ;

# hex of a number or a single (unicode) character
def hex:
  def stream:
    recurse(if . >= 16 then ./16|floor else empty end) | . % 16 ;
  if type=="string" then explode[0] else . end
  | [stream] | reverse
  |  map(if . < 10 then 48 + . else . + 87 end) | implode ;

def lpad($len): tostring | " " * ($len - width) + .;

def q: \(.)»";

def header:
  "\("string"|q|lpad(38)) :  |s| : C : hex  IO=0";

def data:
 "",
 ".",
 "abcABC",
 "XYZ ZYX",
 "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ",
 "😍😀🙌💃😍🙌" ;

header,
  (data
   | firstDuplicate as [$k, $v]
   | "\(q|lpad(38)) : \(length|lpad(4)) : \($k // " ") : \($k |if . then hex else "  " end)   \($v // [])" )

                              «string» :  |s| : C : hex  IO=0
                                    «» :    0 :   :      []
                                   «.» :    1 :   :      []
                              «abcABC» :    6 :   :      []
                             «XYZ ZYX» :    7 : Z : 5A   [2,4]
«1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ» :   36 : 0 : 30   [9,24]
                       «😍😀🙌💃😍🙌» :    6 : 😍:1f60d [0,4]


  

You may also check:How to resolve the algorithm Determine if only one instance is running step by step in the BASIC programming language
You may also check:How to resolve the algorithm Gapful numbers step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Amb step by step in the OpenEdge/Progress programming language
You may also check:How to resolve the algorithm File modification time step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Formatted numeric output step by step in the D programming language