Write a function which outputs the positions of matching bracket pairs. The output should be a dictionary with keys the positions of the open brackets '(' and values the corresponding positions of the closing brackets ')'.

For example: input = "(first)and(second)" should return {0:6, 10:17}

If brackets cannot be paired or if the order is invalid (e.g. ')(') return False. In this kata we care only about the positions of round brackets '()', other types of brackets should be ignored.

Difficulty: introductory

Starter code:

def bracket_pairs(string):
	

Solutions:
def bracket_pairs(string):
    brackets = {}
    open_brackets = []

    for i, c in enumerate(string):
        if c == '(':
            open_brackets.append(i)
        elif c == ')':
            if not open_brackets:
                return False
            brackets[open_brackets.pop()] = i

    return False if open_brackets else brackets


def bracket_pairs(string):
    res, open = {}, []
    for i,c in enumerate(string):
        if c == '(':
            open.append(i)
        if c == ')':
            try:
                res[open.pop()] = i
            except IndexError:
                return False
    return not open and res

def bracket_pairs(stg):
    open, dic = [], {}
    for i, e in enumerate(stg):
        if e =='(':
            open.append(i)
        if e == ')':
            if not open:
                return False
            dic[open.pop(-1)] = i
    return False if open else dic



def bracket_pairs(s):
    a, d = [], {}
    for i, x in enumerate(s):
        if x == "(":
            a.append(i)
        if x == ")":
            if not a: return 0
            d[a.pop()] = i
    return not a and d

def bracket_pairs(string):
    result = {}
    stack = []
    for i, c in enumerate(string):
        if c == ')':
            if not stack:
                return False
            result[stack.pop()] = i
        elif c == '(':
            stack.append(i)
    if stack:
        return False
    return result

class Stack:
    def __init__(self):
        self.items = []

    def is_empty(self):
        return self.items == []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def peek(self):
        return self.items[len(self.items)-1]

    def size(self):
        return len(self.items)

def bracket_pairs(s):
    l = len(s);
    stack = Stack(); i = 0; res = {}; flag = True
    while i < l and flag:
        if s[i] == "(": stack.push(i)
        elif  s[i] == ")":
            if stack.is_empty(): flag = False
            else: 
                a = stack.pop()
                res[a] = i
        i += 1
    if flag and stack.is_empty(): return res
    return False

def bracket_pairs(strng):
    matching_indexes = {}
    open_indexes = []
    for i, a in enumerate(strng):
        if a == '(':
            open_indexes.append(i)
        elif a == ')':
            try:
                matching_indexes[open_indexes.pop()] = i
            except IndexError:
                return False
    return matching_indexes if not open_indexes else False


def bracket_pairs(string):
    opened = []
    result = {}
    for i, ch in enumerate(string):
        if ch == "(": 
            opened.append((ch, i))
        elif ch == ")":
            if not opened: return False
            result[opened[-1][1]] = i
            opened = opened[:-1]
    
    return False if opened else result


def bracket_pairs(string):
    ob, res = [], {}
    
    for n,e in enumerate(string):
        if e == '(':
            ob.append(n)
        elif e == ')':
            if not ob:
                return False
            res[ob.pop()] = n
    return False if ob else res