[关闭]
@Itsu 2015-05-13T04:48:23.000000Z 字数 11595 阅读 2530

LECTURE 1


PROBLEM 1

  1. What is the difference between an Algorithm and a Program?

    • An algorithm is a conceptual idea, a program is a concrete instantiation of an algorithm.
    • An algorithm is limited to mathematical operation, a program can specify all kinds of operations.
    • An algorithm makes a slow program run fast.
    • An algorithm deals with computer hardware, a program deals with computer software.
  2. True or False? A computational mode of thinking means that everything can be viewed as a math problem involving numbers and formulas.

    • True
    • False
  3. True or False? Computer Science is the study of how to build efficient machines that run programs.

    • True
    • False
  4. The two things every computer can do are:

    • Perform calculations
    • Convert electricity to numbers
    • Display results to a screen
    • Remember the results.

PROBLEM 2

True or False?

  1. Declarative knowledge refers to statements of fact.

    • True
    • False
  2. Imperative knowledge refers to 'how to' methods.

    • True
    • False
  3. The recipe for deducing square root cannot be made into a real computer program because computers cannot guess a starting value for y.

    • True
    • False

PROBLEM 3

  1. True or False? A stored program computer is designed to compute precisely one computation, such as a square root, or the trajectory of a missile.

    • True
    • False
  2. True or False? A fixed program computer is designed to run any computation, by interpreting a sequence of program instructions that are read into it.

    • True
    • False
  3. A program counter

    • counts the number of primitive operations executed by the program.
    • counts the number of primitive operations comprising a complex operation.
    • points the computer to the next instruction to execute in the program.
    • remembers how many times a program has been executed.
  4. What does it mean when we say that "the computer walks through the sequence executing some computation"?

    • The computer tests each instruction to ensure it will not harm the circuitry.
    • The computer executes the instructions in strict, linear sequence, just like walking in a straight line.
    • The computer executes the instructions mostly in a linear sequence, except sometimes it jumps to a different place in the sequence.
    • The computer slowly executes instructions so that we can follow its progress, rather than running a program at full speed.
  5. True or False? In order to compute everything that is computable, every computer must be able to handle the sixteen most primitive operations.

    • True
    • False

Problem Set 0


Tab 1 - SETTING UP YOUR ENVIRONMENT

In this course, you will be using a programming environment to write Python programs. We recommend you install and use the Enthought Canopy Python Distribution. If you are taking this course again and have an older version of Canopy installed, we urge you to update to the latest version from the Welcome screen or the Help menu or to uninstall and reinstall the newest version.

Note that previous offerings of this course used IDLE as a programming environment. IDLE is mentioned in some of the lectures but you can use Canopy instead. The course staff can offer support if you use IDLE or Canopy.

SETTING UP YOUR ENVIRONMENT

This initial problem set is simply intended to help you install and test your Python computing environment for use in 6.00.1x this semester. There is no graded assignment for this problem set, but you should still do it as soon as possible. By the end of this problem set, you should be able to execute simple Python commands and expressions, create a file with Python code, and run it in a Python environment.

INSTALLING ON YOUR OWN MACHINE

If you have the ability to install files on your own machine, we strongly recommend that you do so. This will give you the ability to complete your assignments - and future projects after 6.00.1x - on your machine, at any time, regardless of Internet connectivity.

For this course, we will be using Enthought Canopy Python Distribution, which you can download here.

NOTE: Do NOT install any version of Python 3. In 6.00.1x, we will be using Python version 2. 7, and ask that you do the same. If you use any other version of Python 3, your solutions will not be compatible with our tutor platform. Some of your answers will be incorrect. Enthought Canopy provides the proper version of Python, so you don't need to worry about this if you are using Enthought.

How to get started with Enthought
Enthought Canopy includes:

This Canopy application is similar to Python's built-in Idle editor, but is easier to use, more powerful, and suitable for use in your future Python projects.

Your first task is to download Enthought Canopy onto your machine and install it. By clicking on the download link above, you will be taken to a Canopy download web page, which should recognize your operating system and provide the correct version. The next three pages in this problem set present specific instructions for Windows, Mac, and Linux operating systems. Choose the instructions for your computer’s operating system and read on!

If you have questions about installation, post a question in the forum. For general questions, post in the forum at the bottom of this page. For questions specific to your operating system, please post in the forum at the bottom of the specific page that discusses installation instructions for Windows, Mac or Linux.

The course officially uses Canopy, but If you have other IDEs that you have used and recommend, post a description and a link to them in the "Related Resources" section below.

Lecture 2


PROBLEM 1

For each of the following expressions, indicate the type of the expression. While you could simply type these expressions into an IDLE or Canopy shell, we encourage you to answer them directly since this will help reinforce your understanding of basic Python expressions.

Note: The Python 'None' keyword
In Python, the keyword None is frequently used to represent the absence of a value.None is the only value in Python of type NoneType.


PROBLEM 2

For each of the following expressions, indicate the value returned, or if the evaluation would lead to an error, write the word 'error' (note this is a word, not a string). While you could simply type these expressions into an IDLE or Canopy shell, we encourage you to answer them directly since this will help reinforce your understanding of basic Python expressions.

For decimal answers, give the full result, or at least four decimal places of accuracy (whichever is shortest).

In this class, we will be using Python 2. 7 and not Python 3. 0. Take a look at Problem Set 0 to see how to install a Python Integrated Development Environment, if you would like to start typing in the expressions in these and future exercises to see their result.


PROBLEM 3

For each of the following expressions, indicate the value returned, or if the evaluation would lead to an error, write the word 'error' (note this is a word, not a string). While you could simply type these expressions into an IDLE or Canopy shell, we encourage you to answer them directly since this will help reinforce your understanding of basic Python expressions.

Hint: Python boolean types
Remember that in Python words are case-sensitive. The word Ture is a Python keyword (it is the value of the Boolean type) and is not the same as the word true. Refer to the Python documentation on Boolean values.

Hint: Priority order of Boolean operations
For these problems, it's important to understand the priority of Boolean operations. The order of operations is as follows:

  • Parentheses. Before operating on anything else, Python must evaluate all parentheticals starting at the innermost level.
  • not statements.
  • and statements.
  • or statements.

What this means is that an expression like

  1. not True and False

evaluates to False, because the not is evaluated first (not True is False), then the and is evaluated, yielding False and False which is False.

However the expression

  1. not (True and False)

evaluates to True, because the expression inside the parentheses must be evaluated first - True and False is False. Next the not can be evaluated, yielding not False which is True.

Overall, you should always use parenthesis when writing expressions to make it clear what order you wish to have Python evaluate your expression.
As we've seen here, not (True and False) is different from (not True) and False - but it's easy to see how Python will evaluate it when you use parentheses. A statement like not True and False can bring confusion!


PROBLEM 4

For each of the following expressions, indicate the type of the expression and the value returned, or, if the evaluation would lead to an error, choose the type 'NoneType' and write the word 'error' (note this is a word, not a string) as the value returned.

While you could simply type these expressions into an IDLE or Canopy shell, we encourage you to answer them directly since this will help reinforce your understanding of basic Python expressions.


PROBLEM 5

Below is a transcript of a session with the Python shell. For each expression being evaluated, provide the type and the value the expression returns. If evaluating an expression would cause an error, select 'NoneType' and write the word 'error' (note this is a word, not a string) in the box. While you could simply type these expressions into an Idle shell, we encourage you to answer them directly since this will help reinforce your understanding of basic Python expressions.

Assume that the expressions are evaluated in the order shown - that is, each problem part is evaluated directly after the previous problem part(s).


PROBLEM 6

For each of the following expressions, indicate the value returned, or if the evaluation would lead to an error, write the word 'error' (note this is a word, not a string). While you could simply type these expressions into an Idle shell, we encourage you to answer them directly since this will help reinforce your understanding of basic Python expressions.

PROBLEM 7

For each of the expressions below, specify its type and value. If it generates an error, select type 'NoneType' and write the word 'error' (note this is a word, not a string) in the box for the value. While you could simply type these expressions into an Idle shell, we encourage you to answer them directly since this will help reinforce your understand of basic Python expressions.

Assume we've made the following assignments:

  1. > str1 = 'hello'
  2. > str2 = ','
  3. > str3 = 'world'

Note: The Python 'in' operator
The operators in and not in test for collection membership (a 'collection' refers to a string, list, tuple or dictionary - don't worry, we will cover lists, tuples and dictionaries soon!). The expression

  1. element in coll

evaluates to True if element is a member of the collection coll, and False otherwise.

The expression

  1. element not in coll

evaluates to True if element is not a member of the collection coll, and False otherwise.
Note this returns the negation of element in coll - that is, the expression element not in coll is equivalent to the expression not (element in coll).

Note: Advanced String Slicing
You've seen in lecture that you can slice a string with a call such as s[i:j], which gives you a portion of string s from index i to index j-1. However this is not the only way to slice a string! If you omit the starting index, Python will assume that you wish to start your slice at index 0. If you omit the ending index, Python will assume you wish to end your slice at the end of the string. Check out this session with the Python shell:

  1. \>>> s = 'Python is Fun!'
  2. \>>> s[1:5]
  3. 'ytho'
  4. \>>> s[:5]
  5. 'Pytho'
  6. \>>> s[1:]
  7. 'ython is Fun!'
  8. \>>> s[:]
  9. 'Python is Fun!'

That last example is interesting! If you omit both the start and ending index, you get your original string!
There's one other cool thing you can do with string slicing. You can add a third parameter, k, like this: s[i:j:k]. This gives a slice of the string s from index ito index j-1, with step size k. Check out the following examples:

  1. \>>> s = 'Python is Fun!'
  2. \>>> s[1:12:2]
  3. 'yhni u'
  4. \>>> s[1:12:3]
  5. 'yoiF'
  6. \>>> s[::2]
  7. 'Pto sFn'

The last example is similar to the example s[:]. With s[::2], we're asking for the full string s (from index 0 through 13), with a step size of 2 - so we end up with every other character in s. Pretty cool!


PROBLEM 8

Write a piece of Python code that prints out the string hello world


PROBLEM 9

For each of the following expressions, indicate the value that prints out when the expression is evaluated. If the evaluation would lead to an error, write the word 'error'; if nothing would print out, write the word 'blank'.

While you could simply type these expressions into an IDLE or Canopy shell, we encourage you to answer them directly since this will help reinforce your understanding of basic Python expressions.

PS: If the temperatures seem weird to you, like most of the world, you probably use the Celsius system. We Americans still use the crazy Fahrenheit system…


PROBLEM 10

Write a piece of Python code that prints out the string 'hello world' if the value of an integer variable, happy, is strictly greater than 2.
For problems such as these, do not include raw_input statements or define the variable happy in any way. Our automated testing will provide a value of happy for you - so write your code in the following box assuming happy is already defined.


PROBLEM 11

Assume that two variables, varB and varB, are assigned values, either numbers or strings.
Write a piece of Python code that prints out one of the following messages:

For problems such as these, do not include raw_input statements or define the variable varA or varB. Our automating testing will provide values of varA andvarB for you - so write your code in the following box assuming varA and varB are already defined.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注