Python练习题答案: 简单的乐趣#121:Mr.Odd【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战

简单的乐趣#121:Mr.Odd【难度:2级】:

答案1:

def odd(string):
        li,i,string = [],0,list(string)
        while i < len(string):
            if string[i] == 'o':
                temp,pos_to_remove = ['o'],[i]
                for j in range(i + 1, len(string)):
                    if string[j] == 'd' : temp.append('d') ; pos_to_remove.append(j)
                    if len(temp) == 3 : break
                if "".join(temp) == "odd":
                    li.append(1)
                    for k in pos_to_remove:
                        string[k] = "."
            i += 1
        return len(li)

答案2:

import re

pattern = re.compile('o(.*?)d(.*?)d')

def odd(s):
    n = 0
    while pattern.search(s):
        n += 1
        s = pattern.sub(r'\1\2', s, count=1)
    return n​

答案3:

def odd(s):
    r = [i for i in s.lower() if i == 'o' or i == 'd']
    result = []
    while True:
        word = ''
        try:
            o = r.index('o')
            d = r[o+1:].index('d')+o+1
            d2 = r[d+1:].index('d')+d+1
            word += r.pop(d2)+ r.pop(d)+r.pop(o)
            if word[::-1] == "odd":
                result.append(word)
        except Exception:
            break
    return len(result)

答案4:

def odd(s):
  odds=list(s)
  for i in odds:
      if i != 'o' and i != 'd':
          odds.remove(i)
  x=3
  num=0
  t=0
  flag=True
  m=[2, 1]
  l_index=[]
  while x==3:
      if flag==False:
          odds[0:l_index[0]]
          for i in l_index:
              odds.pop(i-t)
              t=t+1
          x=0
          t=0
          num=num+1
          l_index=[]
          index=0
      else:
          x=0
          index=0
      for i in odds:
          if x==0 and i=='o':
              x=x+1
              l_index.append(index)
          elif x in m and i=='d':
              x=x+1
              l_index.append(index)
              flag=False
          index=index+1
  return num​

答案5:

import re
def odd(s):
    for c in range(len(s)):
        last, s = s, re.sub(r'o(.*?)d(.*?)d', r'\1\2', s, count = 1)
        if last == s: return c​

答案6:

def odd(s):
  n, o, d, trying = 0, 0, 0, True
  while trying:
    try:
      o += s[o:].index('o') + 1
      d = max(o, d)
      d += s[d:].index('d') + 1
      d += s[d:].index('d') + 1
      n += 1
    except:
      trying = False
  return n
​

答案7:

import re

def odd(s):
    c = 0
    while re.search(r'o.*d.*d', s):
        s = re.sub(r'o(.*?)d(.*?)d', lambda m: m.group(1)+m.group(2), s, count=1)
        c += 1
    return c​

答案8:

def odd(s):
    answer = 0
    stackO = []
    stackD = []
    for i, c in enumerate(s):
        if c == "o":
            stackO.append(i)
        elif c == "d":
            if stackO:
                if stackD:
                    stackO.pop(0)
                    stackD.pop(0)
                    answer += 1
                else:
                    stackD.append(i)
    return answer​

答案9:

def odd(input):
    stripped = list(input)
    odds = 0
    
    while True:
        try:
            index_o = stripped.index("o")
            stripped[index_o] = "."
        except:
            index_o = -1
        try:
            index_d1 = stripped.index("d", index_o)
            stripped[index_d1] = "."
        except:
            index_d1 = -1
        try:
            index_d2 = stripped.index("d", index_d1)
            stripped[index_d2] = "."
        except:
            index_d2 = -1      
        
        if index_o == -1 or index_d1 == -1 or index_d2 == -1:
            break
        elif index_o < index_d1 and index_o < index_d2:
            odds += 1
    
    return odds​

答案10:

from itertools import dropwhile

def odd(s):
    odds     = list(dropwhile(lambda c: c == "d", s))
    oIndexes = [i for i, e in enumerate(odds) if e == "o"]
    dIndexes = [i for i, e in enumerate(odds) if e == "d"]
    o = d = c = 0

    while o < len(oIndexes) and d < len(dIndexes)-1:
        Ocurr, dCurr, dNext = oIndexes[o], dIndexes[d], dIndexes[d+1]

        if Ocurr < dCurr < dNext:  c += 1; d += 2; o += 1
        else:                      d += 1

    return c​

你可能感兴趣的:(Python编程习题答案)