zoukankan      html  css  js  c++  java
  • 寻找最小term,自制面试题

    #!/usr/bin/env python
    #encoding=utf-8
    def unicode2str(input_str):
      """
      将unicode类型转换为str类型
      """
      if type(input_str)==type(u""):
        return input_str.encode("utf-8","ignore")
      return input_str

    def str2unicode(str):
      """
      将str类型转换为unicode类型
      """
      if type(str)==type(u""):
        return str
      if type(str)==type(""):
        return str.decode("utf-8","ignore")
    a=["AC|||B","ABE|||ACFD","AB|||AC","ABD|||ACD"]

       
    def do(a):
        def cmp(x,y):
            if x.__len__()<y.__len__():
                return -1
            elif x.__len__()==y.__len__():
                return 0
            else:
                return 1

        a.sort(cmp)
        print a
       
        abandom=set()

        def _(a,b):
            #a,b need unicode
            #global abandom
            aa=str2unicode(a)
            bb=str2unicode(b)
            r=bb.__len__()-aa.__len__()
            print aa,bb
            if r>0 and r%2==0:
                laa,raa=aa.split("|||")
                lbb,rbb=bb.split("|||")
                if (laa in lbb) and (raa in rbb):
                    print "===>",aa,bb
                    idx=-1
                    for time in range(0,lbb.count(laa)):
                        idx=lbb.find(laa,idx+1)
                        print lbb[:idx]+lbb[idx+len(laa):]
                        if lbb[:idx]+lbb[idx+len(laa):]==rbb[:idx]+rbb[idx+len(raa):]:
                            abandom.add(b)
            return a

        if a.__len__<=1:
            return abandom
        for idx,elem in enumerate(a):
            if idx==(len(a)-1):break
            reduce(_,a[idx+1:],a[idx])
        return abandom

    print do(a)

  • 相关阅读:
    textdecoration、textdecorationcolor、textdecorationline、textdecorationstyle属性
    深入解读Promise对象
    如何将WCF服务发布到IIS中去VS2010版
    iPhone 常用面试题目
    WCF入门简单教程(图文) VS2010版
    VS2010中如何创建一个WCF
    ObjC: 使用KVO
    iOS面试重点问题
    iOS开发面试题
    《Iphone开发基础教程》第五章 自动旋转和调整大小
  • 原文地址:https://www.cnblogs.com/lexus/p/2796087.html
Copyright © 2011-2022 走看看