ITeye博客

2013 年文章归档

共 21 篇文章

[learn]sendEmail示范Perl脚本编写

sendEmail是一个命令行发Email的工具,示范了Perl脚本编写。 转来学习一下。 http://caspian.dotconf.net/menu/Software/SendEmail/ #!/usr/bin/perl -w ############################################################################## ## sendEmail ## Written by: Brandon Zehm <caspian@dotconf.net> ## ## License: ## sendEmail (hereafter r…

2013-11-07 16:53 · perl

[learn]kerl示范Bash脚本的编写

kerl是一个类似rvm的工具脚本,是下载管理erlang版本用的。 示范一些Shell脚本编写的规范,转过来学习一下。 https://github.com/spawngrid/kerl #! /bin/sh # Copyright (c) 2011, 2012 Spawngrid, Inc # Copyright (c) 2011 Evax Software <contact(at)evax(dot)org> # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this…

2013-11-07 16:50 · Shell

寻找1729 by awk

记得拉马努金关于1729的故事吗? 小脚本算出1729 #/bin/bash echo | awk ' { input = 0 } END { while(check_sum(input) < 2) { input++ } print input } function check_sum(argi) { r = 0 for (i = 1; i < int(argi/2); i++) if ((check_cube(i) == 1) && (check_cube(argi - i) == 1) && (argi != i*2)) { r++ } return r } function chec…

2013-10-30 10:25 · Shell

awk版堆排序 heapsort

The AWK Programming Language书里 7.1节的heapsort段落 #/bin/bash echo | awk ' { A[nr] = $0 } END { hsort(A, NR) for (i = 1; i <= NR; i++) { print A[i] } } function hsort(A, n, i) { for (i = int(n/2); i >= 1; i--) { heapify(A, i, n) } for (i = n; i > 1; i--) { { swap(A, 1, i) } { heapify(A, 1, i-1) } } } f…

2013-10-29 10:56 · Shell

无题 2013.10.22

总是在走路或者坐公交车的时候发呆,胡思乱想,然后就想写点什么。可是每到坐到电脑跟前的时候,就四处灌水、闲扯、看美剧、打发时间。真的荒废了好多好多。 很佩服那些坚持写东西的人,哪怕只是简单的博客或者日记,日积月累,也能收获很多。 其实不止是写东西,做任何事情都是贵在坚持啊。 马上就要26周岁了,时光飞逝,要抓紧时间做点正经的事情了。

2013-10-22 20:18 · 非技术

C语言实现链表

大作业水平... #include<stdio.h> #include<stdlib.h> struct node{ struct node* next; int c; }; struct node* init_node(int input) { struct node* p; p = malloc(sizeof(struct node)); p->c = input; return p; } void insert_node(struct node *head, int new) { struct node *temp = (struct node*)(malloc(sizeof(stru…

2013-10-17 12:30 · C

[Scheme]循环读入并计算数字之和

一段guile脚本文件,循环读入并计算从stdin输入的两个数之和。 #!/usr/local/bin/guile -s !# (define (main) (let ((a (read (current-input-port))) (b (read (current-input-port)))) (if (not (eof-object? a)) (begin (write (+ a b) (current-output-port)) (write-char #\newline (current-output-port)) (main))))) (main)

2013-09-23 16:52 · scheme

博客要荒废了

灌一篇。 知乎上看到一个问答很有意思: 有哪些事,是可以每天坚持 10 分钟,然后造成巨大改变的? http://zhi.hu/TG8H 总结一下,适合我做的就是:写日记,背单词,看书,冥想,锻炼身体。

2013-07-04 23:29 · 非技术

perl在一个文件里搜索一个pattern

perl在一个文件里搜索一个pattern #!/usr/bin/perl open FH,'< ./input.file'; @lines = <FH>; foreach $line (@lines) { print $line if $line =~ m/pattern/; }

2013-02-26 21:01 · perl

C++的STL释放内存

vector<int> vec_int; ...; vec_int.clear(); vector<int>().swap(vec_int); swap这一步必须要啊... 否则内存泄露的一塌糊涂啊... 这小技巧很值得学习。

2013-02-24 23:07 · C++

gentoo安装感受(1)

gentoo安装感受 发现仅仅是读一遍安装手册和文档都有所收获... gentoo的更新相当快,用的镜像是install-x86-minimal-20121213.iso 刚刚在vbox里装了最新的gentoo-3.5.7,完成了安装手册的前10章。 对fdisk分区、/etc/{init.d,conf.d,rc.conf}这几个目录和grub稍有了解。 emerge也挺有趣的。 暂告一段落了,等会睡觉去了。 发现官方的中文文档没有维护到最新,有两处错: 8.b的代码2.8应该去掉,进行下边的操作。 个人理解应该是/etc/init.d/目录下的文件才能进行rc-update add cd …

2013-02-18 17:55 · gentoo

统计学习方法(1)

看了李航的统计学习方法( http://book.douban.com/subject/10590856/ ) 极粗略的看了前五章。 第2章,感知机,主要思想是计算出一个超平面把(可分类的)训练数据做分类。算法是随机选一个分界超平面,用已标注的训练数据逐渐做调整,有一个学习率的参数做调整。 第3章,K-NN。具体实现采用kd树的数据结构。 第4章,朴素贝叶斯,用先验概率和条件概率做分类依据。简化高效,性能不高。 第5章,决策树。用信息增益和信息增益比做分类依据。 ID3选取最大的信息增益的一类特征,用阈值卡一下,然后递归进行。 C4.5和ID3很像,用的是信息增益比。 有决策树的剪枝算法 C…

2013-02-16 00:36 · 非技术

最大公约数的bash脚本

#!/usr/bin/bash gcd() { until test 0 -eq "$2"; do set -- "$2" "`expr "$1" % "$2"`" done test 0 -gt "$1" && set -- "`expr 0 - "$1"`" echo "$1" } gcd $1 $2 此处的set命令用--的参数将$2和'expr $1 % %2'的值交换,比较简洁。

2013-02-15 21:37 · Shell

几个很简单的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

分解质因数小脚本

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

django教程写的好

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

2013-02-12 18:35 · python

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

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

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

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

尝试用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