设为首页 收藏本站
查看: 1685|回复: 0

[经验分享] python的round测试

[复制链接]

尚未签到

发表于 2017-4-29 10:15:18 | 显示全部楼层 |阅读模式
<!--<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, 
12]
[
12]
[
2]
[0, 
123]
[
123]
[
23]
[
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)即可
[-10231]
>>> fun2(_)
[
-10]
[
-102]
[
2]
[
-1023]
[
23]
[
3]
[
-10231]
[
231]
[
31]
[
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<x[i+1]:
                temp.append(x)
            
else:
                x[i
+1]=x
        
if x[len(x)-1]>x[len(x)-2]:
            temp.append(x[len(x)
-1])
    
return temp

[
-1-29610]
>>> fun4(_)
[
-1910]
>>> 
  这题目走了弯路了,一直在想用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!='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=="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是多少

DSC0000.gif DSC0001.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'
  第二小题我没想出来,难道他是递减的函数吗...

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-370670-1-1.html 上篇帖子: Python的网络编程(二) 下篇帖子: python快速入门三
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表