Showing posts with label regular expression. Show all posts
Showing posts with label regular expression. Show all posts

Sunday, January 21, 2018

Word count algorithm practice

January 21, 2018

Introduction


It is another mock interview at 4:00 pm. I had to work on the algorithm called word count in a sentence. The task is to lower the char in sentence, replace ' using empty space, split into words using delimiter string ".!,", and then store words to Dictionary<string, int>, and then apply bucket sort, and then output to array.

30 minutes is the time limit for me to write. I could not finish it.


Please complete the code


Here is the code I wrote in 30 minutes in mock interview. I like to spend time to write today and complete it to pass all test cases on mock interview platform.


Follow up 

January 23, 2018

Here is C# code to pass all test case. I spent over 30 minutes to read Regular.Split and String.Split and figure out how to specify delimiters, how to specify multiple using +, using [ and ] to enclose all delimiters, and understand ( and ) meaning.

C# code is here.


Wednesday, November 8, 2017

Leetcode 10: regular expression - Fun to play

Nov. 8, 2017


Introduction



It is really fun to play with code related to recursion tree after the mock interview. I ran into various error with a simple test case each time using Leetcode online judge, I learn from each failure and try to play with them. It is fun to play over hours and actually I like to learn something here. Let me document the issues first, and then figure out the solution later.

Test cases to help 


I like to list the test cases to help design the algorithm.

"", "a*"
"", "a*b*"
"bbbba",".*a*a"

Here is the C# code with a bug timeout - need to run at least 1 time first for a* pattern, and then run 0 time for a * pattern.


The test case for time out is here:
"aaaaaaaaaaaaab"
"a*a*a*a*a*a*a*a*a*a*c"

And the code causes the problem is shown in the following picture:

Here is the C# code with fix of timeout:


Monday, August 7, 2017

String search algorithm practice

August 7, 2017

Introduction



It is the British Columbia holiday and I booked another mocking practice at 12:00 pm. The practice hours is a very happy time to learn how to work on one algorithm.

Algorithm practice


C# practice code is here. I spent over 10 minutes to fix so many compile errors, but I stopped after running into the execution errors.

After the practice, I found the two problems, one is null pointer error (4 lines of code are added from line 44 to 47), second one is missing a loop (a for loop is added on line 58), I fixed them after mocking practice.

Weakness


There are two issues in my writing in mocking interview. First one is null pointer issue. Second one is to separate the collection from one single item. Need to go over each item in collection, and append each of them.

Plan to do some research on common mistakes in C# code. Using strong typing is a good tip to allow the compiler to help, avoid error in run time checking.