How to resolve the algorithm Semordnilap step by step in the Nanoquery programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Semordnilap step by step in the Nanoquery programming language

Table of Contents

Problem Statement

A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal

This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap pairs, and print 5 examples. Two matching semordnilaps, such as lager and regal, should be counted as one unique pair. (Note that the word "semordnilap" is not in the above dictionary.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Semordnilap step by step in the Nanoquery programming language

Source code in the nanoquery programming language

import Nanoquery.IO

def reverse_str(string)
        ret = ""

        for char in list(string).reverse()
                ret += char
        end

        return ret
end

lst = split(new(File).open("rosetta-code/unixdict.txt").readAll(), "\n")
seen = list()
count = 0
for w in lst
        w = lower(w)
        r = reverse_str(w)
        if r in seen
                count += 1
                if count <= 5
                        print format("%-10s %-10s\n", w, r)
                end
        else
                seen.append(w)
        end
end

println "\nSemordnilap pairs found: " + count

  

You may also check:How to resolve the algorithm Word frequency step by step in the Crystal programming language
You may also check:How to resolve the algorithm Disarium numbers step by step in the Java programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Harbour programming language
You may also check:How to resolve the algorithm Sierpinski carpet step by step in the Swift programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the AdvPL programming language