List the numbers where following result is an even number Result = Sum of Digits at Odd positions – Sum of Digits at Even positions Positions start at 1 not at 0. Example = 70563 Result = (7+5+3) – (0+6) = 9
📌 Challenge Details and Links
ExcelBI Excel Challenge Number: 224
Challenge Difficulty: ⭐️
📥Download Sample File
📥Link to the solutions on LinkedIn
Solving the challenge of Even Result by Position Sum with Power Query
Power Query solution 1 for Even Result by Position Sum, proposed by Bo Rydobon 🇹🇭:
let
Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
Ans = Table.SelectRows(
Source,
each
let
l = List.Transform(
List.Zip(List.Split(List.Transform(Text.ToList([Numbers]), Number.From), 2)),
List.Sum
)
in
Number.IsEven(l{0} - l{1})
)
in
Ans
Power Query solution 2 for Even Result by Position Sum, proposed by Zoran Milokanović:
let
Source = Excel.CurrentWorkbook(){[Name = "Input"]}[Content],
S = Table.SelectRows(
Source,
each Number.IsEven(
List.Sum(
List.Transform(
List.Split(List.Transform(Text.ToList([Numbers]), Number.From), 2),
each _{0} - (_{1}? ?? 0)
)
)
)
)[Numbers]
in
S
Power Query solution 3 for Even Result by Position Sum, proposed by Alejandro Simón 🇵🇦 🇪🇸:
let
Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
Sol = Table.SelectRows(
Table.AddColumn(
Source,
"Custom",
each
let
a = List.Transform(Text.ToList([Numbers]), Number.From),
b = Number.Abs(
List.Sum(List.Select(a, Number.IsOdd)) - List.Sum(List.Select(a, Number.IsEven))
)
in
b
),
each Number.IsEven([Custom])
)[[Numbers]]
in
Sol
Power Query solution 4 for Even Result by Position Sum, proposed by Luan Rodrigues:
let
Fonte = Tabela1,
res = Table.SelectRows(
Table.AddColumn(
Fonte,
"Personalizar",
each [
a = Text.ToList([Numbers]),
b = List.Select(
List.Zip({a, List.Transform(a, each Number.IsOdd(Number.From(_)))}),
each _{1} = true
),
c = List.Select(
List.Zip({a, List.Transform(a, each Number.IsEven(Number.From(_)))}),
each _{1} = true
),
d = List.Sum(List.Transform(b, (x) => Number.From(x{0}))),
e = List.Sum(List.Transform(c, (x) => Number.From(x{0}))),
f = if d - e < 0 then (d - e) * - 1 else d - e
][f]
),
each Number.IsEven([Personalizar]) = true
)[[Numbers]]
in
res
Solving the challenge of Even Result by Position Sum with Excel
Excel solution 1 for Even Result by Position Sum, proposed by Bo Rydobon 🇹🇭:
=FILTER(A2:A10,MAP(A2:A10,LAMBDA(a,ISEVEN(SUM(WRAPROWS(MID(a,SEQUENCE(LEN(a)),1),2,0)*{1,-1})))))
Excel solution 2 for Even Result by Position Sum, proposed by Rick Rothstein:
=FILTER(A2:A10,MAP(A2:A10,LAMBDA(x,ISEVEN(SUM({-1,1}*(0&MID(x,2*SEQUENCE(99)-{0,1},1)))))))
Excel solution 3 for Even Result by Position Sum, proposed by Rick Rothstein:
=FILTER(A2:A10,MAP(A2:A10,LAMBDA(a,ISEVEN(SUM(-(0&MID(a,ROW(1:99),1)))))))
It was posted by محمد حلمي in a reply to one of my replies and I thought it too good for it to be left hidden in a chain of replies where almost no one would see it, so I decided to post it for him as a main reply. Yes, all he did was sum the digits and check if that sum was even or not. I posted a reason why this works in a different reply elsewhere in another sub-reply thread but will post it again as a direct reply to this message so it can easily be seen (this message would be too long and exceed LinkedIn's limits for Replies if I tried to included it here)
Excel solution 4 for Even Result by Position Sum, proposed by Rick Rothstein:
=FILTER(A2:A10,MAP(A2:A10,LAMBDA(x,MOD(SUM({-1,1}*(0&MID(x,2*SEQUENCE(LEN(x))-{0,1},1))),2)=0)))
or one character shorter...
=FILTER(A2:A10,MAP(A2:A10,LAMBDA(x,ISEVEN(SUM({-1,1}*(0&MID(x,2*SEQUENCE(LEN(x))-{0,1},1)))))))
Excel solution 5 for Even Result by Position Sum, proposed by John V.:
=FILTER(A2:A10,MAP(A2:A10,LAMBDA(x,ISEVEN(SUM((0&MID(x,2*ROW(1:15)-{1,0},1))*{1,-1})))))
Excel solution 6 for Even Result by Position Sum, proposed by محمد حلمي:
=LET(a,A2:A10,e,SEQUENCE(,99,,2),FILTER(a,ISEVEN(MMULT((0&MID(a,e,1))-(0&MID(a,e+1,1)),TOCOL(e)^0))))
Excel solution 7 for Even Result by Position Sum, proposed by محمد حلمي:
=FILTER(A2:A10,MAP(A2:A10,LAMBDA(a,ISEVEN(SUM(-MID(a,SEQUENCE(LEN(a)),1))))))
Excel solution 8 for Even Result by Position Sum, proposed by محمد حلمي:
= 13
even number
5 - 8 = -3
even number
So 56845
5-6+8-4+5
same
5+6+8+4+5
so
ISEVEN(SUM(-MID(num,SEQUENCE(LEN(num)),1))))
Excel solution 9 for Even Result by Position Sum, proposed by Kris Jaganah:
=FILTER(A2:A10,MAP(A2:A10,LAMBDA(x,LET(a,SEQUENCE(LEN(x)),b,--MID(x,a,1),--ISEVEN(SUM(IF(MOD(a,2),b,-b)))))))
Excel solution 10 for Even Result by Position Sum, proposed by Julian Poeltl:
=LET(N,A2:A10,L,LEN(N),E,ISEVEN(L),FILTER(N,ISEVEN(MAP(N,L,E,LAMBDA(N,L,E,SUM(--MID(N,SEQUENCE(ROUND(L/2,0),,,2),1))-SUM(--IF(E,MID(N,SEQUENCE(L/2,,2,2),1),MID(N,SEQUENCE(ROUNDDOWN(L/2,0),,2,2),1))))))))
Excel solution 11 for Even Result by Position Sum, proposed by Timothée BLIOT:
=FILTER(A2:A10,MAP(A2:A10,LAMBDA(x,LET(A,IFERROR(WRAPROWS(MID(x,SEQUENCE(LEN(x)),1),2),0),ISEVEN(SUM(TAKE(A,,1)-TAKE(A,,-1)))))))
Excel solution 12 for Even Result by Position Sum, proposed by Oscar Mendez Roca Farell:
=FILTER(A2:A10, ISEVEN(MAP(A2:A10, LAMBDA(a, SUM(IFERROR(MID(a, SEQUENCE(30, 2), 1)*{1-1}, ))))))
Excel solution 13 for Even Result by Position Sum, proposed by Sunny Baggu:
=FILTER(
A2:A10,
MAP(
A2:A10,
LAMBDA(a,
LET(
_t, MID(a, SEQUENCE(LEN(a)), 1),
_e, MAP(SEQUENCE(LEN(a)), LAMBDA(a, N(ISEVEN(a)))),
_o, MAP(SEQUENCE(LEN(a)), LAMBDA(a, N(ISODD(a)))),
ISEVEN(SUM(_t * _o) - SUM(_t * _e))
)
)
)
)
Excel solution 14 for Even Result by Position Sum, proposed by Sunny Baggu:
=FILTER(
A2:A10,
MAP(
A2:A10,
LAMBDA(n,
LET(
_a, BYCOL(
WRAPROWS(TOCOL(MID(n, SEQUENCE(50), 1) + 0, 3), 2, 0),
LAMBDA(x, SUM(x))
),
ISEVEN(CHOOSECOLS(_a, 1) - CHOOSECOLS(_a, 2))
)
)
)
)
Excel solution 15 for Even Result by Position Sum, proposed by Tolga Demirci, PMP, PMI-ACP, MOS-Expert:
=FILTER(A2:A10;MAP(A2:A10;LAMBDA(a;LET(x;MID(a;SEQUENCE(LEN(a));1);y;SEQUENCE(LEN(a));ISEVEN(SUM(VALUE(FILTER(x;ISODD(y))))-SUM(VALUE(FILTER(x;ISEVEN(y)))))))))
Excel solution 16 for Even Result by Position Sum, proposed by Julien Lacaze:
=LET(data,A2:A10,getRow,LAMBDA(arr,r,INDEX(arr,r,0)),
answers,REDUCE("",SEQUENCE(ROWS(data)),LAMBDA(a,v,LET(split,MID(getRow(data,v),SEQUENCE(LEN(getRow(data,v))),1),
Odds,--CHOOSEROWS(split,SEQUENCE(ROWS(split)/2)*2),
Evens,--IFERROR(CHOOSEROWS(split,SEQUENCE(1+ROWS(split)/2,,0)*2+1),0),
results,SUM(Odds)-SUM(Evens),VSTACK(a,IF(ISEVEN(results),getRow(data,v),""))))),
FILTER(answers,answers<>""))
Excel solution 17 for Even Result by Position Sum, proposed by Ziad A.:
=FILTER(A:A,ISEVEN(MAP(A:A,LAMBDA(_,INDEX(QUERY(,"select "&JOIN("-",REGEXREPLACE(_&" ",{"(.).";".(.)"},"$1"&{"+";"-"}&"0"))),2)))))
Excel solution 18 for Even Result by Position Sum, proposed by Ziad A.:
=FILTER(A:A,ISEVEN(MAP(A:A,LAMBDA(_,SUM(MMULT(--MID(_,SEQUENCE(LEN(_),2),1),{1;-1}))))))
Excel solution 19 for Even Result by Position Sum, proposed by Giorgi Goderdzishvili:
=LET(
frmt,TEXT(A2:A10,"##"),
numbers,IFERROR(--MID(frmt,SEQUENCE(,30),1),0),
seq,SEQUENCE(,30),
isodd,IF(MOD(seq,2)=0,1,0),
isEven,NOT(isodd),
bry,BYROW(numbers,LAMBDA(n,SUM(FILTER(n,(isodd)))-SUM(FILTER(n,isEven)))),
final,--FILTER(frmt,ISEVEN(bry)),
final)
Excel solution 20 for Even Result by Position Sum, proposed by Daniel Garzia:
=FILTER(A2:A10,MAP(A2:A10,LAMBDA(x,LET(l,SEQUENCE(LEN(x)),n,--MID(x,l,1),ISEVEN(SUM(FILTER(n,ISODD(l)))-SUM(FILTER(n,ISEVEN(l))))))))
Excel solution 21 for Even Result by Position Sum, proposed by Quadri Olayinka Atharu:
=FILTER(A2:A10,MAP(A2:A10,LAMBDA(x,LET(y,IFERROR(--MID(x,SEQUENCE(ROUNDUP(LEN(x)/2,0),2),1),0),
ISEVEN(SUM(TAKE(y,,1)-(TAKE(y,,-1))))))))
Excel solution 22 for Even Result by Position Sum, proposed by Quadri Olayinka Atharu:
=FILTER(A2:A10,MAP(A2:A10,
LAMBDA(x,LET(l,CEILING(LEN(x)/2,1),s,IFERROR(--MID(x,SEQUENCE(l,2),1),0),
MOD(SUM(TAKE(s,,1))-SUM(TAKE(s,,-1)),2)=0))))
Excel solution 23 for Even Result by Position Sum, proposed by Quadri Olayinka Atharu:
=FILTER(A2:A10,MAP(A2:A10,LAMBDA(x,LET(_s,SEQUENCE(LEN(x)),
_y,MID(x,_s,1)+0,
_o,ISODD(--_s),
ISEVEN(SUM(FILTER(_y,_o))-SUM(FILTER(_y,NOT(_o))))))))
Excel solution 24 for Even Result by Position Sum, proposed by Rayan S.:
=FILTER(A2:A10,ISEVEN(MAP(A2:A10,LAMBDA(arr,LET(n,SEQUENCE(LEN(arr)),arr,MID(arr,n,1),odd,SUM(FILTER(arr,ISODD(n))+0),even,SUM(FILTER(arr,ISEVEN(n))+0),odd-even)))))
Excel solution 25 for Even Result by Position Sum, proposed by Henriette Hamer:
=FILTER(A2:A10;MAP(A2:A10;LAMBDA(a;IF(ISEVEN(+SUM(MID(a;SEQUENCE(LEN(a)/2;;2;2);1)*1)-SUM(MID(a;SEQUENCE(0,5+LEN(a)/2;;1;2);1)*1));1;0))))
Excel solution 26 for Even Result by Position Sum, proposed by Ricardo Alexis Domínguez Hernández:
=INDEX(FILTER(HSTACK(A2:A10,BYROW(A2:A10,LAMBDA(x,ISEVEN(SUM(INDEX(WRAPROWS(MID(x,SEQUENCE(,LEN(x)),1)*1,2,0),,1))-SUM(INDEX(WRAPROWS(MID(x,SEQUENCE(,LEN(x)),1)*1,2,0),,2)))))),INDEX(HSTACK(A2:A10,BYROW(A2:A10,LAMBDA(x,ISEVEN(SUM(INDEX(WRAPROWS(MID(x,SEQUENCE(,LEN(x)),1)*1,2,0),,1))-SUM(INDEX(WRAPROWS(MID(x,SEQUENCE(,LEN(x)),1)*1,2,0),,2)))))),,2)=TRUE),,1)
Excel solution 27 for Even Result by Position Sum, proposed by Victor Yemitan:
=FILTER(A2:A10,MAP(A2:A10,LAMBDA(a,ISEVEN(REDUCE(0,{1,2},LAMBDA(x,y,-x-SUM(IFERROR(--MID(a,SEQUENCE(ROUND(LEN(a)/2,0),,y,2),1),0))))))))
Solving the challenge of Even Result by Position Sum with Python in Excel
Python in Excel solution 1 for Even Result by Position Sum, proposed by Alejandro Campos:
numeros = xl("A2:A10")[0]
def calcular_resultado(numero):
suma_impar = 0
suma_par = 0
for i, digito in enumerate(numero):
if (i + 1) % 2 != 0:
suma_impar += int(digito)
else:
suma_par += int(digito)
return suma_impar - suma_par
resultados = []
for numero in numeros:
resultado = calcular_resultado(numero)
es_par = resultado % 2 == 0
if es_par:
resultados.append(numero)
df
Show translation
Solving the challenge of Even Result by Position Sum with Excel VBA
Excel VBA solution 1 for Even Result by Position Sum, proposed by Enrico Giorgi:
Option Explicit
Sub Excel_Challenge()
Dim LastRow, i, j, z, odd_num, even_num, odd_sum, even_sum As Long
z = 2
With ThisWorkbook.Sheets(1)
LastRow = .Cells.Find("*", .Range("A1"), xlFormulas, , xlByRows, xlPrevious).Row
For i = 2 To LastRow
j = 0
odd_sum = 0
For j = 1 To odd_num
Next j
j = 0
even&_sum = 0
For j = 1 To even_num
Next j
.Cells(z, 2) = .Cells(i, 1)
z = z + 1
End If
Next i
End With
End Sub
&&
