Skip to content

Instantly share code, notes, and snippets.

@sharmaabhinav
Last active August 29, 2015 14:07
Show Gist options
  • Save sharmaabhinav/f91fa22f119743c5adaf to your computer and use it in GitHub Desktop.
Save sharmaabhinav/f91fa22f119743c5adaf to your computer and use it in GitHub Desktop.
module Helper
INPUT_SEPRATOR = " "
PAGE = "p"
QUERY = "q"
def self.user_input
user_input = { pages_keywords: [], queries_keywords: [] }
input = read_from_command_line
array_of_strings = convert_input_to_array_of_strings(input)
until array_of_strings.empty?
if array_of_strings.first.downcase == PAGE
user_input[:pages_keywords].push(array_of_strings.drop(1))
elsif array_of_strings.first.downcase == QUERY
user_input[:queries_keywords].push(array_of_strings.drop(1))
end
input = read_from_command_line
array_of_strings = convert_input_to_array_of_strings(input)
end
return user_input
end
def self.read_from_command_line
input = gets.chomp
return input
end
def self.convert_input_to_array_of_strings(input)
input_array = input.split(INPUT_SEPRATOR)
return input_array
end
def self.output(sol_obj)
sol_obj.queries_keywords.each_index do |query_no|
puts output_format(query_no, sol_obj.rank_pages_for_a_query(query_no))
end
end
def self.output_format(query_no, rank_array)
output_string = "Q" + (query_no + 1).to_s + ":" + " "
rank_array.each do |page_no|
output_string += "P" + (page_no + 1).to_s + " "
end
output_string
end
end
class Solution
attr_accessor :pages_keywords, :queries_keywords # array of array of strings
MAX_KEYWORDS_ALLOWED = 8
MAX_RELEVANT_PAGES_FOR_A_QUERY = 5
def initialize(keywords = {})
self.pages_keywords = keywords.fetch(:pages_keywords, [])
self.queries_keywords = keywords.fetch(:queries_keywords, [])
end
def rank_pages_for_a_query(query_no)
search_strengths_of_pages = search_strengths_of_all_pages_for_query(query_no)
sorted_page_nos = sorted_page_nos_by_search_strength_in_descending_order(search_strengths_of_pages)
ranking = []
sorted_page_nos.each do |page_no|
ranking << page_no
break if ranking.size == MAX_RELEVANT_PAGES_FOR_A_QUERY
end
return ranking
end
private
def search_strengths_of_all_pages_for_query(query_no)
search_strengths = []
pages_keywords.each_index do |page_no|
search_strengths.push(search_strength_of_a_page_for_a_query(page_no, query_no))
end
search_strengths = convert_an_array_to_hash(search_strengths) # page no => value
return search_strengths
end
def convert_an_array_to_hash(array)
hash = Hash.new
array.each_index do |index|
hash[index] = array[index]
end
return hash
end
# eliminates pages with 0 strength
def sorted_page_nos_by_search_strength_in_descending_order(search_strengths_hash)
sorted_page_nos = []
sorted_search_strengths = search_strengths_hash.values.sort.reverse # values in reverse sorted order
sorted_search_strengths.each do |search_strength|
if search_strength != 0
page_no = search_strengths_hash.key(search_strength)
search_strengths_hash.delete(page_no)
sorted_page_nos << page_no
end
end
return sorted_page_nos
end
def search_strength_of_a_page_for_a_query(page_no, query_no)
max_strength = MAX_KEYWORDS_ALLOWED
search_strength = 0
queries_keywords[query_no].each do |query_keyword|
search_strength += max_strength * weight_of_a_query_keyword_in_a_page(query_keyword, page_no)
max_strength -= 1
end
return search_strength
end
def weight_of_a_query_keyword_in_a_page(query_keyword, page_no)
offset = pages_keywords[page_no].index { |element| element.downcase == query_keyword.downcase }
unless offset.nil?
return MAX_KEYWORDS_ALLOWED - offset
else
return 0 # return weight 0 when keyword is missing in page
end
end
end
user_input = Helper.user_input
s = Solution.new (user_input)
Helper.output(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment