27
1
The Task
This is my first challenge so apologies if it is very simple! In this challenge, your task is to write a program or function which takes in a list of integers and a desired sum and outputs a truthy or falsey value based on whether the list contains two numbers that can be summed together to achieve the desired sum.
Input
The input must be acquired in a standard way for your language. The list of integers can be any standard collection type for your language but cannot be a hashed set, sorted set, or any highly functional collection type. Don't assume the input is sorted, any valid integer type is acceptable.
The desired sum is also an integer and must be provided to your program as input.
Output
The output may be a function return, console log, writing to a file, etc... Any valid Truthy or Falsey value is acceptable.
Additional Rules
- The input list may or may not be sorted.
- Duplicate values may occur in the list
- The same item in the list may not be summed to itself to achieve the desired sum (Eg. [3,4,5] with desired sum 6 will expect False because 3 cannot be used twice)
- Standard loopholes are not allowed.
Test Cases
INPUT OUTPUT
[3,4,5], 6 Falsey
[1,2,3,4,5,6],11 Truthy
[4,23,8174,42,-1],41 Truthy
[1,1,1,1,1,3],3 Falsey
[2,1,1,1,53,2],4 Truthy
[2,2,2,2],4 Truthy
[3],3 Falsey
[],-43 Falsey
This is code-golf, so the shortest code in bytes wins!
at least two numbers or just two numbers? – marmeladze – 2017-05-16T12:13:04.877
1@marmeladze Just 2 numbers. – maple_shaft – 2017-05-16T12:16:55.690
8Nice first challenge! – HyperNeutrino – 2017-05-16T12:34:43.850
Can the sum integer be zero or negative? – Kevin Cruijssen – 2017-05-16T13:54:51.500
1Can we assume that the input least contains at least 2 elements? – Arnauld – 2017-05-16T14:09:07.057
@Arnauld No. I should add those test cases. – maple_shaft – 2017-05-16T14:14:32.060
Could we take in a length of the list and.or assume a certain sentinel value will not be in the input? – Rohan Jhunjhunwala – 2017-05-16T14:21:24.483
@RohanJhunjhunwala Any possible integer for desired sum and any possible integer within the list so no sentinel values. Only two inputs, length is not provided. – maple_shaft – 2017-05-16T14:50:20.967
@maple_shaft my language doesn't support a native list type, it can really only take in a null terminated string or a list of numbers with a sentinel,shouldn't this be the "native" list type? – Rohan Jhunjhunwala – 2017-05-16T15:06:06.670
1Can we choose an empty output for falsey and
1
for truthy? – Mr. Xcoder – 2017-05-16T15:43:43.693[3],6
should also be Falsey, which one solution might currently accept as truthy. – Joey – 2017-05-16T15:56:16.1174I think the other challenge should be closed as a duplicate of this one instead. I like this one better. – mbomb007 – 2017-05-16T19:12:41.513