#!/usr/bin/python
# coding=utf-8
# File Name: two_sum.py
# Developer: Vincent Fei
# Data: Mon Feb  2 22:12:01 2015
'''
Question:

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the 
target, where index1 must be less than index2. Please note that your returned answers 
(both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
'''

@@ -576,12 +576,903 @@
 ndex2=2%0A'''%0A
+class solution(object):%0A    #@return tuple(index1, index2)%0A    def twosum(self, numbers, target):%0A        dic = %7B%7D%0A        for i, value in enumerate(sorted(numbers)):%0A            if value in dic: # if found the remain value in dic key%0A                return (dic%5Bvalue%5D+1, i+1)%0A            else:%0A                dic%5Btarget-value%5D = i%0A%0Aif __name__ == '__main__':%0A%0A    #test%0A    num1 = %5B1,3,5,7,9,10%5D%0A    target1 = 12%0A    num2 = %5B0,2,3,5,22%5D%0A    target2 = 5%0A%0A    get_it = solution()%0A    result1 = get_it.twosum(num1, target1)%0A    result2 = get_it.twosum(num2, target2)%0A%0A    print %22Test 1 :%22%0A    print %22The Input : %25s%22 %25 num1 %0A    print %22Target : %25d%22 %25 target1%0A    print %22The Output: Index1: %25d, Index2: %25d%22 %25(result1%5B0%5D, result1%5B1%5D)%0A    print%0A    print %22Test 2 :%22%0A    print %22The Input : %25s Target : %25d%22 %25(num2,target2)%0A    print %22The Output: Index1: %25d, Index2: %25d%22 %25(result2%5B0%5D, result2%5B1%5D)%0A%0A

add solution