How to resolve the algorithm Word ladder step by step in the Racket programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Word ladder step by step in the Racket programming language

Table of Contents

Problem Statement

Yet another shortest path problem. Given two words of equal length the task is to transpose the first into the second. Only one letter may be changed at a time and the change must result in a word in unixdict, the minimum number of intermediate words should be used. Demonstrate the following: A boy can be made into a man: boy -> bay -> ban -> man With a little more difficulty a girl can be made into a lady: girl -> gill -> gall -> gale -> gaze -> laze -> lazy -> lady A john can be made into a jane: john -> cohn -> conn -> cone -> cane -> jane A child can not be turned into an adult. Optional transpositions of your choice.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Word ladder step by step in the Racket programming language

Source code in the racket programming language

#lang racket

(define *unixdict* (delay (with-input-from-file "../../data/unixdict.txt"
                            (compose list->set port->lines))))

(define letters-as-strings (map string (string->list "abcdefghijklmnopqrstuvwxyz")))

(define ((replace-for-c-at-i w i) c)
  (string-append (substring w 0 i) c (substring w (add1 i))))

(define (candidates w)
  (for*/list (((i w_i) (in-parallel (string-length w) w))
              (r (in-value (replace-for-c-at-i w i)))
              (c letters-as-strings)
              #:unless (char=? w_i (string-ref c 0)))
    (r c)))

(define (generate-candidates word.path-hash)
  (for*/hash (((w p) word.path-hash)
              (w′ (candidates w)))
    (values w′ (cons w p))))

(define (hash-filter-keys keep-key? h)
  (for/hash (((k v) h) #:when (keep-key? k)) (values k v)))

(define (Word-ladder src dest (words (force *unixdict*)))
  (let loop ((edge (hash src null)) (unused (set-remove words src)))
    (let ((cands (generate-candidates edge)))
      (if (hash-has-key? cands dest)
          (reverse (cons dest (hash-ref cands dest)))
          (let ((new-edge (hash-filter-keys (curry set-member? unused) cands)))
            (if (hash-empty? new-edge)
                `(no-path-between ,src ,dest)
                (loop new-edge (set-subtract unused (list->set (hash-keys new-edge))))))))))

(module+ main
  (Word-ladder "boy" "man")
  (Word-ladder "girl" "lady")
  (Word-ladder "john" "jane")
  (Word-ladder "alien" "drool")
  (Word-ladder "child" "adult"))


  

You may also check:How to resolve the algorithm Include a file step by step in the Openscad programming language
You may also check:How to resolve the algorithm Phrase reversals step by step in the Ruby programming language
You may also check:How to resolve the algorithm Memory layout of a data structure step by step in the Fortran programming language
You may also check:How to resolve the algorithm Entropy step by step in the ZX Spectrum Basic programming language
You may also check:How to resolve the algorithm Exponentiation operator step by step in the Scheme programming language