Created
October 26, 2014 15:19
-
-
Save yosriady/d3d35172408a266aeeb1 to your computer and use it in GitHub Desktop.
Solution for Palantir Interview Challenge
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
| def read_input | |
| line = STDIN.gets | |
| m, n = line.split(" ").map{|s| s.to_i} | |
| fingerprints = [] | |
| m.times do | |
| fingerprints << STDIN.gets.chomp | |
| end | |
| return fingerprints | |
| end | |
| def count_occurences(fingerprints) | |
| counts = Hash.new 0 | |
| fingerprints.each do |fingerprint| | |
| counts[fingerprint] += 1 | |
| end | |
| return counts | |
| end | |
| def inverse(f) | |
| fingerprint = f.dup | |
| replacements = [ ["P", "X"], ["T", "P"], ["X", "T"] ] | |
| replacements.each {|replacement| fingerprint.gsub!(replacement[0], replacement[1])} | |
| return fingerprint | |
| end | |
| def sum_inverses(counts) | |
| new_counts = Hash.new 0 | |
| counts.each do |fingerprint, count| | |
| inverse_counts = counts[inverse(fingerprint)] | |
| new_counts[fingerprint] = count | |
| if inverse_counts | |
| new_counts[fingerprint] += inverse_counts | |
| end | |
| end | |
| return new_counts | |
| end | |
| def largest_hash_key(hash) | |
| hash.max_by{|k,v| v} | |
| end | |
| def output_result(new_counts) | |
| STDOUT.write(largest_hash_key(new_counts)[1]) | |
| end | |
| fingerprints = read_input | |
| counts = count_occurences(fingerprints) | |
| new_counts = sum_inverses(counts) | |
| output_result(new_counts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment