Created
April 28, 2016 17:55
-
-
Save jeffpwang/2940e810f6ffc3f57f9d95f16b268abe to your computer and use it in GitHub Desktop.
algorithm practice
This file contains 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
def shift_vector(array, i) | |
i.times { array.push(array.shift) } | |
array | |
end | |
#Given a dictionary Find all of the sets of anagrams | |
#For instance, pots, stop, and tops | |
def find_anagrams(word, all_words) | |
word_array = word.split('') # -> ["s", "t", "o", "p"] | |
output = [] | |
dictionary_words = all_words.find_all{|w| w.length == word.length} | |
dictionary_words.each do |word| | |
arr_dict = word.split('') | |
if (word_array & arr_dict).length == word_array.length | |
output << word | |
end | |
end | |
output | |
end | |
def evil_find_anagrams(word, all_words) | |
all_words.find_all {|w| w.count(word) == word.length} | |
end | |
# Implement an algorithm to determine if a string has all unique characters. What if you | |
# can not use additional data structures? | |
def unique_characters(string) | |
arr_string = string.split('').sort | |
result = false | |
arr_string.each_with_index do |letter, index| | |
if letter == arr_string[index-1] | |
return false | |
else | |
result = true | |
end | |
end | |
result | |
end | |
# Write a method to decide if two strings are anagrams or not. | |
# Find the sum of contiguous subarray within a one-dimensional array of numbers which has the largest sum. | |
# [11:18] | |
# Example {-2, -3, 4, -1, -2, 1, 5, -3} | |
# [11:19] | |
# should return 7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment