Skip to content

Instantly share code, notes, and snippets.

@ukd1
Last active December 26, 2015 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ukd1/7127724 to your computer and use it in GitHub Desktop.
Save ukd1/7127724 to your computer and use it in GitHub Desktop.
Carlos
clients
- id
- email
client_plans
- id
- client_id
- plan_id
- date_started
- date_ended
plans
- id
- name
- price
Plans:
1,"small",$10
Clients:
1,"russ@rainforestqa.com"
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
# 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