问:

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

【提高题】:一球从 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. Markdown温故知新(0):导航目录

    Markdown温故知新(0):导航目录 Markdown温故知新(1):Markdown面面观 Markdown温故知新(2):详解七大标准语法 Markdown温故知新(3):六个实用扩展语法 M ...

  2. Python【day 8】文件

    一.文件操作 open(文件路径,mode='模式',encoding='utf-8')模式:r w a rb wb ab r+ w+ a+ r+b w+b a+b常用的:r w ab表示字节,处理费 ...

  3. java 手写 jvm高性能缓存

    java 手写 jvm高性能缓存,键值对存储,队列存储,存储超时设置 缓存接口 package com.ws.commons.cache; import java.util.function.Func ...

  4. ObjC: Foundation Kit

    转自:http://marshal.easymorse.com/tech/objc-foundation-kit Foundation Kit是什么? 你可以把它看作Java JDK中的java.la ...

  5. Spring cloud简单学习总结

    微服务简介 一.spring boot和spring cloud 的关系 spring boot来写各个拆分出来的微服务,spring  cloud把各个微服务联系起来,比如各个微服务通过eurke找 ...

  6. Kubernetes概念之deployment

    一整天一整天的坐在办公室,真的很~~~,懵圈中....,求解救. 本文通过<Kubernetes权威指南>的概念部分学习总结 Deployment作用 Deployment与RC的作用其实 ...

  7. 面试官问我,为什么老司机建议MySQL列属性尽量用 NOT NULL ?

    本文阅读时间大约6分钟. 其实写这篇文章,也是来自一个知识星球读者的提问,他在二面的过程中被问到了,由于他简历中写道有 MySQL 调优经验,但这个问题没有回答好,二面被刷了. 其实我们刚学习 C 语 ...

  8. group by 两个字段

    group by 的简单说明:  group by 一般和聚合函数一起使用才有意义,比如 count sum avg等 使用group by的两个要素:   (1) 出现在select后面的字段 要么 ...

  9. MySQL修炼之路二

    1. 表字段的操作 1. 语法: alter table 表名 执行动作: 2. 添加字段(add) alter table 表名 add 字段名 数据类型: alter table 表名 add 字 ...

  10. MySQL修炼之路一

    1. MySQL概述 1. 什么是数据库 存储数据的仓库 2. 都有哪些公司在用数据库 金融机构.游戏网站.购物网站.论坛网站 ... ... 3. 提供数据库服务的软件 1. 软件分类 MySQL. ...