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的更多相关文章

  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. 三角化---深度滤波器---单目稠密重建(高翔slam---十三讲)

    一.三角化 [1]三角化得到空间点的三维信息(深度值) (1)三角化的提出 三角化最早由高斯提出,并应用于测量学中.简单来讲就是:在不同的位置观测同一个三维点P(x, y, z),已知在不同位置处观察 ...

  2. 在Struts2框架中使用Servlet的API

    1. 在Action类中也可以获取到Servlet一些常用的API * 需求:提供JSP的表单页面的数据,在Action中使用Servlet的API接收到,然后保存到三个域对象中,最后再显示到JSP的 ...

  3. springboot项目maven报错 LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback

    完整信息如下 SLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding in [jar:file:/D:/deve ...

  4. three.js raycaster射线碰撞的坑 (当canvas大小 不是屏幕大小是解决拾取物体的办法)

    这里只是记录一下坑,方便查阅,内容主要援引自:three.js Raycaster 射线拾取 canvas不占满整屏时射线拾取存在偏差 1. 世界坐标系: 世界坐标系位于屏幕的中心(0,0,0),往右 ...

  5. Vue 安装脚手架 工具 vue-cli (最新)

    假如您安装过旧版脚手架工具(vue-cli),您可以通过 npm uninstall vue-cli -g 或 yarn global remove vue-cli卸载. Vue CLI 需要Node ...

  6. 3.Mysql支持的数据类型

    3.Mysql支持的数据类型数据类型用来指定一定的存储格式.约束和有效范围.数据类型主要有:数值类型.字符串类型.日期和时间类型.3.1 数字类型类型名 字节数 有符号最小值 有符号最大值 无符号最小 ...

  7. 【Linux】关于路由跟踪指令traceroute

      稍有计算机常识的人都知道ping命令,是用来检查自己的主机是否与目标地址接通,自己的主机与目标地址的通讯包通讯速率,所谓的通讯包也就是那些什么TCP/IP,UDP包,这里说得通俗一点,比如,就拿这 ...

  8. andorid 手机外部储存

    .xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...

  9. Android.Study.Question

    1. NullPointerException 1.1 发生该异常的原因. 1.2 解决方法有哪几种? try-catch 2. Eclipse 中 debug/run 两个模式,run 是relea ...

  10. 条件随机场_CRF

    无向图 举例:“Bob drank coffee at Starbucks” 标记方式1:(名词,动词,名词,介词,名词) 称为l 标记方式2:(名词,动词,动词,介词,名词) 挑选出一个最靠谱的: ...