Write a function that takes an array of numbers (integers for the tests) and a target number. It should find two different items in the array that, when added together, give the target value. The indices of these items should then be returned in a tuple like so: `(index1, index2)`.

For the purposes of this kata, some tests may have multiple answers; any valid solutions will be accepted.

The input will always be valid (numbers will be an array of length 2 or greater, and all of the items will be numbers; target will always be the sum of two different items from that array).

Based on: http://oj.leetcode.com/problems/two-sum/

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Data structures

Starter code: def two_sum(numbers, target):
	

Solutions:
def two_sum(nums, t):
	for (i, x) in enumerate(nums):
		for (j, y) in enumerate(nums):
			if i != j and x + y == t:
				return [i, j]


def two_sum(nums, target):
	d = {}
	for (i, num) in enumerate(nums):
		diff = target - num
		if diff in d:
			return [d[diff], i]
		d[num] = i


def two_sum(numbers, target):
	d = {}
	for (i, n) in enumerate(numbers):
		if target - n in d:
			return [d[target - n], i]
		d[n] = i


def two_sum(numbers, target):
	for i in range(0, len(numbers)):
		for x in range(0, len(numbers)):
			if numbers[i] + numbers[x] == target and i != x:
				index1 = i
				index2 = x
				break
	return sorted([index1, index2])


def two_sum(numbers, target):
	for (i, val1) in enumerate(numbers[:-1]):
		for (j, val2) in enumerate(numbers[i + 1:]):
			if val1 + val2 == target:
				return (i, j + i + 1)


def two_sum(numbers, target):
	return [[i, numbers.index(target - numbers[i])] for i in range(len(numbers)) if target - numbers[i] in numbers].pop()


def two_sum(n, target):
	for i in range(len(n) - 1):
		if target - n[i] in n[i + 1:]:
			return [i, n[i + 1:].index(target - n[i]) + (i + 1)]
	return None


def two_sum(numbers, target):
	for i in range(len(numbers)):
		for j in range(len(numbers)):
			if i != j and numbers[i] + numbers[j] == target:
				return [i, j]


def two_sum(numbers, target):
	numbers.sort()
	i = 0
	j = len(numbers) - 1
	sum_ = numbers[i] + numbers[j]
	while sum_ != target:
		if sum_ < target:
			i += 1
		elif sum_ > target:
			j -= 1
		sum_ = numbers[i] + numbers[j]
	return [i, j]


from itertools import combinations

def two_sum(numbers, target):
	for (i1, i2) in combinations(list(range(len(numbers))), 2):
		if numbers[i1] + numbers[i2] == target:
			return (i1, i2)