How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Julia programming language

Table of Contents

Problem Statement

Two or more words are said to be anagrams if they have the same characters, but in a different order. By analogy with derangements we define a deranged anagram as two words with the same characters, but in which the same character does not appear in the same position in both words. Use the word list at unixdict to find and display the longest deranged anagram.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Julia programming language

1. Custom Less-Than Operator:

  • The Base.isless function is redefined to provide a custom comparison for any two vectors with the same type.
  • It performs lexicographic comparison, comparing elements one by one, and if they are equal, it proceeds to the next comparison.
  • For vectors of characters (strings), this function compares the characters in the strings.

2. Sorting Function for Strings:

  • The sortchars function is created to sort a string by its characters in lexicographic order.
  • It converts the string into a vector of characters, sorts the vector, and then reconstructs the string.

3. Custom Comparator for Sorting Anagrams:

  • The sortanagr function is defined as a comparator for sorting words.
  • It first compares the lengths of the words, and if they are equal, it compares the sorted characters of the words.
  • This ensures that words with the same characters (anagrams) are placed next to each other in the sorted list.

4. Deranged Anagram Test:

  • The deranged function checks if two strings are deranged anagrams.
  • It iterates through the characters of the two strings and checks if they are not equal. If any pair of characters is equal, the strings are not deranged anagrams.
  • If the strings are deranged, it checks if they have the same characters (i.e., are anagrams).

5. Task Implementation:

  • The wordlist is loaded line by line and sorted using the sort function with the rev and lt (less-than) arguments.
  • rev ensures that longer words come first, and lt uses the custom sortanagr comparator to group anagrams together.
  • The program iterates through the sorted wordlist and checks if neighboring words are deranged anagrams.
  • The first pair of deranged anagrams is guaranteed to be the longest due to the custom sorting.

Source code in the julia programming language

using Base.isless
# Let's define the less than operator for any two vectors that have the same type:
# This does lexicographic comparison, we use it on vectors of chars in this task.
function Base.isless(t1, t2)
    for (a, b) in zip(t1, t2) # zip only to the shorter length
        if !isequal(a, b)
            return isless(a, b)
        end
    end
    return length(t1) < length(t2)
end

# The sort function of Julia doesn't work on strings, so we write one:
# This returns a sorted vector of the chars of the given string
sortchars(s::AbstractString) = sort(collect(Char, s))

# Custom comparator function for sorting the loaded wordlist
sortanagr(s1::AbstractString, s2::AbstractString) =
    if length(s1) != length(s2) length(s1) < length(s2) else sortchars(s1) < sortchars(s2) end

# Tests if two strings are deranged anagrams, returns a bool:
# in our case s2 is never longer than s1
function deranged(s1::AbstractString, s2::AbstractString)
    # Tests for derangement first
    for (a, b) in zip(s1, s2)
        if a == b return false end
    end
    # s1 and s2 are deranged, but are they anagrams at all?
    return sortchars(s1) == sortchars(s2)
end

# Task starts here, we load the wordlist line by line, strip eol char, and sort the wordlist
# in a way that ensures that longer words come first and anagrams go next to each other
words = sort(open(readlines, "./data/unixdict.txt"), rev = true, lt = sortanagr)

# Now we just look for deranged anagrams in the neighbouring words of the sorted wordlist
for i in 1:length(words)-1
    if deranged(words[i], words[i+1])
        # The first match is guaranteed to be the longest due to the custom sorting
        println("The longest deranged anagrams are $(words[i]) and $(words[i+1])")
        break
    end
end


  

You may also check:How to resolve the algorithm First-class functions step by step in the REXX programming language
You may also check:How to resolve the algorithm Null object step by step in the Lasso programming language
You may also check:How to resolve the algorithm Variadic function step by step in the Aime programming language
You may also check:How to resolve the algorithm Ormiston triples step by step in the Java programming language
You may also check:How to resolve the algorithm 15 puzzle game step by step in the C programming language