python的round测试

<!--<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 ) != 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是多少

 

ContractedBlock.gif ExpandedBlockStart.gif 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'

第二小题我没想出来,难道他是递减的函数吗...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值