knitr::opts_chunk$set(echo = TRUE)
# 1. Write a function order.by.string that takes as an input a string list
# (or vector) v and a string s and returns v reordered and as a vector. 
# The ordering matches the order the string first appear in s. Strings in v that
# do not appears in s are to be ommitted.


 vlist<-list("simple","hard","men","women","take","make","is","not")
 vvec<-c("simple","hard","men","women","take","make","is","not")
 s <- "It is strange that only extraordinary men make the discoveries, which later appear so easy and simple. - Georg C. Lichtenberg"

  order.by.string <- function(v,s){
   output <- v
   split <- str_split(s,pattern = " ",simplify = T)
   
   for(i in 1:length(split)){
     
     output <- match(v,split)   

   }
  output <- output[!is.na(output)]

 output <-output[order(output)]
 output
   
  }
  
order.by.string(vvec,s)
## [1] 2 7 8
order.by.string(vlist,s)
## [1] 2 7 8
# or

  order.by.string <- function(v,s){
     output <- v
     split <- str_split(s,pattern = " ",simplify = T)
       for(i in 1:length(split)){
             output <- which(split %in% v)    
       }
          output
   }
  
   order.by.string(vvec,s)
## [1] 2 7 8
   order.by.string(vlist,s)
## [1] 2 7 8