How to Approach Solving a Code Kata Problem: Step by Step Guide

Introduction

The idea of coding katas comes from a common practice found in martial arts.

Kata in Japanese means "form" and refers to the solo drills that practitioners perform during their training sessions. Kata gif.

Katas are meant to be done slowly and deliberately, much more slowly than during a real fighting situation. These patterns are honed through practice and repetition so that when a situation arises that the student has to fight, they can instinctually refer back them.

This also frees their mind to focus on other essential things, such as what their opponent is doing.

Martial artists believe that constant practice is required to master a technique and achieve greatness.

The same idea is true for the code katas. We practice specific aspects of programming dozens of times so that we don't have to struggle with it during our actual work.

Normally, a teacher will make the students take their time with a kata until they are comfortable taking on challenges of a similar difficulty level.

Kata's are also a common way of testing candidates' problem-solving skills and ability during an interview process. I've put together my list of essential steps I take in solving them.

  1. Identify the problem
  2. Break it down
  3. Identify variables
  4. Tackle your subproblems
  5. Troubleshooting
  6. Refactor

1. Identify the problem

data/admin/2021/3/desc-to-comments.jpg

2. Break it down

public class TopWords
{
    public static List Top3(string s)
    {
       // Step 1 trim punctuation from s
    
        // Step 2 - iterate string and put into a dictionary, 
        // Key = word, Value = count 
        //  if (check if NullOrWhiteSpace) {
        //    add to new dictionary
        //    }
        //      else
        //        increase count
        //  }
      
        // Step 3 - Order new dictionary by value by descending   
      
        // Step 4 - Take top 3
      
        // Step 5 return result
      }
}

3. Identify variables

Think about the data and then consider -

public class TopWords
{
    public static List Top3(string s)
    {   
        Dictionary frequencies = new Dictionary();
      
        var punctuation = s.Where(Char.IsPunctuation).Distinct().ToArray();
        var words = s.Split(new char[] {' ',',', ';', '/', ':', '?', '.', '_','-','!' }).Select(x => x.Trim(punctuation)).ToList();
        words = words.ConvertAll(d => d.ToLower());
}

4. Tackle your subproblems

public class TopWords
{
    public static List Top3(string s)
    {   
        Dictionary frequencies = new Dictionary();
      
        // Variables
        var punctuation = s.Where(Char.IsPunctuation).Distinct().ToArray();
        
        // Step 1 trim punctuation from s
        var words = s.Split(new char[] {' ',',', ';', '/', ':', '?', '.', '_','-','!' }).Select(x => x.Trim(punctuation)).ToList();
        words = words.ConvertAll(d => d.ToLower());
        
        // Print result
       foreach (var word in words)
       {
          Console.WriteLine("word");
       } 

        // Step 2 - iterate string and put into a dictionary, 
        // Key = word, Value = count 
        foreach (var word in words) {
          
            if (!String.IsNullOrWhiteSpace(word))
            {
              if (!frequencies.ContainsKey(word))
              { 
                frequencies.Add(word, 0);
                frequencies[word] += 1;
              }
              else
              {
                  frequencies[word] += 1;
              }
             }
           }
           
        // Iterate the dictionary and print result
        foreach (KeyValuePair kvp in frequencies)
        {
          Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
        } 
        
        // Step 3
        // TODO         
}

5. Troubleshooting

6. Refactor