-
-
Save ukd1/7127724 to your computer and use it in GitHub Desktop.
Carlos
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
clients | |
- id | |
client_plans | |
- id | |
- client_id | |
- plan_id | |
- date_started | |
- date_ended | |
plans | |
- id | |
- name | |
- price | |
Plans: | |
1,"small",$10 | |
Clients: | |
1,"[email protected]" | |
ClientPlans: <----- this table | |
1,1,6,2013-01-01,2013-01-03 | |
1,1,7,2013-01-03,2013-01-06 | |
1,1,6,2013-01-07,null | |
# Give me all the ids of clients which were ever on a given plan id. For example, plan id 1 | |
select distinct client_id from ClientPlans where plan_id = '1' | |
Clients.where() | |
scope :for_a_plan | |
ClientPlans.where(id: 1).map {|cp| cp.client.email }.uniq | |
# Give me the email addresses of clients ever on a given plan id |
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
# Given an array of positive integers, give me all the pairs which sum to 100. No duplicates - [51, 49, 51] should only output one pair, 51-49. | |
# Example data: | |
# sample = [0, 1, 99, 100, 0, 10, 90, 30, 55, 33, 55, 75] | |
# output; 1,99, 10,90, | |
def pairs(ints) | |
res = [] | |
ints.each_with_index do |v, i| | |
ints[i, ints.size].each do |s| | |
if v + s == 100 | |
res << [v, s].sort | |
end | |
end | |
end | |
res = res.uniq | |
end | |
puts pairs([0, 1, 99, 100, 0, 10, 90, 30, 55, 33, 55, 75]).inspect | |
#[[0, 100], [1, 99], [10, 90]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment