Find the smallest Palindromes (in length) which can be formed by adding English alphabets before string. if a string is already palindromic, then nothing needs to be added. Ex. abc – cbabc
📌 Challenge Details and Links
ExcelBI Excel Challenge Number: 391
Challenge Difficulty: ⭐️⭐️⭐️
📥Download Sample File
📥Link to the solutions on LinkedIn
Solving the challenge of Form Shortest Palindromic Prefix with Power Query
Power Query solution 1 for Form Shortest Palindromic Prefix, proposed by John V.:
let
S = Excel.CurrentWorkbook(){0}[Content],
T = Text.Reverse,
R = Table.AddColumn(S, "R", each
let
r = (t, i) => let w = Text.Start(T(t), i) & t in if w = T(w) then w else @r(t, 1 + i)
in
r([String], 0)
)[[R]]
in
R
Blessings!
Power Query solution 2 for Form Shortest Palindromic Prefix, proposed by Zoran Milokanović:
let
Source = Excel.CurrentWorkbook(){[Name = "Input"]}[Content],
F = (t, s) =>
let
R = Text.Reverse,
T = R(Text.End(t, s)) & t
in
if T = R(T) then T else @F(t, s + 1),
S = Table.TransformRows(Source, each F([String], 0))
in
S
Power Query solution 3 for Form Shortest Palindromic Prefix, proposed by Alejandro Simón 🇵🇦 🇪🇸:
let
Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
Sol = Table.AddColumn(
Source,
"Answer",
each
let
a = [String],
b = Text.Reverse(a),
c = (z, x) =>
let
A = b & Text.Range(a, Text.Length(a) - x, x)
in
if A = Text.Reverse(A) then A else @c(A, x + 1),
d = c([String], 0)
in
d
)[[Answer]]
in
Sol
Power Query solution 4 for Form Shortest Palindromic Prefix, proposed by Alejandro Simón 🇵🇦 🇪🇸:
let
Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
Sol = Table.AddColumn(
Source,
"Answer",
each
let
a = [String],
b = Text.Reverse([String]),
c = List.Transform({0 .. Text.Length(a) - 1}, each b & Text.Range(a, _, Text.Length(a) - _)),
d = if a = b then a else List.Last(List.Select(c, each _ = Text.Reverse(_)))
in
d
)
in
Sol
Power Query solution 5 for Form Shortest Palindromic Prefix, proposed by Luan Rodrigues:
let
Fonte = Tabela1,
res = Table.AddColumn(
Fonte,
"Personalizar",
each [
a = List.TransformMany(
{0 .. Text.Length([String])},
(x) => {Text.Middle([String], 0, x) & [String]}
& {Text.Middle(Text.Reverse([String]), 0, x) & [String]},
(x, y) => y
),
b =
if [String] = Text.Reverse([String]) then
[String]
else
List.First(List.Select(a, each _ = Text.Reverse(_)))
][b]
)
in
res
Power Query solution 6 for Form Shortest Palindromic Prefix, proposed by Glyn Willis:
let
Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(
Source,
{{"String", type text}, {"Answer Expected", type text}}
),
#"Added Custom" = Table.AddColumn(
#"Changed Type",
"Custom",
each [
t = [String],
p = Text.Length(t),
lg = List.Last(
List.Generate(
() => [
i = 0,
gp = Text.Reverse(Text.End(t, i)),
nt = gp & t,
h = Number.RoundUp(Text.Length(nt) / 2),
pa = (Text.Start(nt, h)) = (Text.Reverse(Text.End(nt, h))),
prvpa = false
],
each ([i] <= p) and ([prvpa] = false),
each [
i = [i] + 1,
gp = Text.Reverse(Text.End(t, i)),
nt = gp & t,
h = Number.RoundUp(Text.Length(nt) / 2),
pa = (Text.Start(nt, h)) = (Text.Reverse(Text.End(nt, h))),
prvpa = [pa]
],
each [nt]
)
)
][lg]
)
in
#"Added Custom"
Solving the challenge of Form Shortest Palindromic Prefix with Excel
Excel solution 1 for Form Shortest Palindromic Prefix, proposed by Bo Rydobon 🇹🇭:
=MAP(
A2:A10,
LAMBDA(
a,
LET(
s,
SEQUENCE(
LEN(
a
)
),
r,
CONCAT(
MID(
a,
-SORT(
-s
),
1
)
),
c,
LEFT(
r,
s-1
)&a,
h,
LEN(
c
)/2,
@TOCOL(
IFS(
LEFT(
c,
h
)=LEFT(
r,
h
),
c
),
3
)
)
)
)
Excel solution 2 for Form Shortest Palindromic Prefix, proposed by John V.:
=LET(p,LAMBDA(t,CONCAT(MID(t,21-ROW(1:20),1))),r,LAMBDA(r,t,i,LET(w,LEFT(p(t),i)&t,IF(w=p(w),w,r(r,t,1+i)))),MAP(A2:A10,LAMBDA(x,r(r,x,))))
Excel solution 3 for Form Shortest Palindromic Prefix, proposed by محمد حلمي:
=MAP(
A2:A10,
LAMBDA(
a,
LET(
s,
SEQUENCE(
20
),
v,
LAMBDA(
a,
CONCAT(
MID(
a,
21-s,
1
)
)
),
i,
MAP(
RIGHT(
a,
s-1
),
v
)&a,
XLOOKUP(
TRUE,
i=MAP(
i,
v
),
i
)
)
)
)
Excel solution 4 for Form Shortest Palindromic Prefix, proposed by Kris Jaganah:
=MAP(
A2:A10,
LAMBDA(
y,
LET(
a,
VSTACK(
y,
SCAN(
,
MID(
y,
SEQUENCE(
LEN(
y
),
,
LEN(
y
),
-1
),
1
),
CONCAT
)&y
),
TAKE(
FILTER(
a,
MAP(
a,
LAMBDA(
x,
CONCAT(
MID(
x,
SEQUENCE(
LEN(
x
),
,
LEN(
x
),
-1
),
1
)
)
)
)=a
),
1
)
)
)
)
Excel solution 5 for Form Shortest Palindromic Prefix, proposed by Timothée BLIOT:
=MAP(
A2:A10,
LAMBDA(
z,
LET(
A,
LEN(
z
),
B,
SEQUENCE(
A
),
C,
CONCAT(
MAP(
B-1,
LAMBDA(
y,
CONCAT(
MAP(
SEQUENCE(
A-y
),
LAMBDA(
x,
CONCAT(
MID(
z,
SEQUENCE(
x,
,
A-y,
-1
),
1
)
)&z
)
)&"|"
)
)
)
),
D,
VSTACK(
z,
TEXTSPLIT(
C,
,
"|",
1
)
),
E,
UNIQUE(
FILTER(
D,
MAP(
D,
LAMBDA(
x,
CONCAT(
MID(
x,
LEN(
x
)-SEQUENCE(
LEN(
x
)
)+1,
1
)
)=x
)
)
)
),
FILTER(
E,
LEN(
E
)=MIN(
LEN(
E
)
)
)
)
)
)
Excel solution 6 for Form Shortest Palindromic Prefix, proposed by Hussein SATOUR:
=MAP(
A2:A10,
LAMBDA(
y,
LET(
a,
LEN(
y
),
b,
VSTACK(
"",
MID(
y,
SEQUENCE(
a-1,
,
a,
-1
),
1
)
),
c,
SCAN(
"",
b,
CONCAT
)&y,
XLOOKUP(
1,
MAP(
c,
LAMBDA(
x,
CONCAT(
MID(
x,
SEQUENCE(
LEN(
x
),
,
LEN(
x
),
-1
),
1
)
)=x
)
)*1,
c
)
)
)
)
Excel solution 7 for Form Shortest Palindromic Prefix, proposed by 🇵🇪 Ned Navarrete C.:
=p)),
IF(
n=1,
f,
CONCAT(
MID(
RIGHT(
f,
n-1
),
s,
1
)
)&f
))))
Excel solution 8 for Form Shortest Palindromic Prefix, proposed by Charles Roldan:
=LET(_S,
LAMBDA(
x,
REPLACE(
x,
LEN(
x
),
1,
)
),
_R,
LAMBDA(f,
LAMBDA(x,
IF(LEN(
x
),
RIGHT(
x
) & f(
f
)(_S(
x
)),
))),
_P,
LAMBDA(f,
LAMBDA(x,
[y],
IF(x = _R(
_R
)(
x
),
_R(
_R
)(y) & x & y,
f(
f
)(_S(
x
),
RIGHT(
x
) & y)))),
_map,
LAMBDA(
f,
LAMBDA(
x,
MAP(
x,
LAMBDA(
x,
f(
x
)
)
)
)
),
_map(
_P(
_P
)
))(A2:A10)
Excel solution 9 for Form Shortest Palindromic Prefix, proposed by JvdV -:
=LET(
r,
A2:A10,
s,
SEQUENCE(
,
98
),
REDUCE(
"",
s,
LAMBDA(
x,
y,
IF(
BYROW(
MID(
x&r,
99-s,
1
),
CONCAT
)=x&r,
x,
x&MID(
r,
99-y,
1
)
)
)
)&r
)
Excel solution 10 for Form Shortest Palindromic Prefix, proposed by Pieter de Bruijn:
=MAP(
A2:A10,
LAMBDA(
a,
REDUCE(
a,
SEQUENCE(
LEN(
a
)
),
LAMBDA(
x,
y,
IF(
CONCAT(
MID(
x,
SEQUENCE(
LEN(
x
),
,
LEN(
x
),
-1
),
1
)
)=x,
x,
CONCAT(
MID(
RIGHT(
a,
y
),
SEQUENCE(
y,
,
y+1,
-1
),
1
),
& a
)
)
)
)
)
)
Excel solution 11 for Form Shortest Palindromic Prefix, proposed by Giorgi Goderdzishvili:
= pd.read_excel(
'2024 Problems.xlsx',
sheet_name ='Ex-391'
)
def sml_pal(
string
):
for i in range(
len(
string
),
0,
-1
):
wrd = string[:i-1:-1]+string
if wrd==wrd[::-1]:
return wrd
df["solution"] = df["String"].apply(
sml_pal
)
Solving the challenge of Form Shortest Palindromic Prefix with Python in Excel
Python in Excel solution 1 for Form Shortest Palindromic Prefix, proposed by John V.:
Hi everyone!
One [Python] option could be:
def p(t):
for i in range(len(t)):
w = t[::-1][:i] + t
if w == w[::-1]:
return w
[p(t) for t in xl("A2:A10")[0]]
or...
[p for t in xl("A2:A10")[0] if (p := next((w for i in range(len(t)) if (w := t[::-1][:i] + t) == w[::-1]), None))]
Blessings!
Python in Excel solution 2 for Form Shortest Palindromic Prefix, proposed by Abdallah Ally:
import pandas as pd
# Read an Excel file
df = pd.read_excel(file_path)
def make_palindrome(col):
value = col
if value == value[::-1]:
return value
for i in range(-1, -len(col), -1):
test = col[i:][::-1] + value
if test == test[::-1]:
value = test
break
return value
df['My Answer'] = df['String'].apply(make_palindrome)
print(df)
# https://github.com/mathematiciantz/Excel_BI_Challenges/blob/main/Excel_Challenge_391-%20Palindrome%20After%20Adding%20in%20the%20Beginning.py
Solving the challenge of Form Shortest Palindromic Prefix with R
R solution 1 for Form Shortest Palindromic Prefix, proposed by Konrad Gryczan, PhD:
library(tidyverse)
library(readxl)
library(stringi)
is_palindrome = function(x) {
x = tolower(x)
x == stri_reverse(x)
}
palindromize = function(string) {
if (is_palindrome(string)) {
return(string)
}
string_rev = stri_reverse(string)
prefixes = map(1:nchar(string), function(i) {
substr(string_rev, 1, i)
})
candidates = map(prefixes, function(prefix) {
paste0(prefix, string)
})
palindromes = data.frame(candidate = unlist(candidates)) %>%
mutate( is_palindrome = map_lgl(candidate, is_palindrome)) %>%
filter(is_palindrome) %>%
select(candidate) %>%
arrange(nchar(candidate)) %>%
slice(1) %>%
pull()
return(palindromes)
}
result = input %>%
mutate(palindromized = map_chr(String, palindromize)) %>%
cbind(test) %>%
mutate(check = palindromized == `Answer Expected`)
Solving the challenge of Form Shortest Palindromic Prefix with DAX
DAX solution 1 for Form Shortest Palindromic Prefix, proposed by Zoran Milokanović:
EVALUATE
ADDCOLUMNS(Input, "Answer Expected",
VAR S = Input[String]
VAR G = ADDCOLUMNS(SELECTCOLUMNS(ADDCOLUMNS(GENERATESERIES(0, LEN(S) - 1), "C", RIGHT(S, [Value])), [C]), "R",
VAR L = LEN([C])
RETURN CONCATENATEX(GENERATESERIES(1, L), MID([C], L + 1 - [Value], 1), "") & S, "M", LEN([C])
)
RETURN
{SELECTCOLUMNS(TOPN(1, FILTER(G,
VAR L = LEN([R])
RETURN CONCATENATEX(GENERATESERIES(1, L), MID([R], L + 1 - [Value], 1), "") = [R]), [M], ASC), [R])}
)
&&
