ITeye博客

polyahu的博客

polyahu · 共 23 篇文章

几个很简单的ruby脚本

下面是一个计算最大公约数的ruby脚本 #!/usr/bin/ruby a = ARGV[0].to_i b = ARGV[1].to_i c = 0 while a != 0 c = b b = a a = c % a end puts b 下面是一个更简洁的写法: #!/usr/bin/ruby a, b = ARGV.map(&:to_i) a, b = 0, 0 if a == 0 or b == 0 a, b = b, a % b while b != 0 puts a 下面是一个检测传入参数是否为素数的脚本 #!/usr/bin/ruby a = ARGV[0].to_i i =…

2013-02-14 22:08 · ruby · 评论(0)

分解质因数小脚本

awk语言的版本,如下: { x = $0; } function foo(a) { for (i = 2; i <= a; i++) if (a%i == 0) {printf("%d ",i);return i;} } END { c = foo(x); while (c < x) { x = x / c; c = foo(x); } } # usage(): $echo 2520 | awk -f script.awk 下边这个是一样思路的Bash版本,不过运行的非常慢。 #!/usr/bin/bash #usage(): $sh script.sh 2520 function foo…

2013-02-12 20:24 · Shell · python · 评论(0)

django教程写的好

http://djangobook.py3k.cn/2.0/ 超赞,拜读。 python的web框架不要太多... django、flask、bottle、tornado...

2013-02-12 18:35 · python · 评论(0)

python读入文件和time/sys模块的简单使用

python读入文件和time/sys模块的简单使用 一些标准库函数的用法也待学习,如:os/re/sets/string/queue/socket #!/usr/bin/python print ord('a') print chr(97) #字符和整型互相转换 fp = open("file.tmp") for line in fp.readlines(): print line, #文件操作readlines()函数一次读入多行,循环输出 for line in fp: print line, #!/usr/bin/python import time #time模块使用 print …

2013-02-12 17:11 · python · 评论(0)

python写json示例

#!/usr/bin/python import json source = '{"name":"polyahu", "type":"title"}' f = open('./json.data') j = json.loads(source) r = json.load(f) s = json.dumps(j) print "j's type", print type(j) print "s's type", print type(s) json.loads()和json.dumps()是将str类型和dict类型互相转换。

2013-02-08 22:06 · python · 评论(0)

scheme或者guile尾递归算阶乘

一段guile脚本程序,基本就是打印一个hello world,然后算个阶乘。 #!/usr/bin/guile \ -s !# (begin (newline) (display "hello world") (newline)) (define (factorial n) (fact-iter 1 1 n)) (define (fact-iter product counter max-count) (if (> counter max-count) product (fact-iter (* counter product) (+ counter 1) max-count))) (di…

2013-02-08 22:03 · scheme · 评论(0)

为什么大家不愿意读正则表达式...

尝试用shell命令做一些批量的重命名,做如下的操作: mv [六人行.第三季][friends.s03e01][圣城家园dream2008.cn][中英双字幕].mp4 \ Friends.S03E01.mp4 想了一下,拼凑出下列命令,试了一下,可用。 ls | sed 's/\(\[.*\[f\)\([^]]*\)\(\].*\]\)\(.*\)/mv \1\2\3\4 F\2\4/'| sh ls | awk -F '[][]' '{printf "mv \""$0 "\" "$4$NF}'|sh ls | awk -F '[][]' \ '{for(i=1;i<=NF;i++)if…

2013-02-06 16:39 · Shell · 评论(0)

Shell随机生成几个不重复的随机数

模拟体育彩票,36选7。 要求:随机,7个不重复,1到36这些整数。 #!/bin/bash touch ./temp >temp while [ `sort temp | uniq | wc -l` != 7 ] do echo `expr $RANDOM % 36 + 1` >> temp done sort temp | uniq rm temp just for fun... 练习一下shell编程。

2013-02-04 23:48 · Shell · 评论(0)