2012年5月4日金曜日

文字列のリスト化回答編(python)

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク

非evalバージョン

class Hoge:
  def __init__(self, xs):                                        
    self.xs = xs                                                 
    self.result = []                                             
    self.sub = None                                              
  
  def run(self):                                                 
    if not self.xs: 
      return self.result
    while self.xs:
      x = self.xs[0]
      self.xs = self.xs[1:]
      if x == '(':                                               
        self.sub = Hoge(self.xs)                                 
        self.sub.run()                                           
        self.result.append(self.sub.result)                      
        self.xs = self.sub.xs                                    
      elif x == ')':                                             
        return                                                   
      else:
        self.result.append(x)                                    
  
def f(x):                                                        
  h = Hoge(x)                                                    
  h.run()                                                        
  return h.result                                                
  
                                                                 
def test0():                                                     
  return f('') == []
                                                                 
def test1():                                                     
  return f('a') == ['a']                                         
                                                                 
def test2():                                                     
  return f('()') == [[]]
                                                                 
def test3():                                                     
  return f('(()())') == [[[],[]]]
                                                                 
def testX():                                                     
  return f("ab(cd)ef") == ['a', 'b', ['c', 'd'], 'e', 'f']       

print test0()                                                    
print test1()                                                    
print test2()
print test3()
print testX()
    

evalバージョン

import re

r = re.compile('a-zA-Z')

def f(xs):
  #'a' => 'a,'
  #'(' => '['
  #')' => ']'
  p = xs
  p = re.sub(r'\(', r"[", p)
  p = re.sub(r'\)', r"]", p)
  p = re.sub(r'([a-zA-Z])', r"'\1',", p)
  p = re.sub(r"\]'([a-zA-Z])", r"], '\1", p)
  p = re.sub(r'\]\[', r"],[", p)
  
  if not p:
    p = ' '
  p = '[' + p + ']'
  print p
  x = eval(p)
  print x
  y = list(x)
  print y
  return y



def test0():
  return f('') == []

def test1():
  return f('a') == ['a']

def test2():
  return f('()') == [[]]

def test3():
  return f('(()())') == [[[],[]]]

def testX():
  return f("ab(cd)ef") == ['a', 'b', ['c', 'd'], 'e', 'f']


print test0()
print test1()
print test2()
print test3()
print testX()
    

0 件のコメント: