Created
May 10, 2016 05:51
-
-
Save anonymous/70171341664de757665fee1243ae388e to your computer and use it in GitHub Desktop.
https://repl.it/BrIb/438 created by anonymous
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Write a function word_unscrambler that takes two inputs: a scrambled | |
# word and a dictionary of real words. Your program must then output | |
# all words that our scrambled word can unscramble to. | |
# | |
# Hint: To see if a string `s1` is an anagram of `s2`, split both | |
# strings into arrays of letters. Sort the two arrays. If they are | |
# equal, then they are anagrams. | |
# | |
# Difficulty: 3/5 | |
def word_unscrambler(word, dictionary) | |
result = [] | |
word_array = word_split(word) | |
dictionary.each do |d| | |
d_array = word_split(d) | |
if word_array == d_array | |
result << d | |
end | |
end | |
return result | |
end | |
def word_split(word) | |
array = [] | |
i = 0 | |
while i < word.length do | |
array << word[i] | |
i = i + 1 | |
end | |
return bubble_sort(array) #array.sort | |
end | |
def bubble_sort(arr) | |
i = 0 | |
while i < arr.length-1 do | |
j = i + 1 | |
if arr[i] > arr[j] | |
arr = swap(arr,i,j) | |
end | |
i = i + 1 | |
end | |
if no_swap?(arr) | |
return arr | |
else | |
bubble_sort(arr) | |
end | |
end | |
def swap(arr,from_position,to_position) | |
to_position_value = arr[to_position] | |
arr[to_position] = arr[from_position] | |
arr[from_position] = to_position_value | |
return arr | |
end | |
def no_swap?(arr) | |
result = true | |
i = 0 | |
while i < arr.length - 1 do | |
j = i + 1 | |
if arr[i] > arr[j] | |
result = false | |
break | |
end | |
i = i + 1 | |
end | |
return result | |
end | |
puts("\nTests for #word_unscrambler") | |
puts("===============================================") | |
puts "word_unscrambler(\"cat\", [\"tac\"]) == [\"tac\"]: " + (word_unscrambler("cat", ["tac"]) == ["tac"]).to_s | |
puts "word_unscrambler(\"cat\", [\"tom\"]) == []: " + (word_unscrambler("cat", ["tom"]) == []).to_s | |
puts "word_unscrambler(\"cat\", [\"tic\", \"toc\", \"tac\", \"toe\"]) == [\"tac\"]: " + (word_unscrambler("cat", ["tic", "toc", "tac", "toe"]) == ["tac"]).to_s | |
puts "word_unscrambler(\"cat\", [\"scatter\", \"tac\", \"ca\"] ) == [\"tac\"]: " + (word_unscrambler("cat", ["scatter", "tac", "ca"] ) == ["tac"]).to_s | |
puts "word_unscrambler(\"turn\", [\"numb\", \"turn\", \"runt\", \"nurt\"]) == [\"turn\", \"runt\", \"nurt\"]: " + (word_unscrambler("turn", ["numb", "turn", "runt", "nurt"]) == ["turn", "runt", "nurt"]).to_s | |
puts("===============================================") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby 2.2.0p0 (2014-12-25 revision 49005) | |
>>> | |
Tests for #word_unscrambler | |
=============================================== | |
word_unscrambler("cat", ["tac"]) == ["tac"]: true | |
word_unscrambler("cat", ["tom"]) == []: true | |
word_unscrambler("cat", ["tic", "toc", "tac", "toe"]) == ["tac"]: true | |
word_unscrambler("cat", ["scatter", "tac", "ca"] ) == ["tac"]: true | |
word_unscrambler("turn", ["numb", "turn", "runt", "nurt"]) == ["turn", "runt", "nurt"]: true | |
=============================================== | |
=> nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment