CodehighlightingproducedbyActiproCodeHighlighter(freeware)
http://www.CodeHighlighter.com/

-->>>>deftest(x=20):a="1.4"+"9"*xforiinxrange(3,len(a)):print"round(%s)=%s,contains%s'9'"%(a[:i],round(float(a[:" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

python的round測試

系統 1741 0
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> >>> ? def ?test(x = 20 ):
????a
= " 1.4 " + " 9 " * x
????
for ?i? in ?xrange( 3 ,len(a)):
????????
print ? " round(%s)=%s,contains?%s?'9' " ? % (a[:i],round(float(a[:i])),(len(a[:i]) - 3 ))

????????
>>> ?test()
round(
1.4 ) = 1.0 ,contains?0? ' 9 '
round(
1.49 ) = 1.0 ,contains? 1 ? ' 9 '
round(
1.499 ) = 1.0 ,contains? 2 ? ' 9 '
round(
1.4999 ) = 1.0 ,contains? 3 ? ' 9 '
round(
1.49999 ) = 1.0 ,contains? 4 ? ' 9 '
round(
1.499999 ) = 1.0 ,contains? 5 ? ' 9 '
round(
1.4999999 ) = 1.0 ,contains? 6 ? ' 9 '
round(
1.49999999 ) = 1.0 ,contains? 7 ? ' 9 '
round(
1.499999999 ) = 1.0 ,contains? 8 ? ' 9 '
round(
1.4999999999 ) = 1.0 ,contains? 9 ? ' 9 '
round(
1.49999999999 ) = 1.0 ,contains? 10 ? ' 9 '
round(
1.499999999999 ) = 1.0 ,contains? 11 ? ' 9 '
round(
1.4999999999999 ) = 1.0 ,contains? 12 ? ' 9 '
round(
1.49999999999999 ) = 1.0 ,contains? 13 ? ' 9 '
round(
1.499999999999999 ) = 1.0 ,contains? 14 ? ' 9 '
round(
1.4999999999999999 ) = 2.0 ,contains? 15 ? ' 9 '
round(
1.49999999999999999 ) = 2.0 ,contains? 16 ? ' 9 '
round(
1.499999999999999999 ) = 2.0 ,contains? 17 ? ' 9 '
round(
1.4999999999999999999 ) = 2.0 ,contains? 18 ? ' 9 '
round(
1.49999999999999999999 ) = 2.0 ,contains? 19 ? ' 9 '
>>> ?

?

在看到python的round時想到js有三個關于取整的方法Math.round,Math.ceil還有一個沒記住,于是做了一些嘗試

還是有點意思的吧?

?

這個是編程之美里的一個題,我用來熟悉一下python的syntx,不考慮什么算法什么的,just get things done

子數組的最大乘積

?

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> >>> ? def ?do(x):
????
return ?reduce( lambda ?x,y:x * y,x)
>>> ? def ?fun2(x):
????
""" x?is?a?list """
????temp
= []
????
for ?i? in ?range( 1 ,len(x) + 1 ):
????????
for ?j? in ?range(len(x) + 1 ):
????????????
if (j < i):
????????????????
print ?x[j:i]
????????????????temp.append(do(x[j:i]))
????
return ?max(temp)

>>> ?fun2(x)
[0]
[0,?
1 ]
[
1 ]
[0,?
1 ,? 2 ]
[
1 ,? 2 ]
[
2 ]
[0,?
1 ,? 2 ,? 3 ]
[
1 ,? 2 ,? 3 ]
[
2 ,? 3 ]
[
3 ]
6

?

數組中的子數組之和的最大值

?

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> def ?do(x):
????
return ?sum(x);
# 改一下這個do函數,繼續復用fun2(x)即可
[ - 10 ,? 2 ,? 3 ,? 1 ]
>>> ?fun2(_)
[
- 10 ]
[
- 10 ,? 2 ]
[
2 ]
[
- 10 ,? 2 ,? 3 ]
[
2 ,? 3 ]
[
3 ]
[
- 10 ,? 2 ,? 3 ,? 1 ]
[
2 ,? 3 ,? 1 ]
[
3 ,? 1 ]
[
1 ]
6

?

求數組中的遞增序列,如1,-1,2,-3,4,-5,6,-7,最長的序列是1,2,4,6

?

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> def ?fun4(y):
????x
= y[:]
????temp
= []
????
if ?len(x) < 1 :
????????
return ?x
????
else :
????????
for ?i? in ?xrange(len(x) - 1 ):
????????????
if ?x[i] < x[i + 1 ]:
????????????????temp.append(x[i])
????????????
else :
????????????????x[i
+ 1 ] = x[i]
????????
if ?x[len(x) - 1 ] > x[len(x) - 2 ]:
????????????temp.append(x[len(x)
- 1 ])
????
return ?temp

[
- 1 ,? - 2 ,? 9 ,? 6 ,? 10 ]
>>> ?fun4(_)
[
- 1 ,? 9 ,? 10 ]
>>> ?

?

這題目走了彎路了,一直在想用reduce,結果進死胡同了,如果是考試肯定答不出了,下面是我錯誤的代碼

?

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> def ?do2(x):
????
global ?temp
????temp
= []
????
def ?nested(a,b):
????????
# global?temp
???????? print ?temp
????????
if (a < b):
????????????
if ?temp == []:
????????????????
print ? " temp?is?[] "
????????????????temp.append(a)
????????????temp.append(b)
????????????
return ?b
????????
else :
????????????temp.append(a)
????????????
return ?a
????
if ?len(x) > 1 :
????????reduce(nested,x)
????
else :
????????temp
= x[:]
????
return ?temp

def ?fun3(x):
????result
= []
????
for ?i? in ?xrange(len(x)):
????????
print ? " current?list= " ,x[i:]
????????result.append(do2(x[i:]))
????
print ? " haha= " ,result
????y
= result.sort( lambda ?x,y:cmp(len(x),len(y)))
????
print ? " x= " ,result
????
return ?result.pop()

給一個N!e.g. N!=362800,N!的末尾有2個0.那N=20時,有x個0,求N!的二進制表示中最低位1的位置

?

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> >>> ? def ?fun5(x):
????temp
= str(reduce( lambda ?a,b:a * b,range( 1 ,x + 1 )))
????
print ?temp
????
for ?i? in ?xrange(len(temp) - 1 , - 1 , - 1 ):
????????
if ?temp[i] != ' 0 ' :
????????????
return ?len(temp) - i - 1

????????
>>> ?fun5( 20 )
2432902008176640000
4

?

?

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> >>> ? def ?fun6(x):
????yy
= reduce( lambda ?a,b:a * b,xrange(x,0, - 1 ))
????
print ? " yy= " ,yy
????zz
= tobin(yy)
????
print ? " zz= " ,zz
????
for ?i? in ?xrange(len(zz) - 1 , - 1 , - 1 ):
????????
if ?zz[i] == " 1 " :
????????????
return ?len(zz) - i - 1

????????
>>> ?
>>> ?fun6( 5 )
yy
= ? 120
zz
= ? 1111000
3
>>> ? def ?tobin(x):
????L
= []
????
while ?(x / 2 ) != 0? or ?(x % 2 ) != 1 :
????????L.append(str(x
% 2 ))
????????x
= x / 2
????
else :
????????L.append(str(x
% 2 ))
????
return ? "" .join(L[:: - 1 ])

>>> ?

?

使用遞歸和非遞歸的方法計算階乘

?

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> >>> ? def ?fun10(x):
????
if ?x > 1 :
????????
return ?x * fun10(x - 1 )
????
else :
????????
return ? 1

????
>>> ?fun10( 3 )
6
>>> ?
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> >>> ? def ?fun9(x):
????
return ?reduce( lambda ?a,b:a * b,xrange( 1 ,x + 1 ));

>>> ?fun9( 3 )
6

?

?給一個十進制的整數N,寫下從1開始到N的所有整數,然后婁一下其中1出現的次數,例如N=2,時寫下1,2,這里只出現1次

寫一個f(N),返回1到N之間出現"1"的次數,比如f(12)-5

滿足條件下f(N)=N的最大的N是多少

?

Code
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> >>> ? def ?fun11(x):
????L
= []
????
for ?i? in ?xrange( 1 ,x + 1 ):
????????
print ?i
????????L.append(str(i).count(
" 1 " ))
????
return ? " contains: " + str(sum(L))

>>> ?fun11( 2 )
1
2
' contains:1 '

第二小題我沒想出來,難道他是遞減的函數嗎...

python的round測試


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日韩亚洲综合精品国产 | 亚洲第一区二区快射影院 | 国产国拍亚洲精品永久不卡 | www色综合| 久热精品香蕉在线视频 | 亚洲欧美日韩国产综合高清 | 国产精品久久在线 | 日韩久久网 | 99re国产精品视频首页 | 日本免费人做人一区在线观看 | 日本不卡一二三区 | 99精品国产在这里白浆 | 五月天婷婷在线视频 | 九九这里只有精品 | 九九视频热| 深夜啪啪网站 | 97精品国产高清久久久久蜜芽 | 国产日本欧美在线观看 | 四虎黄色网址 | 久草在线视频资源 | 国产欧美另类久久久精品免费 | 99久久免费国产精品 | 日日狠狠久久偷偷四色综合免费 | 欧美成人一区二区三区在线视频 | 男女性高爱麻豆 | 18p爽视频在线观看免费 | 免费看成人播放毛片 | 日日摸夜夜添夜夜添影院视频 | 成人欧美一区二区三区在线 | 国产三级不卡 | 国内精品51视频在线观看 | 欧美日韩高清观看一区二区 | 真人一级一级特黄高清毛片 | 99久久精品国产一区二区成人 | 国产真实一区二区三区 | a大片久久爱一级 | 中文字幕日本不卡一二三区 | 亚洲午夜综合网 | 欧美日韩亚洲精品国产色 | 日韩成人免费一级毛片 | 在线看亚洲 |