Leetcode Algorithm No.241 Different Ways to Add Parentheses-学の達人はコードを書きます

4250 ワード

以下の方法はLeetcode Discussから
Python version is 3.4
方法1
'''      python '''
 
import re
 
def diffWaysToCompute(self, input):
    tokens = re.split('(\D)', input)
    '''
    map          ,    TypeError: object of type 'map' has no len()​
        map,    nums    type   char,  eval     
    '''
    #nums = map(int, tokens[::2])
    #nums = [int(i) for i in tokens[::2]]
    nums = tokens[::2]
    ''' nums = [2, 3, 4, 5] '''
    #ops = map({'+':operator.add, '-':operator.sub, '*':operator.mul}.get,tokens[1::2])
    ops = tokens[1::2]
    ''' ops = ['*', '-', '*'] '''
    def build(lo,hi):
        if lo == hi:
            return [nums[lo]]
        '''      eval     '''
        #return [ops[i](a,b)
        return [str(eval(a+ops[i]+b))
                for i in range(lo,hi)
                for a in build(lo,i)
                for b in build(i+1,hi)]
    return [int(num) for num in build(0,len(ops))]
'''
build          nums = [int(i) for i in tokens[::2]],          ,
'''
#s="0"
s="2*3-4*5"
#s='2-1-1'
self = []
a=diffWaysToCompute(self,s)
'''
['-34', '-10', '-14', '-10', '10']
'''
def diffWaysToCompute(self, input):
        tokens = re.split('(\D)', input)
        nums = tokens[::2]
        ''' nums = [2, 3, 4, 5] '''
        #ops = map({'+':operator.add, '-':operator.sub, '*':operator.mul}.get,tokens[1::2])
        ops = tokens[1::2]
        ''' ops = ['*', '-', '*'] '''
        def build(lo,hi):
            if lo == hi:
                return [nums[lo]]
            '''      eval     '''
            #return [ops[i](a,b)
            return [str(eval(a+ops[i]+b))
                    for i in range(lo,hi)
                    for a in build(lo,i)
                    for b in build(i+1,hi)]
        return [int(num) for num in build(0,len(ops))]

方法2
def diffWaysToCompute(self, input):
    return [eval(a+c+b)
            for i, c in enumerate(input) if c in '+-*'
            for a in self.diffWaysToCompute(input[:i])
            for b in self.diffWaysToCompute(input[i+1:])] or [int(input)]

テストデータ:
s="2*3-4*5"#s='2-1-1' self = [] a=diffWaysToCompute(self,s)
実行できませんAttributeError:'list'object has no attribute'diffWaysToCompute'
 
方法3
def diffWaysToCompute(self, input):
    return [a+b if c == '+' else a-b if c == '-' else a*b
            for i, c in enumerate(input) if c in '+-*'
            for a in self.diffWaysToCompute(input[:i])
            for b in self.diffWaysToCompute(input[i+1:])] or [int(input)]

同様に実行できません.AttributeError:'list'object has no attribute'diffWaysToCompute'
 
知識点:
1.enumerateの使い方
以下の公式定義:
enumerate (iterable, start=0) 
Return an enumerate object. iterable must be a sequence, an iterator, or some other object which supports iteration.
>>> s=[1,2,3] >>> a=enumerate(s) >>> a >>> list(a) [(0, 1), (1, 2), (2, 3)]
 
2.import re--正規表現を使用する場合は、reモジュールをインポートする必要があります.
次の例では、正規表現で分割ルールを指定します.
>>>re.split('(\D)', "2*3-4*5") 
出力は['2','*','3','-','4','*','5']
 
3.operatorモジュールは4つの演算を行うライブラリです.使用法は次のとおりです.
operator.add(x, y) = x+y 
それにoperatorもあります.sub , operator.mul等
 
4.listの妙用、tokens=['2','*','3','-','4','*','5']
>>> tokens[::2] 
出力は['2','3','4','5']
 
5.eval関数は、式(文字列)の値を計算するために使用されます.
>>> eval('1+3')
出力4