2021-09-21 Slove End Zero

CheckiO Original URL

Question:

Try to find out how many zeros a given number has at the end.
Input: A positive Int
Output: An Int.

def end_zeros(num: int) -> int:
    # your code here
    return 
    
if __name__ == '__main__':
    print("Example:")
    print(end_zeros(0))

Solution:

uese rstrip() method

example1
Remove white space at the end of string

txt = "     banana     "
x = txt.rstrip()
print("of all fruits", x, "is my favorite")

the result is :

of all fruits      banana is my favorite

example 2
remove trailing characters if they are commas, s,q or w

txt = "banana,,,,,ssqqqww....."
x = txt.rstrip(",.qsw")
print(x)

the result is :

banana

**

Define and Usage
The rstrip() method removes any trailing characters (characters at the end a string), space is the default trailing character to remove.
syntax
string.rstrip(characters)
parameter values

ParameterDescription
CharactersOptional. A set of characters to remove as trailing characters

Answer:

def end_zeros(num: int) -> int:
    # your code here
    s=str(num)
    return len(s)-len(s.rstrip('0'))

if __name__ == '__main__':
    print("Example:")
    print(end_zeros(0))
  • [attention!]:
    0 is not a number anymore. Instead, it is a string right now.
    The mistake I confront was that I didnot put single quotes around it, which resulted in unmatch.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值