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測試

系統 1865 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條評論
主站蜘蛛池模板: 永修县| 东乡县| 时尚| 醴陵市| 东台市| 香河县| 伽师县| 汽车| 丰都县| 明光市| 溧阳市| 都安| 渑池县| 黔西县| 嘉祥县| 延边| 东明县| 乃东县| 铅山县| 施甸县| 军事| 河西区| 郯城县| 从江县| 吉木萨尔县| 清镇市| 西城区| 肃宁县| 大宁县| 新民市| 阳曲县| 门源| 宜城市| 新乡县| 五家渠市| 邯郸市| 嵩明县| 永福县| 泰州市| 正定县| 武鸣县|