python-3.x - 那段代码的时间复杂度是多少?
问题描述
这段代码的时间复杂度是多少?如何改进此代码?
import random
n=input('choose one h/t? ')
v=random.randint(0,1)
if n=='h':
if v==0:
print('Hurrah!!! You win. Result is Head.')
else:
print('alas!!! You lose. Result is Tell.')
elif n=='t':
if v==1:
print('hurrah!!! You win. Result is Tell.')
else:
print('alas!!! You lose. Result is Head.')
解决方案
我建议你:
- 要么合并 h 和 v if 得到一个 if only
- 要么首先确定它是 ah 还是 t 案例,在其他 if 之外。然后,使用一个函数来测试其他部分。
实际上,只需重构以使用函数,因为您正在重复自己。
推荐阅读
- python - 有没有办法在 PCA/LDA 数据上放置圆/椭圆
- java - 为什么 JMH 会为简单的快速排序报告如此奇怪的时间——显然与 N * log(N) 不成比例?
- python - 在较长的字符串中查找几个不同字符串的第一次出现
- javascript - 如何使用 moment.js 循环遍历数组
- php - 在 Apache 或 PHP 中为 Web 服务器设置安全标头和 Cookie 属性?
- azure - 在 KQL(Azure 数据资源管理器)中按时间间隔对数据进行分组
- javascript - 2021 年 2 月新更新后,onbeforeunload 和 unload 事件不会在 android 中的 chrome 上触发
- python - 如何将图像文件(jpeg 或 png)嵌入到 tkinter 根窗口中的图形画布上?
- javascript - 创建 Mongoose 数据库时遇到问题
- unity3d - 水平排列纹理的着色器