栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

[Python学习日记]-coding bat难题记录- Logic 1

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[Python学习日记]-coding bat难题记录- Logic 1

 Logic 1 < squirrel_play

The squirrels in Palo Alto spend most of the day playing. In particular, they play if the temperature is between 60 and 90 (inclusive). Unless it is summer, then the upper limit is 100 instead of 90. Given an int temperature and a boolean is_summer, return True if the squirrels play and False otherwise.


squirrel_play(70, False) → True
squirrel_play(95, False) → False
squirrel_play(95, True) → True

def squirrel_play(temp, is_summer):
  if not is_summer and (temp<=90 and temp>=60):
    return True
  elif is_summer and (temp<=100 and temp>=60):
    return True
  else:
    return False

 写完后感觉很啰嗦,搜索到了一个if...else的简写版本

def squirrel_play(temp, is_summer):
    return 60 <= temp <= (100 if is_summer else 90)

实例:Logic 1 < sorta_sum

Given 2 ints, a and b, return their sum. However, sums in the range 10..19 inclusive, are forbidden, so in that case just return 20.


sorta_sum(3, 4) → 7
sorta_sum(9, 4) → 20
sorta_sum(10, 11) → 21

def sorta_sum(a, b):
  if 10<=a+b<=20:
    return 20
  else:
    return a+b

#以下是简写后
def sorta_sum(a, b):
  return 20 if 10<=a+b<=20 else a+b

Logic 1 < alarm_clock

Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and a boolean indicating if we are on vacation, return a string of the form "7:00" indicating when the alarm clock should ring. Weekdays, the alarm should be "7:00" and on the weekend it should be "10:00". Unless we are on vacation -- then on weekdays it should be "10:00" and weekends it should be "off".


alarm_clock(1, False) → '7:00'
alarm_clock(5, False) → '7:00'
alarm_clock(0, False) → '10:00'

def alarm_clock(day, vacation):
  while not vacation:
    if 0 

 这道题很简单,但是github上咋还有人这么写,思路清晰,值得学习

def alarm_clock(day, vacation):
    weekdays = "10:00" if vacation else "7:00"
    weekends = "off" if vacation else "10:00"
    
    if 1 <=  day and day <= 5:
        return weekdays
                
    return weekends

#https://github.com/mirandaio/codingbat/blob/master/python/logic-1/alarm_clock.py#L5

in1to10​​​​​​​

def in1to10(n, outside_mode):
  if outside_mode:
    return n<=1 or n>=10
  else:
    return 1<=n<=10

​​​​​​​

Given a number n, return True if n is in the range 1..10, inclusive. Unless outside_mode is True, in which case return True if the number is less or equal to 1, or greater or equal to 10.


in1to10(5, False) → True
in1to10(11, False) → False
in1to10(11, True) → True

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/916137.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号