python算法(一)
python算法(一)
一、求数x的因子
x=100
divisors=()#初始化空的元组
for i in range(1,x):
if x%i==0:
divisors=divisors+(i,)
print divisors
二、求数x各个数位之和
sumdigits=0
for c in str(1952):
sumdigits +=int(c)
print sumdigits
print sumdigits
三、鸡兔同笼以及变形
1.有鸡兔两种,共有x个头,y只脚,求解鸡兔各有几只?
def slove(num_heads,num_legs):
for chicken_num in range(0,num_heads+1):
pig_num =num_heads-chicken_num
top_legs=pig_num*4+chicken_num*2
if top_legs==num_legs:
return [chicken_num,pig_num]
return [None,None] def barnYard():
heads=int(raw_input("Enter the number of heads: "))
legs=int (raw_input("Enter the number of legs: "))
chicken,pig=slove(heads,legs)
if chicken==None:
print "不可解"
else:
print "the number of chicken is:",chicken
print "the number of pig is:",pig barnYard()
2.有鸡兔,蜘蛛一共三种,共有x个头,y只脚,求解鸡兔,蜘蛛各有几只?
def slove1(num_heads,num_legs):
for spider_num in range(0,num_heads+1):
for chicken_num in range(0,num_heads-spider_num):
pig_num =num_heads-chicken_num-spider_num
top_legs=pig_num*4+chicken_num*2+spider_num*8
if top_legs==num_legs:
return [chicken_num,pig_num,spider_num]
return [None,None,None] def barnYard1():
heads=int(raw_input("Enter the number of heads: "))
legs=int (raw_input("Enter the number of legs: "))
chicken,pig,spider=slove1(heads,legs)
if chicken==None:
print "不可解"
else:
print "the number of chicken is:",chicken
print "the number of pig is:",pig
print "the number of spider is",spider barnYard1()
3.2中的问题或许不只有一个解答,依次输出符合要求的解答
def slove2(num_heads,num_legs):
solutionFound=False
for spider_num in range(0,num_heads+1):
for chicken_num in range(0,num_heads-spider_num):
pig_num =num_heads-chicken_num-spider_num
top_legs=pig_num*4+chicken_num*2+spider_num*8
if top_legs==num_legs:
print "the number of chicken is:", chicken_num
print "the number of pig is:", pig_num
print "the number of spider is", spider_num
solutionFound=True
if not solutionFound:
print "不可解" def barnYard2():
heads=int(raw_input("Enter the number of heads: "))
legs=int (raw_input("Enter the number of legs: "))
slove2(heads,legs) barnYard2()
四、递归判断字符串是否为回文
解法一:
def isPlalindrome(s):
if len(s)<=1:
return True
else :
return s[0]==s[-1] and isPlalindrome(s[1:-1])
解法二:
def isPlalindrome1(s,indent):
print indent, 'hisPalindromel called with', s
if 1 >= len(s):
print indent, 'About to return True from base case',s
return True
else:
ans= s[0] == s[-1] and isPlalindrome1(s[1:-1], indent + indent)
print indent, 'About to return ',ans
return ans isPlalindrome1("abccba",1)
五、斐波那契数列
def fib(x):
sum=1;
if x==1 or x==0:
return 1;
else:
return fib(x-1)+fib(x-2) print fib(4)
六、求数x平方根
1.二分法求解
def squrtRootBi(x,epsilon):
assert x>=0,"x must be positive"+str(x)
assert epsilon>0,"epsilon must be positive"+str(epsilon)
low=0
#high=x
high=max(x,1.0)
guess=(low+high)/2.0
ctr=1
while abs(guess**2-x)>epsilon and ctr<=100:
#print "low",low,"high",high,"guess",guess
if guess**2<x:
low=guess
else:
high=guess
guess=(low+high)/2.0
ctr+=1
assert ctr<=100,"not perfect square number!"
print "times of Iteration:",ctr," guess",guess
return guess def testBi():
squrtRootBi(4,0.0001)
squrtRootBi(2, 0.0001)
squrtRootBi(0.25, 0.0001) testBi()
2.牛顿迭代法求解
def squrtRootNR(x,epsilon):
assert x >= 0, "x must be positive" + str(x)
assert epsilon > 0, "epsilon must be positive" + str(epsilon)
x=float(x)
guess=x/2.0
#guess=0.001
diff=guess**2-x
ctr=1
while abs(diff)>epsilon and ctr<=100:
# print "error",diff,"guess",guess
guess=guess-diff/(2.0*guess)
diff=guess**2-x
ctr+=1
assert ctr <= 100, "not perfect square number!"
print "times of Iteration:", ctr, " guess", guess
return guess def testBi1():
squrtRootNR(4,0.0001)
squrtRootNR(2, 0.0001)
squrtRootNR(0.25, 0.0001) testBi1()
python算法(一)的更多相关文章
- 安装Python算法库
安装Python算法库 主要包括用NumPy和SciPy来处理数据,用Matplotlib来实现数据可视化.为了适应处理大规模数据的需求,python在此基础上开发了Scikit-Learn机器学习算 ...
- Python算法与数据结构--求所有子数组的和的最大值
Python算法与数据结构--求所有子数组的和的最大值 玄魂工作室-玄魂 玄魂工作室秘书 玄魂工作室 昨天 题目:输入一个整形数组,数组里有正数也有负数.数组中连续的一个或多个整数组成一个子数组,每个 ...
- Python算法:推导、递归和规约
Python算法:推导.递归和规约 注:本节中我给定下面三个重要词汇的中文翻译分别是:Induction(推导).Recursion(递归)和Reduction(规约) 本节主要介绍算法设计的三个核心 ...
- Python算法(含源代码下载)
关键字:Python Algorithms Python算法 Mastering Basic Algorithms in the Python Language 使用Python语言掌握基本算法 P ...
- python算法题 python123网站单元四题目
目录 一:二分法求平方根 二:Collatz猜想 三:算24(只考虑满足,不考虑把所有情况找出来) 下面向大家介绍几个python算法题. 一:二分法求平方根 1.题目要求为 2.输入输出格式为 ...
- python算法介绍:希尔排序
python作为一种新的语言,在很多功能自然要比Java要好一些,也容易让人接受,而且不管您是成年人还是少儿都可以学习这个语言,今天就为大家来分享一个python算法教程之希尔排序,现在我们就来看看吧 ...
- Java && Python 算法面试常用类以及方法总结
数据结构 逻辑结构上: 包括集合,线性结构,非线性结构. 存储结构: 顺序存储,链式存储,索引存储,散列存储. Java 常见数据结构 大专栏 Java && Python 算法面试 ...
- GitHub标星2.6万!Python算法新手入门大全
今天推荐一个Python学习的干货. 几个印度小哥,在GitHub上建了一个各种Python算法的新手入门大全,现在标星已经超过2.6万.这个项目主要包括两部分内容:一是各种算法的基本原理讲解,二是各 ...
- python算法常用技巧与内置库
python算法常用技巧与内置库 近些年随着python的越来越火,python也渐渐成为了很多程序员的喜爱.许多程序员已经开始使用python作为第一语言来刷题. 最近我在用python刷题的时候想 ...
随机推荐
- YII 1.0 上传文件
$upload = CUploadedFile::getInstance($articleModel,'thumb'); if($upload){ $name = date("Ymd&quo ...
- 用mui框架开发手机app项目实践中的那些事儿
http://www.yilingsj.com/xwzj/2015-04-29/260.html 最近在玩mui框架,坑的我是:西湖的水,全都是眼泪!!! 公司的手机app要进行改版,我率先想到的是j ...
- jpg图片在开发板上显示
文件IO项目: 在开发板屏幕上循环显示目录里的图片 a.按照一定的间隔循环显示目录里的bmp图片 b.实现手指滑动来显示目录里的图片(bmp,jpg)上一张,下一张 d1: 1.能操控屏幕(查询开发板 ...
- HDU-1994-利息计算
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1994 水题 题目分析 就是两种储存方式,输出所得本金加利息 代码 #include<stdio. ...
- easelJS - Cache_vday
easelJS - Cache_vday $(function() { init(); }); // Cache_vday var canvas; var stage; var container; ...
- java实现算术表达式求值
需要根据配置的表达式(例如:5+12*(3+5)/7.0)计算出相应的结果,因此使用java中的栈利用后缀表达式的方式实现该工具类. 后缀表达式就是将操作符放在操作数的后面展示的方式,例如:3+2 后 ...
- Chrome控制台详解
Chrome控制台详解 http://www.codeceo.com/article/chrome-console.html console.log('%casdf','font-size:16px; ...
- easyui message show中msg嵌入一个按钮如何绑定事件
http://www.oschina.net/question/945028_171927
- JSP获取绝对物理地址
session.getServletContext().getRealPath(""); 但是 getRealPath("a"+File.separator); ...
- Word常用实用知识3
纯手打,可能有错别字,使用的版本是office Word 2013 转载请注明出处 http://www.cnblogs.com/hnnydxgjj/p/6322813.html,谢谢. 分页符分页 ...