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开发人员来说更容易使用.它拥有许多 ...
随机推荐
- C#中多线程的并行处理
System.Threading.Tasks,在该命名空间下Task是主类,表示一个类的异步的并发的操作,创建并行代码的时候不一定要直接使用Task类,在某些情况下可以直接使用Parallel静态类( ...
- c# 对list 操作的写法总结
1:统计list 内重复值的数量 List<, , , , , , , }; var g = list.GroupBy(i => i); foreach (var item in g) { ...
- Oracle_高级功能(5) 用户、角色、权限
一.用户(模式)1.定义用户:对数据库的访问,需要以适当用户身份通过验证,并具有相关权限来完成一系列动作模式(schema):是某个用户所拥有的对象的集合.具有创建对象权限并创建了对象的用户称为拥有某 ...
- 5C - A == B ?
Give you two numbers A and B, if A is equal to B, you should print "YES", or print "N ...
- android 线性布局
activity_main.xml线性布局 <?xml version="1.0" encoding="utf-8"?> <LinearLay ...
- poj 2528(线段树+离散化) 市长的海报
http://poj.org/problem?id=2528 题目大意是市长竞选要贴海报,给出墙的长度和依次张贴的海报的长度区间(参考题目给的图),问最后你能看见的海报有几张 就是有的先贴的海报可能会 ...
- python之初接触
编程语言相关 1什么是编程语言 编程语言即语言,语言的本质就是沟通,因而编程语言与英语 .法语.日语等所有语言并无区别,只不过英语是人与人之间沟通的介质,而编程语言则是程序员与计算机沟通的介质. 程序 ...
- snort学习笔记
Snort有三种工作模式:嗅探器.数据包记录器.网络入侵检测系统(ids). 嗅探器模式仅仅是从网络上读取数据包并作为连续不断的流显示在终端上. 数据包记录器模式把数据包记录到硬盘上. 网络入侵检测模 ...
- 常用到的photoshop实用设计功能都在这了!
常用到的photoshop实用设计功能都在这了!赶快收藏学起来,需转不谢~ 编辑:千锋UI设计
- $(QTDIR);$(QTDIR)\include\QtCore;$(QTDIR)\include;
$(QTDIR); 在系统环境变量中定义即可 vs属性中设置头文件路径