40  38. 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
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
######################################.
# Setup some sample data
######################################.
df = data.frame(
  fruit = 
    c("apple","apricot","avocado","banana","bell pepper",
      "bilberry","blackberry","blackcurrant","blood orange",
      "blueberry","boysenberry","breadfruit","canary melon",
      "cantaloupe","cherimoya","cherry","chili pepper",
      "clementine","cloudberry","coconut","cranberry","cucumber","currant",
      "damson","date","dragonfruit","durian","eggplant","elderberry",
      "feijoa","fig","goji berry","gooseberry","grape","grapefruit",
      "guava","honeydew","huckleberry","jackfruit","kiwi fruit"
      ),
  price = round(runif(n=40, min=0.99, max=6.99), 2)
)

# show the full dataframe
df
          fruit price
1         apple  6.32
2       apricot  6.04
3       avocado  1.03
4        banana  2.60
5   bell pepper  3.21
6      bilberry  5.24
7    blackberry  6.66
8  blackcurrant  1.13
9  blood orange  4.85
10    blueberry  5.97
11  boysenberry  5.30
12   breadfruit  1.00
13 canary melon  1.02
14   cantaloupe  1.98
15    cherimoya  3.03
16       cherry  5.38
17 chili pepper  1.81
18   clementine  2.73
19   cloudberry  6.16
20      coconut  4.98
21    cranberry  6.68
22     cucumber  1.30
23      currant  1.53
24       damson  3.06
25         date  2.28
26  dragonfruit  5.94
27       durian  2.23
28     eggplant  2.57
29   elderberry  2.64
30       feijoa  5.01
31          fig  0.99
32   goji berry  4.47
33   gooseberry  6.98
34        grape  1.04
35   grapefruit  5.76
36        guava  2.05
37     honeydew  1.91
38  huckleberry  1.53
39    jackfruit  3.77
40   kiwi fruit  2.81
# Show just the las 5% of the rows

###############.
# BEST ANSWER
###############.
tail(df, .05 * nrow(df))    # BEST ANSWER
        fruit price
39  jackfruit  3.77
40 kiwi fruit  2.81
##############################################################.
# Another answer - don't forget the last comma (see below)
##############################################################.
rowNumbers = 1:nrow(df)
df[ rowNumbers > .95*nrow(df) ,  ]
        fruit price
39  jackfruit  3.77
40 kiwi fruit  2.81

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 different values
#         (you may assume that all values are different from each other)
#
# RETURNS: 
#   A list that contains a separate vector for each pair of values
#   from vec. The list should include separate entries for each order of
#   the pair of values (see below). Do not insert entries in the 
#   list for which both values are the same.
#
# EXAMPLE 1: 
#
#   > getAllPairs(c("apple", "orange"))
#   [[1]]
#   [1] "apple"  "orange"
#   
#   [[2]]
#   [1] "orange" "apple" 
#
# EXAMPLE 2: 
#   > getAllPairs(c("apple", "orange", "pear"))
#   [[1]]
#   [1] "apple"  "orange"
#   
#   [[2]]
#   [1] "apple" "pear" 
#  
#   [[3]]
#   [1] "orange" "apple" 
#   
#   [[4]]
#   [1] "orange" "pear"  
#   
#   [[5]]
#   [1] "pear"  "apple"
#   
#   [[6]]
#   [1] "pear"   "orange"#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
##########################.
# ANSWER - with for loops
##########################.

getAllPairs = function( vec ){
  answerList = list()

  for (pos1 in 1:(length(vec) ) ){
    value1 = vec[pos1]
    for (pos2 in 1:length(vec) ){
      value2 = vec[pos2]
      if(value1 != value2){
        twoValues = c(value1, value2)
        answerList = c(answerList , list(twoValues) )    
      }
    }
  }
  
  answerList
}

#-----------.
# Example 1
#-----------.
getAllPairs(c("apple", "orange"))
[[1]]
[1] "apple"  "orange"

[[2]]
[1] "orange" "apple" 
#-----------.
# Example 2
#-----------.
getAllPairs(c("apple", "orange", "pear"))
[[1]]
[1] "apple"  "orange"

[[2]]
[1] "apple" "pear" 

[[3]]
[1] "orange" "apple" 

[[4]]
[1] "orange" "pear"  

[[5]]
[1] "pear"  "apple"

[[6]]
[1] "pear"   "orange"
##########################.
# ANSWER - with while loops
##########################.

getAllPairs = function( vec ){
  answerList = list()

  pos1 = 1
  while (pos1 <= length(vec) ){
    value1 = vec[pos1]
    
    pos2 = 1
    while (pos2 <= length(vec) ){
      value2 = vec[pos2]
      
      if(value1 != value2){
        twoValues = c(value1, value2)
        answerList = c(answerList , list(twoValues) )    
      }
      
      pos2 = pos2 + 1
    }
    
    pos1 = pos1 + 1
  }
  
  answerList
}

#-----------.
# Example 1
#-----------.
getAllPairs(c("apple", "orange"))
[[1]]
[1] "apple"  "orange"

[[2]]
[1] "orange" "apple" 
#-----------.
# Example 2
#-----------.
getAllPairs(c("apple", "orange", "pear"))
[[1]]
[1] "apple"  "orange"

[[2]]
[1] "apple" "pear" 

[[3]]
[1] "orange" "apple" 

[[4]]
[1] "orange" "pear"  

[[5]]
[1] "pear"  "apple"

[[6]]
[1] "pear"   "orange"

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 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
##############.
# ONE ANSWER
##############.
getTopTwoValues = function( vec ) {
  tail( sort(vec), 2)
}  
    

# Try it
getTopTwoValues(c(1,4,2,5,3))
[1] 4 5
getTopTwoValues(c(1,5,2,4,3))
[1] 4 5
getTopTwoValues(c(1,4,2,4,3,4))
[1] 4 4
##################.
# ANOTHER ANSWER
##################.
getTopTwoValues = function( vec ) {
  positions = (length(vec) - 1):length(vec)
  sort(vec) [ positions ]
}
# Try it
getTopTwoValues(c(1,4,2,5,3))
[1] 4 5
getTopTwoValues(c(1,5,2,4,3))
[1] 4 5
getTopTwoValues(c(1,4,2,4,3,4))
[1] 4 4