40  Practice Questions - TOPIC: Assorted Topics

40.1 Additional Questions - Assorted Topics

QUESTION 73 TOPIC: vectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 73
# TOPIC: vectors
# 
# Show the last 5% of the values in a vector named v.
# For example 
# - if there are 100 values in v, show the last 5 values
# - if there are 200 values in v, show the last 10 values
# - etc.
# - if there are 43 values in v, 5% of 43 is 2.15 so round up (use ceiling function)
#   to show 3 last values
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
v = seq(10,1000,by=10)                                    # test data - 100 values
v [ (length(v) - ceiling(.05*length(v)) + 1):length(v) ]  # ANSWER
[1]  960  970  980  990 1000
v = seq(10,2000,by=10)                                       # test data - 200 values
v [ (length(v) - ceiling(.05*length(v)) + 1):length(v) ]     # ANSWER (same as above)
 [1] 1910 1920 1930 1940 1950 1960 1970 1980 1990 2000
v = seq(10,430,by=10)                                        # test data - 43 values
v [ (length(v) - ceiling(.05*length(v)) + 1):length(v) ]     # ANSWER (same as above)
[1] 410 420 430

QUESTION 74 TOPIC: dataframes

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 74
# TOPIC: dataframes
#
# Show the last 5% of the rows of the data in the dataframe named df.
# For example 
# - if there are 100 rows, show the last 5 rows
# - if there are 200 rows, show the last 10 rows
# - etc.
# - if there are 43 rows, 5% of 43 is 2.15 so round up (use ceiling function)
#   to show 3 last rows
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# TODO

QUESTION 75 TOPIC: user defined functions

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 75
# TOPIC: user defined functions (creating your own functions)
#
# Write a function with the "signature", evensBetween = function(x,y). 
# x and y are expected to be numbers (they may be even or odd).
# The function should return the even numbers between x and y (including x and y)
# You may assume that the value of x is less than the value of y.
#
# For example:
# > evensBetween(3,9)    
# [1] 4 6 8
#
# > evensBetween(2,10)
# [1] 2 4 6 8 10
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
evensBetween = function(x,y){
  numsBetweenXandY = x:y
  numsBetweenXandY[ numsBetweenXandY %% 2 == 0 ]
}

# test it:
evensBetween(3,9)    # 4 6 8
[1] 4 6 8
evensBetween(2,10)   # 2 4 6 8 10
[1]  2  4  6  8 10

QUESTION 76 TOPIC: user defined functions

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 76
# TOPIC: user defined functions
# SEE ALSO: question 11 for a similar question that doesn't require the creation of a function
#
# Create a function with the signature, swapFirstAndLast = function(vec,num).
# The vector vec is expected to contain at least 2*num values.
# The function should return a vector that contains the same values as vec except
# the first "num" values in vec should be last and the last "num" values in vec should be first
# (see the examples). 
#
# Examples:  
#       > swapFirstAndLast( seq(10,100,by=10), 3)
#       [1] 80 90 100 40 50 60 70 10 20 30
#
#       > swapFirstAndLast( seq(10,100,by=10), 2)
#       [1] 90 100 30 40 50 60 70 80 10 20
#
#       > swapFirstAndLast( c("abe","bob","carla","dana","ed","frank"), 1)
#       [1] "frank"  "bob"  "carla"  "dana"  "ed"  "abe"
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
swapFirstAndLast = function(vec , num) {
  firstPositions = 1:num
  lastPositions = (length(vec)-num+1):length(vec) 
  middlePositions = (num+1):(length(vec)-num)
  vec[ c( lastPositions , middlePositions, firstPositions) ]
}

# Test the answer
swapFirstAndLast( seq(10,100,by=10), 3)
 [1]  80  90 100  40  50  60  70  10  20  30
swapFirstAndLast( seq(10,100,by=10), 2)
 [1]  90 100  30  40  50  60  70  80  10  20
swapFirstAndLast( c("abe","bob","carla","dana","ed","frank"), 1)
[1] "frank" "bob"   "carla" "dana"  "ed"    "abe"  

QUESTION 77 TOPIC: user defined functions (creating your own functions)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION  77
# TOPIC: user defined functions (creating your own functions)
# (see question 11 for a similar question that doesn't require the creation of a function)
#
# Define a function with the signature:  collapsevector = function( vec )
# vec is expected to be a vector with an even number of entries.
#
# The function should return a vector that contains 
# the sum of the first two items from vec
# followed by the sum of the next two items from vec
# followed by the sum of the next two items from vec
# etc.
# 
# Examples:
#    > collapseVector( c(10,20,30,40,50,60,70,80) )
#    [1]  30  70  110  150
# 
#    > collapseVector( c(3,4,10,2,100,3))
#    [1]  7  12  103
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
collapseVector = function( nums ){
  firstThirdFifthEtc <- nums[seq(1,length(nums),by=2)]   # ANSWER (MULTIPLE LINES)
  secondFourthSixthEtc <- nums[seq(2,length(nums),by=2)]     # ANSWER (MULTIPLE LINES)
  answer <- firstThirdFifthEtc + secondFourthSixthEtc  # ANSWER (MULTIPLE LINES)
  answer
}

# Test the answer
collapseVector( c(10,20,30,40,50,60,70,80) )
[1]  30  70 110 150
collapseVector( c(3,4,10,2,100,3))
[1]   7  12 103

QUESTION 78 TOPICS: user defined functions (creating your own functions)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 78
# TOPICS:  user defined functions (creating your own functions)
#
# Define a function with the signature:  get2ndLowest = function( vec )
# that returns the 2nd lowest value in vec.
#
# EXAMPLES:
# > get2ndLowest(c(423,6,234,3,7,1))
# [1] 3
#
# > get2ndLowest(c(3,9,10,-23,-59,200))
# [1] -23
#
# > get2ndLowest( c("grapes", "apple", "plum", "banana", "pear") )
# [1] "banana" 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
get2ndLowest = function( x ){
  xWithoutLowestNumber = x[ x != min(x) ]
  answer = min(xWithoutLowestNumber) 
  answer
}

# Test the answer
get2ndLowest(c(423,6,234,3,7,1))    # 3 
[1] 3
get2ndLowest(c(3,9,10,-23,-59,200))        # -23
[1] -23
get2ndLowest( c("grapes", "apple", "plum", "banana", "pear") )  # "banana"
[1] "banana"

QUESTION 79 TOPICS: vectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 79
# TOPICS: vectors
#
# x and y are two vectors of the same length 
# combine x and y into a new vector named answer that contains 
# the odd position values from x and the even position values of y.
#
# EXAMPLES
#    > x = c(10,20,30,40)
#    > y = c(100, 200, 300, 400)
#    > # YOUR CODE GOES HERE (it can be a few lines if you like)
#    > answer
#   [1] 10 200 30 400
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x = c(10,20,30,40)

y = c(100, 200, 300, 400)

answer = x                                                     # ANSWER MULTIPLE LINES  - set answer to x

answer[ seq(2,length(y), by=2) ] = y [ seq(2,length(y), by=2)] # ANSWER MULTIPLE LINES - set even positions to the even positions from y

answer # show the result
[1]  10 200  30 400

QUESTION 80 TOPICS: lists, loops, user defined functions

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 80
# TOPICS: lists, loops, user defined functions
# SEE ALSO: question 72 for a similar question that doesn't use loops
# 
# Write a function with the following "signature":
#
# SIGNATURE:  getAllPairs = function (vec)
#
# ARGUMENTS/PARAMETERS 
#
#   vec - a vector that contains 2 or more values
#
# RETURNS: a list that contains a separate vector for each pair of values from vec. 
#          The values in the vectors should be in increasing order of value.
#
# EXAMPLE 1: 
#
#   > getAllPairs(c(200, 100, 300))
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# TODO

QUESTION 81 TOPICS: user defined functions, vectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 81   TOPICS: user defined functions, vectors
#
# Write a function with the following "signature":
#
# SIGNATURE:  getTopTwoValues = function (vec)
#
# ARGUMENTS/PARAMETERS 
#
#   vec - a vector that contains 2 or more values
#
# Returns a vector that contains the two largest
# values from the vector,vec . The values should 
# be returned IN INCREASING ORDER regardless
# of the order in the original vector. For example:
#     > getTopTwoValues(c(1,4,2,5,3))
#     4  5
#     > getTopTwoValues(c(1,5,2,4,3))
#     4  5
# If the largest value in vec appears more than once
# then the return value should include that value twice.
# For example:
#     > getTopTwoValues(c(1,4,2,4,3,4))
#     4  4
#
# Hints: You can write the function in many different ways. 
# Some functions that might help are: sort, head, tail 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# TODO