मैं एक साथ हैक कर लिया है एक छोटे से पुनरावर्ती समारोह है कि होगा था जब आप इसे पास करते हैं तो कई वैक्टरों में लगातार तीन तिगुनाएं पाएं (कम से कम तीन पास करने की आवश्यकता है)। यह शायद थोड़ा कच्चा है, लेकिन काम करने लगता है।
फ़ंक्शन तर्क पारित करने के लिए, इलिप्सिस, ...
का उपयोग करता है। इसलिए यह आपके द्वारा प्रदान किए जाने वाले कई तर्क (यानी संख्यात्मक वैक्टर) ले जाएगा और उन्हें items
सूची में डाल देगा। फिर प्रत्येक उत्तीर्ण वेक्टर के बीच सबसे छोटा मूल्य इसके सूचकांक के साथ स्थित है।
फिर छोटे ट्रिपलेट से संबंधित वैक्टरों के indeces बनाए जाते हैं और for()
लूप का उपयोग करके पुनरावृत्त होते हैं, जहां आउटपुट मान आउटपुट वेक्टर out
पर पास किए जाते हैं।items
में इनपुट वैक्टर काटा जाता है और एक पुनरावर्ती फैशन में फ़ंक्शन में फिर से पारित किया जाता है। केवल, जब सभी वैक्टर NA
हैं, यानी वेक्टर में कोई और मूल्य नहीं हैं, फ़ंक्शन अंतिम परिणाम देता है।
library(magrittr)
# define function to find the triplets
tripl <- function(...){
items <- list(...)
# find the smallest number in each passed vector, along with its index
# output is a matrix of n-by-2, where n is the number of passed arguments
triplet.id <- lapply(items, function(x){
if(is.na(x) %>% prod) id <- c(NA, NA)
else id <- c(which(x == min(x)), x[which(x == min(x))])
}) %>% unlist %>% matrix(., ncol=2, byrow=T)
# find the smallest triplet from the passed vectors
index <- order(triplet.id[,2])[1:3]
# create empty vector for output
out <- vector()
# go through the smallest triplet's indices
for(i in index){
# .. append the coresponding item from the input vector to the out vector
# .. and remove the value from the input vector
if(length(items[[i]]) == 1) {
out <- append(out, items[[i]])
# .. if the input vector has no value left fill with NA
items[[i]] <- NA
}
else {
out <- append(out, items[[i]][triplet.id[i,1]])
items[[i]] <- items[[i]][-triplet.id[i,1]]
}
}
# recurse until all vectors are empty (NA)
if(!prod(unlist(is.na(items)))) out <- append(list(out),
do.call("tripl", c(items), quote = F))
else(out <- list(out))
# return result
return(out)
}
फ़ंक्शन को इनपुट वैक्टर को तर्क के रूप में पास करके बुलाया जा सकता है।
# input vectors
a = c(3,5)
b = c(6,1,8,7)
c = c(4,2,9)
# find all the triplets using our function
y <- tripl(a,b,c)
परिणाम एक सूची है, जिसमें सभी निरंतर जानकारी शामिल है, यद्यपि अनियंत्रित।
print(y)
# [[1]]
# [1] 1 2 3
#
# [[2]]
# [1] 4 5 6
#
# [[3]]
# [1] 7 9 NA
#
# [[4]]
# [1] 8 NA NA
आदेश सब कुछ sapply()
का उपयोग किया जा सकता है:
# put everything in order
sapply(y, function(x){x[order(x)]}) %>% t
# [,1] [,2] [,3]
# [1,] 1 2 3
# [2,] 4 5 6
# [3,] 7 9 NA
# [4,] 8 NA NA
बात है, कि यह तीनो को खोजने के लिए वेक्टर प्रति केवल एक मान का उपयोग करेगा। इसलिए यह लगातार तीन तिहाई c(6,7,8)
नहीं मिलेगा c(6,7,11)
, c(8,9,13)
और c(10,12,14)
। इस उदाहरण में यह c(6,8,10)
लौटाएगा (नीचे देखें)।
a<-c(6,7,11)
b<-c(8,9,13)
c<-c(10,12,14)
y <- tripl(a,b,c)
sapply(y, function(x){x[order(x)]}) %>% t
# [,1] [,2] [,3]
# [1,] 6 8 10
# [2,] 7 9 12
# [3,] 11 13 14
क्या प्रत्येक तत्व को एक तिहाई में अलग-अलग सरणी से आना पड़ता है? क्या '{2,3,4}' वैध ट्रिपल माना जाएगा? – Psidom
हां! , {2,3,4}, {6,7,8} या {7,8,9} मान्य नहीं है। –