March 3, 2020

Your file must be `.R`

, not `.Rhistory`

Files with extension `.Rhistory`

show all the commands you did, not only the “good” ones

It is fine to try and make mistakes when you explore a problem

The answer should contain only the good part

Show what is important, not what is not important

#'title: "Homework 3" #'subtitle: "Computing in Molecular Biology and Genetics II" #'author: "İlker Şam" #'number: 0405150449 #'date: "February 27, 2020" # 1. Draw a pentagon ### DO NOT DELETE THIS ### #+ q1 # write here library(TurtleGraphics) turtle_init(width = 480, height = 360, mode = "clip") turtle_getpos() turtle_up() turtle_goto(200, 240) turtle_setangle(90) turtle_down() turtle_hide() turtle_col(col = "blue") turtle_do({ turtle_move(50) turtle_left(72) turtle_move(50) turtle_left(72) turtle_move(50) turtle_left(72) turtle_move(50) turtle_left(72) turtle_move(50) turtle_left(72) }) ### DO NOT DELETE THIS ###

- Used the provided file template
- Wrote name and number in the correct place
- Easy to understand

library(TurtleGraphics) install.packages("TurtleGraphics") turtle_init(mode = "clip") draw_polygon <- function(N, length, angle){ for (i in 1:N) { turtle_move(length) turtle_right(angle) } } draw_polygon(10, 20, 36) turtle_hide()

- Good: better code, used
`for(){}`

loop - Bad: not used the template
- Bad: not name, not number
- Bad:
`install.packages("TurtleGraphics")`

should not be in the script

library(TurtleGraphics) turtle_init() install.packages("TurtleGraphics") library("TurtleGraphics") turtle_init() turtle_init(mode = "clip") turtle_right(90) turtle_move(60) turtle_init install.packages("TurtleGraphics") install.packages("TurtleGraphics") install.packages("TurtleGraphics") library("TurtleGraphics") turtle_init(mode = "clip") turtle_right(90) turtle_right(180) turtle_move(20) turtle_left(90) turtle_move(20) turtle_right(90) turtle_right(180) turtle_move(40) turtle_move(10) turtle_left(90) turtle_move(20) turtle_left(90) turtle_move(300) install.packages("TurtleGraphics") library("TurtleGraphics") install.packages("TurtleGraphics") turtle_init() install.packages("TurtleGraphics") turtle_init(mode = "clip") install.packages("TurtleGraphics") library("TurtleGraphics") turtle_init() turtle_init(mode = "clip") turtle_right(90) turtle_right(180) > turtle_move(20) > turtle_left(90) > turtle_move(20) > turtle_right(90) > turtle_right(180) > turtle_move(40) > turtle_move(10) > turtle_left(90) > turtle_move(20) > turtle_left(90) turtle_left(90) turtle_move(20) turtle_left(90) turtle_move(20) turtle_left(90) turtle_move(20) turtle_move(30) turtle_left(90) turtle_move(20) turtle_left(90) turtle_move(30) turtle_right(30) turtle_move(20) turtle_right(60) turtle_left(30) install.packages("TurtleGraphics") install.packages("TurtleGraphics") install.packages("TurtleGraphics") library("TurtleGraphics") turtle_init() turtle_init(mode = "clip") turtle_right(90) turtle_right(180) > turtle_move(20) > turtle_left(90) > turtle_move(20) > turtle_right(90) > turtle_right(180) > turtle_move(40) > turtle_move(10) > turtle_left(90) > turtle_move(20) > turtle_left(90) turtle_left(90) turtle_move(20) turtle_left(90) turtle_move(20) turtle_left(90) turtle_move(20) turtle_left(90) turtle_move(20) turtle_right(90) turtle_move(30) turtle_right(90) turtle_move(20) turtle_right(90) turtle_move(30) turtle_setpos(50,50) turtle_left(30) turtle_right(30) turtle_right(60) turtle_move(20) turtle_left(120) turtle_move(20) turtle_setpos(80,50) turtle_right(30) turtle_right(30) turtle_right(60) turtle_move(20) turtle_left(60) turtle_move(30) turtle_setpos(60,40) turtle_move(5) turtle_right(90) turtle_move(5) turtle_right(90) turtle_move(5) turtle_right(90) turtle_move(5) turtle_setpos(75,40) turtle_right(90) turtle_move(5) turtle_right(90) turtle_move(5) turtle_right(90) turtle_move(5) turtle_right(90) turtle_move(5) turtle_setpos(45,30) turtle_right(180) turtle_setpos(44,30) turtle_move(10) turtle_left(90) turtle_move(8) turtle_left(90) turtle_move(10) turtle_setpos(10,95) turtle_setpos(10,90) for (i in 1:5) { turtle_move(10) turtle_right(72) } turtle_setpos(25,90) for (i in 1:5) { turtle_move(10) turtle_left(72) } turtle_setpos(15,15) turtle_setpos(15,30) turtle_left(180) turtle_move(10) turtle_left(90) turtle_move(3) turtle_right(90) turtle_move(6) turtle_right(90) turtle_move(6) turtle_right(90) turtle_move(6) turtle_right(90) turtle_move(3) turtle_setpos(15,30) turtle_move(3) turtle_setpos(15,30) turtle_right(180) turtle_move(3) turtle_setpos(15,37) turtle_right(45) turtle_move(3) turtle_setpos(15,37) turtle_left(90) turtle_left(180) turtle_move(3) turtle_hide()

- This last example was a
`.Rhistory`

- We do not know who sent it
- We do not know what is important, and what is not
- But we can see how the student tries different ideas

All these answers did the correct thing

The comment is not about the *content*, but about the *format*

It is important to follow the format *now*, since we will use it for the exam.

turtle_right(90) turtle_forward(5) turtle_left(90) turtle_forward(10) turtle_left(90) turtle_forward(10) turtle_left(90) turtle_forward(10) turtle_left(90) turtle_forward(5) turtle_right(90)

# neck turtle_forward(7) # arms turtle_left(90) turtle_forward(10) turtle_right(180) turtle_forward(20) turtle_left(180) turtle_forward(10) turtle_right(90)

# body turtle_forward(15) turtle_right(40) # first leg turtle_forward(15) turtle_left(90) turtle_forward(5) turtle_left(180) turtle_forward(5) turtle_right(90) turtle_forward(15)

# direction of second leg turtle_right(100) # second leg turtle_forward(15) turtle_left(90) turtle_forward(5) turtle_hide()

turtle_right(90) turtle_forward(5)turtle_left(90) turtle_forward(10) turtle_left(90) turtle_forward(10) turtle_left(90) turtle_forward(10)turtle_left(90) turtle_forward(5) turtle_right(90)

turtle_right(90) turtle_forward(5)for(i in 1:3) { turtle_left(90) turtle_forward(10) }turtle_left(90) turtle_forward(5) turtle_right(90)

(use `h`

key)

turtle_right(90)turtle_forward(5)for(i in 1:3) { turtle_left(90)turtle_forward(10)} turtle_left(90)turtle_forward(5)

Let’s say `size=10`

(for now)

turtle_right(90)turtle_forward(size/2)for(i in 1:3) { turtle_left(90)turtle_forward(size)} turtle_left(90)turtle_forward(size/2)

(use `h`

key)

# neck turtle_forward(7) # arms turtle_left(90) turtle_forward(10) turtle_right(180) turtle_forward(20) turtle_left(180) turtle_forward(10) turtle_left(90)

# neck turtle_forward(size*7/10) # arms turtle_left(90) turtle_forward(size) turtle_right(180) turtle_forward(size*2) turtle_left(180) turtle_forward(size) turtle_left(90)

# body turtle_forward(15) turtle_right(40) # first leg turtle_forward(15) turtle_left(90) turtle_forward(5) turtle_left(180) turtle_forward(5) turtle_right(90) turtle_forward(15)

# body turtle_forward(size*15/10) turtle_right(40) # first leg turtle_forward(size*3/2) turtle_left(90) turtle_forward(size/2) turtle_left(180) turtle_forward(size/2) turtle_right(90) turtle_forward(size*3/2)

# direction of second leg turtle_right(100) # second leg turtle_forward(15) turtle_left(90) turtle_forward(5)

# get back to initial angle turtle_right(100) # second leg turtle_forward(size*3/2) turtle_left(90) turtle_forward(size/2)

We can *simplify* even more by *decomposing* and looking at the *patterns* in the problem

This can be decomposed as:

- Body
- Head
- 2 arms
- 2 legs

draw_person <- function(size) { draw_head(size) turtle_left(180) turtle_forward(size/2) turtle_left(90) draw_arm(size) turtle_left(180) draw_arm(size) turtle_left(90) turtle_forward(size*3/2) turtle_left(40) draw_leg(size) turtle_right(40) draw_leg(size) }

This is the main function.

It is my part.

I can change it.

You should not change it.

Instead, you have to provide functions for head, arms and legs.

draw_head<-function(size){ } draw_arm<-function(size){ } draw_leg<-function(size){ }

Each part commits to do something

Each part makes a promise

*We promise to leave the turtle in the same position as we received it*

You should always start with the easy parts

The easy part may not be the first part

We receive the turtle pointing in the arm direction

We must leave the turtle in the same place and same angle

draw_arm <- function(size) { turtle_forward(size) turtle_backward(size) }

draw_arm <- function(size) { turtle_forward(size) turtle_backward(size) }

draw_leg <- function(size) { turtle_forward(size*3/2) turtle_left(90) turtle_forward(size/2) turtle_backward(size/2) turtle_right(90) turtle_backward(size*3/2) }

Notice that to undo something you have to undo each part in reverse order

You put your socks first, then your shoes

To undo you first “un-put” your shoes, then your socks

In general

Undo(A,B,C) = Undo(C), Undo(B), Undo(A)

Each function has a separate **environment** with its own variables

We can save *pos* and *angle* at the start, and reset them at the end

draw_leg <- function(size) { old_pos <- turtle_getpos() old_angle <- turtle_getangle() turtle_forward(size*3/2) turtle_left(90) turtle_forward(size/2) turtle_setangle(old_angle) turtle_setpos(old_pos[1], old_pos[2]) }

This is useful when the drawing is complex. **Keep it in mind**

We separated the big problem on **independent** parts

We can change each part without affecting the others, as long as we keep our promises.

For example, you can change the position of the hands, the shape of the head and hands, an others

The code is in stick-person-2.R.

- a small insect.
*informal*a harmful microorganism, as a bacterium or virus.

- an insect of a large order distinguished by having mouthparts that are modified for piercing and sucking.
- a miniature microphone, typically concealed in a room or telephone, used for surveillance.
- an error in a computer program or system.

Debugging is designed to help you find bugs

To do this, you need to:

- Begin running the code
- Stop the code at the point where you suspect there is a problem
- Walk through the code, step-by-step

The most common way to stop on a line of code is to set a breakpoint.

You can do this by clicking to the left of the line number, or by pressing `Shift+F9`

.

Once your code stops, you will enter “debug mode”

Usually in R you’re interacting with the *“global environment”*

In debug mode, RStudio shows the currently function’s environment

- The objects you see in the Environment pane are in the current function
- Your commands will be evaluated in the context of the function

The code window shows you the currently executing function. The line about to execute is highlighted in yellow

While debugging, you’ll notice two changes to the R console

The first is that the prompt is different:

Browse[1]>

This prompt indicates that you’re inside the R environment browser.

While debugging you can use all the normal commands, plus this:

- Commands are evaluated in the current environment
- If your function has a variable named
`x`

, typing`x`

at the prompt will show you the value of that variable

- If your function has a variable named
- Pressing
`Enter`

at the console will execute the current command and move on to the next one - Several special debugging commands are available

This toolbar provides buttons for debug control commands

- There’s no difference between using the toolbar and entering the commands directly
- learn the command shortcuts

Command | Shortcut | Description |
---|---|---|

`n` or `Enter` |
`F10` |
Execute next statement |

`s` |
`Shift+F4` |
Step into function |

`f` |
`Shift+F6` |
Finish function/loop |

`c` |
`Shift+F5` |
Continue running |

`Q` |
`Shift+F8` |
Stop debugging |

You can also type `help`

at the `Browse[N]>`

prompt

The traceback shows you how execution reached the current point,

- from the first function that was run (at the bottom)
- to the function that is running now (at the top).