Last active
August 29, 2015 14:18
-
-
Save johnnyji/32c51a0ccea9abec3325 to your computer and use it in GitHub Desktop.
candidates exercise
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
require 'active_support/all' | |
@candidates = [ | |
{ | |
id: 5, | |
years_of_experience: 4, | |
github_points: 293, | |
languages: ['C', 'Ruby', 'Python', 'Clojure'], | |
date_applied: 5.days.ago.to_date, | |
age: 26 | |
}, | |
{ | |
id: 7, | |
years_of_experience: 1, | |
github_points: 145, | |
languages: ['JavaScript', 'Ruby', 'Go', 'Erlang'], | |
date_applied: 15.days.ago.to_date, | |
age: 19 | |
}, | |
{ | |
id: 9, | |
years_of_experience: 6, | |
github_points: 435, | |
languages: ['JavaScript', 'SQL', 'C#'], | |
date_applied: 1.day.ago.to_date, | |
age: 32 | |
}, | |
{ | |
id: 10, | |
years_of_experience: 3, | |
github_points: 232, | |
languages: ['Java', 'Ruby', 'JavaScript'], | |
date_applied: 12.days.ago.to_date, | |
age: 31 | |
}, | |
{ | |
id: 11, | |
years_of_experience: 12, | |
github_points: 32, | |
languages: ['VB', 'Cobol', 'Fortran'], | |
date_applied: 2.days.ago.to_date, | |
age: 42 | |
}, | |
{ | |
id: 13, | |
years_of_experience: 2, | |
github_points: 328, | |
languages: ['Python', 'Ruby', 'JavaScript'], | |
date_applied: 4.days.ago.to_date, | |
age: 25 | |
}, | |
{ | |
id: 15, | |
years_of_experience: 1, | |
github_points: 400, | |
languages: ['JavaScript', 'Ruby'], | |
date_applied: 3.days.ago.to_date, | |
age: 16 | |
}, | |
] |
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
# In this file we define the methods to help filter out candidates | |
# This way, we keep these methods separated from other potential parts of the program | |
require "pry" | |
require 'colorize' | |
def all | |
@candidates.each do |person| | |
if qualified_candidates(@candidates).include?(person) | |
puts person.to_s.colorize(:green) | |
else | |
puts person.to_s.colorize(:red) | |
end | |
end | |
end | |
def find(input_id) | |
found = @candidates.find {|person| person[:id] == input_id.to_i} | |
found == nil ? "Sorry no one found by the ID of#{input_id}" : found | |
end | |
def experienced?(candidate) | |
candidate.select do |person| | |
person if person[:years_of_experience] >= 2 | |
end.map {|person| person} | |
end | |
def qualified_candidates(candidates) | |
experienced?(candidates).select do |person| | |
person if meets_qualifications(person) | |
end.map {|person| person} | |
end | |
def ordered_by_qualifications(candidates) | |
sorted_candidates = candidates.sort {|a, b| [b[:years_of_experience], b[:github_points]] <=> [a[:years_of_experience], a[:github_points]]} | |
sorted_candidates.each { |person| puts person.to_s.colorize(:green) } | |
end | |
# PRIVATE METHODS | |
private | |
def meets_qualifications(candidate) | |
has_enough_github_points(candidate) && knows_ruby_and_python(candidate) && applied_in_last_fifteen_days(candidate) && is_old_enough(candidate) | |
end | |
def is_old_enough(candidate) | |
candidate[:age] > 17 | |
end | |
def has_enough_github_points(candidate) | |
candidate[:github_points] >= 100 | |
end | |
def knows_ruby_and_python(candidate) | |
candidate[:languages].include?("Ruby") && candidate[:languages].include?("Python") | |
end | |
def applied_in_last_fifteen_days(candidate) | |
candidate[:date_applied] > 15.days.ago.to_date | |
end |
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
# This is the main entrypoint into the program | |
# It requires the other files/gems that it needs | |
require 'pry' | |
require './candidates' | |
require './filters' | |
require 'colorize' | |
def execute_command(input) | |
qualified = qualified_candidates(@candidates) | |
if input == "help" | |
show_instructions | |
elsif input =~ /find [0-9]+$/ | |
puts find(input[-2..-1]) | |
return | |
elsif input == "all" | |
all | |
return | |
elsif input == "qualified" | |
ordered_by_qualifications(qualified) | |
return | |
else | |
puts "Not a valid command (type 'help' for instructions): " | |
return | |
end | |
end | |
def show_instructions | |
puts "'find 1' ".colorize(:light_blue) + "will display the candidate with the ID of 1" | |
puts "'all' ".colorize(:light_blue) + "will print display all candiates to the screen" | |
puts "'qualified' ".colorize(:light_blue) + "will display qualified candidates by experience and points" | |
puts "'quit' ".colorize(:light_blue) + "will end the program: " | |
end | |
start = true | |
until start == false | |
print "Please enter a command (or type 'help' for instructions): " | |
user_input = gets.chomp | |
user_input == "quit" ? start = false : execute_command(user_input) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Johnny,
Your code looks great! I like the slick ternary on like 41 of main.rb, and your clear and easy to read method definitions in filters.rb!
Just a few points I'd like to mention:
In filters.rb you're doing a map at the end of
qualified_candidates
andexperienced
, but the maps are actually returning the same data. Check out the docs for how the map method works: http://ruby-doc.org/core-2.2.0/Array.html#method-i-map - and grab a TA if you'd like some help going over that.On line 8 of the same file, in your
all
method, you're callingqualifed_candidates
at each step of the iteration over the candidates - can you think of a way of re-writing the code so that you only call that method once, instead of each time you iterate? The benefit of that would be that your loop will execute faster, saving computation time.And one other small note. In
execute_command
in main.rb, you don't need to return in each if block - I think you're confusing this with a switch/case statement. Since you've defined your if block using if/elsif/else, it will only ever execute the code in one of the blocks: the one whose expression is true, or if none are true, the else block.