Exercise 169: Look up the documentation for explode and implode.
Then formulate the function arrange-main, which consumes a String and produces all of its possible re-arrangements as a list of Strings.
This is a straight forward question. The method implode will transform a (listof string) -> string) and explode will do the reverse. This will save us from needing to pass in a list of 1strings into the arrange method, and will present the rearranged letters as simple list of strings instead of a list of lists of strings.
Exploding the string being passed into arrange-main is a simple matter. However imploding the resulting list of strings is not - or at least it is not within the context of arrange-main. This is because by the time we get the list, it is already a list of lists of strings.
Imploding them within the arrange method when we have access to the individual lists of strings would be a simple matter, but that was not what was asked for. Instead, we need to create a helper method that will then iterate over our list of lists of strings and implode them one by one.
; a simple test method. Most of the testing is done already
; within the context of the arrange method.
(check-expect (arrange-main "cat")
(list "tac" "tca" "cta" "atc" "act" "cat"))
; This method simply explodes the string passed in so that it can be
; processed by arrange, and the passes the result into imploder-helper
; to implode the individual lists of strings.
;
; String -> List-of-Strings
(define (arrange-main string)
(implode-helper (arrangements (explode string)) empty))
; This method takes in a complete list of lists of stings and an
; initially empty list-of-strings and implodes each list-of-strings
; and appends it to the end of the list-of-strings.
;
; list-of-lists-of-strings, list-of-strings -> list-of-strings
(define (implode-helper list-of-lists list-of-strings)
(cond ((empty? list-of-lists) list-of-strings)
(else (implode-helper (rest list-of-lists)
(cons (implode (first list-of-lists))
list-of-strings)))))
Exercise 168
The code below is from exercise 168 and is only included as it is required to run the code above. The tests and documentation for the following code is detailed within the context of that exercise.(define (arrangements w)
(cond
[(empty? w) (list empty)]
[else (insert-everywhere/in-all-words (first w)
(arrangements (rest w)))]))
(define (insert-everywhere/in-all-words letter words)
(cond ((empty? words) empty)
(else (append
(insert letter (list empty (first words)) empty)
(insert-everywhere/in-all-words letter (rest words))))))
(define (insert letter word-lists combinations)
(cond ((empty? (second word-lists)) (append combinations
(ins-middle letter word-lists)))
(else (insert letter
(progress-list word-lists)
(append combinations (ins-middle letter word-lists))))))
(define (progress-list word-lists)
(list
(append (first word-lists) (list (first (second word-lists))))
(cond ((empty? (rest (second word-lists))) empty)
(else (rest (second word-lists))))))
(define (ins-middle letter word-lists)
(cond ((empty? (first word-lists)) (list (cons letter (second word-lists))))
(else (list (append (first word-lists)
(list letter)
(second word-lists) empty)))))
No comments:
Post a Comment