background preloader

Electrical Engineering and Computer Science

Electrical Engineering and Computer Science

Search Algorithms | Unit 4: Probability and Planning | Introduction to Electrical Engineering and Computer Science I | Electrical Engineering and Computer Science Introduction to Electrical Engineering and Computer Science I | Electrical Engineering and Computer Science 8.7. sets — Unordered collections of unique elements — Python v2.7.5 documentation Deprecated since version 2.6: The built-in set/frozenset types replace this module. The sets module provides classes for constructing and manipulating unordered collections of unique elements. Common uses include membership testing, removing duplicates from a sequence, and computing standard math operations on sets such as intersection, union, difference, and symmetric difference. Like other collections, sets support x in set, len(set), and for x inset. Most set applications use the Set class which provides every set method except for __hash__(). The set classes are implemented using dictionaries. class sets.Set([iterable]) Constructs a new empty Set object. class sets.ImmutableSet([iterable]) Constructs a new empty ImmutableSet object. Because ImmutableSet objects provide a __hash__() method, they can be used as set elements or as dictionary keys. 8.7.1. Instances of Set and ImmutableSet both provide the following operations: 8.7.2. 8.7.3. Sets can only contain immutable elements. 8.7.4.

Appendix A: Debugging Warning: the HTML version of this document is generated from Latex and may contain translation errors. In particular, some mathematical expressions are not translated correctly. Different kinds of errors can occur in a program, and it is useful to distinguish among them in order to track them down more quickly: Syntax errors are produced by Python when it is translating the source code into byte code. The first step in debugging is to figure out which kind of error you are dealing with. Syntax errors Syntax errors are usually easy to fix once you figure out what they are. On the other hand, the message does tell you where in the program the problem occurred. If you are building the program incrementally, you should have a good idea about where the error is. If you are copying code from a book, start by comparing your code to the book's code very carefully. Here are some ways to avoid the most common syntax errors: If nothing works, move on to the next section... Runtime errors Infinite Loop

Errors and Exceptions — Python v2.7.5 documentation Until now error messages haven’t been more than mentioned, but if you have tried out the examples you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions. 8.1. Syntax Errors Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while you are still learning Python: >>> while True print 'Hello world' File "<stdin>", line 1, in ? The parser repeats the offending line and displays a little ‘arrow’ pointing at the earliest point in the line where the error was detected. 8.2. Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. >>> 10 * (1/0)Traceback (most recent call last): File "<stdin>", line 1, in ? The last line of the error message indicates what happened. The rest of the line provides detail based on the type of exception and what caused it. Built-in Exceptions lists the built-in exceptions and their meanings. 8.3.

The Art and Craft of Programming -- Recursion In this recurrence equation, a and b are the dividend and the divisor, respectively. Recall that the modulus operator % returns the remainder. Using the recurrence equation as a guide, we can easily implement a function for computing the gcd of two numbers. def gcd(dividend,divisor): if (dividend % divisor == 0): return divisor else: return gcd(divisor,dividend % divisor) Note that in our implementation of gcd, we used more descriptive variables than a and b. def gcd(dividend,divisor): remainder = dividend % divisor if (remainder == 0): return divisor else: return gcd(divisor,remainder) Look at how the recursive version turns the divisor into the dividend by passing divisor as the first argument in the recursive call. def gcd(dividend,divisor): remainder = dividend % divisor print("gcd:",dividend,divisor,remainder) if (remainder == 0): return divisor else: return gcd(divisor,remainder) After doing so, we get the following output: We can also write a iterative version of gcd: The map pattern

The Art and Craft of Programming -- Lists Recall that what we are calling arrays are called lists by the Python community25. Now we will introduce true lists; these lists will be our first attempt at building a data structure. A data structure is simply a bundling of pieces of data together in a single entity along with a set of operations (implemented using functions) to manipulate such entities. The Node Data Structure Our lists will use an intermediate data structure known as a node. In actuality, the data items are the addresses in memory where the values and nodes reside; such memory addresses are known as pointers. All data structures are built upon other data structures, so there must be some low-level data structure that is built into the language. Each of our nodes will actually be an array of length two. def NodeCreate(value,next): #this is the constructor return [value,next] Note that the constructor takes two arguments, the value and the next pointer and simply returns an array holding those two values. on a Mac.

Chapter 4: Conditionals and recursion Warning: the HTML version of this document is generated from Latex and may contain translation errors. In particular, some mathematical expressions are not translated correctly. 4.1 The modulus operator The modulus operator works on integers (and integer expressions) and yields the remainder when the first operand is divided by the second. In Python, the modulus operator is a percent sign (%). The syntax is the same as for other operators: >>> quotient = 7 / 3 >>> print quotient 2 >>> remainder = 7 % 3 >>> print remainder 1 So 7 divided by 3 is 2 with 1 left over. The modulus operator turns out to be surprisingly useful. Also, you can extract the right-most digit or digits from a number. 4.2 Boolean expressions A boolean expression is an expression that is either true or false. >>> 5 == 5 True >>> 5 == 6 False In the first statement, the two operands are equal, so the value of the expression is True; in the second statement, 5 is not equal to 6, so we get False. x ! 4.3 Logical operators block

Related: