How to resolve the algorithm Compare length of two strings step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Compare length of two strings step by step in the Wren programming language

Table of Contents

Problem Statement

Given two strings of different length, determine which string is longer or shorter. Print both strings and their length, one on each line. Print the longer one first. Measure the length of your string in terms of bytes or characters, as appropriate for your language. If your language doesn't have an operator for measuring the length of a string, note it. Given more than two strings: list = ["abcd","123456789","abcdef","1234567"] Show the strings in descending length order.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Compare length of two strings step by step in the Wren programming language

Source code in the wren programming language

import "./upc" for Graphemes

var printCounts = Fn.new { |s1, s2, c1, c2|
   var l1 = (c1 > c2) ? [s1, c1] : [s2, c2]
   var l2 = (c1 > c2) ? [s2, c2] : [s1, c1]
   System.print(  "%(l1[0]) : length %(l1[1])")
   System.print(  "%(l2[0]) : length %(l2[1])\n")
}

var codepointCounts = Fn.new { |s1, s2|
   var c1 = s1.count
   var c2 = s2.count
   System.print("Comparison by codepoints:")
   printCounts.call(s1, s2, c1, c2)
}

var byteCounts = Fn.new { |s1, s2|
   var c1 = s1.bytes.count
   var c2 = s2.bytes.count
   System.print("Comparison by bytes:")
   printCounts.call(s1, s2, c1, c2)
}

var graphemeCounts = Fn.new { |s1, s2|
   var c1 = Graphemes.clusterCount(s1)
   var c2 = Graphemes.clusterCount(s2)
   System.print("Comparison by grapheme clusters:")
   printCounts.call(s1, s2, c1, c2)
}

for (pair in [ ["nino", "niño"], ["👨‍👩‍👧‍👦", "🤔🇺🇸"] ]) {
    codepointCounts.call(pair[0], pair[1])
    byteCounts.call(pair[0], pair[1])
    graphemeCounts.call(pair[0], pair[1])
}

var list = ["abcd", "123456789", "abcdef", "1234567"]
System.write("Sorting in descending order by length in codepoints:\n%(list) -> ")
list.sort { |a, b| a.count > b.count }
System.print(list)


  

You may also check:How to resolve the algorithm Trigonometric functions step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the DWScript programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the Dart programming language
You may also check:How to resolve the algorithm Synchronous concurrency step by step in the F# programming language
You may also check:How to resolve the algorithm Solve the no connection puzzle step by step in the C++ programming language