1.  
  1. 1 # 整数部分十进制转二进制
  2. 2
  3. 3 num = int(raw_input(">>>"))
  4. 4
  5. 5 if num < 0:
  6. 6 isNeg = True
  7. 7 num = abs(num)
  8. 8 else:
  9. 9 isNeg = False
  10. 10 result = ''
  11. 11 if num == 0:
  12. 12 result = ''
  13. 13 while num > 0:
  14. 14 result = str(num%2) + result
  15. 15 num = num/2
  16. 16 if isNeg:
  17. 17 result = '-' + result
  18. 18 # 小数部分十进制转二进制
  19. 19
  20. 20 x = float(raw_input('Enter a decimal number between 0 and 1: '))
  21. 21
  22. 22 p = 0
  23. 23 while ((2**p)*x)%1 != 0:
  24. 24 print('Remainder = ' + str((2**p)*x - int((2**p)*x)))
  25. 25 p += 1
  26. 26
  27. 27 num = int(x*(2**p))
  28. 28
  29. 29 result = ''
  30. 30 if num == 0:
  31. 31 result = ''
  32. 32 while num > 0:
  33. 33 result = str(num%2) + result
  34. 34 num = num/2
  35. 35
  36. 36 for i in range(p - len(result)):
  37. 37 result = '' + result
  38. 38
  39. 39 result = result[0:-p] + '.' + result[-p:]
  40. 40 print('The binary representation of the decimal ' + str(x) + ' is ' + str(result))
  41. # 穷举法猜测检验平方根
  42. x = 25
  43. epsilon = 0.01
  44. step = epsilon**2
  45. numGuesses = 0
  46. ans = 0.0
  47. while (abs(ans**2 - x)) >= epsilon and ans <= x:
  48. ans += step
  49. numGuesses += 1
  50. print('numGuesses = ' + str(numGuesses))
  51. if abs(ans**2-x) >= epsilon:
  52. print('Failed on square root of ' + str(x))
  53. else:
  54. print(str(ans) + ' is close to the square root of ' + str(x))
  55. # 二分法猜测检验平方根
  56. # bisection search for square root
  57.  
  58. x = 12345
  59. epsilon = 0.01
  60. numGuesses = 0
  61. low = 0.0
  62. high = x
  63. ans = (high + low)/2.0
  64. while abs(ans**2 - x) >= epsilon:
  65. print('low = ' + str(low) + ' high = ' + str(high) + ' ans = ' + str(ans))
  66. numGuesses += 1
  67. if ans**2 < x:
  68. low = ans
  69. else:
  70. high = ans
  71. ans = (high + low)/2.0
  72. print('numGuesses = ' + str(numGuesses))
  73. print(str(ans) + ' is close to square root of ' + str(x))
  74. # Lecture 3.7, slide 3
  75.  
  76. # 牛顿-罗斐逊 算法搜寻平方根(g-(g**2-k)/2g)
  77.  
  78. epsilon = 0.01
  79. y = 24.0
  80. guess = y/2.0
  81.  
  82. while abs(guess*guess - y) >= epsilon:
  83. guess = guess - (((guess**2) - y)/(2*guess))
  84. print(guess)
  85. print('Square root of ' + str(y) + ' is about ' + str(guess))
  86.  
  87. #第一个python程序
  88. import pickle as p
  89.  
  90. linkmanfile = 'linkman.data'
  91. #the name of the file where we will store the object
  92.  
  93. linkman = { 'zhangyunpeng' : '',
  94. 'xuleisen' : '',
  95. 'yinrui' : '',
  96. 'yancangkuo' : '',
  97. 'lijizhou' : '',
  98. 'liuyulong' : ''
  99. }
  100. #set up linkman data base
  101.  
  102. print '%d lineman:' % len(linkman)
  103. for name, qq in linkman.items():
  104. print '%s : %s' % (name, qq)
  105. #list original listing
  106.  
  107. print'(1-search 2-delete 3-add 0-revise)'
  108. #prompting of operation
  109.  
  110. k = int(raw_input('please input:'))
  111. if k == 1:
  112. s = raw_input('Search the linkman name:')
  113. print '%s' % linkman[s]
  114.  
  115. elif k == 2:
  116. d = raw_input('delete the linkman name:')
  117. del linkman[d]
  118.  
  119. elif k == 3:
  120. a = raw_input('add the linkman name:')
  121. A = raw_input('add the linkman number:')
  122. linkman[a] = A
  123.  
  124. elif k == 0:
  125. r = raw_input('which revise:')
  126. linkman[r] = raw_input('revised number:')
  127. #code of process
  128.  
  129. for name, qq in linkman.items():
  130. print '%s : %s' % (name, qq)
  131. #print new listing
  132.  
  133. f = file(linkmanfile, 'w')
  134. p.dump(linkman, f)
  135. #put data into a file for using next
  1.  
  1.  
  1.  
  1.  

codehouse的更多相关文章

  1. Python爬虫之豆瓣-新书速递-图书解析

    1- 问题描述 抓取豆瓣“新书速递”[1]页面下图书信息(包括书名,作者,简介,url),将结果重定向到txt文本文件下. 2- 思路分析[2] Step1 读取HTML Step2 Xpath遍历元 ...

  2. [tornado]使用webscoket的使用总是403错误

    使用的tornado版本为4.0+ 后台: PS D:\CodeHouse\tornado\websocket> python .\ws_app.py WARNING:tornado.acces ...

  3. [Flask]学习杂记一 Hello程序

    这几天买了本  <Flask Web开发:基于Python的Web应用开发实战>,之前也用过flask 但是不怎么系统,有时候需要搭建一些临时的测试服务,用falsk比较方面,一个文件就可 ...

  4. [PythonCode]扫描局域网的alive ip地址

    内网的主机都是自己主动分配ip地址,有时候须要查看下有那些ip在使用,就写了个简单的脚本. linux和windows下都能够用,用多线程来ping1-255全部的地址,效率不高.2分钟左右. 先凑合 ...

  5. Sitecore 9 介绍

    Sitecore 9就在这里.这个最新版本更大,更智能,更易于使用 - 并且更好地帮助您实现业务和数字目标. 现在,Sitecore 9对营销人员和非Sitecore开发人员来说更容易使用.它拥有许多 ...

随机推荐

  1. C#中多线程的并行处理

    System.Threading.Tasks,在该命名空间下Task是主类,表示一个类的异步的并发的操作,创建并行代码的时候不一定要直接使用Task类,在某些情况下可以直接使用Parallel静态类( ...

  2. c# 对list 操作的写法总结

    1:统计list 内重复值的数量 List<, , , , , , , }; var g = list.GroupBy(i => i); foreach (var item in g) { ...

  3. Oracle_高级功能(5) 用户、角色、权限

    一.用户(模式)1.定义用户:对数据库的访问,需要以适当用户身份通过验证,并具有相关权限来完成一系列动作模式(schema):是某个用户所拥有的对象的集合.具有创建对象权限并创建了对象的用户称为拥有某 ...

  4. 5C - A == B ?

    Give you two numbers A and B, if A is equal to B, you should print "YES", or print "N ...

  5. android 线性布局

    activity_main.xml线性布局 <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  6. poj 2528(线段树+离散化) 市长的海报

    http://poj.org/problem?id=2528 题目大意是市长竞选要贴海报,给出墙的长度和依次张贴的海报的长度区间(参考题目给的图),问最后你能看见的海报有几张 就是有的先贴的海报可能会 ...

  7. python之初接触

    编程语言相关 1什么是编程语言 编程语言即语言,语言的本质就是沟通,因而编程语言与英语 .法语.日语等所有语言并无区别,只不过英语是人与人之间沟通的介质,而编程语言则是程序员与计算机沟通的介质. 程序 ...

  8. snort学习笔记

    Snort有三种工作模式:嗅探器.数据包记录器.网络入侵检测系统(ids). 嗅探器模式仅仅是从网络上读取数据包并作为连续不断的流显示在终端上. 数据包记录器模式把数据包记录到硬盘上. 网络入侵检测模 ...

  9. 常用到的photoshop实用设计功能都在这了!

    常用到的photoshop实用设计功能都在这了!赶快收藏学起来,需转不谢~ ​ 编辑:千锋UI设计

  10. $(QTDIR);$(QTDIR)\include\QtCore;$(QTDIR)\include;

    $(QTDIR); 在系统环境变量中定义即可  vs属性中设置头文件路径