library(tidyverse)
library(readxl)
<- "Excel/632 Create Triangle from Words.xlsx"
path <- read_excel(path, range = "B2:D3", col_names = FALSE) %>% as.matrix()
test1 <- read_excel(path, range = "B5:F7", col_names = FALSE) %>% as.matrix()
test2 <- read_excel(path, range = "B9:F11", col_names = FALSE) %>% as.matrix()
test3 <- read_excel(path, range = "B13:H16", col_names = FALSE) %>% as.matrix()
test4 <- read_excel(path, range = "B18:J23", col_names = FALSE) %>% as.matrix()
test5
<- function(n) {
triangular_numbers * (n + 1) / 2
n
}
<- function(word) {
draw_triangle_from_word <- 1
n while (triangular_numbers(n) < nchar(word)) {
<- n + 1
n
}
<- paste0(word, strrep("#", triangular_numbers(n) - nchar(word)))
padded_word <- strsplit(padded_word, "")[[1]]
word_chars <- split(word_chars, rep(1:n, 1:n))
word_split
<- map(word_split, ~str_pad(paste0(.x, collapse = " "), n * 2 - 1, side = "both")) %>%
formatted_lines map(~strsplit(.x, "")) %>%
unlist() %>%
matrix(nrow = n, ncol = n * 2 - 1, byrow = TRUE) %>%
replace(., . == " ", NA) %>%
as.data.frame() %>%
filter(!if_all(everything(), is.na)) %>%
as.matrix()
formatted_lines
}
= c("thu", "moon", "excel", "skyjacking", "embezzlements")
words
all.equal(draw_triangle_from_word(words[1]), test1, check.attributes = FALSE) # TRUE
all.equal(draw_triangle_from_word(words[2]), test2, check.attributes = FALSE) # TRUE
all.equal(draw_triangle_from_word(words[3]), test3, check.attributes = FALSE) # TRUE
all.equal(draw_triangle_from_word(words[4]), test4, check.attributes = FALSE) # TRUE
all.equal(draw_triangle_from_word(words[5]), test5, check.attributes = FALSE) # TRUE
Excel BI - Excel Challenge 632
Challenge Description
Give a formula to create triangle from alphabets in a word. First row would have first alphabet, second row would have two alphabets and so on. To ensure symmetry, if you run out of alphabets, then use # for padding.
🔗 Link to Excel file: 👉https://lnkd.in/dwitwkm3
Solutions
Logic:
triangular_numbers
: Computes the required length for the triangle.strrep("#", ...)
: Pads the word with#
to meet the triangle’s requirements.strsplit
andsplit
: Divides characters into rows based on triangular numbers.str_pad
: Centers each row for symmetry.
Strengths:
Compact Logic: The use of tidyverse functions ensures clarity and conciseness.
Symmetry Handling: Padding and alignment achieve perfect symmetry.
Areas for Improvement:
- The triangle formatting could directly handle NA or empty spaces instead of filtering them later.
Gem:
- The use of
triangular_numbers
to dynamically calculate the required triangle size is a standout.
- The use of
import pandas as pd
import numpy as np
= "632 Create Triangle from Words.xlsx"
path = pd.read_excel(path, usecols="B:D", skiprows=1, nrows=2, header=None).fillna(' ').values
test1 = pd.read_excel(path, usecols="B:F", skiprows=4, nrows=3, header=None).fillna(' ').values
test2 = pd.read_excel(path, usecols="B:F", skiprows=8, nrows=3, header=None).fillna(' ').values
test3 = pd.read_excel(path, usecols="B:H", skiprows=12, nrows=4, header=None).fillna(' ').values
test4 = pd.read_excel(path, usecols="B:J", skiprows=17, nrows=6, header=None).fillna(' ').values
test5
def triangular_numbers(n):
return n * (n + 1) // 2
def draw_triangle_from_word(word):
= 1
n while triangular_numbers(n) < len(word):
+= 1
n
= word + "#" * (triangular_numbers(n) - len(word))
padded_word = list(padded_word)
word_chars = [word_chars[triangular_numbers(i-1):triangular_numbers(i)] for i in range(1, n+1)]
word_split
= []
formatted_lines for line in word_split:
= ' '.join(line).center(n * 2 - 1)
formatted_line list(formatted_line))
formatted_lines.append(
= np.array(formatted_lines, dtype=object)
formatted_matrix
return formatted_matrix
= ["thu", "moon", "excel", "skyjacking", "embezzlements"]
words
print((draw_triangle_from_word(words[0]) == test1).all())
print((draw_triangle_from_word(words[1]) == test2).all())
print((draw_triangle_from_word(words[2]) == test3).all())
print((draw_triangle_from_word(words[3]) == test4).all())
print((draw_triangle_from_word(words[4]) == test5).all())
Logic:
triangular_numbers
: Calculates the size of the triangle.Padding with
#
: Ensures the triangle is complete.List slicing: Splits characters into rows dynamically.
center
: Ensures rows are symmetric.
Strengths:
Modularity: Functions are well-structured for clarity and reuse.
Symmetry Handling: Padding and centering achieve proper formatting.
Areas for Improvement:
- Iterating over rows for formatting could be optimized using numpy.
Gem:
- The combination of list slicing and centering ensures both correctness and clarity.
Difficulty Level
This task is moderate:
Requires understanding of triangular numbers.
Involves string manipulation, dynamic slicing, and formatting for symmetry.