Lecture 1 How to Read Code

advertisement
Susie’s lecture notes are in the presenter’s notes, below the slides
Disclaimer: Susie may have made errors in transcription or understanding. If
there is any confusion, please email the lecture presenter.
How to Read Code
Benfeard Williams
6/11/2015
Concepts You Will Learn
• Programming
 Practice, skill, art, science, engineering, creativity
• Problem-solving
 How to solve problems using programming and a
computer
• Impact of computer science
 Scale and automation
• Foundation for future work
Interpreted vs Compiled Code
• Interpreted
 Read and executed by another program on the
target machine
 Easy to implement
• Compiled
 Expressed specifically for the target machine
 Faster performance
Computers Do What You Tell Them
• They are fast but not smart
• You need to plan exactly what the computer
needs to do in order to solve a problem
Declarative Knowledge
• Statements of fact
• “A good health care plan improves the quality
of medical care while saving money”
• “y is the square root of x if and only if y*y = x”
Imperative Knowledge
• How to accomplish something (recipe)
1) Start with a guess, g
2) If g*g is close enough to x, then g is a good
approximation of the square root of x
3) Otherwise, create a new guess by averaging g
and x/g.
4) Using this new guess, go back to step 2
Building Blocks
• Variables
 Name of a storage location
 Use ‘=‘ to assign a value to a variable
• Types
 Various classifications of data
 A variable can stores data of different types
• Operators for numbers
 Arithmetic: + - * / % **
• Functions (subroutines)
 Sequence of instructions to perform a specific task
Variables
• Names are unique and can be abstract
 What is http://205.251.242.103
 What is http://www.amazon.com
• Declare a variable and assign a value
 Name and type
 Examples: int age = 18;
age = 18
Data Types
• Int (integer)
 4, 13, -7
• Float (floating-point number)
 3.33337
• Work just like normal numbers
 ans = 2 * (7 + 4) – 1
 ans = 21
Data Types
• Char (characters)
 ‘H’, ‘8’
• Str (string)
 “HELLO”, “Echo 123”
• Arithmetic manipulation
 phrase = “My name is ” + “Benfeard”
 phrase = “My name is Benfeard”
Data Types
• Arrays
 Collection of elements
 [ 1, 2, 3, 4, 5]
[ ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ ]
• Manipulation
 myArray = [1, 2, 3]
 myArray = myArray * 2
 myArray = [1, 2, 3, 1, 2, 3]
• Indexing elements
Functions
• Allows you to easily recall a procedure or subroutine
def sum(a, b):
return a+b
• Parameters
 values in the call, that you pass to the function to use as input
• Output
 Return value
 Call Function
 Answer = sum(7,3)
 Answer = 10
Functions
• Example function that returns a value
def greet(name):
print "Hello " + name
• Advantages
 Repeat code, call multiple times
 Flexible, call with different arguments
• Functions
 greet("Sue")
Booleans
• True or False
• Useful for comparisons
 Greater than, less than
 Is equal to, is not equal to
• Supports algebraic operations
 And, or, not
If Statements and Loops
• If statement
 If this statement is true, do something
• For loops
 For all values in an array, do something repeatedly
• While loops
 While a statement is true, do something
repeatedly
Computers do what you tell them
• They are fast but not smart
• You need to plan exactly what the computer
needs to do in order to solve a problem
• Outline the solution to your problem
 Pseudocoding (step by step walkthrough of code)
Algorithms
• Describe in words how to solve a problem
• Like a recipe, must be detailed and precise
How to make a Peanut Butter & Jelly Sandwich
Ingredients:
Two slices of bread
Peanut Butter
Jelly
1. Spread peanut butter on one slice
2. Spread jelly on the other slice
3. Combine slices together
Does this provide enough
information for anyone to
make the sandwich?
Find Narten
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Anderson
Applegate
Bethune
Brooks
Carter
Edwards
Foggle
Griffin
Holhouser
Jefferson
Klatchy
Morgan
Munson
Narten
Oliven
Parken
Rivers
Roberts
Stevenson
Thomas
Wilson
Woodrow
Yarbrow
• Did you find Narten?
• Can you explain your step-by-step
process?
• If this list had millions of names,
what would be an efficient way to
find Narten?
Creating an Algorithm
• Is the algorithm correct?
 Try it again and again and again
 Reason through it: logically and informally
• How efficient is the algorithm?
 How many guesses would it take you to find
Narten in the worst case scenario?
 Should we care about efficiency?
How To Dissect Code
## Author’s comments ##
def main():
filename = “romeo.txt”
fileToProcess = open(filename)
Important information about
function and usage from
author
Opening a romeo.txt file
count = 0
for line in fileToProcess:
words = line.strip().split()
for word in words:
if len(word) == 4:
count = count + 1
print “count is”, count
Counting something
Looping through lines
if __name__ == “__main__”:
main()
Mysterious code…
Splitting lines into words?
Words of length equal to 4?
Increase counter
Print count at the end
Example In TextWrangler
Computers Read In Order
• We give computers the entire code but the
computer cannot read ahead
## Author’s comments ##
def main():
filename = “romeo.txt”
count = 0
for line in fileToProcess:
words = line.strip().split()
for word in words:
if len(word) == 4:
count = count + 1
fileToProcess = open(filename)
Print “count is”, count
Important Resources
•
•
•
•
•
Howtolearntocode.web.unc.edu
Stackoverflow.com
BCB Modules
ITS Research Computing
HtLtC teachers
Download