codehouse
1 # 整数部分十进制转二进制
2
3 num = int(raw_input(">>>"))
4
5 if num < 0:
6 isNeg = True
7 num = abs(num)
8 else:
9 isNeg = False
10 result = ''
11 if num == 0:
12 result = ''
13 while num > 0:
14 result = str(num%2) + result
15 num = num/2
16 if isNeg:
17 result = '-' + result
18 # 小数部分十进制转二进制
19
20 x = float(raw_input('Enter a decimal number between 0 and 1: '))
21
22 p = 0
23 while ((2**p)*x)%1 != 0:
24 print('Remainder = ' + str((2**p)*x - int((2**p)*x)))
25 p += 1
26
27 num = int(x*(2**p))
28
29 result = ''
30 if num == 0:
31 result = ''
32 while num > 0:
33 result = str(num%2) + result
34 num = num/2
35
36 for i in range(p - len(result)):
37 result = '' + result
38
39 result = result[0:-p] + '.' + result[-p:]
40 print('The binary representation of the decimal ' + str(x) + ' is ' + str(result))
# 穷举法猜测检验平方根
x = 25
epsilon = 0.01
step = epsilon**2
numGuesses = 0
ans = 0.0
while (abs(ans**2 - x)) >= epsilon and ans <= x:
ans += step
numGuesses += 1
print('numGuesses = ' + str(numGuesses))
if abs(ans**2-x) >= epsilon:
print('Failed on square root of ' + str(x))
else:
print(str(ans) + ' is close to the square root of ' + str(x))
# 二分法猜测检验平方根
# bisection search for square root x = 12345
epsilon = 0.01
numGuesses = 0
low = 0.0
high = x
ans = (high + low)/2.0
while abs(ans**2 - x) >= epsilon:
print('low = ' + str(low) + ' high = ' + str(high) + ' ans = ' + str(ans))
numGuesses += 1
if ans**2 < x:
low = ans
else:
high = ans
ans = (high + low)/2.0
print('numGuesses = ' + str(numGuesses))
print(str(ans) + ' is close to square root of ' + str(x))
# Lecture 3.7, slide 3 # 牛顿-罗斐逊 算法搜寻平方根(g-(g**2-k)/2g) epsilon = 0.01
y = 24.0
guess = y/2.0 while abs(guess*guess - y) >= epsilon:
guess = guess - (((guess**2) - y)/(2*guess))
print(guess)
print('Square root of ' + str(y) + ' is about ' + str(guess)) #第一个python程序
import pickle as p linkmanfile = 'linkman.data'
#the name of the file where we will store the object linkman = { 'zhangyunpeng' : '',
'xuleisen' : '',
'yinrui' : '',
'yancangkuo' : '',
'lijizhou' : '',
'liuyulong' : ''
}
#set up linkman data base print '%d lineman:' % len(linkman)
for name, qq in linkman.items():
print '%s : %s' % (name, qq)
#list original listing print'(1-search 2-delete 3-add 0-revise)'
#prompting of operation k = int(raw_input('please input:'))
if k == 1:
s = raw_input('Search the linkman name:')
print '%s' % linkman[s] elif k == 2:
d = raw_input('delete the linkman name:')
del linkman[d] elif k == 3:
a = raw_input('add the linkman name:')
A = raw_input('add the linkman number:')
linkman[a] = A elif k == 0:
r = raw_input('which revise:')
linkman[r] = raw_input('revised number:')
#code of process for name, qq in linkman.items():
print '%s : %s' % (name, qq)
#print new listing f = file(linkmanfile, 'w')
p.dump(linkman, f)
#put data into a file for using next
codehouse的更多相关文章
- Python爬虫之豆瓣-新书速递-图书解析
1- 问题描述 抓取豆瓣“新书速递”[1]页面下图书信息(包括书名,作者,简介,url),将结果重定向到txt文本文件下. 2- 思路分析[2] Step1 读取HTML Step2 Xpath遍历元 ...
- [tornado]使用webscoket的使用总是403错误
使用的tornado版本为4.0+ 后台: PS D:\CodeHouse\tornado\websocket> python .\ws_app.py WARNING:tornado.acces ...
- [Flask]学习杂记一 Hello程序
这几天买了本 <Flask Web开发:基于Python的Web应用开发实战>,之前也用过flask 但是不怎么系统,有时候需要搭建一些临时的测试服务,用falsk比较方面,一个文件就可 ...
- [PythonCode]扫描局域网的alive ip地址
内网的主机都是自己主动分配ip地址,有时候须要查看下有那些ip在使用,就写了个简单的脚本. linux和windows下都能够用,用多线程来ping1-255全部的地址,效率不高.2分钟左右. 先凑合 ...
- Sitecore 9 介绍
Sitecore 9就在这里.这个最新版本更大,更智能,更易于使用 - 并且更好地帮助您实现业务和数字目标. 现在,Sitecore 9对营销人员和非Sitecore开发人员来说更容易使用.它拥有许多 ...
随机推荐
- Razor 模板引擎的使用
安装Razor的模板引擎,通过vs的“扩展管理器”,查找"RazorEngine"并安装.安装的dll包括”RazorEngine.dll“和”System.Web.Razor.d ...
- HTML DOM 事件对象
HTML DOM 事件对象 由 youj 创建,小路依依 最后一次修改 2016-08-04 HTML DOM 事件 HTML DOM 事件 HTML DOM 事件允许Javascript在HTML文 ...
- Linux移植之make uImage编译过程分析
编译出uboot可以运行的linux内核代码的命令是make uImage,下面详细介绍下生成linux-2.6.22.6/arch/arm/boot/uImage的过程: 1.vmlinux.Ima ...
- Oracle性能优化5-索引的不足
索引的不足 1.索引开销 a.访问开销 反问集中导致热块的竞争(对最新数据的查询) 回表性能取决聚合因子 索引的访问开销,返回几条数据快,但是返回大量的数据很慢 全表扫描与全扫描 ...
- Oracle_PL/SQL(10) 定时器job
定时器job1.定义 定时器指在特定的时间执行特定的操作. 可以多次执行.说明:特定的操作:指一个完成特定功能的存储过程.多次执行:指可以每分钟.每小时.每天.每周.每月.每季度.每年等周期性的运行. ...
- CSS学习总结1:CSS样式
1.CSS背景 属性 background-color:为元素设置背景色,值可以是任何合法的颜色值.实例:p {background-color: gray;} background-image:为元 ...
- (转)easyui datagrid 部分参数说明
easyui datagrid 部分参数 数据表格属性(DataGrid Properties) 属性继承控制面板,以下是数据表格独有的属性. 名称 类型 描述 默认值 columns array 数 ...
- python: "TypeError: 'type' object is not subscriptable"
目前stackoverflow找到两种情况的解决办法: 1.TypeError: 'type' object is not subscriptable when indexing in to a di ...
- Mongodb数据导出工具mongoexport和导入工具mongoimport介绍(转)
原文地址:http://chenzhou123520.iteye.com/blog/1641319 一.导出工具mongoexport Mongodb中的mongoexport工具可以把一个colle ...
- PS故障风海报制作技术分享
1.首先找一张看起来很酷的图(也可以选择自己喜欢的图片): 2. 复制图层,点击添加图层样式,选择混合选项,在高级混合里面的通道选项,有R.G.B三个通道选项,默认是全部勾选的状态,选择其中一个勾掉( ...