Python练习题答案: 乒乓球服务问题【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战

乒乓球服务问题【难度:2级】:

答案1:

def service(score):
  turn = sum(int(i) for i in score.split(":"))
  condition = (turn%10 < 5) if turn < 40 else (turn%4 < 2)
  return "first" if condition else "second"

答案2:

def service(score):
    turns_played = sum(map(int, score.split(":")))
    serves_each = 5 if (turns_played<40) else 2
    return "second" if (turns_played//serves_each)%2 else "first"

答案3:

def service(score):
    s = sum(int(x) for x in score.split(':'))
    return 'second' if s // (5 if s < 40 else 2) % 2 else 'first'

答案4:

def service(score):
  return [['first', 'second'][z//5%2 if z < 40 else z%4>1] for z in [sum(map(int, score.split(':')))]][0]

答案5:

def service(score):
    total = sum(int(n) for n in score.split(":"))
    return "second" if (total // (5 if total < 40 else 2) % 2) else "first"
    


    #if total < 40:
    #    turn = total // 5 
    #else:
    #    turn = total // 2
    #if turn % 2:
    #    return "second"
    #else:
    #    return "first

答案6:

def service(score):
    total = sum(int(a) for a in score.split(':'))
    return ('first', 'second')[(total / (5 if total < 40 else 2)) % 2]

答案7:

def service(score):
    totalscore = sum(map(int,score.split(":")))
    #print totalscore
    if totalscore < 40:
        return "first" if (totalscore/5)%2 == 0 else "second"
    return "first" if (totalscore/2)%2 == 0 else "second"

答案8:

def service(score):
    s = sum(map(int, score.split(':')))
    return ['first', 'second'][s // (5 if s < 40 else 2) % 2]

答案9:

def service(s):n=sum(map(int,s.split(':')));return'sfeicrosntd'[1-n%40/(2+(n<40)*3)%2::2]

答案10:

def service(score):
    points = sum(map(int, score.split(":")))
    turn = points // 5 if points < 40 else (points - 40) // 2
    return ["first", "second"][turn % 2]

你可能感兴趣的:(Python编程进阶练习题)