February 23, 2018

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
Algorithms
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 cat at coordinates (0,150)
  • hide the cat
  • clear any existing drawing
  • put the pen down
  • point in direction 130 degrees
  • repeat 40 times
    • move 200 steps
    • turn right 130 degrees

Abstraction: Input

Let’s forget about specific numbers.

In other words, let’s make an abstraction

  • N: How many sides we will draw
  • angle: how do you turn after each side
  • R: radius, distance from the center to each peak
  • initial_angle: initial angle
  • size: length of each side

Decomposition: Steps

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

  • Move R steps to the first corner
  • Point in the direction initial_angle
  • repeat N times
    • draw a line of length size
    • turn right angle degrees

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

You already have seen a first 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

Procedures
A group of specific steps grouped together

In R we represent procedures 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)}

then

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

Hint
initial_angle must be 90+angle/2
size must be 2*R*sin(angle*pi/360)

Homework: Why these values?

We use R scripts

Last semester we used RMarkdown

This semester we will use R Scripts

Be sure of understanding the difference