February 26, 2020

## Key parts of computational thinking

Decomposition
breaking down a complex problem or system into smaller parts
Pattern Recognition
looking for similarities among and within problems
Abstraction
focusing on the important parts only, ignoring irrelevant detail
Algorithm Design
developing a step-by-step solution to the problem

## You can see these when drawing stars

To draw a star, you decompose in these steps:

• put the pen down
• hide the turtle
• repeat 5 times
• move 30 steps
• turn right 144 degrees

## Abstraction: Input

In other words, let’s make an abstraction

• corners: How many sides we will draw
• angle: how do you turn after each side
• initial_angle: initial angle
• length: length of each side

## Decomposition

Separating a complex problem into smaller, more manageable parts

Here we separate a complex figure into many smaller parts

## Decomposition: Steps

Assuming that you know the input values, you need to:

• Point in the direction initial_angle
• repeat corner times
• draw a line of length length
• turn right angle degrees

## Pattern recognition

Is this problem similar to another?

Are there parts of this problem that are similar to each other?

## Pattern recognition

In this case it is easy to see a pattern:

• draw a line of length size
• turn right angle degrees

This pattern is repeated N times

You can do this easily with a loop

## Algorithms: loops

We already know an algorithmic structure: loops

repeat (10)

which in R we write as

for(i in 1:10) {
...
}

## Algorithms

The second algorithmic structure you can use are

Sub-routines
A group of specific steps grouped together

In R we represent sub-routines with functions

## Functions

In Math and Informatics, a function is a “black box”

A rule to transform the input elements into an output

• e.g. logarithm of a number, length of a vector

The same input should produce always the same output

Notice that there may be more than one input element

## Functions in R

In R functions are a type of data. We have

• numeric
• character
• logic
• factor
• formulas
• functions
• and others

To create a function we need to assign it to a variable

## Function syntax

newFunc <- function(input) {
commands
commands
return(output)
}

The keyword return can be omitted

In that case the output is the result of the last command

## Input values

• A function can get zero, one or several inputs
• They are mandatory, unless a default value is specified
• For example, if
my_func <- function(a, b=2) {return(a*b)}
my_func(3, 3)
## [1] 9
my_func(3)
## [1] 6

## What if the input is wrong?

my_func()
## Error in my_func(): argument "a" is missing, with no default
my_func(1,2,3,4)
## Error in my_func(1, 2, 3, 4): unused arguments (3, 4)

## Writing a function

Before writing the function we need to decide:

• the name of the function (it’s harder than it seems)
• which are the inputs
• what is the output

Then we can start writing the code

## Practice

Let’s write a function to draw stars

First in Scratch, then in R

## We use R scripts

Last semester we used RMarkdown

This semester we will use R Scripts

Be sure of understanding the difference

## We are building R programs, no R documents

Last semester we built documents, like papers and slides

These are files with .Rmd extension

This semester we build programs and scripts

These are files with .R extension

Question What are “file extensions”?

## Editing and Executing Code

To create a new file you use the File -> New File menu:

## Executing a Single Line

To execute the line of source code where the cursor currently resides you press the Ctrl+Enter key (or use the Run toolbar button):

## Executing Multiple Lines

We have seen two ways to execute multiple lines:

• Select the lines and press the Ctrl+Enter key (or use the Run toolbar button)

• To run the entire document press the Ctrl+Shift+Enter key (or use the Source toolbar button).

Here Source means “run all code from the file”

## Keyboard Shortcuts

There are many other shortcuts available. Some of the more useful ones are:

Ctrl+Shift+N
New document
Ctrl+O
Open document
Ctrl+S
Save active document
Ctrl+1
Move focus to the Source Editor
Ctrl+2
Move focus to the Console

## Homework 3

Using R Turtle Graphics, write programs to draw:

• A house
• A person
• A pentagon
• A polygon of N sides

Filename must be homework3.R.
Send it to andres.aravena+cmb@istanbul.edu.tr
Write your student number in the email’s Subject