Isograms and heterograms

Revision as of 12:01, 17 June 2022 by PureFox (talk | contribs) (Created new draft task and added a Wren solution.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.

Isograms and heterograms is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Definitions

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.


Examples

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.


Task

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.


Reference


Other tasks related to string operations:
Metrics
Counting
Remove/replace
Anagrams/Derangements/shuffling
Find/Search/Determine
Formatting
Song lyrics/poems/Mad Libs/phrases
Tokenize
Sequences


Wren

Library: Wren-str

<lang ecmascript>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"))</lang>

Output:
List of n-isograms(33) where n > 1:
  aaa
  iii
  beriberi
  bilabial
  caucasus
  couscous
  teammate
  appall
  emmett
  hannah
  murmur
  tartar
  testes
  anna
  coco
  dada
  deed
  dodo
  gogo
  isis
  juju
  lulu
  mimi
  noon
  otto
  papa
  peep
  poop
  teet
  tete
  toot
  tutu
  ii

List of heterograms(32) of length > 10:
  ambidextrous
  bluestocking
  exclusionary
  incomputable
  lexicography
  loudspeaking
  malnourished
  atmospheric
  blameworthy
  centrifugal
  christendom
  consumptive
  countervail
  countryside
  countrywide
  disturbance
  documentary
  earthmoving
  exculpatory
  geophysical
  inscrutable
  misanthrope
  problematic
  selfadjoint
  stenography
  sulfonamide
  switchblade
  switchboard
  switzerland
  thunderclap
  valedictory
  voluntarism