From 50f7f200c063367a28226f81e1d6e10b2f5e7b13 Mon Sep 17 00:00:00 2001 From: Average-user Date: Sun, 6 Oct 2019 19:41:23 -0300 Subject: [PATCH] Fix tests and exmaple of Anagram --- exercises/anagram/anagram.example | 31 +++++------------ exercises/anagram/anagram_tests.plt | 53 +++++++++++++++++++---------- 2 files changed, 43 insertions(+), 41 deletions(-) diff --git a/exercises/anagram/anagram.example b/exercises/anagram/anagram.example index a7ab9833..268f0692 100644 --- a/exercises/anagram/anagram.example +++ b/exercises/anagram/anagram.example @@ -1,24 +1,9 @@ -% turns a string into a sorted list of characters -format_word(Word, Formatted) :- - string_lower(Word, LowercaseWord), - string_chars(LowercaseWord, CharList), - sort(CharList, Formatted). +is_anagram(A,B) :- + string_lower(A,A1), + string_lower(B,B1), + not(A1 == B1), + string_codes(A1,Ac), + string_codes(B1,Bc), + permutation(Ac,Bc), !. -anagram(Word, Options, Matching) :- - format_word(Word, FormattedWord), - anagram(Word, Options, Matching, FormattedWord). - -anagram(_, [], [], _) :- !. -anagram(Word, [Option|Options], Matching, CharList) :- - Word \== Option, - format_word(Option, FormattedOption), - equal_lists(CharList, FormattedOption), - anagram(Word, Options, NextMatching, CharList), - Matching = [Option | NextMatching], - !. -anagram(Word, [_ | Options], Matching, CharList) :- - anagram(Word, Options, Matching, CharList). - -equal_lists([], []). -equal_lists([H|T1], [H|T2]) :- - equal_lists(T1, T2). +anagram(Word, Options, Matching) :- include(is_anagram(Word), Options, Matching). diff --git a/exercises/anagram/anagram_tests.plt b/exercises/anagram/anagram_tests.plt index 99155e01..da84a63e 100644 --- a/exercises/anagram/anagram_tests.plt +++ b/exercises/anagram/anagram_tests.plt @@ -10,55 +10,72 @@ pending :- anagram("diaper", [ "hello", "world", "zombies", "pants"], Result), is_list(Result). - test(detects_simple_anagram, condition(pending)) :- - anagram("ant", ["tan", "stand", "at"], ["tan"]). + test(detects_simple_anagram, condition(true)) :- + anagram("ant", ["tan", "stand", "at"], Xs), + Xs == ["tan"]. test(does_not_detect_false_positives, condition(pending)) :- - anagram("galea", ["eagle"], []). + anagram("galea", ["eagle"], Xs), + Xs == []. test(detects_multiple_anagrams, condition(pending)) :- - anagram("master", ["stream", "pigeon", "maters"], ["stream", "maters"]). + anagram("master", ["stream", "pigeon", "maters"], Xs), + Xs == ["stream", "maters"]. test(does_not_detect_anagram_subsets, condition(pending)) :- - anagram("good", ["dog", "goody"], []). + anagram("good", ["dog", "goody"], Xs), + Xs == []. test(detects_anagram, condition(pending)) :- - anagram("listen", ["enlists", "google", "inlets", "banana"], ["inlets"]). + anagram("listen", ["enlists", "google", "inlets", "banana"], Xs), + Xs == ["inlets"]. test(detects_multiple_anagrams, condition(pending)) :- - anagram("allergy", ["gallery", "ballerina", "regally", "clergy", "largely", "leading"], ["gallery", "regally", "largely"]). + anagram("allergy", ["gallery", "ballerina", "regally", "clergy", "largely", "leading"], Xs), + Xs == ["gallery", "regally", "largely"]. test(does_not_detect_identical_words, condition(pending)) :- - anagram("corn", ["corn", "dark", "Corn", "rank", "CORN", "cron", "park"], ["cron"]). + anagram("corn", ["corn", "dark", "Corn", "rank", "CORN", "cron", "park"], Xs), + Xs == ["cron"]. test(does_not_detect_nonanagrams_with_identical_checksum, condition(pending)) :- - anagram("mass", ["last"], []). + anagram("mass", ["last"], Xs), + Xs == []. test(detects_anagrams_case_insensitively, condition(pending)) :- - anagram("Orchestra", ["cashregister", "Carthorse", "radishes"], ["Carthorse"]). + anagram("Orchestra", ["cashregister", "Carthorse", "radishes"], Xs), + Xs == ["Carthorse"]. test(detects_anagrams_using_case_insensitive_subject, condition(pending)) :- - anagram("Orchestra", ["cashregister", "carthorse", "radishes"], ["carthorse"]). + anagram("Orchestra", ["cashregister", "carthorse", "radishes"], Xs), + Xs == ["carthorse"]. test(detects_anagrams_using_case_insensitive_possible_matches, condition(pending)) :- - anagram("orchestra", ["cashregister", "Carthorse", "radishes"], ["Carthorse"]). + anagram("orchestra", ["cashregister", "Carthorse", "radishes"], Xs), + Xs == ["Carthorse"]. test(does_not_detect_a_word_as_its_own_anagram, condition(pending)) :- - anagram("banana", ["Banana"], []). + anagram("banana", ["Banana"], Xs), + Xs == []. test(does_not_detect_a_anagram_if_the_original_word_is_repeated, condition(pending)) :- - anagram("go", ["go Go GO"], []). + anagram("go", ["go Go GO"], Xs), + Xs == []. test(anagrams_must_use_all_letters_exactly_once, condition(pending)) :- - anagram("tapper", ["patter"], []). + anagram("tapper", ["patter"], Xs), + Xs == []. test(detects_unicode_anagrams, condition(pending)) :- - anagram("ΑΒΓ", ["ΒΓΑ", "ΒΓΔ", "γβα"], ["ΒΓΑ", "γβα"]). + anagram("ΑΒΓ", ["ΒΓΑ", "ΒΓΔ", "γβα"], Xs), + Xs == ["ΒΓΑ", "γβα"]. test(eliminates_misleading_unicode_anagrams, condition(pending)) :- - anagram("ΑΒΓ", ["ABΓ"], []). + anagram("ΑΒΓ", ["ABΓ"], Xs), + Xs == []. test(capital_word_is_not_own_anagram, condition(pending)) :- - anagram("BANANA", ["Banana"], []). + anagram("BANANA", ["Banana"], Xs), + Xs == []. :- end_tests(anagram).