690 likes | 806 Views
Computer Science Concepts. What is an algorithm? Binary information coding Programming concepts via Scratch. Designed for learners 10 years old up to 100. Materials for hands on activities. a set of cards with numbers, say, 42, 17, 43, 55, 20, 32, 12
E N D
Computer Science Concepts What is an algorithm? Binary information coding Programming concepts via Scratch Designed for learners 10 years old up to 100. 2012 CSE-BEACON camp
Materials for hands on activities • a set of cards with numbers, say, 42, 17, 43, 55, 20, 32, 12 • A set of cards with binary place values: 1, 2, 4, 8, 16 3) Scratch downloaded from www.scratch.mit.edu 4) www.learnscratch.org for tutorials for homework 2012 CSE-BEACON camp
What is an algorithm? What is an algorithm? Algorithms for average and min An algorithm for sorting 2012 CSE-BEACON camp
Algorithm for computing the average of two numbers • Let the first number be A; say A=12 • And the second number be B; say B=15 • To compute the average _____________ • So the average of 12 and 15 = ________ • WHAT IS THE AVERAGE ALGORITHM? 2012 CSE-BEACON camp
What is an algorithm? An algorithm computes some output from the given input using precise steps that a machine can follow (machine or other agent) 2012 CSE-BEACON camp
Hands on activity: finding MIN • Organize into 2-person teams • Team has “person A” and “person B” • Each team has a stack of cards • There is one number on each card • No one looks at numbers yet 2012 CSE-BEACON camp
problem: finding min • Person B is the “worker” for A. • Person A has cards, but cannot look at the numbers. A must find the min of all cards. • B can look at only 2 numbers at a time -- B returns to A the smaller smaller first. • HOW CAN PERSON A FIND THE MIN? • Shuffle the cards • A and B switch roles: new person A finds MIN 2012 CSE-BEACON camp
review: team finding the min • Person A can look at no numbers! • No numbers can be seen by A, EVER! • Person B can look at only two numbers on cards given by person A. • Person B gives back to A first the card with the smaller number (if numbers are = then it doesn’t matter which is given first) • Person A must find the min and show it to the instructor WITHOUT EVER SEEING IT • A and B should switch roles and repeat. 2012 CSE-BEACON camp
Thought Questions • If A has 10 cards with numbers, how many times does A call on B? _____ • If A has 100 cards, how many times does A call on B? _____ • If A has N cards, how many times does A call on B? _____ • Finding the MIN of a list of numbers has “complexity” _______ 2012 CSE-BEACON camp
Thought Question • Suppose A has 2 assistants – B and C • Can A find the MIN any faster? • HOW? • Computers and computer programs can be structured to do more than one operation at a time. 2012 CSE-BEACON camp
Problem: how to sort? Use set of cards for each team. How can A sort using B as comparer? TRY IT: instructor will check your final output 2012 CSE-BEACON camp
Sorting two-person activity • This is a 2-person exercise. Person A CANNOT see the numbers – EVER! • To solve the sorting puzzle, person A must form an ordered list of cards, which the instructor will check for order (this is the OUTPUT). • Person B can only compare two numbers by seeing the numbers. Person B gives A the smaller number card first (precise operations). • Person A can only ask Person B which of two cards has the smaller number. Person A never ever looks at any numbers. • To solve the sorting puzzle, person A must start from an unordered list of cards (this is the INPUT). 2012 CSE-BEACON camp
Sorting thought questions • How many times will A call on B to sort 10 numbers? _____ • How many calls to sort 100 numbers? _____ • How many calls to sort N numbers? _____ • There are many sorting algorithms. Some are fast when N is small but slow when N is big. Some are the opposite. Computer scientists learn these algorithms and many others. 2012 CSE-BEACON camp
Algorithms often do operations over and over • Called “looping” • Also called “iteration” or “repetition” • “Recursion” is a related idea – first select the MIN from the list and then “recurse” on the rest of the list 2012 CSE-BEACON camp
Binary Information Coding Only 2 symbols: WHY? Can we code numbers, characters, music, images, movies, car designs using just 2 symbols? 2012 CSE-BEACON camp
Coding activities • Guessing the number of fingers in decimal • Guessing 1 finger versus 0 fingers • Encoding small integers using binary cards • Coding using animate versus inanimate objects (“My teacher plays rock, paper, scissors with me.”) * Instructor quickly brings, say, 7 fingers from behind the back. Students make errors in guessing the number. Few errors in 0 versus 1 discrimination. 2012 CSE-BEACON camp
Fundamental coding principle • Can rep 2 “things” with 1 bit: yes/no; smiley/sad; high/low; odd/even • Can rep 4 “things” with 2 bits; 00:clubs; 01:diamonds; 10:hearts; 11:spades • Can rep 8 things with 3 bits 0 00 1 00 0 01 1 01 0 10 1 10 0 11 1 11 2012 CSE-BEACON camp
Fundamental coding principle • Each time we add one more bit, we __________ the number of binary strings • There are _____ binary strings of length N=7 (think about ASCII code) • There are thus ______ possible binary strings of length N bits 2012 CSE-BEACON camp
Recursive description • Append a 0 to all 0 00 0 01 0 10 0 11 “all” is what we had before • Append a 1 to all 1 00 1 01 1 10 1 11 WHY are all 8 different? 2012 CSE-BEACON camp
ASCII code for characters CHAR CODE CHAR CODE A 001000001 a 001100001 B 001000010 b 001100010 C 001000011 c 001100011 + 000101011 . 000101110 * 000101010 % 000100101 2012 CSE-BEACON camp
How many characters does a computer need to use? • Old Univac system used 6 bit char – a design disaster: _____ symbols max • IBM 8-bit code enabled ____ symbols (digits, caps, small letters, punctuation, graphic symbols, bell, null, etc.) • Unicode uses 16 bits to code all of the above and international characters: • ______ symbols. 2012 CSE-BEACON camp
CD has many tracks of spots Laser reads shiny versus dull spots What’s a “CD”? • Shiny spot is 0 • Burned spot is 1 • 5 billion spots total • 700 MB total • 44,000 x 16 spots for just 1 second of high fidelity music CD CD spins 2012 CSE-BEACON camp
Even parity to detect errors • To the 7-bit ASCII code, append one parity bit to have an even # of 1 bits • Sender always sends an even number of bits • If some disturbance causes a bit to change, receiver gets an odd number or 1-bits in the char code. • If error, receiver asks for retransmit. 2012 CSE-BEACON camp
2nd coding fundamental • Any object can be represented by a bit string in a computer • A symbol from an alphabet • A word from a dictionary • One second of music • A movie • An engine design • Your genetic code 2012 CSE-BEACON camp
Numbers as bit strings Binary Places Decimal Cave Man 16 8 4 2 1 0 0 0 0 0 0 0 0 0 0 1 1 ϡ 0 0 0 1 0 2 ϡϡ 0 0 1 1 1 7 ϡϡϡϡϡϡϡ 1 0 1 0 1 21 … 2012 CSE-BEACON camp
Addition has few rules. Multiplication has few rules These can be carried out in nanoseconds by computer circuits 0+0 = 0 0+1 = 1+0 = 1 1+1 = 0 with 1 carry 0x0 = 0x1 = 0 1x0 = 0 1x1 = 1 Binary Ops easy for machine 2012 CSE-BEACON camp
1 is 0 is • What number? • What number? • What number? 2012 CSE-BEACON camp
0 is 1 is • What number? ____ • What number? ____ • What number? 2012 CSE-BEACON camp
0 is 1 is • What number? ______ • What number? ______ • What number? ______ • What number? ______ 2012 CSE-BEACON camp
Binary coding in a computer • Symbol 0 can be represented by +5V and 1 by -5V • 0 can be darkness and 1 lightness, as used on CDs and DVDs or in optical fibers 2012 CSE-BEACON camp
Using your “binary cards” • Make your age • The month of your birth • The day of your birth • Your father’s age • Algorithm: precisely how to do it? 2012 CSE-BEACON camp
Intuitive algorithm 1 • Need a list of powers of 2 • Go down the list to find the largest power of 2 less than the decimal # • Use that power of 2 • Subtract it from the decimal # • If nothing left, then DONE, else RECURSE 2012 CSE-BEACON camp
Conversion of decimal to binary: Algorithm #1 • Let D1 be a decimal integer • Let R1 = remainder of D1 divided by 2; R1 is the 1’s bit • Let D2 be the dividend of D1/2 • Let R2 = remainder of D2 divided by 2; R2 is the bit in the 2’s place. & so on 2012 CSE-BEACON camp
Decimal to binary conversion INPUT: a decimal number OUTPUT: a binary number PRECISE STEPS: repeated division by 2; recording the remainder TERMINATES?: always (why?) CORRECT?: why? 2012 CSE-BEACON camp
Guessing games • Guessing birthdays: just day of month • Guessing month and day • Guessing face value of cards • Guessing suit and face value 2012 CSE-BEACON camp
Programming concepts via Scratch Free download of software available at www.scratch.mit.edu Video tutorials for learning available at www.learnscratch.org 2012 CSE-BEACON camp
What can we do with Scratch? Learn some computing concepts. Learn some practical algorithms. Use Scratch as computing tool. Have fun with Scratch creating stories, games, art. 2012 CSE-BEACON camp
Start scratch and let’s go! In the lab, Click on the cat icon Scratch programming environment comes up First do simple things 2012 CSE-BEACON camp
Explore Scratch environment Stage is at upper right (where actors act and information is displayed) Sprite is another name for actor. Instruction menus/panel at left (instructions for the actors) A script is another name for program or method; a script tells the actor what to do. Programming area in center; here is where we construct scripts for the sprites 2012 CSE-BEACON camp
The VARIABLES Menu • Implement an average algorithm • Make a variable “FIRST” and set it to 12 • Make a variable “Second” and set to 15 • Make variable “Average” • Use OPERATIONS Menu to set the Average to (FIRST+SECOND)/2 • Test Scratch and algorithm 2012 CSE-BEACON camp
Square root by repetition • Guess sqrt(N) to be 1.0 • Set root1 = 1.0 (first factor) • Set root2 = N/root1 (second factor) • root1 is not likely to = root2 • Reset root1 to the average of root1 and root2 (it will be in between them, why?) • Reset root2 = N/root1 • Repeat all of the above until root1 and root2 are “very close” 2012 CSE-BEACON camp
Algorithm study is RICH! • Algorithm to find your BEST mate? • What if you had the coding of genes for all possible mates? • Algorithm to make bet on Big 10 game? • Algorithm to find best molecule to bond with a given input molecule? • Algorithm to bond 2 molecules to create “best properties”. 2012 CSE-BEACON camp
Information theory is RICH • SETI: Search for Extra Terrestrial Intelligence • Jodi Foster movie “Contact” • What are the chances that a random signal from space codes “I love you”? Or “X^2+Y^2 = R^2”? 2012 CSE-BEACON camp
The LOOKS menu Has instructions for setting the color, size, and visibility of a sprite. Costumes will be used later. 2012 CSE-BEACON camp
The “hello” script (program) • Choose the Looks menu • Click on the “say hello” lego block • Check your sprite behavior at the right • Then click “say hello for 2 secs” Your very first Scratch program! 2012 CSE-BEACON camp
Try some other looks operations (click on menu items) Change color effect by 25 Change color effect by 25 again Hide Show Change size by 10 Change size by 10 again Set size to 100% 2012 CSE-BEACON camp
Let’s write a script to Say “hello” for 2 seconds Then change color by 25 Then think “Hmm..” for 4 seconds Then change color by 75 Then change size by 200 Drag each instruction from the menu to the center script area. Connect them into a single block. Edit the parameters to get the numbers we want. 2012 CSE-BEACON camp
Our script (program) 2012 CSE-BEACON camp
A sequence or block is a simple script or program The first instruction is done first The second instruction is done second The last instruction is done last. (if any one instruction is done, then every on of them is done) 2012 CSE-BEACON camp
The WAIT instruction WAIT is needed to slow down the acting so we can see or hear it properly (computers are too fast sometimes) Get the wait instruction from the CONTROL menu. Ignore the other menu options for now. Insert a wait in our looks script 2012 CSE-BEACON camp