问题描述:

You have a string that consist only of digits. You need to find how many zero digits ("0") are at the beginning of the given string.(一共有多少个0在字符串前面)

Input: A string, that consist of digits.

Output: An Int.

def beginning_zeros(number: str) -> int:# your code herereturn 0if __name__ == '__main__':print("Example:")print(beginning_zeros('100'))# These "asserts" are used for self-checking and not for an auto-testingassert beginning_zeros('100') == 0assert beginning_zeros('001') == 2assert beginning_zeros('100100') == 0assert beginning_zeros('001001') == 2assert beginning_zeros('012345679') == 1assert beginning_zeros('0000') == 4print("Coding complete? Click 'Check' to earn cool rewards!")

要求是输入是一个字符串,输出是一个整数。然后统计一共有多少个0出现在前面。

for循环历遍整个字符串,遇到0就统计加一,遇到非0就返回统计。

def beginning_zeros(number: str) -> int:# your code herec=0for i in number:if i=='0':c+=1else:return creturn c

其他解决方案,

def beginning_zeros(number: str) -> int:return len(number) - len(number.lstrip('0'))

这个方法也很好,把整个字符串的长度减去,去掉左边所有0之后的字符串的长度。

str.lstrip([chars])

返回原字符串的副本,移除其中的前导字符(就是左边的字符,lstrip就是leftstrip左边剥离)。 chars 参数为指定要移除字符的字符串。 如果省略或为 None,则 chars 参数默认移除空白符。 实际上 chars 参数并非指定单个前缀;而是会移除参数值的所有组合

Beginning Zeros相关推荐

  1. PyTorch 笔记(02)— 常用创建 Tensor 方法(torch.Tensor、ones、zeros、eye、arange、linspace、rand、randn、new)

    1. Tensor 概念分类 PyTorch 中的张量(Tensor)类似 NumPy 中的 ndarrays,之所以称之为 Tensor 的另一个原因是它可以运行在 GPU 中,以加速运算. 1.1 ...

  2. Numpy 一维、二维数组、size/dtype/shape属性、数组函数arange/linspace/logspace /diag/zeros/ones/random 、多维数组索引和筛选)

    参考: https://gitbook.cn/gitchat/column/undefined/topic/5e3bceadec8d9033cf924665 打开 IPython ,创建 Python ...

  3. System Poles and Zeros 系统零点和极点

    LTI系统,连续的有s传递函数,离散的有z传递函数. s=jw,所以连续传递函数的零极点都对应频率值.零点的物理意义是,某个频率的输入信号(正弦信号)不会产生任何输出,被block掉了:极点的物理意义 ...

  4. pandas删除dataframe中行内容全是0的数据行(remove rows with all zeros in dataframe)

    pandas删除dataframe中行内容全是0的数据行(remove rows with all zeros in dataframe) 目录 pandas删除dataframe中行内容全是0的数据 ...

  5. pandas将dataframe日期数据列的日期转化为日期当月第一天(每月1号)的日期并生成新的数据列(Setting date to beginning of month in dataframe)

    pandas将dataframe日期数据列的日期转化为日期当月第一天(每月1号)的日期并生成新的数据列(Setting date to beginning of month in dataframe) ...

  6. A.CPP (blur.CPP)如何调用B.CPP (zeros.cpp)中定义的方法

    ///A.CPP   (blur.CPP)关键内容 #include "headers/blur.h" #include "headers/zeros.h" u ...

  7. [LeetCode/LintCode] Factorial Trailing Zeros

    Problem Write an algorithm which computes the number of trailing zeros in n factorial. Challenge 11! ...

  8. Beginning IOS 7 Development Exploring the IOS SDK - Handling Basic Interface Fun

    Beginning IOS 7 Development Exploring the IOS SDK 目前使用的是Objective-C,用这本书,简单记录一下 第一章,图书简介 第二章,简要介绍使用x ...

  9. BEGINNING SHAREPOINT#174; 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers 总结

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers  总结 本章节向你介绍了SP平台上event recei ...

最新文章

  1. marquee实现文字移动效果;js+div实现文字无缝移动效果
  2. 转载 用python 获取当前时间
  3. javascript的实现事件的一些实例
  4. Project2010的新特性
  5. java8日期转时间戳_Java 8日期和时间
  6. CC++中的qsort库函数
  7. 实战演练:MySQL手动注册binlog文件造成主从同步异常
  8. Python之爬虫(七)正则的基本使用
  9. 机器学习-多元梯度下降
  10. 机器数与码制——如何在计算机内部存储数字、进行数据运算
  11. 用C#实现基于TCP协议的网络通讯(1)
  12. 计算机管理(一)C盘瘦身
  13. 微信jssdk开发 java_Java微信公众平台开发(十一)--微信JSSDK中Config配置
  14. 12款网盘搜索神器以备不时之需要
  15. filter函数 与filtfilt函数的效果区别
  16. EXCEL数据组合的用法
  17. MySQL自学笔记(三)
  18. mysql全文检索 中文_mysql全文索引不支持中文的解决方案
  19. 自从我使用HiFlow场景连接器后,在也不用担心成为“落汤鸡”了
  20. AI的下一个战场:认知智能的突围

热门文章

  1. EnablePrefetcher注册表项的修改与电脑提速
  2. html如何做旅游网页,HTML+CSS旅游网站
  3. redisTemplate Hash 使用方法
  4. 进入docker容器命令行
  5. Qt使用.pri模块化工程
  6. Qt网络编程post请求,数据格式为json或x-www-form-urlencoded
  7. 工具类 | 集合工具类Collections介绍
  8. freertos任务优先级分配
  9. Ubuntu 重启后突然无法连接网络的问题
  10. 计算机毕业设计JAVA考生服务系统mybatis+源码+调试部署+系统+数据库+lw