Skip to content

Instantly share code, notes, and snippets.

@jeffpwang
Created April 28, 2016 17:56
Show Gist options
  • Save jeffpwang/1f77f81da83dab668e5174639d456cab to your computer and use it in GitHub Desktop.
Save jeffpwang/1f77f81da83dab668e5174639d456cab to your computer and use it in GitHub Desktop.
algorithm pracitce
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