https://www.testdome.com/questions/python/two-sum/14289?questionIds=14288,14289&generatorId=92&type=fromtest&testDifficulty=Easy

Write a function that, given a list and a target sum, returns zero-based indices of any two distinct elements whose sum is equal to the target sum. If there are no such elements, the function should return (-1, -1).

For example, find_two_sum([1, 3, 5, 7, 9], 12) should return a tuple containing any of the following pairs of indices:

1 and 4 (3 + 9 = 12)
2 and 3 (5 + 7 = 12)
3 and 2 (7 + 5 = 12)
4 and 1 (9 + 3 = 12)

In [7]:
# Это единственный комментарий который имеет смысл
# I s
def find_index(m,a):
    try:
        return a.index(m)
    except :
        return -1
    
    
def find_two_sum(a, s):
    '''
    >>> (3, 5) == find_two_sum([1, 3, 5, 7, 9], 12)
    True
    '''
    if len(a)<2: 
        return (-1,-1)

    idx = dict( (v,i) for i,v in enumerate(a) )

    for i in a:
        m = s - i
        k = idx.get(m,-1)
        if k != -1 :
            return (i,k)

    return (-1, -1)


print(find_two_sum([1, 3, 5, 7, 9], 12))


if __name__ == '__main__':
    import doctest; doctest.testmod()


(3, 4)
**********************************************************************
File "__main__", line 11, in __main__.find_two_sum
Failed example:
    (3, 5) == find_two_sum([1, 3, 5, 7, 9], 12)
Expected:
    True
Got:
    False
**********************************************************************
1 items had failures:
   1 of   1 in __main__.find_two_sum
***Test Failed*** 1 failures.

https://stackoverflow.com/questions/28309430/edit-ipython-cell-in-an-external-editor

Edit IPython cell in an external editor

This is what I came up with. I added 2 shortcuts:

  • 'g' to launch gvim with the content of the current cell (you can replace gvim with whatever text editor you like).
  • 'u' to update the content of the current cell with what was saved by gvim. So, when you want to edit the cell with your preferred editor, hit 'g', make the changes you want to the cell, save the file in your editor (and quit), then hit 'u'.

Just execute this cell to enable these features:


In [14]:
%%javascript

IPython.keyboard_manager.command_shortcuts.add_shortcut('g', {
    handler : function (event) {
        
        var input = IPython.notebook.get_selected_cell().get_text();
        
        var cmd = "f = open('.toto.py', 'w');f.close()";
        if (input != "") {
            cmd = '%%writefile .toto.py\n' + input;
        }
        IPython.notebook.kernel.execute(cmd);
        //cmd = "import os;os.system('open -a /Applications/MacVim.app .toto.py')";
        //cmd = "!open -a /Applications/MacVim.app .toto.py";
        cmd = "!code .toto.py";

        IPython.notebook.kernel.execute(cmd);
        return false;
    }}
);

IPython.keyboard_manager.command_shortcuts.add_shortcut('u', {
    handler : function (event) {
        function handle_output(msg) {
            var ret = msg.content.text;
            IPython.notebook.get_selected_cell().set_text(ret);
        }
        var callback = {'output': handle_output};
        var cmd = "f = open('.toto.py', 'r');print(f.read())";
        IPython.notebook.kernel.execute(cmd, {iopub: callback}, {silent: false});
        return false;
    }}
);



In [49]:
# v=getattr(a, 'pop')(1)
s='print 4 7  '
commands={
    'print':print,
    'len':len
    }


def exec_string(s):
    global commands
    chunks=s.split()
    func_name=chunks[0] if len(chunks) else 'blbl'
    func=commands.get(func_name,None)
    
    params=[int(x) for x in chunks[1:]]
    if func:
        func(*params)

exec_string(s)


4 7

Symmetric Difference

https://www.hackerrank.com/challenges/symmetric-difference/problem

Task

Given sets of integers, and , print their symmetric difference in ascending order. The term symmetric difference indicates those values that exist in either or but do not exist in both.

Input Format

The first line of input contains an integer, . The second line contains space-separated integers. The third line contains an integer, . The fourth line contains space-separated integers.

Output Format

Output the symmetric difference integers in ascending order, one per line.

Sample Input

4
2 4 5 9
4
2 4 11 12
Sample Output
5
9
11
12

In [5]:
M = int(input())
m =set((map(int,input().split())))
N = int(input())
n =set((map(int,input().split())))


Enter size of a set. Press Enter for 4:
2 4 5 9
4
2 4 11 12

In [9]:
m ^ n


Out[9]:
{5, 9, 11, 12}

In [35]:
S='add 5 6'
method, *args = S.split()
print(method)
print(*map(int,args))
method,(*map(int,args))

# methods
# (*map(int,args))

# command='add'.split()
# method, args = command[0], list(map(int,command[1:]))
# method, args


add
5 6
Out[35]:
('add', 5, 6)

In [40]:
for _ in range(2):
    met, *args = input().split()
    print(met, args)
    try:
        pass

#         methods[met](*list(map(int,args)))
    except:
        pass


add 5 6
add ['5', '6']
add
add []