February 22, 2019
Write a script that make the cat do the following:
You can install TurtleGraphics in R with this command
install.packages("TurtleGraphics")
Then you load into the session using
library(TurtleGraphics)
Now you start a new “terrarium” with
turtle_init()
Set Up a new Terrarium
turtle_init(width = 100, height = 100, mode = "error")
turtle_move(10)
move (10) steps
turtle_right(15)
turn cw (15) degrees
turtle_left(15)
turn ccw (15) degrees
turtle_goto(0,0)
go to x:(0) y:(0)
turtle_setangle(90)
point in direction (90 v)
turtle_setpos(0,0)
Move without drawingturtle_up()
pen up
turtle_down()
pen down
turtle_col(0)
set pen color to (0)
turtle_lwd(1)
set pen size to (1)
turtle_lty(1)
Change line typeturtle_hide()
hide
turtle_show()
show
turtle_getangle()
(direction)
turtle_getpos()
(x position)
(y position)
Evaluate a Larger Portion of Turtle Drawing Code
turtle_do({
code here})
turtle_init()
turtle_getangle()
angle 0
turtle_getpos()
x y 50 50
So we have to adapt our code (how?)
turtle_move(80)
Error in .turtle_draw_error(distance, curX, curY, curAng, curGp, curDraw, : The Turtle escaped from the terrarium. :-(
turtle_init(mode = "clip")
Please write in paper what we do on Rstudio
What happens when the turtle gets out?
Since the turtle is at (50,50),
How do we change our code?
Separating a complex problem into smaller, more manageable parts
Here we separate a complex figure into many smaller parts
Is this problem similar to another?
Are there parts of this problem that are similar to each other?
Please answer this question
You already have seen a first algorithmic structure: loops
repeat (10)
which in R we write as
for(i in 1:10) { ... }
The second algorithmic structure you can use are
In R we represent sub-routines with functions
In Math and Informatics, a function is a “black box”
A rule to transform the input elements into an output
The same input should produce always the same output
Notice that there may be more than one input element
In R functions are a type of data. We have
To create a function we need to assign it to a variable
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
my_func <- function(a, b=2) {return(a*b)}
my_func(3, 3)
[1] 9
my_func(3)
[1] 6
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)
Before writing the function we need to decide:
Then we can start writing the code
Computers can be used to help us solve problems
Before a problem can be tackled, we need to understand the problem itself and the ways in which it could be solved
Computational thinking allows us to do this
Computational thinking allows us to take a complex problem, understand what the problem is and develop possible solutions
We can then present these solutions in a way that a computer, a human, or both, can understand
decomposition
pattern recognition
abstraction
algorithm design
Breaking down a complex problem or system into smaller, more manageable parts
Looking for similarities among and within problems
Focusing on the important information only, ignoring irrelevant detail
Developing a step-by-step solution to the problem, or the rules to follow to solve the problem
They are like legs on a table
if one leg is missing, the table will collapse
Correctly applying all four techniques will help when programming a computer.
A complex problem is one that, at first glance, we don’t know how to solve easily.
Computational thinking involves taking that complex problem and breaking it down into a series of small, more manageable problems (decomposition). Each of these smaller problems can then be looked at individually, considering how similar problems have been solved previously (pattern recognition) and focusing only on the important details, while ignoring irrelevant information (abstraction). Next, simple steps or rules to solve each of the smaller problems can be designed (algorithms).
Finally, these simple steps or rules are used to program a computer to help solve the complex problem in the best way.
Computational thinking is about problem solving
Almost any problem can be solved using computational thinking
For example: Sports, Projects, Science
Using R Turtle Graphics, write programs to draw:
Filename must be homework2.R
.
Send it to andres.aravena+cmb@istanbul.edu.tr
Write your student number in the email’s Subject