f = function(n){
return(n*2)
}
# What does this display?
#f(3) + 100
# What does this display?
#x = f(3)
# What does this display?
#356 56. Practice Questions - TOPICS: cat, loops (reading code)
We went over many of these questions in our final review class (Fall 2023). In order to be able to answer these questions effectively it is imperative that you keep track the values of arguments and variables on a piece of paper as you mentally step through the code. Below are links to the videos from those review classes as well as PDFs of the notes that I took to keep track of the variables during those review classes.
-Prof. Rosenthal
Beren Review Class
Wilf Review Class
56.1 TOPICS: return vs cat
56.1.1 QUESTION 1 - Topics: return value of a function
What is displayed by the following code?
56.1.2 QUESTION 2 - Topics: cat
# What is displayed by the following code?
#cat(3)
# What is displayed by the following code?
#cat(3) * 2
# What is displayed by the following code?
#x = cat(3)
# What is displayed by the following code?
#x56.1.3 QUESTION 3 - Topics: return vs cat
What is displayed by the following code?
f = function(n){
cat(n*2)
return(0)
}# What does this display?
#f(3) + 100# What does this display?
#x = f(3)# What does this display?
#x56.1.4 QUESTION 4 - Topics: return vs cat
What is displayed by the following code?
f = function(n){
cat(n*2)
}
# What does this display?
#f(3) + 100
# What does this display?
#x = f(3)
# What does this display?
#x56.2 TOPICS: loops (for and while)
56.2.1 QUESTION 5 - Topics: for loops
What is displayed by the following code?
f = function(n){
for (x in 1:n){
cat(x * x, " ")
}
cat("\n")
return(c(x, n))
}
# What does this display?
#result = f(4)
# What does this display?
#result56.2.2 QUESTION 6 - Topics: while loops, cat vs return
What is displayed by the following code?
f = function(n){
x = 1
while (n >= 1){
cat(x, " ")
x = x * 2
n = n - 1
}
cat("\n")
return(c(x, n))
}
# What does this display?
#x = f(4)
# What does this display?
#x56.2.3 QUESTION 7 - Topics: for loops
What is displayed by the following code?
f = function(n){
for (x in 1:n){
cat("*** starting outer for loop again***\n")
for (y in x:n){
cat("x is ", x, " ")
cat("y is ", y, "\n")
}
}
return( c(x,y) )
}
# What does this display?
#result = f(4)
# What does this display?
#result56.2.4 QUESTION 8 - Topics: for loops, nested loops, if
What is displayed by the following code?
drawSomething = function(size){
if (!is.numeric(size) || trunc(size) != size || size < 0){
stop("size must be a non-negative whole number")
}
for (row in 1:size){
for (col in 1:size){
if (col == row || col == size - row + 1){
cat("@")
} else {
cat(".")
}
}
cat("\n")
}
cat("\nHope you like the picture! :)\n")
}
# What do these display?
#drawSomething(3)
#drawSomething(5)
#drawSomething(1)
#drawSomething(0) # be careful! This is tricky. Probably not what you'd expect.56.2.5 QUESTION 9 - Topics: for loops, nested loops, if
The only thing that changed are the lines that are highlighted in the code with “# THIS CHANGED”.
What will be displayed now?
drawSomething = function(size){
if (!is.numeric(size) || trunc(size) != size || size < 0){
stop("size must be a non-negative whole number")
}
for (row in 1:size){
for (col in 1:size){
if (col == row || col == size - row + 1){ # THIS CHANGED
# THIS CHANGED
if ( row %% 2 == 0) # THIS CHANGED
cat("0") # THIS CHANGED
else # THIS CHANGED
cat("X") # THIS CHANGED
# THIS CHANGED
} else {
cat(".")
}
}
cat("\n")
}
cat("\nHope you like the picture! :)\n")
}
# What does this display?
#drawSomething(5)56.2.6 QUESTION 10 - Topics: for loops, nested loops, if
What is displayed by the following code?
drawSomething = function(size){
if (!is.numeric(size) || trunc(size) != size || size < 0){
stop("size must be a non-negative whole number")
}
for(row in 1:size){
if( row == 1 || row == size ){
for (x in 1:size){
cat("-")
}
cat("\n")
} else {
for (col in 1:size) {
if( row == 1 || row == size ){
for (x in 1:size){
cat("-")
}
cat("\n")
} else {
if (col == 1){
cat("|")
} else if (col == size){
cat("|\n")
} else if (row == col) {
cat("x")
} else {
cat(".")
}
}
}
}
}
}
# What does this display?
#drawSomething(5)
# What does this display?
#drawSomething(10)56.2.7 QUESTION 11 - tic tac toe game - Topics: loops
Complete the following code. The code should allow the user to play a game of tic tac toe against the computer. The code as it is now just allows for the first move or two. Complete the code so that it plays a complete game. A the end of the game the program should ask the user if they want to play again (yes/no). If the user types yes or y then the game should be played again. If the user answers no or n then the program should stop. If the user answers anything else, then the question should be posed again.
########################################
# COPMLETE THIS CODE
########################################
#####################################
# This function is complete already
#####################################
isValidMove = function(board, position){
# At this point we are assuming that position is a character value
if(! position %in% 1:9 )
return(FALSE)
# Convert the position to a number since the index in the following
# code must be a numeric value
position = as.numeric(position)
if(board[position] == "x" || board[position] == "o")
return(FALSE)
return(TRUE)
}
# The following function should return
# "x" if x won
# "o" if o won
# "tie" if the board is full and no one one
# "continue" if the game is not over yet
whoWon = function(board){
###################
# FILL IN THE CODE
###################
}
#######################
# FINISH THIS FUNCTION
#######################
ticTacToe = function(){
# Start with an empty board
# i.e. 1 4 7
# 2 5 8
# 3 6 9
board = matrix(as.character(1:9), nrow=3, ncol=3)
userLetter = readline("x or o? ")
# make sure the user chose "x" or "o" and if they didn't
# then ask them to enter their choice again
while(userLetter != "x" && userLetter != "o"){
userLetter = readline("invalid, x or o? ")
}
# figure out the computer's letter
if(userLetter == "x"){
computerLetter = "o"
} else {
computerLetter = "x"
# computer is x so the computer picks the first position
pos = sample(1:9, 1)
board[pos] = "x"
}
cat("user is ", userLetter, "\n")
cat("computer is ", computerLetter, "\n")
# display the board for the very first time
print(board)
# ask the user to enter a value
userPosition = readline("Enter a position: ")
while( isValidMove(board, userPosition) == FALSE){
userPosition = readline("invalid, enter a position: ")
}
board[as.numeric(userPosition)] = userLetter
# computer picks a position
# (important - on the next line we used a single &, NOT &&.
# This is because we want multiple TRUE,FALSE values to be "and'ed"
# together in a vectorized way - this is how & works. However, &&
# does not work at all with logical vectors that are more than just
# one TRUE/FALSE on the left and one TRUE/FALSE on the right side
# of the &&)
validPositions = board [ board != "x" & board!= "o" ]
validPositions = as.numeric(validPositions)
computerPosition = sample(validPositions, 1)
board[computerPosition] = computerLetter
print(board)
}