58
My Summary for CS001x MyCS: Computer Science for Beginners Prepared By

My summary for cs001x computer science for beginners

Embed Size (px)

Citation preview

Page 1: My summary for cs001x computer science for beginners

My Summaryfor

CS001xMyCS: Computer Science for Beginners

Prepared By

Typewritten text
May 2015
Page 2: My summary for cs001x computer science for beginners

Syllabus

MyCS: Computer Science for Beginners is composed of five units ofcurriculum, which alternate back and forth between the ideas that shapecomputer science and Scratch programming activities. The topics of theseunits are listed below:

Unit1:What is Computer Science? …………………………………….. (03) Answer broad questions about the role of computers and the goals of

computer scientists. Explore the definition of intelligence as it relates to computers.

Unit2:Solving Puzzles in Scratch ……………………………………… (10) Learn the basics of Scratch programming through a series of pre-made

mazes of increasing difficulty.

Unit3:DataandCodes ………………………………………………………. (17) Practice encoding and decoding information using a variety of codes and

methods. Learn to represent numbers in binary. Connect these concepts to computer science and working with data.

Unit4:ProjectsinScratch ………………………………………………….. (26) Create your own stories, games, and interactions using Scratch. Practice design skills for making unique programming projects.

Unit5: Problem Solving and Algorithm ……………………………… (45) Build intuition for how people and computers solve problems differently. Learn basic algorithms for searching and sorting information, as well as

how we can compare these algorithms.

parts of this Scratch programming course will be review, it will also explorein much more depth how problem solving and algorithms can be usedwithin Scratch.

After completing Week (5), students wishing for additional practice inScratch should sign up for CS002x: Programming in Scratch. While early

(02)

Page 3: My summary for cs001x computer science for beginners

(03)

Ibrahim Omar CS 2015

Week(1)What is Computer Science?

Computer Science: a branch of science that deals with the theory ofcomputation (stream of 1s and 0s) or the design of computers.

Computer Scientists study how to solve problems using computers.Think of computers as machines that can store information (Input), usethat information according to a set of rules (Processing), and thenprovide the results to the user again (Output).

My Definition: Computer as machine that store input data, thenprocessing it to appear the final form required.Calculator, phone, Camera and Brain do the same things as laptop.

What's In a Computer?

At the simplest level, the inside of a computer is a very complicatedcircuit. Electricity runs through all of the different parts of the computer toallow us to store information, send visual and audio messages, processdata, and even run a small fan designed to keep the machine fromoverheating.

Parts of a Computer: 1. Hardware Team

photos

Page 4: My summary for cs001x computer science for beginners

(04)

Ibrahim Omar CS 2015

Page 5: My summary for cs001x computer science for beginners

(05)

Ibrahim Omar CS 2015

Page 6: My summary for cs001x computer science for beginners

(06)

Ibrahim Omar CS 2015

Page 7: My summary for cs001x computer science for beginners

(07)

Ibrahim Omar CS 2015

Page 8: My summary for cs001x computer science for beginners

(08)

Ibrahim Omar CS 2015

2. Software Team

photo

The Software in a computer tells the Hardware Team agents how tostore and display information by providing each agent with a set ofinstructions.

Unlike Hardware, which is limited by how it's built, Software can becompletely rewritten and replaced; this flexibility is why it's called Soft-ware.

Software can also be undesirable! Computer viruses are softwareprograms that give the Hardware Team agents bad instructions.

However, as with any team, the agents need instructions on howto work together and accomplish their part of the task.

Page 9: My summary for cs001x computer science for beginners

(09)

Ibrahim Omar CS 2015

Types of Software:

Operating Systems (Windows or Mac) are a type of software thatmanage the basic operations of your computer and how you caninteract with it. They provide instructions to your machine, as well as astructure for how other pieces of software can communicate withhardware.

Computer Games, Web Browser like Google Chrome or MozillaFirefox, written an essay in Microsoft Word or play music in iTunes.

The Apps on a phone are also pieces of software: games, e-mail, to-do lists, calendars, even basic alarm systems and timers.

Software and Hardware must work together to create a functionalcomputer, which Hardware can store and process information andSoftware provides the instructions.

Computer Intelligence

When we refer to Artificial Intelligence (AI), we're specifically describingthe ability for computers to think and respond like humans.

Computers don't make decisions but Humans do, then those decisionsare carried out through clear linear instructions provided by the Software.

Alan Turing (1912-1954) was an English mathematician and computerscientist. In 1950, He wrote a paper asking the question: "Can machinesthink?" He proposed a thought experiment where a judge, a human anda computer are all placed in separate rooms. The judge converses withthe human and with the computer without knowing which entity is thehuman and which is the computer. The judge then must decide who is thecomputer and who is the human. If the computer can successfullyimpersonate a human to the point where the judge can't tell who is thehuman and who is the computer, then the computer is considered"intelligent".

Today, we call this test the Turing Test. So far, most computer scientistsagree that no computer has managed to fully pass this test.

Page 10: My summary for cs001x computer science for beginners
Page 11: My summary for cs001x computer science for beginners

(10)

Ibrahim Omar CS 2015

Week (2)Solving Puzzles in Scratch

What is Scratch?

Scratch is an online programming language created by MIT to teach thebasic concepts of Computer Science to people who are new toprogramming (https://scratch.mit.edu).

Rather than typing out lines of code, we program using "blocks" thatrepresent different actions our program can take. By clicking anddragging blocks into stacks, we create lists of commands for thecomputer to follow.

the Blocks area in the middle of the screen.

Loops are a programming tool that let us easily repeat a step or series ofsteps as many times as we need. In Scratch, we can make loops usingthe "Repeat 10" block, which is the second block in the "Control" tab of

Scratch Puzzle 1.1 - 1.5: YouTube.com/watch?v=PBhbtmLDZlQ

Page 12: My summary for cs001x computer science for beginners

(11)

Ibrahim Omar CS 2015

In general, writing a program whether in Scratch or in any otherlanguage, isn't something that you usually do "Right" on your first try. It'simportant to experiment and make some mistakes to find a Script that willwork really well.

Scratch Puzzle 1.6 - 1.10: YouTube.com/watch?v=p8wvnirhPO0

Scratch Puzzle 2.1 - 2.10: YouTube.com/watch?v=p3-NO3IkQuw

Page 13: My summary for cs001x computer science for beginners

(12)

Ibrahim Omar CS 2015

Conditional Statements

In most programming languages, including Scratch, the way that weconstruct a conditional statement is in two parts:

(1) If A is true, (2) then do B.

If A is not met, B will not run.

This allows us to run different versions of a program depending on thesituations the program is trying to address.

Repeat Until

In some cases, we might not know exactly how many times we want aseries of steps to repeat, then we can use a different kind of loop "repeatuntil" to build our program.

Rather than stopping after a set number of repetitions, it will simply checkat the end of each repetition whether the condition is true. If not, it will runthrough the loop again.

Scratch Puzzle 3.1 - 3.5 YouTube.com/watch?v=UGMIynQ_zYA

Page 14: My summary for cs001x computer science for beginners

(13)

Ibrahim Omar CS 2015

Costumes and Changing looks

Sprites in Scratch have a feature called costumes. This means that it'spossible for a Sprite to switch between more than one possible "look" inthe stage area.

In the case of Puzzle 3.6, if you simply move Sidney to the portalentrance, nothing will happen, it won't teleport to the portal exit. Thisportal only works if your sprite switches to its second costume (green)before passing through (that is, before moving onto the portal).

Scratch Puzzle 3.6 - 3.10 YouTube.com/watch?v=c0lI6M87Xig

Page 15: My summary for cs001x computer science for beginners

(14)

Ibrahim Omar CS 2015

Note: Sometimes, you may want to repeat until one of multiple thingshappens (for instance, landing at either of two portal exits). In this caseyou'll want to use the "or" block in the Operators tab of the blocks menu.

Scratch Puzzle 4.1 - 4.10 YouTube.com/watch?v=gQNUSHyhIRE

Page 16: My summary for cs001x computer science for beginners

(15)

Ibrahim Omar CS 2015

Sensing SpritesIn these puzzles, though the backdrop color doesn't change betweendifferent portal exits, so we'll need to test where the Sprite has goneusing an alternate block.

In the Sensing tab in the Blocks area, you can find the "touching __"block. This block allows you to select a Sprite from the dropdown menu,allowing you to test for whether the Sprite is coming into contact with aspecific portal or obstacle.

Sensing SpritesThe "broadcast" block, found in the Events section of the Block area,allows Sprites to send messages to each other while a program isrunning. This can be useful to schedule the order in which multiple spritesshould move or interact, as well as to indicate the "statuses" of spritesas they reach different stages of a program.

Scratch Puzzle 5.1 - 5.5 YouTube.com/watch?v=8mXJZ20xIEY

Page 17: My summary for cs001x computer science for beginners

(16)

Ibrahim Omar CS 2015

After Week (4) Projects in Scratch, you'll know a lot more about howto build your own projects in Scratch.

Scratch 1.1 - 5.10: https://scratch.mit.edu/studios/1401512

Scratch Puzzle 5.6 - 5.10 YouTube.com/watch?v=yYpmQAoAfBI

Page 18: My summary for cs001x computer science for beginners
Page 19: My summary for cs001x computer science for beginners

(17)

Ibrahim Omar CS 2015

Week (3)Data and Codes

Defining Encoding

Encoding is simply the process of changing information from one form toanother, either to communicate it or to store it for later use. Encoding cantake many forms and be used for many reasons, many of which tie intocomputer science.

Encoding Letters to Numbers

This simple encoding matches up each letter in the alphabet to a number,starting with A = 1, B = 2, C = 3, .… all the way to Z = 26.

For example, if we wanted to encode the word ROBOT = 18-15-2-15-20

Then, if our friends received the message 18-15-2-15-20, they couldreverse the method to find the letter that corresponds to each number,getting the word ROBOT again. This process of Reverse Encoding iscalled Decoding.

9 + 3-1-14 + 4-5-3-15-4-5 = I Can Decode

Dashes "-" separate characters in the same word and plus signs "+"represent spaces between words.

HARVEY MUDD COLLEGE

= 8-1-18-22-5-25 + 13-21-4-4 + 3-15-12-12-5-7-5OR = 8-1-18-22-5-25 13-21-4-4 3-15-12-12-5-7-5

Page 20: My summary for cs001x computer science for beginners

(18)

Ibrahim Omar CS 2015

Computers use character encodings such as ASCII (American StandardCode for Information Interchange) to represent a large number ofcharacters using numbers. Encodings like this are important forcomputers to be able to send, receive, and store our information.

The table below shows the number codes and "Hex" codes for thefirst 128 ASCII characters. You'll notice that ASCII includes separatenumbers for upper and lower case letters as well as for small numbers,symbols like & and #, and some "characters" that only have meaningto the computer. This way, computers can turn complicated messagesinto just a series of numbers to store, send or interpret.

Page 21: My summary for cs001x computer science for beginners

(19)

Ibrahim Omar CS 2015

Cryptography Cryptography is the art of creating, using or cracking these sorts of

codes, which are intended to allow people to communicate securely.

Computers have entered widespread use, cryptography has become areally important area of work. People have more and more privateinformation available online, whether through banks, email, or socialmedia. On the other hand, computers are getting faster and faster atcracking codes, making it more and more difficult to protect thatinformation.

So, people who study cryptography use a combination of computerscience, math and engineering to better understand how to keep ourinformation safe.

Key Words:

Cipher: a series of instructions on how to encode or decode privatemessages.

Encryption: the process of encoding a message to make it no longerreadable by the public.

Decryption: the process of decoding an encrypted message to read itsoriginal content.

The Caesar Cipher

The Cipher was simple: he would take each letter of his message, thenreplace it with the letter that came three after it alphabetically. Forexample, he would replace an A in his original message with a D in hisencrypted one, and would replace a B with an E. If he reached the end ofthe alphabet, he would simply restart the alphabet again after Z, so thatX, Y and Z would become A, B and C in his encoded messages.

Page 22: My summary for cs001x computer science for beginners

(20)

Ibrahim Omar CS 2015

Today, we call any cipher like this one a Caesar cipher, where eachletter of the message is replaces by the letter some number of placesaway in the alphabet. It's an example of a Substitution Cipher, wheresmall pieces of text (in this case, single letters) are replaced by other,enciphered pieces of text (in this case, other letters).

If our cipher has a Left-Shift of 2, what do we get when we encode"LATIN"? JYRGL

If our cipher has a Right-Shift of 3, what do we get when we decode"VSB"? SPY

While the Caesar Cipher was very effective 2,000 years ago, it is nearlyuseless for keeping messages secret today. With the help of computers,any Caesar Cipher-Encoded message can be deciphered almostinstantly, with very high accuracy.

You can try testing out some Caesar Cipher Decryption tools:Like this: http://xarg.org/tools/caesar-cipher

The reason? When it comes down to it, there are really only 26 possibleversions of the Caesar Cipher using the English Alphabet, a computercan do this instantly, creating the 26 possible decoding of the message.

Page 23: My summary for cs001x computer science for beginners

(21)

Ibrahim Omar CS 2015

Representing Information with Numbers

In order for a computer to store or use any information - not just text, butalso images, sound and anything else that happens on your computeris eventually reduced down to a series of numbers, which can then beinterpreted by the machine to produce the right combination of coloredlights, sounds and processes.

And doesn't stop there; your computer can only use two digits (0 and 1)to represent any information it uses. So, how are computers able torepresent so much with such small building blocks?

Number Bases: Decimal (base ten)

When you first learned to count, you probably learned the decimalnumber system, also known as base ten. In base ten, each digit of ournumber can be one of ten different options: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.Then, the amount that each digit represents depends on where it's placedin the number.

The rightmost digit of a number is what we call the "ones" place, with the"tens" place to its left, and then the "hundreds" place to the left of that.

We can find the next place value to the left by multiplying the currentplace value by 10 and so on.

3 4 9= (3x10x10) + (4x10) + (9x1)= (3x100) + (4x10) + (9x1)= 300 + 40 + 9

The number can be split into parts by multiplying each digit by itsplace value (one, ten, one hundred); adding the results back togethergives the total number.

3 4 9= 300 + 40 + 9= (3x100) + (4x10) + (9x1)= (3x10x10x1) + (4x10x1) + (9x1)

Page 24: My summary for cs001x computer science for beginners

(22)

Ibrahim Omar CS 2015

So, why do we use tens so much in our number system? Mostly forone simple reason:

Throughout History, people have used many other bases other thanbase ten for math, For instance, the Ancient Mayan Civilization used abase twenty counting system and the Babylonians used a base sixtycounting system. Nowadays, pretty much every person uses base ten.

Computers don't use hands to count; they use electricity. And from theperspective of a computer, it's much easier to measure whetherelectricity is "on" or "off" than to check ten different possible amountsof electricity that could pass through its circuits.

Number Bases: Binary

So, computers use a base two counting system, which we call binary.This way, there are only two digits to worry about: (0 and 1), where 1represents a circuit that's "on" and 0 represents one that's "off".

Because the majority of people have ten fingers to count with. However, the number ten is a choice and not the only one we can make.

Page 25: My summary for cs001x computer science for beginners

(23)

Ibrahim Omar CS 2015

Representing numbers with Binary

As decimal numbers, each of the digits in our binary numbers is goingto represent a place value. We call these digits bits in binary numbers,and each one can either be a 0 or a 1.

The rightmost digit (bit) is still the "ones" place, but since we're usingbase 2, we'll find the next place to its left by multiplying the place valueby 2 instead of 10:

1 x 1 = 1,1 x 2 = 2,1 x 2 x 2 = 4,1 x 2 x 2 x 2 = 8, and so on.

So, if we have a six-digit binary number like the one below whichrepresents forty-two, we can denote the place values as powers of 2:

101010

= (1 x 2 x 2 x 2 x 2 x 2) + (0 x2x2x2x2) + (1 x 2 x 2 x 2) + (0 x2x2) + (1 x 2) + (0x1)= (1 x 32) + 0 + (1 x 8) + 0 + (1 x 2) + 0= 32 + 8 + 2= 42

Page 26: My summary for cs001x computer science for beginners

(24)

Ibrahim Omar CS 2015

Computers tend to store numbers in sets of eight bits, called a byte,which can represent any decimal value from 0 to 255 (256 patterns).

2 bits = 4 patterns (0 - 3) 3 bits = 8 patterns (0 - 7) 4 bits = 16 patterns (0 - 15) 5 bits = 32 patterns (0 - 31) 6 bits = 64 patterns (0 - 63) 7 bits = 128 patterns (0 - 127) 8 bits = 256 patterns (0 - 255) 9 bits = 512 patterns (0 - 511) 10 bits = 1024 patterns (0 - 1023)

With ten bits, you can count up to 1023. That's why computer scientiststhink that 1024 is a nice, round number.In fact, on a computer, one k is 1024 patterns (0 - 1023) not 1000.

You can count on binary on your hand. Each finger either up (1) ordown (0). So you can count from (0 - 31) on one hand and with twohands, you can count to 1023.

Binary Counting with Dots: YouTube.com/watch?v=b6vHZ95XDwU

For each bit that we add, we double the range of numbers:

1 bit = 2 patterns (0 - 1)

Page 27: My summary for cs001x computer science for beginners

(25)

Ibrahim Omar CS 2015

Binary: 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010.

Decimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

So, the same rules of counting apply with our different digits. These samerules govern any counting system with any number base, as long as it's awhole, positive number.

Binary-Decimal Conversion

There are many ways to convert from decimal to binary, but perhaps theeasiest is what we call "greedy" method, which we'll demonstrate below.

Let's start with the number 21 in decimal. The largest place value that isnot larger than 21 is 16 or 2 x 2 x 2 x 2 x 1 (fifth place from the right).

1 _ _ _ _

Then subtract 16 from 21 to get 5. Then, we repeat the process: thelargest place value that is not larger than 5 is 4 or 2 x 2 x 1 (the thirdplace value from the right).

1 _ 1 _ _

We subtract 4 from 5, and have only 1 left over. The largest place valuethat is not larger than 1 is just the ones place on the far right, so we'llput a 1 there.

1 _ 1 _ 1

Then, if we subtract 1 from 1, we're left with 0. So, we can replace everyunused space with a 0.

1 0 1 0 1

The digits we've put in place should add up to 21. Let's check:

= (1 x 2 x 2 x 2 x 2) + (0 x 2 x 2 x 2) + (1 x 2 x 2) + (0 x 2) + (1 x 1)= (1 x 16) + 0 + (1 x 4) + 0 + (1 x 1)= 16 + 4 + 1= 21

Page 28: My summary for cs001x computer science for beginners
Page 29: My summary for cs001x computer science for beginners

(26)

Ibrahim Omar CS 2015

Week (4)Projects in Scratch

Scratch from Scratch

Computer Science in general and programming specifically, is as muchabout creativity as it is about science.

Computer Science can be used to solve big math problems, but also itcan be used to animate movies, play music or run your favorite games.

Then, click the "Create" button to open up a new Scratch project.

However, we'll also remind you where to find certain blocks andbuttons as you go.

In Unit (2), we practiced using the basic tools of Scratch Programmingto navigate a series of puzzles. Now, we'll put those tools and more tocreate our own projects, so now is a good time to openhttps://scratch.mit.edu in a new tab.

If you like, you can walk through Scratch's built in tutorial to refresh onthe basics of using the Scratch Studio. Also you can see these TutorialVideos: https://scratch.mit.edu/help/videos

Page 30: My summary for cs001x computer science for beginners

(27)

Ibrahim Omar CS 2015

After clicking "Create" you should end up at a page that looks like this:

As with the puzzles in Unit (2), we'll be clicking and dragging blocksfrom the blocks menu in the middle of the screen to the Scripts Areaon the right in order to control what happens in the Stage Area on theleft.

Now, we only have one sprite on our stage, known as the Scratch Cat.We'll start out by writing programs for this cat, then learn about addingnew sprites and programs.

"Hello, world!"

Computer Scientists often start learning a new programming by creatinga program that prints "Hello, world!" This exercise will explain how towrite a Hello World program in Scratch.

We use the "say" block in the Looks tab to control what our sprite issaying. Create a script with a green flag block and attach a "say __"block to get started. Then, type "Hello, World!" into the say block tocontrol what the cat says when the program runs.

Finally click the green flag at the top-right corner of the Stage Area towatch the cat says your message.

Page 31: My summary for cs001x computer science for beginners

(28)

Ibrahim Omar CS 2015

Animating "Hello, world!"

Now, we'll add some animation to our Hello World Script. The scriptbelow moves the Sprite to the side of the stage and Decreases its size.

The "go to x, y" block can be found in the Motion tab, where the twonumbers refer to a point somewhere on the Stage. In this case, x:-213means that the sprite will be on the far left side of the stage and y:0means that the cat will be neither above nor below the middle height ofthe stage.

The "set size to %" block can be found in the Looks tab, and will causethe Sprite to shrink to 40% of its original size when it runs.

Page 32: My summary for cs001x computer science for beginners

(29)

Ibrahim Omar CS 2015

Now, add another Motion block that makes the Script glide to theposition x = 0, y = 0 over 2 seconds.

This is different from the "go to" block because instead of jumping fromone location to another, the sprite will move gradually between the twoover the given amount of time.

Then, after the motion block, we can add a wait block from the Controltab to let the cat rest for a second before saying "Hello, world!"

Let's add one bit of code to change the size of the sprite as it moves.Since blocks in a single script execute in order and we want this sizechange to occur at the same time as something in the other script, we'llneed a second script that will change the size of the sprite inside arepeat.

As a reminder, the "repeat" block can be found in the Control tab andwill run the steps it contains in order however many times are indicated toright of the word "repeat." In this case, the step that will be repeated 60times is increasing the size of our sprite by 1, making it look like thesprite is gradually growing. This "change size" block can be found inthe Looks tab.

Page 33: My summary for cs001x computer science for beginners

(30)

Ibrahim Omar CS 2015

We can repeat about 30 times per second, so the sprite will finishgrowing after about two seconds, around the same time it finishesmoving across the stage.

Making a StoryWe are going to make the following Story:

Page 34: My summary for cs001x computer science for beginners

(31)

Ibrahim Omar CS 2015

You'll find that the Duck and Cat have completely separate Script areas.Click on each character to see their script area.

(1) To make a new sprite - click on the small character icon abovethe sprites area.

(2) These will be helpful to get the characters facing each other. Try tofigure out what each command (left) and button (right) does.

Page 35: My summary for cs001x computer science for beginners

(32)

Ibrahim Omar CS 2015

Cat's Scripts:

Duck's Scripts:

Press the green flag to start the short Story.

(Note): You'll notice that we chose to name the messages that werebroadcast so that it would help us keep track of what we were doing. Werecommend you do this in your projects.

Page 36: My summary for cs001x computer science for beginners

(33)

Ibrahim Omar CS 2015

Sound

You should feel comfortable with a variety of Scratch sound blocks andwith using the repeat block to make your code more concise.

Basic Sound Blocks

The play note block is one of the most important sounds block inScratch. This block plays a note for a certain duration. You can changeboth the note that is played and its duration.

Another important sound block is the play drum block. This block willplay the drum you select for the specified duration.

The rest block is a third important sound block. Its only variable is theduration of the rest.

Try making a song using these blocks.

Tempo

Tempo is a variable that controls how many beats are played in aminute. A higher tempo increases the speed of music that is beingplayed. So, the same song played at a higher tempo sounds faster.

You can see the current value of the tempo on the stage by clickingthe checkbox next the tempo variable in the Sound tab.

Page 37: My summary for cs001x computer science for beginners

(34)

Ibrahim Omar CS 2015

Setting and Changing Tempo

The tempo can be changed using two different blocks, the set tempoblock and the change tempo block.

When using the set tempo block, it doesn't matter what the currenttempo is. It will set the new tempo to the number you type in.

When using the change tempo block, it does matter what the currenttempo is. It will add or subtract from the current tempo.

Volume

Volume works the same way as tempo!

Use the checkbox next to the "volume" block to always see the volume.

Use the set volume block to reset the volume to the level you want.

Use the change volume block to increase or decrease the volume bya certain amount.

Page 38: My summary for cs001x computer science for beginners

(35)

Ibrahim Omar CS 2015

Designing Games

In this section, we'll try building a few example games together, whichwe'll call the target game and the helicopter game. Then, you can usethe skills from those games to develop your own projects.

Basic Target Game

In this first exercise, you'll create a game where a (flying or swimming)sprite tries to reach a target while avoiding an enemy. The next fewpages will give examples of possible additions to this basic game.

Flying

First, you'll create a new Scratch project and add a flying character toyour new Scratch project. Just under the stage area "New Sprite"followed by Click the "Choose Sprite from Library" button to add asprite from Scratch's collection of pre-made sprites.

Choose a new sprite, preferably one that either flies or swims. Then,add blocks like these that allow your sprite to move around the screenusing the four arrows keys:

After you've tested that your sprite moves as expected, add thefollowing scripts so that the sprite can jump to the left side of thestage by moving to the right edge, effectively "wrapping around" thestage area.

Page 39: My summary for cs001x computer science for beginners

(36)

Ibrahim Omar CS 2015

These scripts will wrap for a sprite to opposite side when movinginto the side edge. You can add wrapping scripts as you see fit.

Then, add an enemy sprite. Your Hero will want to avoid this enemy.We want the enemy to move continuously as it tries to prevent thehero from reaching its goal.

Add this script to your enemy to allow it to move continuously:

Start Location

We want our hero sprite to always start the game at the same locationand to teleport back to that same location each time it runs into an enemysprite. We'll use the position x:-170, y:-140 as our start location.

Page 40: My summary for cs001x computer science for beginners

(37)

Ibrahim Omar CS 2015

The first block of the script makes sure that when the programstarts, the sprite moves to the starting location.

Then, the "forever" block below will continue to check whether thehero sprite is touching the enemy sprite for as long as the programis running. If it touches the Enemy, it will go to the same startinglocation as in the first block.

Customizing Your Target Game

Now, you're ready to customize your game. Add at least two of thesefeatures:

Add a changing score variable.

Add more enemies.

Have the enemies move unpredictably.

Add "power-up" sprites that change your hero's size or speed.

Add portals that teleport your hero randomly.

Have the game end when a timer runs out.

Keeping Score

Click on the "Data" tab and then click the "Make a Variable" button.Name our new variable "score".

Then write a script that checks if the hero is touching the goal. If it is,then increase our score variable using the "change score by 1" block.

Page 41: My summary for cs001x computer science for beginners

(38)

Ibrahim Omar CS 2015

If you try running the game with this previous script, you'll see that thescore increases way too fast!, To fix this problem, add a block thatwaits to add a point to the score until the hero is no longertouching the goal.

This block should go inside previous script in "if" block before the"change score" block. This way, we'll only add one point each timethe hero touches the goal.

Making the Enemy move Randomly

To make the game a little more interesting, we'll make our enemymove unpredictably around the stage area.

Adding a Timer

Scratch has a built-in timer to count seconds from the "Sensing" tab.To activate the timer, click on the checkbox next to the "timer"block, you may want to include a "reset timer" block after "when flagclicked" to make sure that the timer starts at 0 when the game starts.

We'll need to add a script that ends the game when the timer reaches atime limit. We'll use the "wait until" block, which pauses the scriptafter a certain time, for example: 20 seconds.

Page 42: My summary for cs001x computer science for beginners

(39)

Ibrahim Omar CS 2015

To let the player know that the game is over, you may want your spriteto say "Game over!" before or after stopping the program.

Adding Power-up

We'll show how to add a power-up, which will appear in a randomlocation on the stage, then disappear after the hero claims it. After afew seconds, it will reappear in a different location. Power-up willincrease the score by one point when the hero touches it.

As soon as the game starts, the power-up will immediately becomeinvisible and teleport to a random spot in the stage area. Then,after 2 seconds, it will appear, and will wait in place until the Hero istouching it.

At that point, it will add one point to the score, then disappearagain for another two seconds as it jumps to another randomlocation. The forever loop ensures that the power-up will keepregenerating after 2 seconds for as long as the program is running.

Page 43: My summary for cs001x computer science for beginners

(40)

Ibrahim Omar CS 2015

Helicopter Game

We're going to start working on a new project, building a game with a"Helicopter" that flies. After finishing the project, the Helicopter will beable to fly to the right and the left and land on a platform. It will also beaffected by gravity.

(1) Start by making a new project.

(2) Open up a new sprite by click on the button.

In the Sprite Library, under the "Transportation" category double clickon the "Helicopter".

Then, write a script for each of the following, you can use the Targetgame as a reference:

When you press the left arrow, make the Helicopter move left.

When you press the right arrow, make the Helicopter move right.

Page 44: My summary for cs001x computer science for beginners

(41)

Ibrahim Omar CS 2015

Switching Directions with Costumes

The Helicopter might look a little funny flying left while it faces right.Make a new costume for the helicopter where it faces the correct wayby: Click on the "costumes" tab.

(3) Then click the "Flip Left-Right" button so that the new costume isnow facing the other direction.

Now you have 2 costumes for your helicopter like the picture below.

Page 45: My summary for cs001x computer science for beginners

(42)

Ibrahim Omar CS 2015

Use the new costume you just made when the helicopter flies left.This block might be helpful:

Gravity

Whenever the up arrow is not pressed, the helicopter should floatdown like is being pulled down by gravity.

Try using a forever block to always change the helicopter's y value bysome small negative number. For example: - 0.5

Platforms

In this step, we'll make the helicopter stop falling when it lands onplatforms.

Make sure you have 2 points that mark the bottom of your helicoptersprite as (2 green dots you see in the picture). This should be a colorthat you're not using in any of your other sprites.

Page 46: My summary for cs001x computer science for beginners

(43)

Ibrahim Omar CS 2015

Make sure you add the dots to both the left-facing and right-facingcostumes.

You also need to have ONE platform sprite, make a new sprite usingthe "Paint New Sprite" button shown below. The platform should looksomething like the one below (it can be any color).

Now add this Script to your helicopter. The first color is the colorof your dots, the second color is the color of your platform.

When you click on the color, you will be able to choose any color onyour screen by clicking on that new color with your mouse.

Page 47: My summary for cs001x computer science for beginners

(44)

Ibrahim Omar CS 2015

Adding Features

It's time to add some obstacles to your helicopter game. Here are someexamples of the many possible ideas:

The helicopter can land on other platforms.

The helicopter can collect prizes to gain points or speed boosts.

The helicopter is escaping from some sort of enemies.

The helicopter loses points if it hits lava at the bottom.

The helicopter loses points if it is hit by moving bird.

The helicopter falls faster if it touches a black hole.

The helicopter spins out of control if it hits a rock or when a rockfalls on it from above.

Note: If the rock is going to move, it has to be a Sprite. There is a rockSprite in the "Sprite Library" under the "category Things". If the rockis stationary, it can be drawn on the stage background.

No limit for innovation, you can add any other tools in your game.

The Shark (My 1st Scratch Game): scratch.mit.edu/projects/71581906

Page 48: My summary for cs001x computer science for beginners

(45)

Ibrahim Omar CS 2015

Week (5)Problem Solving and Algorithms

What are Algorithms?

When we solve problems with computers, we want our tools to work fornot just one specific problem, but a whole category of problems. In orderto make a tool like this, we have to make a step-by-step procedurecalled an algorithm.

Think of an algorithm as a set of instructions that we can use to solveany version of a particular kind of problem. For instance, long division isan algorithm that we use to divide one number by another. It doesn'tmatter two numbers we use; if we follow the steps of long division, we'llfind the correct quotient at the end of the process.

Humans and computers alike use algorithms to solve all sorts ofproblems. Press on the video link below, created by TED-Ed andHarvard CS Professor David J. Malan, to see an example of howalgorithms work: https://www.youtube.com/watch?v=6hfOvs8pY1k

Problem Solving Techniques

To create good algorithms, there's a lot that goes into the design of agood algorithm. It can't be just set of instructions: it should be clearand relatively quick, and it should work every time for the category ofproblems it's designed to solve.

Page 49: My summary for cs001x computer science for beginners

(46)

Ibrahim Omar CS 2015

These questions are a good starting point to know what ouralgorithm is supposed to do:

1) What information are we given? While each version of a problemmay have its own specific information, the categories of informationprovided in each version of the problem should be the same. Forexample, in an addition problem, we start with a list of numbers to addtogether. These are the inputs to our addition algorithm and the outputwill be their sum.

2) What should our solution look like? We might be looking for anumber, a word, a list of words, or maybe just an answer to a "True"or "False" question. Our algorithm should end with us finding orcreating this solution.

3) How do we know our solution is correct? To answer this question,we have to understand the rules of our problem that our solutionwill need to follow. For instance, if we successfully solve a Rubik'sCube, then each side of the cube will be entirely one color. We can usethese rules as a way of checking our answers for our algorithm.

4) What are the simplest examples of this problem? If we try to createan algorithm to find the tallest person in any group of people, weshould start by considering the easiest versions of this problem.For instance, if our "group" of people only has one person, then thatperson is guaranteed to be the tallest.

5) What are the trickiest examples of this problem? To make sure ouralgorithm works on every version of our problem, we need to thinkabout the versions of the problem most likely to make ouralgorithm mess up. For our addition problem, what do we output ifwe're asked to add up a list with no numbers in it? For our tallestperson problem, what answer to we give if there are two people of thesame height? Our algorithms may need special instructions fordealing with these trickier problems.

Page 50: My summary for cs001x computer science for beginners

(47)

Ibrahim Omar CS 2015

Problem: Handshakes

We can use algebra to represent the number of handshakes at a partywith n guests.

You have to shake (n – 1) hands، because you're not shaking your hand.

Every person at the party needs to shake n minus 1 hands, becauseeverybody has to shake everybody else's hands.

We have n people that need to do this.

So we multiply this by n. So we have n times (n – 1).

However, we over counted, because handshakes go two ways. Wecounted going one direction and again for the other direction. To accountfor that, we just divide the whole number by two and that's our answer.

The Answer = n (n – 1)/2

If we have 8 guests, how many handshakes will a correctalgorithm require?

The Answer = (8 x 7)/2 = 28 handshakes

Page 51: My summary for cs001x computer science for beginners

(48)

Ibrahim Omar CS 2015

Search Problems

As computers and the internet have grown more popular and morepowerful, there's been a surge in the amount of data and informationthat can be found. But with all of this information, how do we find whatwe are looking for?

Linear Search: https://youtube.com/watch?v=kjurNV5Ffv0

Inputs: a target (the item that you want to find), and a list of items tosearch through.

Instructions:

1. Start at the beginning of the list.

2. Check the first item on the list to see if it's what you're looking for.

If it is the right item, you're done.

If not, move to the next item on the list.

3. Continue to check the items in order until either you find youritem or you reach the end of the list.

This algorithm can work well when you don't know anything about howthe list is organized, but when lists get big, this method becomes slow.

The simplest search algorithm is linear search. Here's how it works:

Page 52: My summary for cs001x computer science for beginners

(49)

Ibrahim Omar CS 2015

Binary Search: https://youtube.com/watch?v=iDVH3oCTc2c

If we know the way that items on our list are organized, we can takeadvantage of that information to make searching much faster.

To use the binary search algorithm, we need to know the rule that thelist that we're searching has been sorted based upon it. This could bealphabetical order, numerical order or something else entirely. If wecompare any two items from our list, we'll know from our rules which onecomes before the other.

Imagine that our list is a dictionary, where all of the items are wordsarranged in alphabetical order. Here's how it works:

Inputs: a target item (what you're searching for) and a sorted list.

Instructions:

1. Start at the item half-way through your sorted list.

2. Compare this center item to your target.

If the two items match, you're done.

Page 53: My summary for cs001x computer science for beginners

(50)

Ibrahim Omar CS 2015

If your target item should come before this center item, then youritem won't be in the second half of the list. Ignore the second halfand move to the center of the first half of your list.

If your target item should come after this center item, then youritem won't be in the first half of the list. Ignore the first half andmove to the center of the second half of your list.

3. Repeat this process of checking an item at the center of theremaining list, determining whether the target should come beforeor after the center item and eliminating the other half of the list.

4. End when either you find the target or you eliminate all items fromthe list.

We only need one more additional step, when we double thenumber of items in our list, because our first step will cut the listin half, making the problem the same size as before again.

Linear vs. Binary Search

The difference between the two becomes more pronounced with largerdata sets; after all, linear search will probably take twice as long tosearch through a data set that's twice as big, whereas binarysearch will only require one additional step.

But binary search requires a sorted list, it's less flexible than linearsearch. Not all data lends itself well to sorting and even the fastestalgorithms required to sort data are still much slower than the ones weuse to search through it. This can make linear search a betterchoice on occasion, since it will work on any list, sorted or not,without any additional work.

Page 54: My summary for cs001x computer science for beginners

(51)

Ibrahim Omar CS 2015

Comparing Algorithms

We don't just care whether they successfully solve our problem. Often,multiple algorithms will work to solve the same problem, but will run atdifferent speeds and sometimes even give back different correct answers.

Many computer scientists focus their work on algorithms, answeringquestions such as:

need to solve it?

Sorting Problems

In this section, we'll be looking at different algorithms that can beused to sort objects. Just like with searching, some algorithms performfaster than others; we'll compare them as we go.

Can this problem be solved with an algorithm.

How good are the results that this algorithm finds.

How quickly does this algorithm solve the problem.

Is there another algorithm faster than this one? How much faster.

How much does the size of our problem affect the amount of time

Page 55: My summary for cs001x computer science for beginners

(52)

Ibrahim Omar CS 2015

Thinking about Sorting

In general, computers must sort by comparing only two elements at atime, without being able to assume much about the rest of the list. Goodsorting algorithms try to use as few of these comparisons as possible tosort a list, but we can't take shortcuts by eliminating part of the list:even the fastest ones will still have to look at every element of thelist at least once and usually multiple times.

In this algorithm, computer repeatedly searches through the unsortedlist to find the smallest element, then puts that element at thebeginning of the list before checking for the next smallest element.

Inputs: an unsorted list and a method for sorting them (in this case,in increasing numerical order).

Instructions:

1. Start at the beginning of the list.

2. Compare the first two numbers in the list. Whichever number issmaller will be our current minimum.

3. Repeatedly move down the list one space and compare ourcurrent minimum to the next element in the list.

If the next element is smaller than our current minimum, wereplace our current minimum with the next element before movingdown the list.

If the next element is larger than our current minimum, we holdonto our minimum and move down the list.

4. When we reach the end of the list, we move whatever element wassmallest to the beginning of our list.

1. Selection Sort: YouTube.com/watch?v=92BfuxHn2XE

Page 56: My summary for cs001x computer science for beginners

(53)

Ibrahim Omar CS 2015

5. We repeat steps 1-4 with the unsorted part of the list, leaving theearlier minimum values at the far left. Once every element hasbeen moved to the sorted list on the left, we're done.

This algorithm is called Selection Sort because each time we passthrough the unsorted list, we select precisely one element to put nextin our sorted list.

In this algorithm, the computer "marches through" the unsorted listand compares only numbers that are right next to each other, two at atime. If the numbers are out of order, it switches them.

Inputs: an unsorted list and a method for sorting them (in this case,in increasing numerical order).

Instructions:

1. Start at the beginning of the list.

2. Compare the first two numbers in the list.

If the first number is smaller than the second number, do nothing.

If the first number is larger than the second number, have thesetwo numbers trade places so that the smaller one comes first.

3. Repeat step 2 until you reach the end of the list, then start againat the beginning of the rearranged list.

4. Repeat steps 1-3 until you can pass through the entire list withoutswitching the locations of any numbers. Then, you're done.

This algorithm is called Bubble Sort because the largest number"bubbles up" from the unsorted list on the left into the sorted list onthe right.

2. Bubble Sort: YouTube.com/watch?v=Cq7SMsQBEUw

Page 57: My summary for cs001x computer science for beginners

Ibrahim Omar CS 2015

3. Merge Sort: https://youtube.com/watch?v=ZRPoEKHXTJg

It works by splitting the list you want to sort in half and sorting eachhalf individually.

Those halves will be sorted the same way, by dividing the quarterlists in half and so on until every "half list" is just a single element.Then, these elements get merged back together into sorted lists.

Inputs: an unsorted list and a method for sorting them (in this case,in increasing numerical order).

Instructions:

1. If the list is only one item long, it's sorted.

2. If the list has more than one item, split it in half and sort each halfusing Merge Sort.

3. Then, repeatedly compare the first elements of each half, movingthe smaller one to the left side of a final, sorted list.

4. When all of the elements of the two halves are added to the final,sorted list, the algorithm is done.

This algorithm is called Merge Sort because we sort the list byrepeatedly merging two independently sorted lists. Merge Sort canseem a bit magical in how it works, since it's not clear at what stepelements are being put into the correct order.

The idea behind the algorithm, though, is very similar to what makesBinary Search so effective: because we halve the size of the listrepeatedly, we don't need to make as many comparisons, meaning thisis a much faster algorithm.

Page 58: My summary for cs001x computer science for beginners

Professor, Departm

ent of Com

puter ScienceH

arvey Mudd C

ollege

Zachary Dodds

HO

NO

R C

OD

E C

ER

TIFICA

TEVerify the authenticity of this certificate at

CERTIFIC

ATEH

ON

OR C

OD

E

Ibrahim

Om

arsuccessfully co

mp

leted and

received a p

assing g

rade in

CS0

01

x: MyC

S: Co

mp

uter Science for B

eginners

a course o

f study o

ffered b

y HarveyM

udd

X, an o

nline learning

initiative of H

arvey Mud

d C

olleg

e throug

h edX

.

Issued July 2

7, 2

01

5http

s://verify.edx.o

rg/cert/b

87

34

98

d9

63

84

10

ab3

f0b

25

b2

e0b

68

45