DEV Community

loading...

Discussion on: Coding Puzzles: Week of 4/8

Collapse
aspittel profile image
Ali Spittel Author

Monday (8KYU): How many stairs will Suzuki climb in 20 years?

codewars.com/kata/56fc55cd1f5a93d6...

Collapse
mihassan profile image
Md Imrul Hassan
def stairs_in_20(stairs):
    return 20*sum(sum(stairs, []))
Collapse
laurieontech profile image
Laurie • Edited

Javascript (ES6):

function stairs_in_20(stairs) {
       return stairs.reduce((steps, day) => steps.concat(day)).reduce((sum, count) => sum + count) * 20;
}
Collapse
jellebekker profile image
Jelle Bekker

Not a one liner like you guys but C#:

using System;
public class Kata
{
  public static long StairsIn20(int[][] stairs)
  {
    long sum = 0;
    foreach (var weekday in stairs)
        foreach (var steps in weekday)
          sum += steps;

    return sum * 20;
   }           
}
Collapse
jellebekker profile image
Jelle Bekker

Okay, I couldn't help myself:

using System;
using System.Linq;

public class Kata
{
  public static long StairsIn20(int[][] stairs)
  {
    return stairs.SelectMany(x => x).Sum() * 20;
  }
}
Collapse
aspittel profile image
Ali Spittel Author • Edited

My Python solution:

def stairs_in_20(stairs):
      return sum(i for sublist in stairs for i in sublist)  * 20
Collapse
clandau profile image
Courtney

Practicing TypeScript for my upcoming internship. And I figured out how to add code highlighting!

function stairsIn20(stairs : number[][]) : number {
    let totalStairs : number = 0;
    for(let day of stairs) {
      totalStairs += day.reduce((total, num) => total + num);
    }
    return totalStairs * 20;
  }