Created
April 2, 2016 16:59
-
-
Save FioFiyo/9f82866bcf672fd42dd285b2446090b1 to your computer and use it in GitHub Desktop.
Sort candidates according to conditions.
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' | |
#--------------candidates file added to test----------- | |
require './candidates' | |
#-------------------TASK NUMBER 2--------------------- | |
#Takes in a single candidate (hash). Note: not the array. | |
#Returns true if the candidate has 2 years of experience or more | |
#Returns false otherwise | |
def experienced?(candidate) | |
#It's a hash within an array so scan the array to get each candidate | |
if candidate[:years_of_experience] >= 2 | |
return true | |
else | |
return false | |
end | |
end | |
#-------------------TASK NUMBER 3--------------------- | |
# Takes in an id | |
# Returns the candidate with that :id | |
# If there is no candidate with that id, it naturally returns nil | |
# require_relative './candidates' | |
def find(id) | |
@candidates.each do |candidate| | |
if candidate[:id] == id | |
return candidate | |
end | |
end | |
nil | |
end | |
#-------------------TASK NUMBER 4--------------------- | |
#Takes in the collection of candidates | |
#Returns a subset of the candidates that meet the following criteria: | |
#Are experienced [:years_of_experience] > 3 | |
#Have 100 or more Github points [:github_points] > 100 | |
#Know at least Ruby or Python [:languages] *its an array | |
#Applied in the last 15 days [:date_applied] | |
#Are over the age of 17 (18+) [:age] > 17 | |
@hired = [] | |
def qualified_candidates(candidates) | |
puts "CALLING DATES" | |
date_applied(candidates) | |
puts "CALLING QUALIFIED LANGUAGES" | |
language_part(candidates) | |
puts "CALLING QUALIFIED CANDIDATES" | |
puts "==============================" | |
year_git(candidates) | |
puts "Now Calling AGE GROUP" | |
puts "===========================" | |
old_people_age(candidates) | |
end | |
def year_git(candidates) | |
candidates.each do |qualified| | |
if qualified[:years_of_experience] >= 3 && qualified[:github_points] >= 100 | |
@hired << qualified | |
end | |
end | |
pp @hired | |
end | |
def old_people_age(candidates) | |
candidates.each do |qualified| | |
if qualified[:age] >= 17 | |
@hired << qualified | |
end | |
end | |
pp @hired | |
end | |
def language_part(candidates) | |
candidates.each do |qualified| | |
lan = qualified[:languages] | |
if lan.any? {|l| ['Ruby','Python'].include?(l)} | |
@hired << qualified | |
end | |
end | |
pp @hired | |
end | |
def date_applied(candidates) | |
candidates.each do |candidate| | |
now = Time.now.to_date | |
thn = candidate[:date_applied] | |
if (now - thn) <= 15 | |
@hired << candidate | |
end | |
pp @hired | |
end | |
end | |
#-------------------TASK NUMBER 5--------------------- | |
#Takes in a collection of candidates | |
#Instead of filtering on the candidates, returns them all back but reordered such that: | |
#Candidates with the most experience are at the top(1) | |
#For Candidates that have the same years of experience, they are further sorted by their number of Github points (highest first) | |
def order_by_qualifications(candidates) | |
sorted = [] | |
#use sort with a var and search for both keys over the hash | |
sorted = candidates.sort_by do |sorting| | |
[sorting[:years_of_experience],sorting[:github_points]] | |
end | |
pp sorted | |
end | |
#-------------TASK 6---------------- | |
#Type on commands for user: | |
#find 1: This will display candidate with id 1 | |
#all: This will print them all out to the screen (one per line) | |
#qualified: This will print only qualified candidates, ordered by experience and points (one per line) | |
#quit: Exit the REPL / program | |
def menu(chosen) | |
case chosen | |
when "find" | |
print "Which ID do you want?" | |
candidate_num = gets.chomp.to_i | |
if candidate_num | |
pp find(candidate_num) | |
end | |
when "all" | |
pp @candidates | |
when "qualified" | |
year_git(@candidates) | |
when "quit" | |
puts "Goodbye" | |
end | |
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' | |
## Your test code can go here; REMOVE COMMENT TO RUN | |
#-----Task 3 ----------- | |
# puts "Which ID do you want to see?" | |
# idd = gets.chomp.to_i | |
# find(idd) | |
# searched_candidate = find(idd) | |
# if searched_candidate.nil? | |
# puts "Not Found" | |
# else | |
# puts searched_candidate | |
# end | |
#----Task 4 ------------- | |
#qualified_candidates(@candidates) | |
#----Task 5 ------------- | |
#order_by_qualifications(@candidates) | |
#---------Menu---------- | |
# puts "What would you like to see? Pick one:" | |
# puts "1. Find" | |
# puts "2. All" | |
# puts "3. Qualified" | |
# puts "4. quit" | |
# puts "What's it going to be?" | |
# chosen = gets.chomp.downcase | |
# menu(chosen) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment