Write a function that takes an array of integers and a target number.
The function should find two different integers in the array that give the target value when added together. Return the indices in a tuple [e.g. (index1, index2)]. Some tests will have multiple solutions.
Test Cases
[1234,5678,9012], 14690
[1,2,3], 4
[2,2,3], 4
[5,10,15,20,25,30], 50
Happy coding!
This challenge comes from wthit56 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Discussion (11)
Scala
And the tests
Simple Ruby method chain:
Note: it's generally preferable to use
each_with_object
instead ofinject
+ explicitly returningr
in the block, but this is just a coding challenge and it made the line fit within 80 characters 😉Ruby 😍
This one is ugly tho
You’re more than welcome to provide an alternative.
Javascript
Doesn't this return the values that sum up to the target value and not their indices in the array?
It did. I've now corrected it, I hope.
I can't believe thwre are people saying JS is beautiful
Rust:
python
# defines result of the code
TypeScript