问:

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

【提高题】:一球从 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. 判读是不是对象字面量(纯对象)。对象字面量创建方式有{}、new Object()创建

    //判读是否是自身属性 function isHasPro(obj,pro){ return obj.hasOwnProperty(pro) ? true : false; } //判读是不是对象字面 ...

  2. ios证书制作与上架指南

    项目开发完了,要上架 ios AppStore 记录一下经过,以及需要提前准备和预防的东西,以便下次省心! 一.首先要申请开发者账号: 账号按流程注册申请,当时申请了够10遍,总结以下经验: 1.申请 ...

  3. uni-app项目记录

    1.如何定义一个全局属性 在App.vue 文件中,以 global.属性名= XXX; 定义 在其他页面就以 global.属性名来调用 或者在min.js 中使用 Vue.prototype 挂载 ...

  4. iOS编程

    一.语法 1. performSelector 2.

  5. 【转】解决Oracle 11g在用EXP导出时,空表不能导出

    一.问题原因: 11G中有个新特性,当表无数据时,不分配segment,以节省空间 .insert一行,再rollback就产生segment了. 该方法是在在空表中插入数据,再删除,则产生segme ...

  6. day 46

    目录 CSS样式操作 给字体设置长宽 字体颜色 语义 背景图片 边框 display 盒子模型 浮动(**************) 浮动带来的影响 clear overflow溢出属性 定位 位置的 ...

  7. IntelliJ idea SpringBoot打war包

    简单易用的使用idea 将SpringBoot工程打war包的方法 pom.xml中添加标签 1. 声明打包格式 <packaging>war</packaging> 2.  ...

  8. django-配置404页面

    setting.py文件配置 # SECURITY WARNING: don't run with debug turned on in production! DEBUG = False ALLOW ...

  9. update的where条件要把索引的字段带上,要不然就全表锁

    update的where条件要把索引的字段带上,要不然就全表锁 文章目录 update的where条件要把索引的字段带上,要不然就全表锁        本文主要内容        背景        ...

  10. WPF系列 —— 控件添加依赖属性(转)

    WPF系列 —— 控件添加依赖属性 依赖属性的概念,用途 ,如何新建与使用.本文用做一个自定义TimePicker控件来演示WPF的依赖属性的简单应用. 先上TimePicker的一个效果图. 概念 ...