Notes on codes, projects and everything

Hitting a nail with the wrong hammer (my writing tools)

I just failed a programming assessment test miserably yesterday and thought I should at least document it down. However, the problem with this is that the questions are copyrighted, so I guess I would write it from another point of view. So the main reason I failed was because I chose the wrong strategy to the problem, thinking it should be solution but as I put in time to that I ended up creating more problems.

So there’s this script I wrote in the past to assist in my writing. I mostly write in Chinese, and the problem with my writing is that I tend to repeat certain words more than it should. Therefore in order to fix this, I wrote a script to generate something to list all the ngrams and (reverse) order them by the frequency of use.

So the core function that makes this possible is


function input_generate_ngram(Array $input, $n) {
    return array_reduce(
            range(0, count($input) - $n),
            function(Array $current, $next) use($input, $n) {
                return array_merge(
                    $current,
                    array(implode(NULL, array_slice($input, $next, $n))));
            },
            array());
}

What it does is that, given an array of words (Chinese character in this case) I can use this to generate an array of n-grams. For example, if I have send this as input (assuming $n = 2)

['今', '天', '天', '氣', '很', '好']

I would get

[['今', '天'], ['天', '天'], ['天', '氣'], ['氣', '很'], ['很', '好']]

The code itself is not written for efficiency, and could use some polish. However I am not publishing it because I am using it personally for my writing projects so I guess it is good enough. Tokenization is also not considered as I am not doing in-depth analysis to the text, I am not a linguist, and the script was used just to discover repetitive keywords.

So the assessment test was about a function that replace a string repetitively by following a set of rules. In order to not get too specific about it, think of this problem as simplifying a mathematical expression. For example in order to simplify (-(-x) * (-x) + y + (-y) / (-y)), we would perform a series of steps as follows


(-(-x) / -x + y + (-y) / (-y))
= (x / -x + y + (-y) / (-y))
= (-1 + y + (-y) / (-y))
= (-1 + y + 1)

So the rules here are obviously

  • ['-', '-x'] => x
  • ['x', '/-x'] => -1
  • ['-y', '/-y'] => 1

So putting the expression into the function (again I can’t replicate the original problem so the example output here doesn’t make sense, just assume as it is), and set $n = 2


INPUT =['-', '-x', '+y', '+', '-y', '/-y']
OUTPUT = [['-', '-x'], ['-x', '+y'], ['+y', '+'], ['+', '-y'], ['-y', '/-y']]

So when I got really excited when I arrived this state. So I did all the match and replace, but a new problem came up when I attempted to merge the string together. Practically, I made a new problem by following this solution. However, I was too obsessed with the strategy then, so I kept on trying, and obviously I failed because once I got one case covered, then I got a new problem on its own.

In the end I got fed up and started again from fresh, however, it was already too late and I didn’t have the time to properly check it. Also due to the ticking clock I was unable to complete the question in time. So out of the three questions, I got none of them right (the first one was a bug fix, which I skipped at first, and this post is about the second one, I didn’t have time to read the third). The test was 90 minutes long, and should be sufficient if I wasn’t stuck in the wrong strategy. Guess I chose the wrong hammer to hit the nail, and paid quite a price for that.

So the point is, I am open for a new job opportunity, if you are interested leave me a message LOLOL.

Related Posts Plugin for WordPress, Blogger...

leave your comment

name is required

email is required

have a blog?

This blog uses scripts to assist and automate comment moderation, and the author of this blog post does not hold responsibility in the content of posted comments. Please note that activities such as flaming, ungrounded accusations as well as spamming will not be entertained.

Pings

Click to change color scheme