We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The R option has no pre-populated code, so here's what it would look like if it were analogous to the setup for python:
Player<-setRefClass("Player",fields=list(name="character",score="numeric"),methods=list(initialize=function(name,score){name<<-namescore<<-score},show=function(){cat(name,score,"\n")},comparator=function(other){# Your solution here}))quicksort<-function(players){if(length(players)<=1){return(players)}pivot<-players[[1]]less<-list()greater<-list()equal<-list(pivot)for(iin2:length(players)){cmp<-players[[i]]$comparator(pivot)if(cmp<0){less<-append(less,list(players[[i]]))}elseif(cmp>0){greater<-append(greater,list(players[[i]]))}else{equal<-append(equal,list(players[[i]]))}}return(c(quicksort(less),equal,quicksort(greater)))}if(!interactive()){stdin<-file("stdin")open(stdin)input_lines<-readLines(stdin,warn=FALSE)close(stdin)n<-as.integer(trimws(input_lines[1],which="both"))input_lines<-input_lines[-1]player_list<-vector("list",n)for(iin1:n){input<-strsplit(trimws(input_lines[i],which="both")," ")[[1]]name<-input[1]score<-as.numeric(input[2])player<-Player$new(name,score)player_list[[i]]<-player}sorted_data<-quicksort(player_list)for(playerinsorted_data){player$show()}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sorting: Comparator
You are viewing a single comment's thread. Return to all comments →
The R option has no pre-populated code, so here's what it would look like if it were analogous to the setup for python: