问:

【基础题】:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数

【提高题】:一球从 100 米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第 10 次落地时,共经过多少米?第 10 次反弹多高?

答:

【基础题】:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数

方法1:

import re


def split_func():
tmp_str = input('请输入字符串:')
char_num = 0
dig_num = 0
space_num = 0
other_num = 0
for i in range(len(tmp_str)):
if re.match('[a-zA-Z]', tmp_str[i]):
char_num += 1
elif re.match('\d', tmp_str[i]):
dig_num += 1
elif re.match('\s', tmp_str[i]):
space_num += 1
else:
other_num += 1
print('字符:', char_num)
print('数字:', dig_num)
print('空格:', space_num)
print('其他:', other_num)


split_func()

方法2:

s = input('请输入字符串:')
dic = {'letter': 0, 'integer': 0, 'space': 0, 'other': 0}
for i in s:
if i > 'a' and i < 'z' or i > 'A' and i < 'Z':
dic['letter'] += 1
elif i in '':
dic['integer'] += 1
elif i == ' ':
dic['space'] += 1
else:
dic['other'] += 1

print('统计字符串:', s)
print(dic)
print('------------显示结果2---------------')
for i in dic:
print('%s=' % i, dic[i])
print('------------显示结果3---------------')
for key, value in dic.items():
print('%s=' % key, value)

【提高题】:一球从 100 米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第 10 次落地时,共经过多少米?第 10 次反弹多高?

方法1:

# 数学方法求总位移
s = 100 + (100 * (1 - 0.5 ** 9)) * 2
print("小球工经过", s, "米")


# 第10次小球弹起高度
def get_height_of_bounce(initial_height):
bounce_off_height = initial_height / 2
while True:
yield bounce_off_height
bounce_off_height = bounce_off_height / 2


number_of_bounce = 10
initial_height = 100
bounce_off_heights = []
bounce_height = get_height_of_bounce(initial_height)

for i in range(number_of_bounce):
bounce_off_heights.append(bounce_height.__next__())

print("每次小球弹起的高度为", bounce_off_heights)
print("第10次弹起高度为", bounce_off_heights[9], "米")

方法2:

def cal_distance(n: int) -> None:
distance = 100
height = distance / 2
for i in range(2, n+1):
distance += 2 * height
height /= 2

print(f"经过{n}次落地后总长{distance}")
print(f"经过{n}次落地后反弹高度{height}")


cal_distance(2)
cal_distance(3)
cal_distance(10)

方法3:

def reboud(n):
height = 100
sum_distance = 100
for i in range(1, n+1):
reboud_height = (0.5 ** i) * height
sum_distance += (reboud_height * 2)
if i == n-1:
print("第{}次落地经过的总距离为{}米".format(n, sum_distance))

print("第{}次反弹的高度为{}米".format(n, reboud_height))


reboud(10)

Python【每日一问】26的更多相关文章

  1. [python每日一练]--0012:敏感词过滤 type2

    题目链接:https://github.com/Show-Me-the-Code/show-me-the-code代码github链接:https://github.com/wjsaya/python ...

  2. Python 每日一练(5)

    引言 Python每日一练又开始啦,今天的专题和Excel有关,主要是实现将txt文本中数据写入到Excel中,说来也巧,今天刚好学校要更新各团支部的人员信息,就借此直接把事情做了 主要对于三种数据类 ...

  3. Python每日一练(1):计算文件夹内各个文章中出现次数最多的单词

    #coding:utf-8 import os,re path = 'test' files = os.listdir(path) def count_word(words): dic = {} ma ...

  4. python每日一函数 - divmod数字处理函数

    python每日一函数 - divmod数字处理函数 divmod(a,b)函数 中文说明: divmod(a,b)方法返回的是a//b(除法取整)以及a对b的余数 返回结果类型为tuple 参数: ...

  5. 每日一问:Android 消息机制,我有必要再讲一次!

    坚持原创日更,短平快的 Android 进阶系列,敬请直接在微信公众号搜索:nanchen,直接关注并设为星标,精彩不容错过. 我 17 年的 面试系列,曾写过一篇名为:Android 面试(五):探 ...

  6. 每日一问:谈谈 volatile 关键字

    这是 wanAndroid 每日一问中的一道题,下面我们来尝试解答一下. 讲讲并发专题 volatile,synchronize,CAS,happens before, lost wake up 为了 ...

  7. 每日一问:讲讲 Java 虚拟机的垃圾回收

    昨天我们用比较精简的文字讲了 Java 虚拟机结构,没看过的可以直接从这里查看: 每日一问:你了解 Java 虚拟机结构么? 今天我们必须来看看 Java 虚拟机的垃圾回收算法是怎样的.不过在开始之前 ...

  8. 每日一问:你了解 Java 虚拟机结构么?

    对于从事 C/C++ 程序员开发的小伙伴来说,在内存管理领域非常头疼,因为他们总是需要对每一个 new 操作去写配对的 delete/free 代码.而对于我们 Android 乃至 Java 程序员 ...

  9. 每日一问:LayoutParams 你知道多少?

    前面的文章中着重讲解了 View 的测量流程.其中我提到了一句非常重要的话:View 的测量匡高是由父控件的 MeasureSpec 和 View 自身的 `LayoutParams 共同决定的.我们 ...

  10. 每日一问:简述 View 的绘制流程

    Android 开发中经常需要用一些自定义 View 去满足产品和设计的脑洞,所以 View 的绘制流程至关重要.网上目前有非常多这方面的资料,但最好的方式还是直接跟着源码进行解读,每日一问系列一直追 ...

随机推荐

  1. html页面的渲染And<script>位置的影响

    周末加班敲代码的时用到了<script>标签,突然想到了一个问题:别的自测项目里面<script>我把他放在了不同位置,这里应该会对代码的执行与渲染后影响吧?于是今天专门进行了 ...

  2. 【翻译】Tusdotnet中文文档(3)自定义功能和相关技术

    自定义功能和相关技术 本篇按照如下结构翻译 自定义功能 自定义数据仓库 相关技术 架构和总体概念 自定义数据仓库 tusdotnet附带一个存储库TusDiskStore,它将文件保存在磁盘上的一个目 ...

  3. php配置出错,引发上传文件出错

    今天在做文件上传的时候,按正常逻辑提交,可提交到服务器后,$_FILES['tmp_name']死活不对,表单的enctype="multipart/form-data" 这个也加 ...

  4. cache verilog实现

    cache原理: https://www.cnblogs.com/mikewolf2002/p/10984976.html cache的verilog实现 实现的cache是16k, 4way组相连c ...

  5. CentOS 8 安装

    截止目前为止CentOS的最新版本为CentOS 8版本,接下来就介绍CentOS Linux 8.0.1905的安装过程 1. 安装CentOS 8 成功引导系统会显示如上图的界面: # 界面说明 ...

  6. CentOS6.7编译安装mysql5.5(详解编译选项)

    注意!  mysql5.5之前一般都是用make编译 mysql5.5 -5.6 一般都是用cmake编译 cmake : 跨平台编译器, mysql官方提供的rpm包 mysql-client :提 ...

  7. Python 实现基于信息熵的 ID3 算法决策树模型

    版本说明 Python version: 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:21:07) [MSC v.1900 32 bit (Int ...

  8. [PAT] 目录

    题号  PAT Basic  PAT Advaced  PAT Top 1001 害死人不偿命的(3n+1)猜想     1002 写出这个数     1003 我要通过!     1004 成绩排名 ...

  9. linux突然不能上网,eth0网卡消失

    情况:之前可以正常浏览网页,没有动其它的地方,浏览器突然不能上网 ifconfig # 发现eth0网卡不见了,只有lo卡 ifconfig -a # 发现了eth0,但是没有IP地址 dhclien ...

  10. python关于type()与生成器generator的用法

    如果按这种形式写  type(a)(b) 那此处的b是个可迭代对象,这个对象迭代完成后,再放到type里    from pymysql._compat import range_type, text ...