Python学习笔记(rev.0)

String Manipulation:

Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.

Like strings (and all other built-in sequence type例如 "str"), lists can be indexed and sliced

List Manipulation:

stupidly wrong: 
>>> cubes = [1, 2, 3, 4, 5]
>>> cubes[:][0] = 0
>>> cubes
[1, 2, 3, 4, 5]
 
correct:
>>> cubes[:1] = [0]
>>> cubes
[0, 2, 3, 4, 5]
 
 

Shallow& Deep Copying:

The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):

  • A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.

  • A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

Demonstration:

 

import copy

a = [1, 2, 3]
b = [4, 5, 6]
c = [a, b]

#Using normal assignment operatings to copy:

d = c

print id(c) == id(d)          # True - d is the same object as c
print id(c[0]) == id(d[0])    # True - d[0] is the same object as c[0]

#Using a shallow copy:

d = copy.copy(c)

print id(c) == id(d)          # False - d is now a new object
print id(c[0]) == id(d[0])    # True - d[0] is the same object as c[0]

#Using a deep copy:

d = copy.deepcopy(c)

print id(c) == id(d)          # False - d is now a new object
print id(c[0]) == id(d[0])    # False - d[0] is now a new object

Access level modifiers的区别

阅读全文

ArrayList刚用到的几个method

1. ArrayList  合并2个arraylist's, 即把第二个合并到第一个里面去,而不是产生一个新的

Method Signiture:

addAll(Collection<? extends E> c)

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.

List<String> newList = new ArrayList<String>(listOne);
newList.addAll(listTwo);

以上即为stackoverflow上的建议代码.

 

 

 

File, formater

public class WordCount {

	// EDIT DIR PATH WHEN NEEDED, DEFINED AS A CONSTANT
	private static final String FILE_DIR = "//Users//charles//Documents//workspace//assignment1//";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// list to store file names
		List<String> fileNameList = new ArrayList<String>();
		// total size of files
		int total = 0;

		// input file names
		System.out.println("Enter the files to be calculated(\"q\" to indicate no more files to input): ");

		// read all possible file names
		try{
			BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
		String s = bufferRead.readLine();
			while(!s.equals("q")){
				fileNameList.add(s);
				s = bufferRead.readLine();
			}

		}catch(IOException e){
			e.printStackTrace();
		}

		// attempt to open files 
		for(String fileName : fileNameList){
			File file = new File(FILE_DIR.concat(fileName));

			// if file exists, format print it, and recalculate total size
			// exception if file cannot be accessed
			try{
				if(file.exists()){

					System.out.println(String.format("%7d", file.length()) +" "+ fileName);
					total += file.length();
				}
			}catch(SecurityException e){
				System.out.println("Cannot access file: " + fileName);
			}
		}

		// print total size
		System.out.println(String.format("%7d", total) + " total");

	}
// this is the comment i made from GVim, good !~~~
}

 

fdgdg

How to find files with specific extension

import java.io.*;
 
public class FindCertainExtension {
 
	private static final String FILE_DIR = "c:\\folder";
	private static final String FILE_TEXT_EXT = ".jpg";
 
	public static void main(String args[]) {
		new FindCertainExtension().listFile(FILE_DIR, FILE_TEXT_EXT);
	}
 
	public void listFile(String folder, String ext) {
 
		GenericExtFilter filter = new GenericExtFilter(ext);
 
		File dir = new File(folder);
 
		if(dir.isDirectory()==false){
			System.out.println("Directory does not exists : " + FILE_DIR);
			return;
		}
 
		// list out all the file name and filter by the extension
		String[] list = dir.list(filter);
 
		if (list.length == 0) {
			System.out.println("no files end with : " + ext);
			return;
		}
 
		for (String file : list) {
			String temp = new StringBuffer(FILE_DIR).append(File.separator)
					.append(file).toString();
			System.out.println("file : " + temp);
		}
	}
 
	// inner class, generic extension filter
	public class GenericExtFilter implements FilenameFilter {
 
		private String ext;
 
		public GenericExtFilter(String ext) {
			this.ext = ext;
		}
 
		public boolean accept(File dir, String name) {
			return (name.endsWith(ext));
		}
	}
}

Ut nonummy habent soluta claritas veniam. Typi nunc soluta hendrerit mutationem sollemnes. Quis lius dolore et insitam vel. Aliquip consequat futurum claram ut mazim. Facilisi accumsan dolore ii imperdiet consequat. Claritatem aliquip quod putamus vulputate iusto. Doming minim typi zzril lius usus. In clari mutationem autem non sit. Qui augue mirum dynamicus gothica ut. Demonstraverunt processus soluta sequitur autem demonstraverunt.

 

先留个底,过两天过来补完

calculate actual time的经典复杂方式,记忆犹新,还是想贴出来

private void calculateTime(int month,int year){ i=true; while (i==true){/* The logic is a loop using.*/ if(month==4||month==6||month==9||month==11){ // If the month only has 30 days,but the day greater than 30, if(new_d>30){ new_d=new_d-30;month++; // then the day should minus 30 and the month should add 1. }else i=false; }else if(month==2){ if ((year%4==0&&year%100!=0)||(year%400==0&&year%3200!=0)){ /* The leap year and in Febuary.*/ if(new_d>29){ /* Febuary will has 29 days.*/ new_d=new_d-29;month++; }else i=false; }else{ /* Febuary in a normal year.*/ if(new_d>28){ /* Febuary only has 28 days.*/ new_d=new_d-28;month++; }else i=false; } }else if (month==1||month==3||month==5||month==7||month==8||month==10||month==12){ if(new_d>31) { new_d=new_d-31;month++; }else i=false; } new_m=month; if(month>12){ //If month is greater than 12, new_y=year+month/12; //both the year and the month will be changed. new_m=month%12; month=new_m; year=new_y; } } //The loop can ensure that the month and the year keep changing until the day is in a correct range. }
 
PS. 到现在还是想不明白,当时为了计算正确的叠加后的月份与日子,尽然想了很久,最后还是借鉴了别人的答案。不过就是一步一步得叠加秒与分钟,叠加到月的时候用个循环罢了,为什么当时就觉得那么复杂呢?
由此我又要检讨自己了。这是一个相当正常的思路,quote
 
if(month>12){              //If month is greater than 12,
new_y=year+month/12;   //both the year and the month will be changed.
 
可是我却想了很久,就是因为我没有把思路理清,为何没有理清呢?就是源于没有把我觉得复杂的可能性列在纸上,连笔都不愿意动的人,怎么能够独立思考一道题,更不要说得到最终的个人最佳答案,我该狠狠得反省了。很多结果的生成并不是因为实力的不足够,而是源于信心的不充足,对自己的不信任,不能够冷静得把逻辑理清楚,是浮躁了!成功是需要静下心来想办法达到的,这次冒险过了,下次就不能luck out。
 
谨以此文逼迫自己谨记假期梦中的haunting headache。
 
 

 

 

 

 

 

 

新的开始

今天开始仔细学习git的工作原理,还有egit的用法。 加油!