[This article was first published on Numbers around us - Medium, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)


Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.Puzzles no. 484–488

PuzzlesAuthor: ExcelBI

All files (xlsx with puzzle and R with solution) for each and every puzzle are available on my Github. Enjoy.

Puzzle #484I think we all know what Pythageorean Theorem is. Or at least I hope so… And today we need to use properties of right triangle to solve our challenge. We need to find groups of 3 numbers (called Pythagorean triplets), that are about two things. Sum of a squared and b squared are equal c squared, but also sum of a, b and c (means circumference) is equal to given number. There is possibility that given number has more then one triplet, and it is true for one case.

Loading libraries and data library(tidyverse)library(readxl)library(gmp)path = "Excel/484 Pythagorean Triplets for a Sum.xlsx"input = read\_xlsx(path, range = "A2:A10")test = read\_xlsx(path, range = "B2:D10") %>% mutate(across(everything(), as.numeric)) Transformation find\_pythagorean\_triplet <- function(P) { m\_max <- floor(sqrt(P / 2)) possible\_values <- expand\_grid(m = 2:m\_max, n = 1:(m\_max - 1)) %>% filter(m > n, m %% 2 != n %% 2, gcd(m, n) == 1) triplets <- possible\_values %>% pmap(function(m, n) { k <- P / (2 * m * (m + n)) if (k == floor(k)) { a <- k * (m^2 - n^2) b <- k * 2 * m * n c <- k * (m^2 + n^2) return(c(a, b, c)) } else { return(NULL) } }) triplet <- triplets %>% compact() %>% keep(~ sum(.x) == P) if (length(triplet) > 0) { result <- triplet[[1]] } else { result <- c(NA\_real\_, NA\_real\_, NA\_real\_) } tibble(a = result[1], b = result[2], c = result[3])}result = input %>% pmap\_dfr(~ find\_pythagorean\_triplet(..1)) Validation ```

in one case (for 132) I get another but correct result

``` Puzzle #485Sequences, world is full of them, and some have even their name and well researched properties. It is the case with Padovan sequence, which is similar to Fibonacci’s but has little bit bigger step and another initial elements. Check it out.
PS. To run it efficiently and save some time I also use memoise.

Loading libraries and data library(purrr)library(memoise)library(readxl)library(tidyverse)path = "Excel/485 Pandovan Sequence.xlsx"input = read\_excel(path, range = "A1:A10")test = read\_excel(path, range = "B1:B10") Transformation padovan <- function(n) { if (n <= 2) { return(1) } else { return(padovan\_memo(n - 2) + padovan\_memo(n - 3)) }}padovan\_memo = memoise(padovan)result = input %>% mutate(`Answer Expectecd` = map\_dbl(n, padovan\_memo)) Validation identical(result$`Answer Expectecd`, test$`Answer Expectecd`)# [1] TRUE Puzzle #486When we are measuring thing like ranges and distances, sometimes for better and more readable notes, we are shortening down sequences of consecutive numbers using hyphenated range notation. And that is our task today. Let’s get party started.

Loading libraries and data library(tidyverse)library(readxl)path = "Excel/486 Create Integer Intervals.xlsx"input = read\_excel(path, range = "A1:A8")test = read\_excel(path, range = "B1:B8") Transformation group\_consecutive = function(number\_string) { numbers <- str\_split(number\_string, ",") %>% unlist() %>% as.numeric() tibble(numbers = sort(numbers)) %>% mutate(group = cumsum(c(TRUE, diff(numbers) != 1))) %>% summarise(range = if\_else(n() > 1, paste0(min(numbers), "-", max(numbers)), as.character(numbers[1])), .by = group) %>% pull(range) %>% paste(collapse = ", ")}result = input %>% mutate(`Answer Expected` = map\_chr(Problem, group\_consecutive)) Validation identical(result$`Answer Expected`, test$`Answer Expected`)#> [1] TRUE Puzzle #487In this puzzle we need to find most frequent characters in given strings. It was like warm up before warm up. With no further words, lets do it.

Loading libraries and data library(tidyverse)library(readxl)path = "Excel/487 Maximum Frequency Characters.xlsx"input = read\_xlsx(path, range = "A2:A11")test = read\_xlsx(path, range = "B2:C11") Transformation find\_most\_freq\_char = function(input) { result = input %>% strsplit("") %>% unlist() %>% table() df = data.frame( Characters = names(result), Frequency = as.numeric(result)) %>% filter(Frequency == max(Frequency)) %>% summarise(Characters = paste(sort(Characters), collapse = ", "), .by = Frequency) %>% select(Characters, Frequency) return(df)}result = input$Strings %>% map(find\_most\_freq\_char) %>% bind\_rows() Validation: ```

Validation on eye, all correct. In three cases there is different sorting than# in provided example but with the same characters.

``` Puzzle #488Combinations, possible inputs to given output. I am sure we all love it. Today we are given bunch of numbers and one output. We need to find all combinations of this numbers that will sum up to given target. Code is quite long, but I think also easy to understand. Let me show it.

Loading libraries and data library(gtools)library(tidyverse)library(readxl)path = "Excel/488 Numbers to Meet Target Sum.xlsx"input = read\_excel(path, range = "A1:A10")target = read\_excel(path, range = "B1:B2") %>% pull()test = read\_excel(path, range = "C1:C5") Transformation find\_combinations <- function(numbers, target) { combs <- map(1:length(numbers), ~combinations(length(numbers), ., v = numbers)) valid\_combs <- combs %>% map(as\_tibble) %>% bind\_rows() %>% mutate(sum = rowSums(., na.rm = TRUE)) %>% filter(sum == target) return(valid\_combs)}combinations <- find\_combinations(input$Numbers, target) %>% unite("Combination", - sum, sep = ", ", remove = T, na.rm = T) sort\_numbers <- function(numbers) { paste(sort(as.numeric(strsplit(numbers, ",")[[1]])), collapse = ", ")}test <- test %>% mutate(Combination = map\_chr(`Answer Expected`, sort\_numbers)) Validation identical(sort(combinations$Combination), sort(test$Combination))# [1] TRUE Feel free to comment, share and contact me with advices, questions and your ideas how to improve anything. Contact me on Linkedin if you wish as well.
On my Github repo there are also solutions for the same puzzles in Python. Check it out!


R Solution for Excel Puzzles was originally published in Numbers around us on Medium, where people are continuing the conversation by highlighting and responding to this story.

To leave a comment for the author, please follow the link and comment on their blog: Numbers around us - Medium.


R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.


Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.Continue reading: R Solution for Excel Puzzles