大家好,我是小白君。今天,我们就开始python 100题的第三篇笔记,共2小题。前两篇文章的直达链接在这:
希望我们都可以坚持住,坚持学习,坚持交流,坚持分享。
今天学习数学计算库math、python内置函数map和array数组。阅读全文大约需要3 minutes,建议关注+收藏,边撸代码边学习,效率更高哦!
6.题目
Write a program that calculates and prints the value according to the given formula:
Q = Square root of [(2 * C * D)/H]
Following are the fixed values of C and H: C is 50. H is 30.
D is the variable whose values should be input to your program in a comma-separated sequence.For example Let us assume the following comma separated input sequence is given to the program:
100,150,180
The output of the program should be:18,22,24
中文对照: 编写一个程序,根据给定的公式计算并打印值:
- 公式:Q = [(2xCxD)/H]的平方根
- C和H的固定值如下:C是50。H是30。
- D是变量,它的值应该以逗号分隔的序列输入到程序中。
让我们假设下面的逗号分隔输入序列是给程序的:100,150,180。则程序的输出应该是:18,22,24
题目写的挺长挺复杂的,但是,看完后会发现就是个paper tiger,简单理解就是代入变量,公式计算问题。老步骤,按流程来
关键分析:
- 公式:Q =Square([(2xCxD)/H])
- fixed values:C=50,H=30
- D是变量(variable),逗号分隔输入
- 求Q输出
关键决策:
- input接收输入
- 逗号分隔输入字符
- 引入数学计算库:math
方法实现: method 1:
import math
c = 50
h = 30
value = []
items = [x for x in input('Please input 3 num split by , :').split(',')]
for d in items:
value.append(str(int(round(math.sqrt(2*c*float(d)/h)))))
print(value)
print(','.join(value))
output:
Please input 3 num split by , :100,150,180
['18', '22', '24']
18,22,24
关键注释:
- 引入数学计算库math,在开根号时候,使用了math.sqrt,更多的数学计算方式,后面有机会我们进一步探讨
- ','.join(value):将序列中的元素以指定的字符连接生成一个新的字符串。
7. 题目
Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i _ j
Note: i=0,1,···, X-1; j=0,1,···,Y-1. Suppose the following inputs are given to the program: 3,5
Then, the output of the program should be: [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
中文对照: 写一个程序,以两个数字,X,Y作为输入,生成一个二维数组。数组的第i行和第j列的元素值应为i_ j
注意:i=0,1,···, X-1; j=0,1,···,Y-1。假设程序有以下输入:3,5 那么,程序的输出应该是:[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
这个题目就稍微与之前所遇到的题目,就有所不同了。输入时数字3和5。但是,输出是一个3行5列的2维数组(2-dimensional array)。从这里开始,就开始多维数组的学习。
关键分析:
- 输入X,Y
- 生成2维数组
- 数组形式是由i=0,1,···, X-1; j=0,1,···,Y-1构成
关键决策:
- 输入还是用input
- 2维数组是新玩意
- 从i和j的例子,可以看出是递等差数列,可以用range
方法实现: method 1:
X,Y=int(input('please input int_num :')),int(input('please input int_num :'))
lst=[]
for i in range(X):
tmp=[]
for j in range(Y):
tmp.append(i*j)
lst.append(tmp)
print(lst)
output:
please input int_num :3
please input int_num :5
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
上面对题目的要求输入稍微有些违背,我这里是分两次输入,分别传给X和Y。下面就按照题目的要求,用另一种方法,再实现一次。
method 2:
X,Y = map(int,input('please input 2 digits, X,Y :').split(','))
lst = [[i*j for j in range(Y)] for i in range(X)]
print(lst)
output:
please input 2 digits, X,Y :3,5
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
关键注释:
- 方法1没有太多需要注释的地方,主要是2维list的生成,3个1x5的list,组合到一起,形成一个3行5列的二维数组
- 方法2主要是牵扯到map函数的应用:
map(function, iterable, ...) 其中:
function -- 函数
iterable -- 一个或多个序列 map() 会根据提供的函数对指定序列做映射。
第一个参数 function以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。
翻译成人话就是说:map函数是python的一个内置函数,它的参数需要包含一个func函数,将一个或多个序列,产生func的一一映射。
在这里就是对输入的string类型的3,5,通过split分割成类别的['3','4'],最后将string的'3',通过函数int,转成整形的3,即可。
今天是连续更新的第三天了,相信自己,我们能赢,加油吧。喜欢就收藏起来,或转发给小伙伴,一起学习,效果更佳哦。
慢即是快(slow is fast),每日一题,跟着更新,就会变强。欢迎各位留言、私信。本公众号是有留言功能的,这样也方便我们的交流。让我们一起在学习这条道儿上,携手前行。PS:CV初学者 招合作编辑,喜欢写作总结的小伙伴,一起来分享,后台联系加小白君微信。