Skip to content

Instantly share code, notes, and snippets.

@Kwisses
Created February 25, 2017 05:12
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 Kwisses/55600672c1b61d763a6acb05e2df774b to your computer and use it in GitHub Desktop.
Save Kwisses/55600672c1b61d763a6acb05e2df774b to your computer and use it in GitHub Desktop.
[Daily Challenge #115 - Intermediate] - Sum-Pairings - r/DailyProgrammer
# ***** DAILY CHALLENGE #115 - INTERMEDIATE *****
# (Intermediate): Sum-Parings
#
# Let the term "sum-pair" be a pair of integers A and B such that the sum of
# A and B equals a given number C. As an example, let C be 10. Thus,
# the pairs (5, 5), (1, 9), (2, 8), etc. are all sum-pairs of 10.
#
# Your goal is to write a program that, given an array through standard
# input (console), you echo out all sum-pairs of a given integer C.
#
# Formal Inputs & Outputs
#
# Input Description:
#
# On the console, you will be first given an integer N. This is the number
# of following integers that are part of the array. After the N integers,
# you will be given an integer C which represents the sum-pair you are
# attempting to match.
#
# Output Description
#
# Your program must print all unique pair of integers in the given list,
# where the sum of the pair is equal to integer C.
#
# Sample Inputs & Outputs
#
# Input (Through Console)
#
# 4
# 1 -3 4 10aw
# 5
#
# Output (Through Console)
#
# 1, 4
#
# Note that there is only one pair printed to the console since there is
# only one unique pair (1, 4) that has the sum of C (5).
#
# Challenge Input
#
# We will show the solution of this problem data-set in 7-days after the
# original submission.
#
# 14
# 10 -8 2 1 4 -9 6 1 9 -10 -5 2 3 7
# 7
#
# Note
#
# (Awesome points awarded to /u/drigz for getting some important information
# into my thick-skull: there are linear-time solutions!)
#
# This is a common interviewing problem for programming jobs, so treat it as
# such! There is a very trivial solution, but the trivial solution will run
# in O(N2 ) time. There are a few other known solutions: one that runs in
# O(N Log(N)) time (hint: takes advantage of sorting), and another in linear
# time (hint: dictionary).
# -----------------------------------------------------------------------------
def sum_pairings(numbers, n, c):
"""Get unique pairings for sum-pairing i + j in numbers.
Args:
numbers (list): Numbers to add.
n (int): Number of numbers to add.
c (int): Number to add to.
Returns:
list: Contains all unique pairings of summing i + j.
"""
pairings = []
numbers = numbers[:n+1]
for i in numbers:
for j in numbers:
if i + j == c and (j, i) not in pairings:
pairings.append((i, j))
pairings.sort()
return pairings
def main():
"""Print pair in pairings from sum_pairings(numbers, n, c)."""
numbers = [1, -3, 4, 10]
n, c = 4, 5
pairings = sum_pairings(numbers, n, c)
for pair in pairings:
print(pair)
if __name__ == "__main__":
main()
@Kwisses
Copy link
Author

Kwisses commented Feb 25, 2017

[Daily Challenge #115 - Intermediate] from the DailyProgrammer subreddit. Sum-Pairings.

Reference: https://www.reddit.com/r/dailyprogrammer/comments/15wm48/132013_challenge_115_intermediate_sumpairings/

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