python小算法

1.长度为m的字符串a,长度为n的字符串b,(m>n) 判断b中的字母是否全在a中? O(n)最小.
class Num(object):

  def getNum(self, m):
    numList = filter(lambda x: not [x%i for i in range(2,x) if x%i==0], range(2,500))
    return numList[0:m]

  def getSizeDict(self, string):
    numList = self.getNum(len(string))
    strDict = {}
    for s in set(string):
      strDict[s] = numList[0]
      del numList[0]

    return strDict

  def getDiff(self, string, string2):
      j = 1
      flag = 1
      strDict = self.getSizeDict(string)
      numList =self.getNum(len(string))
      for i in numList:
        j*=i

      for s in string2:
        try:
          d = strDict[s]
        except:
          flag = 0
          break 
      return flag

if __name__ == ‘__main__‘:
  string = raw_input(‘string:‘)
string2 = raw_input(‘another:‘)   main = Num()   num = main.getDiff(string,string2)   print num == 1 and ‘yes‘ or ‘no‘   

  

python小算法,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。