Download Parsing Strings Lists, Tuples, and Dictionaries

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
SGD 125-60
Introduction to Python
In-Class Exercises
Answer the questions in the spaces provided on the question sheets. If you
run out of room for an answer, continue on the back of the page.
Name:
Parsing Strings
10
1. Given the list x = 'artificial intelligence for games', state the resulting list
after performing the following array slicing operations.
(a) x[15]
(b) x[0:5]
(c) x[0:10]
(d) x[2:5:2]
(e) x[:10:]
Lists, Tuples, and Dictionaries
Use the following table for the questions in this section:
Page 1 of 5
SGD 125-60
Introduction to Python
Unit
X
In-Class Exercises
Y
Marine
10 10
Dropship
5 15
Ghost
7 5
Goliath
2 22
Science Vessel 12 8
Wraith
5 10
2. Create a list called units containing all of the units in the above table.
3. Create a list of tuples for each of the X and Y positions of the units.
4. Create a dictionary using the table, so that unit['marine'] is (10, 10).
These three data structures are central to Python. Be sure that you can use these
effectively!
Ranges
5. Re-write the following lists using the range function.1
5
6. (a) range(1,6)
1
http://docs.python.org/library/functions.html#range
Page 2 of 5
SGD 125-60
5
(b) range(1,10,2)
5
(c) range(10,3,-3)
Introduction to Python
In-Class Exercises
Loops
7. Write a Python for loop to print all of the odd numbers between 1 and 100.
Variables
8. Draw a memory diagram that represents the following code:
x
y
r
s
=
=
=
=
[’a’, ’b’, ’c’, ’d’]
x
3
3
x[2] = ’q’
s = 5
print x, y
print r, s
Logic
9. Given a variable n, if n is divisible by 3, print Fizz, if n is divisible by 5, print Buzz,
and if n is divisible by both 3 and 5, print FizzBuzz. Otherwise, print “None”.
Page 3 of 5
SGD 125-60
Introduction to Python
In-Class Exercises
Dictionary Implementation
10. Assume a dictionary of name and value pairs having 7 entries numbered 0 through 6.
Draw a diagram of the resulting dictionary after applying the following insertions if it is
a linearly probed hash table. For the hash function, let A = 1, B = 2, C = 3, . . . , Z = 26.
d[’A’]
d[’D’]
d[’H’]
d[’B’]
d[’Z’]
=
=
=
=
=
"Larry"
"Curly"
"Moe"
"Fred"
"Sarah"
Page 4 of 5
SGD 125-60
Introduction to Python
Question Points
1
10
2
0
3
0
4
0
5
0
6
15
7
0
8
0
9
0
10
0
Total:
25
Page 5 of 5
Score
In-Class Exercises