Wednesday, January 6, 2021

Leetcode discuss: 158. Read N Characters Given Read4 II - Call multiple times

 Here is the link. 

Troubleshooting - C# - "abcde", [1, 4], ["a", "bcde"] - Simulation

Jan. 5, 2020
Introduction
It is important for me to learn how to quickly fix a bug. I could not find the issue by reading my own code, so I wrote some debug code. This is easy for me to review later. If you have interest to learn how to debug the code, please continue; Or leave comment for tips.

Test case
"abcde", [1, 4], ["a", "bcde"] - My code could not work, the return is ["a", "bcd"], so I figured out that I should leave n unchanged, declare a copy variable. In order for me to find the issue, I created my own copy of Read4 API which only works for two calls.

My goal
It is so easy for me to write code using my own Read4 API and make one test case work first.
"abcde", [1, 4], ["a", "bcde"]
It is also important for me to be humble, when I design and add feature to read from stored buffer C# List, I should think about how to design a test case by myself. Do not leave online judge to show me failed test case.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace read4
{    

    public class Solution 
    {
        static void Main(string[] args)
        {
            var buf = new char[4];
            var result = Read(buf, 1);
            
            buf = new char[4];
            var result2 = Read(buf, 4);          
        }

        private static List<char> buffer = new List<char>();
        private static int times = 0; 

        private static int Read4(char[] buffer)
        {
           // buffer = new char[4];
           // buffer = "abc".ToCharArray(); 
            if (times == 0)
            {
                buffer[0] = 'a';
                buffer[1] = 'b';
                buffer[2] = 'c';
                buffer[3] = 'd';

                times++;
                return 4;
            }
            else
            {
                buffer[0] = 'e';
                return 1; 
            }            
        }
        /**
         * @param buf Destination buffer
         * @param n   Number of characters to read
         * @return    The number of actual characters read
         */
        public static int Read(char[] buf, int n)
        {
            // read 5, twice, first one 4, second one
            // read 5, twice, first one 4, second 4 - but only save 5        

            var index = 0;

            // read from buffer
            var copy = n; 
            while (buffer.Count > 0 && copy > 0)  // if should be a while statement
            {
                buf[index++] = buffer[0];
                buffer.RemoveAt(0);
                copy--;
            }

            var totalTimes = (copy + 3) / 4;

            for (int i = 0; i < totalTimes; i++)
            {
                var tmp = new char[4];
                var actual = Read4(tmp);

                for (int j = 0; j < actual; j++)
                {
                    if (index < n) //  n should not be changed
                    {
                        buf[index++] = tmp[j];
                    }
                    else
                    {
                        buffer.Add(tmp[j]);
                    }
                }

                if (actual != 4)
                {
                    break;
                }
            }

            return index;
        }
    }
}

No comments:

Post a Comment