
编程小记
沙泓州
这个作者很懒,什么都没留下…
展开
-
python读大文件
先前需要做一个使用python读取大文件(大于1G),并逐条存入内存进行处理的工作。做了很多的尝试,最终看到了如下的文章。http://stackoverflow.com/questions/8009882/how-to-read-large-file-line-by-line-in-python 该文章实际上提供了集中读取大文件的方式,先经过测试总结如下1. for line i转载 2014-10-17 11:44:34 · 417 阅读 · 0 评论 -
python 3里没有cmp这个函数了
3开始没这个函数了,官方文档是这么写的The cmp() function should be treated as gone, and the __cmp__() special method is no longer supported. Use __lt__() for sorting, __eq__() with __hash__(), and other rich compariso转载 2014-10-19 09:13:45 · 3575 阅读 · 1 评论 -
二叉树的最短根到叶路径中点的个数
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */cla原创 2014-10-21 23:29:46 · 402 阅读 · 0 评论 -
平衡树
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe原创 2014-10-21 23:57:35 · 594 阅读 · 0 评论 -
Python SocketAPI
将上节中的C#该成Python版的容易程度大大超出了我的意料之外。从来没有发现,仅仅用灰尘简单的几句话就实现了该程序的主要功能,可见python的简易和强大之处。这里先对SocketAPI 做一下总结。Socket API 笔记1、Socket的地址表示单独的字符串,用于AF_UNIX地址族(host,port)对,用于AF_INET地址族。其中ho转载 2014-12-25 22:31:21 · 389 阅读 · 0 评论 -
序列S的所有可能情况
输入正整数m和n 输出n个数,形成一个序列S={s1,s2,...,sn}。其中s_i满足取值为[0,m-1],且后一个数比前一个数大(s_i > =s_j if i >= j),输出S的所有可能情况。原创 2015-06-25 18:10:11 · 537 阅读 · 0 评论 -
Python使用heapq实现小顶堆(TopK大)、大顶堆(BtmK小)
需求:给出N长的序列,求出TopK大的元素,使用小顶堆,heapq模块实现。01import heapq02import random03 04class TopkHeap(object):05 d转载 2015-06-09 17:55:20 · 1575 阅读 · 0 评论 -
matplotlib使用指南
http://mytrix.me/2013/08/matplotlib-animation-tutorial/原创 2015-06-15 09:34:20 · 232 阅读 · 0 评论 -
matlab2012b与matlab7.1执行set(gca,'Yscale','log')之后画到的直方图结果居然不同
这是一个之前没发现的现象。同样一段代码:b=10000;c=randn(1, b); %产生10000个正态分布的随机数d=100;[a,b]=hist(c,d); %平均分成100份a=a/length(c); %把个数转换成频度bar(a);原创 2014-10-10 20:14:50 · 3091 阅读 · 0 评论 -
[LeetCode]Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321考虑输入是abc,返回结果是cba,那么如果用除法(除以10)取余数操作的话,是先入先出的操作(第一次入abc%10=c),因此选择使用队列。复习队列的方法有q.size(),q.front()原创 2014-10-27 21:53:53 · 628 阅读 · 0 评论 -
后台程序结果重定向到文件,结果看不到文件?缓冲区的问题
工作之余,遇到了一个问题。就是当我在执行转载 2014-10-24 17:47:27 · 1154 阅读 · 1 评论 -
交叉验证(CrossValidation)方法思想简介
以下简称交叉验证(Cross Validation)为CV.CV是用来验证分类器的性能一种统计分析方法,基本思想是把在某种意义下将原始数据(dataset)进行分组,一部分做为训练集(train set),另一部分做为验证集(validation set),首先用训练集对分类器进行训练,在利用验证集来测试训练得到的模型(model),以此来做为评价分类器的性能指标.常见CV的方法如下:转载 2014-04-20 17:07:45 · 562 阅读 · 0 评论 -
去除Python readline()函数读入的\n
用Python的readline()函数读入文件的一行,读入的结果会带有换行符,如'\n',例如,文件内容为1 2 3 4执行:f = open('test.txt')a = f.readline()print a结果为 ' 1 2 3 4\n'可用如下方法解决f = open('test.txt')a = f.readline()l = len(a)转载 2014-05-12 15:14:11 · 1601 阅读 · 0 评论 -
linux下调试python程序
之前调试python程序都是用print参数,感觉有点弱爆啊,最近发现python也有类似C语言gdb的工具pdb,记录下pdb的使用方法和心得。 先找了段简单的测试程序:#!/usr/bin/pythonfrom ftplib import FTPimport sysimport socketimport pdbdef passwordCorrect(ip,por转载 2014-05-12 11:45:15 · 3670 阅读 · 0 评论 -
如何清空dns缓存
一、Linux下清空DNS缓存 Linux下DNS缓存实现通常有两种方式:一种是用DNS缓存程序NSCD(name service cache daemon)负责管理DNS缓存。另一种实现DNS缓存则是用Bind来架设Caching Name Server来实现。 如果是清除NSCD上的Cache,可重新启动NSCD服务来达成清除DNS Cache的效果。用这个命令:?转载 2014-05-12 13:19:06 · 1715 阅读 · 0 评论 -
使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)
一、为什么要使程序在后台执行我们计算的程序都是周期很长的,通常要几个小时甚至一个星期。我们用的环境是用putty远程连接到日本Linux服务器。所以使程序在后台跑有以下三个好处:1:我们这边是否关机不影响日本那边的程序运行。(不会像以前那样,我们这网络一断开,或一关机,程序就断掉或找不到数据,跑了几天的程序只能重头再来,很是烦恼)2:不影响计算效率2:让程序在后台跑后,不会占据终端转载 2014-05-13 14:02:41 · 6022 阅读 · 0 评论 -
urllib2.urlopen超时问题
问题描述: 没有设置timeout参数,结果在网络环境不好的情况下,时常出现read()方法没有任何反应的问题,程序卡死在read()方法里,搞了大半天,才找到问题,给urlopen加上timeout就ok了,设置了timeout之后超时之后read超时的时候会抛出socket.timeout异常,想要程序稳定,还需要给urlopen加上异常处理,再加上出现异常重试,程序就完美了。转载 2014-05-14 15:35:48 · 811 阅读 · 0 评论 -
回文题
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a原创 2014-10-21 22:10:26 · 591 阅读 · 0 评论 -
给定二叉树求最小深度
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node./** * Definition for binary原创 2014-10-21 23:28:14 · 424 阅读 · 0 评论 -
从有序链表中去掉重复的
Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.关键点:判断比较节点为空后,别忘了把当前节点的原创 2014-10-23 08:10:52 · 492 阅读 · 0 评论 -
判断链表是否有环
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?关键点:1)判断链表是否有环。2)一个小坑在判断root和root的next是否为空上。3)可以看为追及问题。最关键的坑在判断快走(每次走2步的节点),走1步会原创 2014-10-22 22:56:24 · 448 阅读 · 0 评论