How to resolve the algorithm Textonyms step by step in the 11l programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Textonyms step by step in the 11l programming language
Table of Contents
Problem Statement
When entering text on a phone's digital pad it is possible that a particular combination of digits corresponds to more than one word. Such are called textonyms. Assuming the digit keys are mapped to letters as follows:
Write a program that finds textonyms in a list of words such as Textonyms/wordlist or unixdict.txt. The task should produce a report: Where: At your discretion show a couple of examples of your solution displaying Textonyms. E.G.:
Use a word list and keypad mapping other than English.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Textonyms step by step in the 11l programming language
Source code in the 11l programming language
[Char = String] CH2NUM
L(chars) ‘abc def ghi jkl mno pqrs tuv wxyz’.split(‘ ’)
V num = L.index + 2
L(ch) chars
CH2NUM[ch] = String(num)
F mapnum2words(words)
DefaultDict[String, [String]] number2words
V reject = 0
L(word) words
X.try
number2words[word.map(ch -> :CH2NUM[ch]).join(‘’)].append(word)
X.catch KeyError
reject++
R (number2words, reject)
V words = File(‘unixdict.txt’).read().rtrim("\n").split("\n")
print(‘Read #. words from 'unixdict.txt'’.format(words.len))
V wordset = Set(words)
V (num2words, reject) = mapnum2words(words)
F interactiveconversions()
L(inp) (‘rosetta’, ‘code’, ‘2468’, ‘3579’)
print("\nType a number or a word to get the translation and textonyms: "inp)
I all(inp.map(ch -> ch C ‘23456789’))
I inp C :num2words
print(‘ Number #. has the following textonyms in the dictionary: #.’.format(inp, (:num2words[inp]).join(‘, ’)))
E
print(‘ Number #. has no textonyms in the dictionary.’.format(inp))
E I all(inp.map(ch -> ch C :CH2NUM))
V num = inp.map(ch -> :CH2NUM[ch]).join(‘’)
print(‘ Word #. is#. in the dictionary and is number #. with textonyms: #.’.format(inp, (I inp C :wordset {‘’} E ‘n't’), num, (:num2words[num]).join(‘, ’)))
E
print(‘ I don't understand '#.'’.format(inp))
V morethan1word = sum(num2words.keys().filter(w -> :num2words[w].len > 1).map(w -> 1))
V maxwordpernum = max(num2words.values().map(values -> values.len))
print(‘
There are #. words in #. which can be represented by the Textonyms mapping.
They require #. digit combinations to represent them.
#. digit combinations represent Textonyms.’.format(words.len - reject, ‘'unixdict.txt'’, num2words.len, morethan1word))
print("\nThe numbers mapping to the most words map to #. words each:".format(maxwordpernum))
V maxwpn = sorted(num2words.filter((key, val) -> val.len == :maxwordpernum))
L(num, wrds) maxwpn
print(‘ #. maps to: #.’.format(num, wrds.join(‘, ’)))
interactiveconversions()
You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the Jsish programming language
You may also check:How to resolve the algorithm Pentagram step by step in the Wren programming language
You may also check:How to resolve the algorithm Magnanimous numbers step by step in the Julia programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the Lang programming language
You may also check:How to resolve the algorithm Arrays step by step in the Oberon-2 programming language