HtLtC Lecture 2

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.
Psuedo Code
Real Code
Pseudo Code
• Pseudo = Fake
• Code = Fancy computer stuff
• A way to organize your program
before coding it
General Guidelines
• Input
• Output
• Steps in between
• Not Language Specific
• Only you need to understand it
• Should not work
ToolBox
•
•
•
•
•
•
•
Variables (int, float, char, string)
Arrays
If
If-Else
While loop
For loop
Functions
If - Else
If (True or False Statement)
Run Code
Run More Code
Else
Run Other Code
Problem 1
• Matt keeps getting on your computer and
running your code
• Write a program that asks for the users name
• If its Matt tell him to get back to work
• Input: User’s name
• Output: Chiding Statement
Problem 1: Word Solution
• Load in a user name from the keyboard
• Check if that name is matt
• If it is matt, say something
Problem 1: Pseudo Code
User Name = userInput(stuff)
If User Name is Matt
Print Boo Matt
Problem 1: Pseudo Code
User Name = userInput(stuff)
If User Name is Matt
Print Angry Statement
Else
Print You do You!
Class Problem 1
• Write a program that takes in a number from a
user and tells them if it is Divisible by 11
• Input = User Number
• Output = Printed statement on divisibility
• Hint: Mod (%) gives the remainder
• 5 Mod 2 = 1
Class Problem 1: Word Solution
•
•
•
•
Load in a number from the user
See if that number is divisible by 11
If it is tell them
If it is not, tell them
Class Problem 1: Pseudo Code Solution
Number = userInput(stuff)
If Number Mod 11 is 0
Print Affirmative Statement
Else
Print Negative Statement
While
While (True or False Statement)
Do Code
Do More Code
Problem 2
• Write a program that takes in a number from a
user and tells them if it is Divisible by 11
• But keep asking until the user enters the
number 0
Problem 2: Pseudo Code
While(Number is not 0)
Number = userInput(stuff)
If Number Mod 11 is 0
Print Affirmative Statement
Else
Print Negative Statement
Why will
this not
work?
Class Problem 2: Pseudo Code
Number = 1
While(Number is not 0)
Number = userInput(stuff)
If Number Mod 11 is 0
Print Affirmative Statement
Else
Print Negative Statement
Array
•
•
•
•
•
Cute = [‘Puppy’,’Kitten’,’Piglet’,’Tigger’]
0 indexed: 0
1
2
3
1 indexed: 1
2
3
4
0 indexed: cute[1] = Kitten
1 indexed: cute[1] = Puppy
For Loop
• Two Main components:
–Iterator Variable
–Range
For Loop
• For counter in 1 – 10
–Print “pass” + counter
• For i = 1, i <= 10, i = i + 1
–Print “pass” + counter
• For word in Words
–Print word
Problem 3
• You are given a DNA Sequence and have to report
how many times the Motif CAT appears
• Input = DNA Sequence (GATTACA), CAT
• Output = CAT Count
• Hint1: A string is really just as an array of
characters
• [‘G’,’A’,’T’,’T’,’A’,’C’,’A’]
• Hint2: you can take a slice out of an array
– Array[2:4]
Problem 3: Word Solution
• Load DNA Sequence and CAT into variables
• Set a motif counter to 0
• Iterate through DNA sequence
– Pull out sets of three letters and compare to CAT
– If we find CAT increment the counter
• Report the final count
Visual Representation
•G A T T A C A
Problem 3: Pseudo Code
Motif = CAT
DNASeq = loadFile(InputDNA)
CAT_Count = 0
For i in DNASeq Range – 2
if(DNASeq i to DNASeq i+2 is CAT)
CAT_Count + 1
Print CAT_Count
Class Problem 3
• You are given a DNA sequence and have to
determine its GC content
• Input = DNA sequence (GATTACA)
• Output = GC content
• Hint: You can use a For loop to iterate through
the elements of an array
Class Problem 3: Pseudo Code
DNASeq = loadFile(InputDNA)
GC_Count = 0
For Letter in DNASeq
if Letter is G or C
GC_Count + 1
DNALength = length(DNASeq)
GC = GC_Count/DNALength X 100%
Print GC
Real Code
Better Real Code
Parting Lessons
•
•
•
•
•
Inputs
Outputs
Toolbox
Take Your Time
Only you have to understand your pseudo
code
Download