File Processing - Bryn Mawr Computer Science

File Processing
Recap
String Processing: Review
 Escape Sequences: print the unprintable
 '\<symbol>': escape sequence
 '\n': newline
 '\t': tab
 '\"': "
>>> print 'Hello\nWorld'
Hello
World
>>> x = eval('40\n')  x = 40
File Processing Review:
 Open File
>>> infile = open('myfile.txt', 'r')
>>> outfile = open('myfile.txt', 'w')
 Manipulate File
 Read
 Write
 Close File
>>> infile.close()
File Processing Review:
 Writing
myfile.txt
outfile.write(<string>)
 Reading
infile.read()
infile.readlines()
infile.readline()
Spam
and
Eggs
>>> infile = open('mytext.txt', 'r')
>>> x = infile.read()
>>> x
'Spam\nand\nEggs\n'
File Processing Review:
 Starts from last line read
myfile.txt
>>> x = infile.readline()
>>> x
'Spam\n‘
Spam
and
Eggs
>>> x = infile.readlines()
>>> x
['and\n', 'Eggs\n‘]
 To start from the beginning of the file, need to
close and reopen
End of the File?
 .read(), .readlines(): read until end of file
 .readline()?
 Empty string ('')
x = 'stuff'
while x != '':
x = infile.readline()
print x
Spam
and
Eggs
 Files are sequences!
for line in infile:
<process the line>
Recursion
Rock, Paper, Scissors
def main():
# define items
# Player and Computer make their selection...
# inform Player of choices
# figure out who wins
# print out results of the game
main() # invoke the program
Rock, Paper, Scissors
def main():
# define items
# initialize score
# Player and Computer make their selection...
# inform Player of choices
# figure out who wins and update score
# print out results of the game
main() # invoke the program
Rock, Paper, Scissors
# initialize score
def main():
# define
# Player
# inform
# figure
Recursion
items
and Computer make their selection...
Player of choices
out who wins and update score
# ask if user wants to play again
if play_again == 'Y':
main()
else:
# print out results of the game
main() # invoke the program
Recursion
 Function being defined is invoked within its own
definition
 Usually do not recurse main
Rock, Paper, Scissors
# initialize score
def main():
# define
# Player
# inform
# figure
items
and Computer make their selection...
Player of choices
out who wins and update score
# ask if user wants to play again
if play_again == 'Y':
main()
else:
# print out results of the game
main() # invoke the program
A Better Recursive Solution...
def playgame(score):
# Player and Computer make their selection...
# inform Player of choices
# figure out who wins and update score
# ask if user wants to play again
if play_again == 'Y':
playgame(score)
else:
# print out results of the game
def main():
# define items
# initialize score
playgame(score) # play the game
main() # invoke the program
Recursion
 Some problems can only be solved with recursion:
 3! = 3 Factorial = 3 * 2 * 1 = 6
 n! = n * (n – 1) * (n – 2) * ... * (n – (n - 1))
 1! = 1, 0! = 1
def factorial(n):
if n <= 1:
return 1
else:
return factorial(n-1)
Recursion
def factorial(n):
if n <= 1:
return 1
else:
return n * factorial(n-1)
def main():
factorial(4)
Stack
factorial(1)
factorial(2)
factorial(3)
main()
factorial(4)
 A lot of overhead
main()
Rock, Paper, Scissors
def main():
# define items
# initialize score
# Player and Computer make their selection...
# inform Player of choices
# figure out who wins and update score
# print out results of the game
main() # invoke the program
A Better Solution
def main():
# define items
# initialize score
done = 'N'
while done == 'N':
# Player and Computer make their selection...
# inform Player of choices
# figure out who wins and update score
# ask if user wants to play again
# print out results of the game
main() # invoke the program
Multivariate Data
CMSC 120: Visualizing Information
Lecture 4/17/08
Types of Analysis
Univariate
Bivariate
 A single attribute
 Two attributes
 Characterize Observations
 Number
 Type
 Similarity
 Describe Associations
 How variables simultaneously
change together
 Are two groups the same?
 Is there a relationship?
 What is the nature of the
relationship?
Bivariate Analysis
 How two variables co-vary
 How two variables are correlated
 Describes a how a change in one variable is
related to a change in another
 Relationships are not causal!
Bivariate Analysis
 How two variables co-vary
 Describes the degree of similarity between two variables
(X, and Y)
 Measure of how two variables vary together about the
mean
 How two variables are correlated
 Indicates strength and directionality of a linear
relationship between X and Y
 Departure of relationship from independence
Lizard
Plant
Lizard
10.1
8.4
Plant
8.4
12.0
25
20
15
Plant Diversity
10
-15
5
0
-10
Correlation
-5
0
5
10
15
-5
-10
-15
-20
X
Y
X
Variance
Covariance
Y
Covariance
Variance
Lizard Diversity
Multivariate Data
 3 or more variables
 Extension of Bivariate
 Relationships among Variates
Multivariate Visualization
Projection
 Image of an imaginary multi-dimensional object is
projected on a planar (2-D surface)
 Mathematical calculation
Projection
 Image of an imaginary multi-dimensional object is
projected on a planar (2-D surface)
 Mathematical calculation
Projection
 Image of an imaginary multi-dimensional object is
projected on a planar (2-D surface)
 Mathematical calculation
 Distort the data