Largest int from concatenated ints: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: simplified, added syntax colouring, marked p2js compatible) |
(R language) |
||
Line 1,941: | Line 1,941: | ||
<pre>998764543431 |
<pre>998764543431 |
||
6054854654</pre> |
6054854654</pre> |
||
=={{header|R}}== |
|||
<lang R>Largest_int_from_concat_ints <- function(vec){ |
|||
#recursive function for computing all permutations |
|||
perm <- function(vec) { |
|||
n <- length(vec) |
|||
if (n == 1) |
|||
return(vec) |
|||
else { |
|||
x <- NULL |
|||
for (i in 1:n){ |
|||
x <- rbind(x, cbind(vec[i], perm(vec[-i]))) |
|||
} |
|||
return(x) |
|||
} |
|||
} |
|||
permutations <- perm(vec) |
|||
concat <- as.numeric(apply(permutations, 1, paste, collapse = "")) |
|||
return(max(concat)) |
|||
} |
|||
#Verify |
|||
Largest_int_from_concat_ints(c(54, 546, 548, 60)) |
|||
Largest_int_from_concat_ints(c(1, 34, 3, 98, 9, 76, 45, 4)) |
|||
Largest_int_from_concat_ints(c(93, 4, 89, 21, 73)) |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
[1] 6054854654 |
|||
[1] 998764543431 |
|||
[1] 938973421 |
|||
</pre> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |