Skip to content

Instantly share code, notes, and snippets.

@HileryChao
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HileryChao/a33f39f1ce65e986ab83 to your computer and use it in GitHub Desktop.
Save HileryChao/a33f39f1ce65e986ab83 to your computer and use it in GitHub Desktop.
;;Program's Function: When given a word and a dictionary, print out all the anagrams of the word
;; that exist in the dictionary. The main procedure is called "create-anagrams".
;;the general idea of the program is to sort the given word in alphabetical order,
;;and compare it to a word in the dictionary, which is also sorted in alphabetical order.
;;if the two words match, then the word in the dictionary is outputted, and then
;;the inputted word is continued being compared to the rest of the words in the dictionary
;;until all the anagrams are outputted.
;;less alphabetically
;;takes in a letter, char, and a list of strings, alos,
;;outputs all the letters less alphabetically
;;than the inputted letter as a list of strings
(define (less-alphabetically char alos)
(filter (lambda (x) (string<=? x char)) alos))
;;greater-alphabetically
;;takes in a letter, char, and a list of strings, alos,
;;outputs all the letters greater alphabetically
;; than the inputted letter as a list of strings
(define (greater-alphabetically char alos)
(filter (lambda (x) (string>? x char)) alos))
;;sort-alphabetically
;; takes in a list of letters
;; outputs a sorted list of letters in alphabetical order
(define (sort-alphabetically alos)
(cond
[(empty? alos) empty]
[(cons? alos) (append (sort-alphabetically (less-alphabetically (first alos) (rest alos)))
(cons (first alos)
(sort-alphabetically (greater-alphabetically (first alos) (rest alos)))))]))
;; takes in a word, represented as a string,
;; and the dictionary, represented as a list of strings, and outputs all
;; the anagrams of that word which exist in the dictionary
;; -"explode" turns a string into a list with each letter as one element in the list
(define (create-anagrams word dictionary)
(cond
[(empty? dictionary) empty]
[(cons? dictionary) (if (equal? (sort-alphabetically (explode word))
(sort-alphabetically (explode (first dictionary))))
(cons (first dictionary)
(create-anagrams word (rest dictionary)))
(create-anagrams word (rest dictionary)))]))
;;tests for less-alphabetically
(check-expect (less-alphabetically "a" empty) empty)
(check-expect (less-alphabetically "a" (list "a")) (list "a"))
(check-expect (less-alphabetically "a" (list "a" "b" "c")) (list "a"))
(check-expect (less-alphabetically "b" (list "a" "b" "c")) (list "a" "b"))
(check-expect (less-alphabetically "c" (list "a" "b" "c")) (list "a" "b" "c"))
(check-expect (less-alphabetically "c" (list "a" "b" "d" "c")) (list "a" "b" "c"))
;;tests for greater-alphabetically
(check-expect (greater-alphabetically "c" empty) empty)
(check-expect (greater-alphabetically "c" (list "c")) empty)
(check-expect (greater-alphabetically "a" (list "a" "b" "c")) (list "b" "c"))
(check-expect (greater-alphabetically "b" (list "a" "b" "c")) (list "c"))
(check-expect (greater-alphabetically "c" (list "a" "b" "c")) empty)
(check-expect (greater-alphabetically "c" (list "a" "b" "c" "d" "z")) (list "d" "z"))
;;test cases for sort-alphabetically
(check-expect (sort-alphabetically empty) empty)
(check-expect (sort-alphabetically (list "a")) (list "a"))
(check-expect (sort-alphabetically (list "a" "c")) (list "a" "c"))
(check-expect (sort-alphabetically (list "c" "a")) (list "a" "c"))
(check-expect (sort-alphabetically (list "b" "c" "a" "d" "z")) (list "a" "b" "c" "d" "z"))
(check-expect (sort-alphabetically (list "a" "b" "c" "d" "z")) (list "a" "b" "c" "d" "z"))
(check-expect (sort-alphabetically (list "a" "z" "c" "d" "b")) (list "a" "b" "c" "d" "z"))
(check-expect (sort-alphabetically (list "b" "c" "d" "z" "a")) (list "a" "b" "c" "d" "z"))
;;test cases for create-anagrams
(check-expect (create-anagrams "hi" empty) empty)
(check-expect (create-anagrams "hi" (list "hi")) (list "hi"))
(check-expect (create-anagrams "hilery" (list "irelhy")) (list "irelhy"))
(check-expect (create-anagrams "hilery" (list "irelhy" "ylerhi")) (list "irelhy" "ylerhi"))
(check-expect (create-anagrams "hilery" (list "hi" "irelhy" "ler" "ylerhi" "sjfhaf")) (list "irelhy" "ylerhi"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment