codehouse


  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 = '0'
 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 = '0'
 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 = '0' + 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 
 88 #第一个python程序
 89 import pickle as p
 90 
 91 linkmanfile = 'linkman.data'
 92 #the name of the file where we will store the object
 93 
 94 linkman = {           'zhangyunpeng' :   '434498027',
 95                             'xuleisen'            :   '405929608',
 96                             'yinrui'                :   '1650502423',
 97                             'yancangkuo'     :   '1046287430',
 98                             'lijizhou'              :   '641224214',
 99                             'liuyulong'           :   '1919734130'
100 }
101 #set up linkman data base
102 
103 print '%d lineman:' % len(linkman)
104 for name, qq in linkman.items():
105 print '%s : %s' % (name, qq)
106 #list original listing
107 
108 print'(1-search 2-delete 3-add 0-revise)'
109 #prompting of operation
110 
111 k = int(raw_input('please input:'))
112 if k == 1:
113 s = raw_input('Search the linkman name:')
114 print '%s' % linkman[s]
115 
116 elif k == 2:
117 d = raw_input('delete the linkman name:')
118 del linkman[d]
119 
120 elif k == 3:
121 a = raw_input('add the linkman name:')
122 A = raw_input('add the linkman number:')
123 linkman[a] = A
124 
125 elif k == 0:
126 r = raw_input('which revise:')
127 linkman[r] = raw_input('revised number:')
128 #code of process
129 
130 for name, qq in linkman.items():
131 print '%s : %s' % (name, qq)
132 #print new listing
133 
134 f = file(linkmanfile, 'w')
135 p.dump(linkman, f)
136 #put data into a file for using next


 


 
原文地址:https://www.cnblogs.com/Real-Ying/p/4393725.html