Last active
March 8, 2019 20:49
-
-
Save RichardJordan/b5ecda765d910554b6a6b697a091b687 to your computer and use it in GitHub Desktop.
Interview Questions -- 3
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
# My solution here was kinda meh. I should never do an interview | |
# before lunch. I stay up too late. I'm not awake yet. | |
# | |
# My original solution got lost in copy/paste, so I've tidied it up | |
# a bit here so you're not seeing all my flaws (though my solution | |
# worked and was built within the hour, while chatting, which is | |
# what counts). So this is sort of a tidy refactoring of my answer | |
# with maybe an extra 20-30 mins to clean it up added. The basic | |
# shape of the solution is the same though. I just moved around | |
# downcasing, and used select instead of .map.conmpact - that kind | |
# of thing. | |
#--- | |
# Write a program with the following inputs: | |
# * A search string | |
# * A list of results matching that search string | |
# Sort those results according to the following order: | |
# 1. First exact matches | |
# 2. Results where one of the words is an exact match | |
# 3. Results that start with the search string | |
# 4. The rest-, alphabetically sorted- | |
# For example, given the search string "word" a correct ordering would be | |
# "Word", "Microsoft Word", "Wordpress", "1Password", "Google AdWords". | |
class OrderedSearch | |
attr_reader :search, :ordered_results, :results | |
def initialize(search, results) | |
@ordered_results = [] | |
@results = results || [] | |
@search = search.downcase | |
end | |
def print_ordered_results | |
build_ordered_results | |
puts ordered_results | |
end | |
private | |
def build_ordered_results | |
extend_ordered_results(matches_exact) | |
extend_ordered_results(matches_one_word) | |
extend_ordered_results(matches_start) | |
extend_ordered_results(remaining_results) | |
end | |
def extend_ordered_results(matches) | |
ordered_results.tap { |ord_res| ord_res << matches - ord_res }.flatten! | |
end | |
def matches_exact | |
@exact_matches ||= results.select {|r| r.downcase == search } | |
end | |
def matches_one_word | |
@owm ||= results.select { |r| r.split.map(&:downcase).include?(search) } | |
end | |
def matches_start | |
@sm ||= results.select { |r| r.split.map(&:downcase).any? { |word| word.start_with?(search) } } | |
end | |
def remaining_results | |
results - ordered_results | |
end | |
end | |
# $ sample_input = ["1Password", "Google AdWords", "Microsoft Word", "Wordpress", "Word"] | |
# $ sample_search = "word" | |
# $ filter = OrderedSearch.new(sample_search, sample_input) | |
# $ filter.print_ordered_results | |
# => Word | |
# Microsoft Word | |
# Wordpress | |
# 1Password | |
# Google AdWords | |
# $ correct_output = ["Word", "Microsoft Word", "Wordpress", "1Password", "Google AdWords"] | |
# $ filter.ordered_results == correct_output | |
# => true | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment