How to resolve the algorithm Isograms and heterograms step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Isograms and heterograms step by step in the Wren programming language

Table of Contents

Problem Statement

For the purposes of this task, an isogram means a string where each character present is used the same number of times and an n-isogram means an isogram where each character present is used exactly n times. A heterogram means a string in which no character occurs more than once. It follows that a heterogram is the same thing as a 1-isogram.

caucasus is a 2-isogram because the letters c, a, u and s all occur twice. atmospheric is a heterogram because all its letters are used once only.

Using unixdict.txt and ignoring capitalization:

  1. Find and display here all words which are n-isograms where n > 1. Present the results as a single list but sorted as follows: a. By decreasing order of n; b. Then by decreasing order of word length; c. Then by ascending lexicographic order.

  2. Secondly, find and display here all words which are heterograms and have more than 10 characters. Again present the results as a single list but sorted as per b. and c. above.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Isograms and heterograms step by step in the Wren programming language

Source code in the wren programming language

import "io" for File
import "./str" for Str

var isogram = Fn.new { |word|
    if (word.count == 1) return 1
    var map = {}
    word = Str.lower(word)
    for (c in word) {
        if (map.containsKey(c)) {
            map[c] = map[c] + 1
        } else {
            map[c] = 1
        }
    }
    var chars = map.keys.toList
    var n = map[chars[0]]
    var iso = chars[1..-1].all { |c| map[c] == n }
    return iso ? n : 0
}

var isoComparer = Fn.new { |i, j|
    if (i[1] != j[1]) return i[1] > j[1]
    if (i[0].count != j[0].count) return i[0].count > j[0].count
    return Str.le(i[0], j[0])
}

var heteroComparer = Fn.new { |i, j|
    if (i[0].count != j[0].count) return i[0].count > j[0].count
    return Str.le(i[0], j[0])
}

var wordList = "unixdict.txt" // local copy
var words = File.read(wordList)
                .trimEnd()
                .split("\n")
                .map { |word| [word, isogram.call(word)] }

var isograms = words.where { |t| t[1] > 1 }
                    .toList
                    .sort(isoComparer)
                    .map { |t| "  " + t[0] }
                    .toList
System.print("List of n-isograms(%(isograms.count)) where n > 1:")
System.print(isograms.join("\n"))

var heterograms = words.where { |t| t[1] == 1 && t[0].count > 10 }
                       .toList
                       .sort(heteroComparer)
                       .map { |t| "  " + t[0] }
                       .toList
System.print("\nList of heterograms(%(heterograms.count)) of length > 10:")
System.print(heterograms.join("\n"))


  

You may also check:How to resolve the algorithm Pythagorean triples step by step in the OCaml programming language
You may also check:How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the PL/SQL programming language
You may also check:How to resolve the algorithm Ethiopian multiplication step by step in the AutoIt programming language
You may also check:How to resolve the algorithm Soundex step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Variadic function step by step in the Python programming language