python 编码问题_常见的Python编码面试问题:第5部分

python 编码问题

编写一个函数,该函数需要一年并返回“ True”或“ False”,无论该年是a年。 在公历中,使用三个条件来标识leap年: (Write a function that takes in a year and returns ‘True’ or ‘False’ whether that year is a leap year. In the Gregorian calendar, three conditions are used to identify leap years:)

  • The year can be evenly divided by 4, is a leap year, unless:

    该年可以除以4,即a年,除非:
  • The year can be evenly divided by 100, it is NOT a leap year, unless:

    年份可以平均除以100,不是NOT年,除非:
  • The year is also evenly divisible by 400. Then it is a leap year.

    年份也可以平均除以400。然后是a年。
def is_leap(year):
leap = False
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
leap = True
else:
leap = True
return leap

步骤1:声明功能 (Step 1: Declare the function)

Since you are asked to write a function, it’s important to do so! Right away you should type ‘def’ and come up with an appropriate name for your function. Since the prompt tells you that the function will be taking in a year, be sure to include one argument in your function declaration that will represent that year.

因为要求您编写函数,所以这样做很重要! 您应该立即输入“ def”并为您的函数命名一个合适的名称。 由于提示告诉您该函数将使用一年,因此请确保在函数声明中包括一个代表该年份的参数。

步骤2:将变量默认设置为“ False” (Step 2: Set variable as ‘False’ by default)

Since the majority of years are not leap years, it makes sense to initiate the return value as ‘False’ from the get-go. Doing this prevents you from having to have a lot of unnecessary ‘leap == False’ statements in your code. Only a very specific set of conditions will change leap to ‘True’ and otherwise leap will simply stay as ‘False’.

由于大多数年份不是leap年,因此从一开始就将返回值初始化为“ False”是有意义的。 这样做可以避免您的代码中有很多不必要的'leap == False'语句。 只有非常特定的一组条件才能将跳跃更改为“ True”,否则跳跃将简单地保持为“ False”。

步骤3:撰写您的条件陈述 (Step 3: Write your conditional statements)

The prompt nicely lays out the three necessary conditions for a year to be a leap year, and it presents them in order too. All we need to do is take the written prompt and translate it into code. First, using the modulo operator, we check to make sure the year can evenly be divided by 4. If it can’t, we keep ‘False’ as the value for our leap variable and return it. If it can though, we move onto the next check: can the year be evenly divided by 100. In this case, if the year can be divided by 100 we have to proceed to the final condition in order to evaluate it. If it can’t be divided by 100, it is definitely a leap year and our leap value can be set to ‘True’ to be returned. If the year can be evenly divided by both 4 and 100 and it can also be divided by 400, it is a leap year and we must return ‘True’. If it can’t be divided by 400, however, ‘False’ must be returned.

提示很好地列出了要成为leap年的一年的三个必要条件,并且也按顺序列出了它们。 我们需要做的就是获取书面提示并将其转换为代码。 首先,使用运算符,检查以确保年份可以除以4。如果不能 ,则将'False'保留为jump变量的值并返回。 如果可以 ,则进入下一个检查:年份可以除以100。在这种情况下,如果年份可以除以100,则必须进行最终条件评估。 如果不能将其除以100,则肯定是a年,并且可以将我们的leap跃值设置为“ True”以返回。 如果年份可由 两个 4和100平分秋色,它也可以通过400分,这一个闰年,我们必须返回“真”。 但是,如果不能将其除以400,则必须返回'False'。

第4步:返回“ True”或“ False” (Step 4: Return ‘True’ or ‘False’)

Lastly, as with any function, we have to return something. In this case, the prompt asks that we return ‘True’ if the year is indeed a leap year or ‘False’ if it is not.

最后,与任何函数一样,我们必须返回一些东西。 在这种情况下,提示要求我们如果年份确实是a年,则返回“ True”,如果不是,则返回“ False”。

def is_leap(year):
leap = False
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
leap = True
else:
leap = True
return leap
print (is_leap(1996))
--> Trueprint (is_leap(1900))
--> Falseprint (is_leap(2020))
--> True

Problem solved!

问题解决了!

翻译自: https://medium.com/swlh/common-python-coding-interview-questions-part-5-d8639d01db16

python 编码问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值