Skip to content

Instantly share code, notes, and snippets.

@ukd1

ukd1/readme.md Secret

Created February 19, 2015 21:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ukd1/cb96a75be5a8a08a737c to your computer and use it in GitHub Desktop.
Save ukd1/cb96a75be5a8a08a737c to your computer and use it in GitHub Desktop.

Given an array of positive integers, write a function which returns all the unique pairs which add (equal) up to 100.

Example data:

sample_data = [0, 1, 100, 99, 0, 10, 90, 30, 55, 33, 55, 75, 50, 51, 49, 50, 51, 49, 51]
sample_output = [[1,99], [0,100], [10,90], [51,49], [50,50]]
@ukd1
Copy link
Author

ukd1 commented Feb 19, 2015

clients
 - id
 - email

client_plans
 - id
 - client_id
 - plan_id
 - date_started
 - date_ended

plans
 - id
 - name
 - price

@ukd1
Copy link
Author

ukd1 commented Feb 19, 2015

Give me all the ids of clients which were ever on plan_id 7

@sealocal
Copy link

SELECT DISTINCT(CLIENT_ID) FROM CLIENT_PLANS
WHERE PLAN_ID = 7

@ukd1
Copy link
Author

ukd1 commented Feb 19, 2015

Give me the email addresses of clients ever on plan_id 7

@sealocal
Copy link

SELECT DISTINCT(CLIENTS.EMAIL) FROM CLIENT_PLANS
JOIN CLIENTS
ON CLIENT.ID = CLIENT_PLANS.CLIENT_ID
WHERE PLAN_ID = 7

@sealocal
Copy link

sample_data = [0, 1, 100, 99, 0, 10, 90, 30, 55, 33, 55, 75, 50, 51, 49, 51, 49, 51]
# sample_output = [[1,99], [0,100], [10,90], [51,49], [50,50]]

sample_output = []
sample_data.each_with_index do |first_number, first_index|
  sample_data.each_with_index do |second_number, second_index|
    next if first_index == second_index
    if (first_number + second_number == 100 &&
      !sample_output.include?([first_number, second_number]) &&
      !sample_output.include?([second_number, first_number]))
      sample_output << [first_number, second_number]
    end
  end
end
p sample_output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment