├── README.md ├── code_example ├── 01-numInput │ └── numberInput.py ├── 02-circleCalc │ └── example2.py ├── 03-numberOps │ └── example3.py ├── 04-assignment │ └── example4.py ├── 05-haiku │ ├── 05-haiku.htm │ ├── 05-haiku.py │ └── Haiku.txt ├── 06-ifStatements │ ├── example6A.py │ ├── example6B.py │ └── example6C.py ├── 07-factorial │ └── 7-factorial.py ├── 08-primes │ └── 8-prime_simple.py ├── 09-inputCheck │ └── 9-input.py ├── 10-primeCheck │ └── 10-prime_check.py ├── 10.5-password │ └── password.py ├── 11-Palindromes │ ├── pal1.py │ ├── pal2.py │ ├── pal3.py │ ├── simpleStringFormatting.py │ └── timedPal.py ├── 12-simpleLists │ └── simpleLists.py ├── 13-forExample │ └── forExamples.py ├── 14-perfectNumbers │ └── perfect.py ├── 15-simpleTuples │ ├── simpleStringFormatting.py │ └── simpleTuples.py ├── 16-simpleFns │ └── simpleFns.py ├── 16B-perfectNumFn │ └── perfectFn.py ├── 16C-mileagePuzzle │ └── mileagePuz.py ├── 17-stringFormatting │ └── simpleStringFormatting.py ├── 18-simpleFile │ ├── simpleFile.py │ └── textFile.txt ├── 19-listComprehension │ └── simpleListComp.py ├── 20-sets │ ├── countWords.py │ ├── gburg.txt │ └── simpleSets.py ├── 21-dicts │ ├── gburg.txt │ ├── numbers.txt │ ├── phoneBook.py │ ├── simpleDicts.py │ └── wordFreq.py ├── 21.5-funcParams │ ├── defaultArgs.py │ └── simpleTime.py ├── 22-classes │ └── dateClass.py ├── 23-scope │ └── scope.py ├── 24-NoteClass │ ├── note.py │ └── notes.txt ├── 25-introspection │ └── introspection.py ├── 26-fraction class 1 │ └── fracClass1.py ├── 27-fraction class 2 │ └── Rational.py ├── 27A-properties │ └── props.py ├── 28-expansion class │ └── expansion.py ├── 29-sortableDict │ └── sortableDict.py ├── 29A-shapes │ └── shapes.py ├── 30-2DLists │ └── 2DList.py ├── 31-PythonArray │ └── 2dArray.py ├── 32-exceptionExamples │ ├── IOExceptions.py │ ├── exceptionFlow.py │ ├── fullException.py │ ├── raise.py │ ├── stackException.py │ └── test.txt ├── 33-sort_and_search │ ├── search.py │ └── sort.py ├── 34-scriptExample │ ├── gburg.txt │ └── scriptExample.py ├── 35_testing │ ├── doctestExample.py │ └── unittestExample.py ├── 36_Recursion │ ├── program16-1.py │ ├── program16-2.py │ ├── program16-3.py │ ├── program16-4.py │ ├── program16-5.py │ ├── program16-6.py │ ├── program16-7.py │ ├── program16-8.py │ └── program16-9.py ├── 37_PythonToC++ │ ├── dateClass.cpp │ └── dateClass.py ├── Example01 │ └── numberInput.py ├── OldPython2 │ ├── 18B-osModuleExamples │ │ ├── complexFileSearch.py │ │ └── simpleFileSearch.py │ ├── complexFileSearch.py │ └── simpleFileSearch.py └── py2html.py └── homework ├── proj01.dir ├── README.md ├── calc.png ├── project01.md ├── project01.pdf ├── simple.jpg └── solution.py ├── proj02.dir └── project02.pdf ├── proj03.dir └── Project03.pdf ├── proj04.dir └── project04.pdf ├── proj05.dir ├── Project05.pdf └── turtleSample.py ├── proj06.dir ├── project06.md ├── project06.pdf ├── solution.py ├── table.csv └── temp.csv ├── proj07.dir ├── easy.txt ├── hard.txt ├── listStr.py └── project07.pdf ├── proj08.dir ├── ap_docs.txt └── project08.pdf ├── proj09.dir ├── Project09.pdf ├── README.html ├── cardsBasic.py ├── cardsDemo.py ├── seahaven.py ├── seahavenStart.py ├── seahaven_cpython_3_2.pyc └── seahaven_cpython_3_3.pyc ├── proj10.dir ├── Project10.pdf ├── main.py ├── panama.txt └── senegal.txt └── proj11.dir └── Project11.pdf /README.md: -------------------------------------------------------------------------------- 1 | # 密歇根州立大学编程语言入门课后作业翻译 2 | 3 | 相比较国内大部分高校使用C语言作为大一新生入门语言,密歇根大学使用python来引领学生学习编程语言确实很好! 4 | python的优点就是简单,易学,强大,功能丰富,语法灵活,代码优美,效率很高,是一门全能的编程语言。 5 | 6 | 我们大一也是先学习C语言,当时学习的时候确实感觉比较难,毕竟C语言对语法要求比较严格, 7 | 比如变量使用前没定义,缺少分号,缺少引号,缺少头文件,大括号位置不对等都会引发好多错误, 8 | 对没接触过编程语言的新生来说往往不知道如何解决。 9 | C语言指针也是一大难点,初学者理解起来也比较费劲。这些都会打击新生的学习积极性。 10 | 11 | 第一门编程语言应该是什么样的?我认为,首先这门编程语言要足够简单,不能过分拘泥与语法规则, 12 | 还要尽可能得去隐藏一些底层的细节,否则容易分散学生的注意力,第一门编程语言要让学生知道什么是编程。 13 | 其次,要引起学生对编程的兴趣,让学生学过之后就能编写一些简单实用的小程序,这样学生就会有一种成就感。 14 | 还有,编程语言必须包括必要的编程思想,如条件,分支,循环,递归等。python具备这些优点,很适合初学者入门。 15 | 16 | 翻译这个项目的另一个目的是对中西放教育进行对比,看看国外知名大学是如何教新生学计算机的, 17 | 然后反思我们如何提高国内的教育教学水平。 18 | 19 | 目前只翻译这门课程的课后作业,并对课后作业进行解答。日后会考虑翻译课程的教材或演示文档, 20 | 欢迎对这个项目感兴趣的朋友一块参与进来。 21 | 22 | ### 课程主页 23 | 24 | - 课程主页: [http://www.cse.msu.edu/~cse231/](http://www.cse.msu.edu/~cse231/) 25 | - 教程: [http://www.cse.msu.edu/~cse231/Labs/](http://www.cse.msu.edu/~cse231/Labs/) 26 | - 课后作业: [http://www.cse.msu.edu/~cse231/Projects/](http://www.cse.msu.edu/~cse231/Projects/) 27 | 28 | ### 任务分配 29 | 30 | - proj01 ma6174 31 | - proj02 zhwei 32 | - proj03 liuzhe0223 33 | - proj04 tuteng 34 | - proj05 timeship 35 | - proj06 ma6174 36 | - proj07 37 | - proj08 38 | - proj09 39 | - proj10 40 | -------------------------------------------------------------------------------- /code_example/01-numInput/numberInput.py: -------------------------------------------------------------------------------- 1 | # first program in python 2 | # input two numbers, add them together, print them out 3 | # wfp, 9/1/07; rje, 5/14/12 4 | # hi mom 5 | 6 | num_str1 = input('Please enter an integer:') # hi mom 7 | num_str2 = input('Please enter a floating point number:') 8 | 9 | str1_int = int(num_str1) 10 | str2_float = float(num_str2) # this is a comment 11 | 12 | print('The numbers are: ',str1_int,' and ',str2_float) 13 | print('Their sum is:',str1_int+str2_float, 'and their product is:',str1_int*str2_float) 14 | -------------------------------------------------------------------------------- /code_example/02-circleCalc/example2.py: -------------------------------------------------------------------------------- 1 | # wfp, 5/30/07, wfp: updated 9/5/11; rje 5/14/12 2 | # prompt user for the radius, give back the circumference and area 3 | 4 | import math 5 | 6 | radius_str = input("Enter the radius of your circle:") 7 | radius_float = float(radius_str) 8 | 9 | circumference_float = 2 * math.pi * radius_float 10 | area_float = math.pi * radius_float * radius_float 11 | # also, area = math.pi * math.pow(radius_float,2) 12 | # also, area = math.pi * radius_float ** 2 13 | 14 | print() 15 | print("The cirumference of your circle is:",circumference_float,\ 16 | ", and the area is:",area_float) 17 | -------------------------------------------------------------------------------- /code_example/03-numberOps/example3.py: -------------------------------------------------------------------------------- 1 | # example3.py, wfp, 5/3/07. wfp 9/5/11 updated names 2 | # some simple examples of number manipulations 3 | # wfp, 1/3/13, to py3 4 | 5 | import math 6 | 7 | a_int = 3 8 | b_int = 4 9 | c_int = 5 10 | 11 | # we can chain multiple assignments onto one line separated by a ";" 12 | x_float = 6.5; y_float= 9.5 13 | 14 | print("Integer computations") 15 | # a print with no string just prints a new line 16 | print() 17 | print(a_int, " + ", b_int, " equals ", a_int + b_int) 18 | print(a_int, " - ", b_int, " equals ", a_int - b_int) 19 | print(a_int, " * ", b_int, " equals ", a_int * b_int) 20 | # division is always a float % gets the remainder 21 | print(a_int, " / ", b_int, " equals ", a_int / b_int) 22 | # integer division done with // 23 | print(a_int, " // ", b_int, " equals ", a_int//b_int," remainder ", a_int % b_int) 24 | print() 25 | print(c_int, " + ", 5, " equals ", c_int + 5) 26 | print(c_int, " - ", 5, " equals ", c_int - 5) 27 | print(c_int, " * ", 5, " equals ", c_int * 5) 28 | print(c_int, " / ", 5, " equals ", c_int / 5, " remainder ", c_int % 5) 29 | 30 | print() 31 | print("Real number computations") 32 | print() 33 | print(x_float, " + ", y_float, " equals ", x_float + y_float) 34 | print(x_float, " - ", y_float, " equals ", x_float - y_float) 35 | print(x_float, " * ", y_float, " equals ", x_float * y_float) 36 | print(x_float, " / ", y_float, " equals ", x_float / y_float) 37 | 38 | print() 39 | print("Mixed computations ") 40 | print() 41 | # note that the integer is "promoted" to a float 42 | print(a_int, " - ", x_float, " equals ", a_int - x_float) 43 | print(a_int, " / ", x_float, " equals ", a_int / x_float) 44 | 45 | print() 46 | print("Compound Computations, precedence") 47 | print(a_int,"+",b_int,"/",c_int," equals",a_int+b_int/c_int) 48 | print("(",a_int,"+",b_int,")/",c_int," equals ",(a_int+b_int)/c_int) 49 | 50 | print() 51 | print("Simple Type Conversion") 52 | print("Value of a_int is:",a_int) 53 | # note that the value of a_int is not changed, but a new float value is returned! 54 | print("Value of float(a_int) is:",float(a_int)) 55 | print("Value of x_float is:",x_float) 56 | # again, x_float not changed but the returned value loses information 57 | print("Value of int(x_float) is:",int(x_float)) 58 | 59 | -------------------------------------------------------------------------------- /code_example/04-assignment/example4.py: -------------------------------------------------------------------------------- 1 | # rje, 05/07 Example 4 Assignments 2 | # wfp, 05/31/07, updated: 3 | # wfp, 8/13/11, updated names 4 | # rje, 5/15/12 Python 3 5 | # - include short circuit 6 | # - T/F compares 7 | # - var names 8 | # - str compares 9 | 10 | a_bool,b_bool = True, False 11 | c_int,d_int = 3, 8 12 | e_str,f_str = 'z', '5' 13 | 14 | print('Booleans a_bool, b_bool: ', a_bool, b_bool) 15 | print('Integers c_int, d_int: ', c_int, d_int) 16 | print('Characters (strings) e_str, f_str: ', e_str, f_str) 17 | 18 | print() 19 | print("More on True and False") 20 | print("a_bool== True:", a_bool==True) 21 | print("True == 1:", True == 1) 22 | # remember, anything not false is logically true 23 | print("True == 2:", True == 2) 24 | print("b_bool == False:", b_bool == False) 25 | print("False == 0:", False == 0) 26 | # anything empty is in fact logically false 27 | print("False == []:", False == []) 28 | 29 | print() 30 | print("Some Comparison Results, c_int=3, d_int=8, e_str='z', f_str='5'") 31 | print('c_int == d_int: ', c_int == d_int) 32 | print('c_int != d_int: ', c_int != d_int) 33 | print('c_int < 3: ', c_int < 3) 34 | print('c_int <= 3: ', c_int <= 3) 35 | print('c_int > 3: ', c_int > 3) 36 | print('c_int >= 3: ', c_int >= 3) 37 | print("e_str == 'z':", e_str == 'z') 38 | print("e_str >= 'a':", e_str >= 'a') 39 | print("f_str >= e_str:", f_str >= e_str) 40 | print("f_str == '5'", f_str == '5') 41 | print("f_str == 5:", f_str == 5) 42 | 43 | print() 44 | print("Some Boolean Results: c_int=3 and d_int=8") 45 | print('c_int >= 5 or d_int <= 9: ', c_int >= 5 or d_int <= 9) 46 | print('c_int >= 0 and d_int >= 4: ', c_int >= 0 and d_int >= 4) 47 | print('c_int != d_int or c_int < -6: ', c_int != d_int or c_int < -6) 48 | print('c_int <= 0 and d_int > -6: ', c_int <= 0 and d_int > -6) 49 | print('1 <= c_int and c_int <= 5: ', 1 <= c_int and c_int <= 5) 50 | print('1 <= d_int and d_int <= 5: ', 1 <= d_int and d_int <= 5) 51 | 52 | print() 53 | print("Some Short Circuit Results") 54 | print("True or False", True or False) 55 | print("True and False", True and False) 56 | print("2 or 3:", 2 or 3) 57 | print("0 or 3:", 0 or 3) 58 | print("2 and 3:", 2 and 3) 59 | print("3 and 0:", 3 and 0) 60 | print("'' and 3:", '' and 3) 61 | 62 | 63 | -------------------------------------------------------------------------------- /code_example/05-haiku/05-haiku.htm: -------------------------------------------------------------------------------- 1 | <>
# rje, 5/07, example 5
 2 | # list the words in Windows Haiku
 3 | 
 4 | import os
 5 | haiku = ''
 6 | for line in file("Haiku.txt"):
 7 |     haiku += line
 8 | print haiku
 9 | print "\n"
10 | words = haiku.split()
11 | wordList = []
12 | for w in words:
13 |     w = w.strip(' .,;:"!-?')
14 |     w = w.lower()
15 |     wordList.append(w)
16 | wordList.sort()
17 | for a in wordList:
18 |     print a
<