How to resolve the algorithm Isograms and heterograms step by step in the Julia programming language
How to resolve the algorithm Isograms and heterograms step by step in the Julia 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:
-
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.
-
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 Julia programming language
This code takes a list of words from a text file and analyzes them for two criteria: n-isograms and heterograms. An n-isogram is a word where each letter occurs exactly n times. A heterogram is a word with no repeating letters.
The code first reads the words from the text file and converts them to lowercase. It then splits the words on whitespace and collects them into an array.
The isogram
function takes a word as input and determines if it is an n-isogram. It first collects the unique characters in the word and checks if the length of the unique characters is equal to the length of the word. If it is, then the word is an n-isogram and the function returns 1.
If the word is not an n-isogram, the function counts the number of times the first unique character appears in the word. It then checks if the number of times each other unique character appears in the word is equal to the number of times the first unique character appears. If it is, then the word is an n-isogram and the function returns the number of times the first unique character appears. Otherwise, the function returns 0.
The orderlengthtuples
array is created by combining the results of the isogram
function with the length of each word. The array is then sorted by the tcomp
function, which sorts the tuples by the isogram
value, the length of the word, and the word itself.
The nisograms
array is created by filtering the orderlengthtuples
array to include only the words that are n-isograms with an n-value greater than 1. The array is then sorted by the tcomp
function.
The heterograms
array is created by filtering the orderlengthtuples
array to include only the words that are heterograms and have a length greater than 10. The array is then sorted by the tcomp
function.
The code then prints the results of the analysis to the console.
Source code in the julia programming language
function isogram(word)
wchars, uchars = collect(word), unique(collect(word))
ulen, wlen = length(uchars), length(wchars)
(wlen == 1 || ulen == wlen) && return 1
n = count(==(first(uchars)), wchars)
return all(i -> count(==(uchars[i]), wchars) == n, 2:ulen) ? n : 0
end
words = split(lowercase(read("documents/julia/unixdict.txt", String)), r"\s+")
orderlengthtuples = [(isogram(w), length(w), w) for w in words]
tcomp(x, y) = (x[1] != y[1] ? y[1] < x[1] : x[2] != y[2] ? y[2] < x[2] : x[3] < y[3])
nisograms = sort!(filter(t -> t[1] > 1, orderlengthtuples), lt = tcomp)
heterograms = sort!(filter(t -> t[1] == 1 && length(t[3]) > 10, orderlengthtuples), lt = tcomp)
println("N-Isogram N Length\n", "-"^24)
foreach(t -> println(rpad(t[3], 8), lpad(t[1], 5), lpad(t[2], 5)), nisograms)
println("\nHeterogram Length\n", "-"^20)
foreach(t -> println(rpad(t[3], 12), lpad(t[2], 5)), heterograms)
You may also check:How to resolve the algorithm Disarium numbers step by step in the jq programming language
You may also check:How to resolve the algorithm Sleep step by step in the Perl programming language
You may also check:How to resolve the algorithm Keyboard input/Keypress check step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Random numbers step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Character codes step by step in the Objeck programming language