Kata of the Day #7

Counting sheep

kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
kata
Kata of the Day #7

Write a function that takes a string of braces, and determines if the order of the braces is valid. It should return true if the string is valid, and false if it's invalid.

This Kata is similar to the Valid Parentheses Kata, but introduces new characters: brackets [], and curly braces {}. Thanks to @arnedag for the idea!

All input strings will be nonempty, and will only consist of parentheses, brackets and curly braces: ()[]{}. What is considered Valid?

A string of braces is considered valid if all braces are matched with the correct brace.

TypeScript


function countSheeps(arrayOfSheep: boolean[]): number {
  return arrayOfSheep.reduce((acc, val) => acc + (val === true ? 1 : 0), 0);
}

Go


package kata

func CountSheeps(arrayOfSheep []bool) int {
  count := 0
  for _, sheep := range arrayOfSheep {
    if sheep {
      count++
    }
  }
  return count
}

Python


def count_sheeps(array_of_sheep):
    count = 0
    for sheep in array_of_sheep:
        if sheep == True:
            count += 1
    return count

PHP


function countSheeps($arrayOfSheep) {
    $count = 0;
    foreach ($arrayOfSheep as $sheep) {
        if ($sheep === true) {
            $count++;
        }
    }
    return $count;
}

Go to Kata
ByJulie Kohl

Need some help?

Want us to give you some help with your business? No problem, here's a video on who we can help, and how, along with our calendar so you can book in a call to discuss us working together.

Let's see
budget
budget
budget
budget
budget
budget
budget
budget
budget
budget

Subscribe to our newsletter

subscribe
subscribe
subscribe
subscribe
subscribe
subscribe
subscribe
subscribe
subscribe
subscribe