Home » Sum of Numbers following Hashes

Sum of Numbers following Hashes

Solving the challenge of Sum of Numbers following Hashes with Power Query

Power Query solution 1 for Sum of Numbers following Hashes, proposed by Kris Jaganah:
let
  A = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content], 
  B = Table.AddIndexColumn(A, "Id", 1), 
  C = Table.AddColumn(
    B, 
    "Acc", 
    each List.Accumulate(List.FirstN(B[Data], [Id]), 0, (x, y) => if y = "#" then x + 1 else x)
  ), 
  D = Table.AddColumn(
    C, 
    "Answer Expected", 
    each 
      let
        a = List.Sum(Table.SelectRows(C, (x) => x[Data] <> "#" and x[Acc] = [Acc])[Data])
      in
        if [Data] = "#" then a else [Data]
  )[[Answer Expected]]
in
  D
Power Query solution 2 for Sum of Numbers following Hashes, proposed by Aditya Kumar Darak 🇮🇳:
let
  Source = Excel.CurrentWorkbook(){[Name = "data"]}[Content], 
  Group = Table.Group(
    Source, 
    "Data", 
    {
      "A", 
      each Table.ReplaceValue(_, "#", List.Sum(List.Skip([Data])), Replacer.ReplaceValue, {"Data"})
    }, 
    0, 
    (x, y) => Number.From(y = "#")
  ), 
  Return = Table.Combine(Group[A])
in
  Return
Power Query solution 3 for Sum of Numbers following Hashes, proposed by Alejandro Simón 🇵🇦 🇪🇸:
let
  Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content], 
  Sol = Table.Combine(
    Table.Group(
      Source, 
      "Data", 
      {
        "A", 
        (x) =>
          let
            a = x[Data], 
            b = List.Skip(a), 
            c = Table.FromColumns({{List.Sum(b)} & b}, Table.ColumnNames(x))
          in
            c
      }, 
      0, 
      (x, y) => Number.From(y = "#")
    )[A]
  )
in
  Sol
Power Query solution 4 for Sum of Numbers following Hashes, proposed by Luan Rodrigues:
let
  Fonte = Tabela1, 
  grp = Table.Group(
    Fonte, 
    "Data", 
    {
      "grp", 
      each Table.TransformColumns(
        _, 
        {"Data", (x) => if x = "#" then List.Sum(List.RemoveFirstN(_[Data])) else x}
      )
    }, 
    0, 
    (a, b) => Number.From(b = "#")
  )[grp], 
  res = Table.Combine(grp)
in
  res
Power Query solution 5 for Sum of Numbers following Hashes, proposed by Abdallah Ally:
let
  Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content], 
  AddInd = Table.AddIndexColumn(Source, "Index"), 
  AddCol = Table.AddColumn(
    AddInd, 
    "My Answer", 
    each 
      if [Data] <> "#" then
        [Data]
      else
        [
          a = Table.SelectRows(AddInd, each [Data] = "#")[Index], 
          b = List.Transform(
            a, 
            each {
              _, 
              List.Sum(
                List.Range(
                  AddInd[Data], 
                  _ + 1, 
                  (try a{List.PositionOf(a, _) + 1} otherwise Table.RowCount(Source)) - _ - 1
                )
              )
            }
          ), 
          c = List.Select(b, (x) => x{0} = [Index]){0}{1}
        ][c]
  )[[Data], [Answer Expected], [My Answer]], 
  Result = Table.AddColumn(AddCol, "Check", each [Answer Expected] = [My Answer])
in
  Result
Power Query solution 6 for Sum of Numbers following Hashes, proposed by Ramiro Ayala Chávez:
let
  S = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content], 
  a = Table.AddColumn(S, "A", each if [Data] = "#" then 0 else 1), 
  b = Table.Group(a, {"A"}, {"G", each [Data]}, 0)[G], 
  c = List.Generate(
    () => [i = 0], 
    each [i] < List.Count(b), 
    each [i = [i] + 1], 
    each if Number.IsEven([i]) then {List.Sum(b{[i] + 1})} else b{[i]}
  ), 
  Sol = Table.FromRows(List.Zip({S[Data], List.Combine(c)}), {"Data", "Answer Expected"})
in
  Sol
Power Query solution 7 for Sum of Numbers following Hashes, proposed by CA Raghunath Gundi:
let
  Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content][[Data]], 
  Index = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type), 
  Grp = Table.AddColumn(Index, "Grp", each if [Data] = "#" then [Index] else null), 
  FillDown = Table.FillDown(Grp, {"Grp"}), 
  #"No#" = Table.SelectRows(FillDown, each ([Data] <> "#")), 
  Grp2 = Table.Group(#"No#", {"Grp"}, {{"Sum", each List.Sum([Data]), type number}}), 
  Merge = Table.NestedJoin(FillDown, {"Grp"}, Grp2, {"Grp"}, "Grp2", JoinKind.LeftOuter), 
  ExpandedGrp2 = Table.ExpandTableColumn(Merge, "Grp2", {"Sum"}, {"Sum"})[[Data], [Sum]], 
  Result = Table.AddColumn(ExpandedGrp2, "Result", each if [Data] = "#" then [Sum] else [Data])[
    [Result]
  ]
in
  Result
Power Query solution 8 for Sum of Numbers following Hashes, proposed by Ahmed Ariem:
let
  f = (x) =>
    List.Combine(
      Table.Group(
        x, 
        "Data", 
        {"tmp", (x) => [a = List.Skip(x[Data]), b = {List.Sum(a)} & a][b]}, 
        0, 
        (x, y) => Number.From(y = "#")
      )[tmp]
    ), 
  Source = f(Excel.CurrentWorkbook(){[Name = "Table1"]}[Content])
in
  Source
Power Query solution 9 for Sum of Numbers following Hashes, proposed by Alexandre Garcia:
let
  A = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content][Data], 
  B = Table.FromList(
    List.Transform(
      List.Zip({A, List.Positions(A)}), 
      each if _{0} = "#" then List.Sum(List.FirstN(List.Skip(A, _{1} + 1), each _ <> "#")) else _{0}
    ), 
    each {_}, 
    {"Answer"}
  )
in
  B
Power Query solution 10 for Sum of Numbers following Hashes, proposed by Krzysztof Kominiak:
let
  Source = Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText(
          "i45WUlaK1YlWMjYGUyYGYAoiZmoCpoyMICrMkKQMDZBVmEEoYxNkQagZxpgGmppDzAVSsQA=", 
          BinaryEncoding.Base64
        ), 
        Compression.Deflate
      )
    ), 
    let
      _t = ((type nullable text) meta [Serialized.Text = true])
    in
      type table [Data = _t]
  ), 
  GroupRows = Table.Group(
    Source, 
    {"Data"}, 
    {
      {"NT", each _[Data]}, 
      {
        "Cr", 
        each {List.Sum(List.Transform(List.Skip(_[Data]), Number.From))}
          & List.Transform(List.Skip(_[Data]), Number.From)
      }
    }, 
    0, 
    (x, y) => Number.From(y[Data] = "#")
  ), 
  Result = Table.Combine(
    Table.AddColumn(
      GroupRows, 
      "All", 
      each Table.FromColumns({[NT], [Cr]}, {"Data", "Answer Expected"})
    )[All]
  )
in
  Result
Power Query solution 11 for Sum of Numbers following Hashes, proposed by Francesco Bianchi 🇮🇹:
let
  Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content], 
  g = 
    let
      a = List.Transform(Source[Data], each Number.From(_ = "#")), 
      b = List.Generate(
        () => [RT = a{0}, i = 0], 
        each [i] < List.Count(a), 
        each [RT = [RT] + a{[i] + 1}, i = [i] + 1], 
        each [RT]
      ), 
      c = Table.FromRows(List.Zip({Source[Data], b}))
    in
      c, 
  s = List.Combine(
    Table.Group(
      g, 
      {"Column2"}, 
      {{"Answer", each {List.Sum(List.Skip(_[Column1]))} & List.Skip(_[Column1])}}
    )[#"Answer"]
  )
in
  s
Power Query solution 12 for Sum of Numbers following Hashes, proposed by Tyler N.:
let
  a = YourTable, 
  b = Table.AddIndexColumn(a, "i", 0, 1), 
  c = Table.AddColumn(
    b, 
    "x", 
    each 
      let
        d = [i], 
        e = {
          [Data], 
          List.Sum(
            List.Transform(
              {
                d + 1 .. try
                  Table.SelectRows(b, each [Data] = "#" and [i] > d)[i]{0} - 1
                otherwise
                  List.Last(b[i])
              }, 
              each a[Data]{_}
            )
          )
        }
      in
        e{Int8.From([Data] = "#")}
  )
in
  c[x]
Power Query solution 13 for Sum of Numbers following Hashes, proposed by Joevan Bedico:
let
  Source = Excel.CurrentWorkbook(){[Name = "Table5"]}[Content], 
  Answer = Table.FromColumns(
    {
      Source[Data], 
      List.Combine(
        Table.Group(
          Source, 
          "Data", 
          {
            "A", 
            each 
              let
                d = List.Skip([Data])
              in
                {List.Sum(d)} & d
          }, 
          0, 
          (x, y) => Number.From(y = "#")
        )[A]
      )
    }, 
    {"Data", "Answer Expected"}
  )
in
  Answer

Solving the challenge of Sum of Numbers following Hashes with Excel

Excel solution 1 for Sum of Numbers following Hashes, proposed by Bo Rydobon 🇹🇭:
=MAP(
    A2:A20,
    LAMBDA(
        x,
        IF(
            x>"",
            SUM(
                TAKE(
                    x:A20,
                    XMATCH(
                        "#",
                        DROP(
                            VSTACK(
                                x:A20,
                                "#"
                            ),
                            1
                        )
                    )
                )
            ),
            x
        )
    )
)
Excel solution 2 for Sum of Numbers following Hashes, proposed by Bo Rydobon 🇹🇭:
=LET(z,A2:A20,s,SCAN(0,z>"",SUM),IF(z>"",LOOKUP(s,GROUPBY(s,z,SUM,0)),z))
Excel solution 3 for Sum of Numbers following Hashes, proposed by Rick Rothstein:
=LET(
    c,
    A2:A20,
    r,
    ROWS(
        c
    ),
    s,
    SEQUENCE(
        r,
        ,
        r,
        -1
    ),
    IF(
        c="#",
        INDEX(
            SCAN(
                0,
                INDEX(
                    c,
                    s
                ),
                LAMBDA(
                    a,
                    x,
                    IF(
                        x="#",
                        ,
                        a+x
                    )
                )
            ),
            s-1
        ),
        c
    )
)
Excel solution 4 for Sum of Numbers following Hashes, proposed by John V.:
=LET(
    d,
    A2:A20,
    s,
    SCAN(
        0,
        d>"",
        SUM
    ),
    IF(
        d>"",
        MAP(
            s,
            LAMBDA(
                x,
                SUM(
                    IF(
                        x=s,
                        d
                    )
                )
            )
        ),
        d
    )
)
Excel solution 5 for Sum of Numbers following Hashes, proposed by 🇰🇷 Taeyong Shin:
=LET(
    d,
    A2:A20,
    DROP(
        GROUPBY(
            HSTACK(
                SCAN(
                    0,
                    d>"",
                    SUM
                ),
                ROW(
                    d
                )
            ),
            d,
            SUM,
            1,
            -2,
            ,
            d<""
        ),
        1,
        2
    )
)
Excel solution 6 for Sum of Numbers following Hashes, proposed by Kris Jaganah:
=LET(a,A2:A20,b,SCAN(,N(a="#"),SUM),IF(a="#",MAP(b,LAMBDA(x,SUM(IF(a="#",0,a)*(b=x)))),a))
Excel solution 7 for Sum of Numbers following Hashes, proposed by Julian Poeltl:
=LET(
    D,
    A2:A20,
    S,
    VSTACK(
        D,
        "#"
    ),
    DROP(
        IF(
            S="#",
            MAP(
                SEQUENCE(
                    ROWS(
                        D
                    )
                ),
                LAMBDA(
                    A,
                    SUM(
                        TAKE(
                            DROP(
                                S,
                                A
                            ),
                            XMATCH(
                                "#",
                                DROP(
                                S,
                                A
                            )
                            )
                        )
                    )
                )
            ),
            S
        ),
        -1
    )
)
Excel solution 8 for Sum of Numbers following Hashes, proposed by Aditya Kumar Darak 🇮🇳:
=LET(
 _data, A2:A20,
 _scan, SCAN(0, _data = "#", SUM),
 _grp, GROUPBY(_scan, _data, SUM, 0, 0),
 _rtrn, IF(_data <> "#", _data, VLOOKUP(_scan, _grp, 2)),
 _rtrn
)
Excel solution 9 for Sum of Numbers following Hashes, proposed by Timothée BLIOT:
=LET(A,A2:A20,DROP(GROUPBY(FILTER(HSTACK(SCAN(0,A,LAMBDA(w,v,IF(v="#",w+1,w))),SEQUENCE(ROWS(A))),A<>"#"),FILTER(A,A<>"#"),SUM,,-2),1,2))
Excel solution 10 for Sum of Numbers following Hashes, proposed by Oscar Mendez Roca Farell:
=MAP(A2:A20,LAMBDA(a,IF(a>"",SUM(TAKE(a:A21,SUM(N(TAKE(TOCOL(IF(a:A21<"",e,ROW(a:A21)),2),2)*{-1;1})))),a)))
Excel solution 11 for Sum of Numbers following Hashes, proposed by Duy Tùng:
=LET(a,A2:A20,b,SCAN(0,a>"",SUM),IF(a>"",LOOKUP(b,GROUPBY(b,a,SUM,0)),a))
Excel solution 12 for Sum of Numbers following Hashes, proposed by Sunny Baggu:
=LET(
    
     d,
     A2:A20,
    
     _a,
     IF(
         
          ISNUMBER(
              --d
          ),
         
          d,
         
          "#" & SCAN(
              0,
               IF(
                   d = "#",
                    1,
                    0
               ),
               LAMBDA(
                   a,
                    v,
                    a + v
               )
          )
          
     ),
    
     _b,
     CONCAT(
         d & ","
     ),
    
     _c,
     BYROW(
         --TEXTSPLIT(
             _b,
              ",",
              "#",
              1,
              ,
              0
         ),
          LAMBDA(
              a,
               SUM(
                   a
               )
          )
     ),
    
     _d,
     "#" & SEQUENCE(
         ROWS(
             _c
         )
     ),
    
     IFNA(
         XLOOKUP(
             _a,
              _d,
              _c
         ),
          _a
     )
    
)
Excel solution 13 for Sum of Numbers following Hashes, proposed by Sunny Baggu:
=LET(
    
     _a,
     SEQUENCE(
         ROWS(
             A2:A20
         )
     ),
    
     _b,
     SORTBY(
         IF(
             ISNUMBER(
             A2:A20
         ),
              A2:A20,
              0
         ),
          _a,
          -1
     ),
    
     _c,
     SCAN(
         0,
          _b,
          LAMBDA(
              a,
               v,
               IF(
                   v,
                    a + v,
                    v
               )
          )
     ),
    
     _d,
     FILTER(
         _a,
          _b = 0
     ),
    
     _f,
     XLOOKUP(
         _d - 1,
          _a,
          _c
     ),
    
     _g,
     IFNA(
         XLOOKUP(
             _a,
              _d,
              _f
         ),
          _b
     ),
    
     SORTBY(
         _g,
          _a,
          -1
     )
    
)
Excel solution 14 for Sum of Numbers following Hashes, proposed by LEONARD OCHEA 🇷🇴:
=LET(d,
    A2:A20,
    i,
    SCAN(
        ,
        N(
            d=A2
        ),
        SUM
    ),
    n,
    N(
        +d
    ),
    MAP(i,
    n,
    LAMBDA(a,
    b,
    IF(b,
    b,
    SUM((a=i)*n)))))
Excel solution 15 for Sum of Numbers following Hashes, proposed by Anshu Bantra:
=LET(
    
    arr_,
     --TEXTSPLIT(
         TEXTJOIN(
             ",",
             ,
             A2:A20
         ),
         ",",
         "#"
     ),
    
    DROP(
        TOCOL(
            HSTACK(
                BYROW(
                    arr_,
                     LAMBDA(
                         ro_,
                          SUM(
                              IFERROR(
                                  ro_,
                                  0
                              )
                          )
                     )
                ),
                arr_
            ),
            3
        ),
        1
    )
    
)
Excel solution 16 for Sum of Numbers following Hashes, proposed by Md. Zohurul Islam:
=LET(
a,A2:A20,
b,SEQUENCE(COUNTA(a)),
d,MAP(b,a,LAMBDA(x,y,IF(y="#",x,0))),
e,SCAN(0,d,LAMBDA(x,y,MAX(x,y))),
f,GROUPBY(e,a,SUM,0,0),
g,XLOOKUP(b,TAKE(f,,1),TAKE(f,,-1),0),
h,MAP(a,g,LAMBDA(x,y,IF(x="#",y,x))),
h)
Excel solution 17 for Sum of Numbers following Hashes, proposed by Pieter de B.:
=MAP(
    A2:A20,
    LAMBDA(
        a,
        LET(
            b,
            INDEX(
                A2:A20,
                ROW(
                    a
                )+1
            ),
            IF(
                a="#",
                SUM(
                    a:XLOOKUP(
                        "#",
                        b:A20,
                        b:A20,
                        A20
                    )
                ),
                a
            )
        )
    )
)
Excel solution 18 for Sum of Numbers following Hashes, proposed by Hamidi Hamid:
=LET(
    f,
    A2:A20,
    m,
    ROWS(
        f
    ),
    mm,
    SEQUENCE(
        m,
        ,
        m,
        -1
    ),
    y,
    SORTBY(
        f,
        mm,
        1
    ),
    d,
    IFERROR(
        y*1,
        0
    ),
    x,
    SORTBY(
        SCAN(
            0,
            d,
            LAMBDA(
                a,
                b,
                IF(
                    b=0,
                    -b,
                    b+a
                )
            )
        ),
        mm
    ),
    IF(
        f="#",
        VSTACK(
            DROP(
                x,
                1
            ),
            0
        ),
        f
    )
)
Excel solution 19 for Sum of Numbers following Hashes, proposed by Asheesh Pahwa:
=LET(
    d,
    A2:A20,
    xm,
    XMATCH(
        d,
        "#"
    ),
    s,
    SEQUENCE(
        ROWS(
            xm
        )
    ),
    I,
    IF(
        ISNUMBER(
            xm
        ),
        s,
        0
    ),
    sc,
    SCAN(
        0,
        I,
        LAMBDA(
            x,
            y,
            IF(
                y,
                y,
                x
            )
        )
    ),
    u,
    UNIQUE(
        sc
    ),
    REDUCE(
        A1,
        u,
        LAMBDA(
            a,
            v,
            VSTACK(
                a,
                LET(
                    f,
                    FILTER(
                        d,
                        sc=v
                    ),
                    d,
                    DROP(
                        f,
                        1
                    ),
                    VSTACK(
                        SUM(
                            d
                        ),
                        d
                    )
                )
            )
        )
    )
)
Excel solution 20 for Sum of Numbers following Hashes, proposed by ferhat CK:
=LET(w,SCAN(,IF(A2:A20="#",1,0),SUM),DROP(REDUCE(0,UNIQUE(w),LAMBDA(x,y,VSTACK(x,LET(a,DROP(FILTER(A2:A20,w=y),1),VSTACK(SUM(a),a))))),1))
Excel solution 21 for Sum of Numbers following Hashes, proposed by Ankur Sharma:
=LET(r, A2:A20,
MAP(r, SEQUENCE(COUNTA(r)), LAMBDA(y, z,
LET(tr, DROP(VSTACK(r, "#"), z),
IF(y="#", SUM(TAKE(tr, XMATCH("#", tr))), y)))))
Excel solution 22 for Sum of Numbers following Hashes, proposed by Bilal Mahmoud kh.:
="#",
    SUM(
        OFFSET(
            x:A20,
            1,
            0,
            IFNA(
                MATCH(
                    "#",
                    DROP(
                        x:A20,
                        1
                    ),
                    0
                )-1,
                COUNTA(
                    x:A20
                )
            ),
            1
        )
    ),
    x)))
Excel solution 23 for Sum of Numbers following Hashes, proposed by Imam Hambali:
=LET(
    
    d,
     A2:A20,
    
    sc,
     SCAN(
         ,
          IF(
              d="#",
              1,
              0
          ),
         SUM
     ),
    
    sb,
     SUBSTITUTE(
         d,
         "#",
         0
     )*1,
    
    gb,
     GROUPBY(
         sc,
         sb,
         SUM,
         0,
         0
     ),
    
    IF(
        sb=0,
        XLOOKUP(
            sc,
            TAKE(
                gb,
                ,
                1
            ),
            TAKE(
                gb,
                ,
                -1
            )
        ),
        sb
    )
    
)
Excel solution 24 for Sum of Numbers following Hashes, proposed by Julien Lacaze:
=LET(d,
    A2:A20,
    
m,
    MAP(
        d,
        LAMBDA(
            a,
            XMATCH(
                TRUE,
                A2:a="#",
                ,
                -1
            )
        )
    ),
    
s,
    --SUBSTITUTE(
        d,
        "#",
        0
    ),
    
MAP(m,
    d,
    s,
    LAMBDA(a,
    b,
    c,
    IF(c,
    b,
    SUM(s*--(m=a))))))
Excel solution 25 for Sum of Numbers following Hashes, proposed by Andres Rojas Moncada:
=LET(nn,
    IFERROR(
        A2:A20/1,
        0
    ),
    ii,
    SCAN(
        0,
        nn=0,
        SUM
    ),
    MAP(nn,
    ii,
    LAMBDA(n,
    i,
    IF(n,
    n,
    SUM((ii=i)*nn)))))
Excel solution 26 for Sum of Numbers following Hashes, proposed by Gabriel Pugliese:
=LET(z;
    A2:A20;
    z_;
    LAMBDA(
        x;
        INDEX(
            x;
            SEQUENCE(
                COUNTA(
                    x
                );
                1;
                COUNTA(
                    x
                );
                -1
            );
            1
        )
    );
    
q;
    z_(z);
    
sc;
    SCAN(
        0;
        q;
        LAMBDA(
            a;
            v;
            IF(
                ISNUMBER(
                    v
                );
                a+v;
                0
            )
        )
    );
    
w;
    DROP(z_(sc);
    1);
    
HSTACK(
    z;
    IF(
        z="#";
        w;
        z
    )
))
Excel solution 27 for Sum of Numbers following Hashes, proposed by Tomasz Jakóbczyk:
=IFNA(
    IF(
        A2="#",
        SUM(
            OFFSET(
                A2,
                ,
                ,
                MATCH(
                    "#",
                    A3:$A$20,
                    0
                )
            )
        ),
        A2
    ),
    SUM(
        OFFSET(
            $A$1,
            CELL(
                "row",
                XLOOKUP(
                    "#",
                    $A$2:$A$20,
                    $A$2:$A$20,
                    "",
                    0,
                    -1
                )
            ),
            ,
            ROWS(
                $A$1:$A$20
            )-CELL(
                "row",
                XLOOKUP(
                    "#",
                    $A$2:$A$20,
                    $A$2:$A$20,
                    "",
                    0,
                    -1
                )
            )
        )
    )
)
Excel solution 28 for Sum of Numbers following Hashes, proposed by Gupta Harsh:
=IF(
    A2="#",
     SUM(
         OFFSET(
             A2,
             ,
             ,
             IFNA(
                 MATCH(
                     "#",
                     A3:$A$20,
                     0
                 ),
                 
                 COUNT(
                     A3:$A$20
                 )+1
             )
         )
     ),
    A2
)
2nd Solution:- 
=IF(
    A2<>"#",
    A2,
     
    SUM(
        A2:INDEX(
            A3:$A$20,
            IFERROR(
                MATCH(
                    "#",
                    A3:$A$20,
                    0
                ),
                COUNT(
                     A3:$A$20
                 )
            )
        )
    )
)
Excel solution 29 for Sum of Numbers following Hashes, proposed by Christian Mbolanantenaina:
=LET(
in,A2:A20,
Join,";"&TEXTJOIN(";",,in)&";",
Sm,BYROW(--IFERROR(TEXTSPLIT(Join,";","#",1),0),SUM),
Tb,--TEXTSPLIT(Join,";","#",1),
Tab,HSTACK(Sm,Tb),
out,TOCOL(Tab,3),
out)

Solving the challenge of Sum of Numbers following Hashes with Python

Python solution 1 for Sum of Numbers following Hashes, proposed by Konrad Gryczan, PhD:
import pandas as pd
path = "591 Sum of Numbers following Hashes.xlsx"
input = pd.read_excel(path, usecols="A", nrows=20)
test = pd.read_excel(path, usecols="B", nrows=20)
input['group'] = (input['Data'] == '#').cumsum()
input['Data'] = pd.to_numeric(input['Data'], errors='coerce')
input['Data'] = input['Data'].fillna(input.groupby('group')['Data'].transform('sum')).astype('int64')
print(input['Data'].equals(test['Answer Expected']))    # True
                    
                  
Python solution 2 for Sum of Numbers following Hashes, proposed by Luan Rodrigues:
PY Solution!
import pandas as pd
file = 'Excel_Challenge_591 - Sum of Numbers following Hashes.xlsx'
df = pd.read_excel(file,usecols="A")
df['grp'] = df.index.to_series().where(df['Data'] == '#').ffill()
grp = df[df['Data'] != '#'].groupby('grp').sum()
merge = pd.merge(df, grp,left_on='grp',right_index=True, how='inner')[['Data_x','Data_y']]
merge['rst'] = merge['Data_x'].where(merge['Data_x'] != '#',merge['Data_y'])
print(merge[['rst']])
                    
                  

Solving the challenge of Sum of Numbers following Hashes with Python in Excel

Python in Excel solution 1 for Sum of Numbers following Hashes, proposed by Alejandro Campos:
df = xl("A1:A20", headers=True)
df_inverted = df.iloc[::-1].reset_index(drop=True)
answers = []
cumulated_sum = 0
for value in df_inverted['Data']:
 if value == '#':
 answers.append(cumulated_sum)
 cumulated_sum = 0 
 else:
 cumulated_sum += value
 answers.append(value)
df_inverted['Answer Expected'] = answers
df_final = df_inverted.iloc[::-1].reset_index(drop=True)
df_final
                    
                  
Python in Excel solution 2 for Sum of Numbers following Hashes, proposed by Anshu Bantra:
data = list(xl("A1:A20", headers=True).values)
for i in range(len(data)):
 sum_below = 0
 if data[i] == "#":
 for x in data[i+1:]:
 if x == "#":
 break
 else:
 sum_below += x
 data[i] = sum_below
data
                    
                  
Python in Excel solution 3 for Sum of Numbers following Hashes, proposed by Anshu Bantra:
lst = list(xl("A1:A20", headers=True).values)
idxs = [idx for idx, num in enumerate(lst) if num=='#']
for idx in range(len(idxs)):
 if idx == len(idxs)-1:
 lst[idxs[idx]] = sum(lst[idxs[idx]+1:len(lst)])
 else:
 lst[idxs[idx]] = sum(lst[idxs[idx]+1:idxs[idx+1]])
lst
                    
                  

Solving the challenge of Sum of Numbers following Hashes with R

R solution 1 for Sum of Numbers following Hashes, proposed by Konrad Gryczan, PhD:
library(tidyverse)
library(readxl)
path = "Excel/591 Sum of Numbers following Hashes.xlsx"
input = read_excel(path, range = "A1:A20")
test = read_excel(path, range = "B1:B20")
result = input %>%
 mutate(group = cumsum(Data == "#")) %>%
 mutate(`Answer Expected` = ifelse(Data == "#", 
 sum(as.numeric(Data), na.rm = TRUE), 
 as.numeric(Data)), .by = group)
all.equal(result$`Answer Expected`, test$`Answer Expected`)
#> [1] TRUE
                    
                  

&&

Leave a Reply