<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ITworks &#187; test</title>
	<atom:link href="http://itworks.hu/tag/test/feed/" rel="self" type="application/rss+xml" />
	<link>http://itworks.hu</link>
	<description>Random musings in IT</description>
	<lastBuildDate>Mon, 09 Jan 2012 08:01:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Regexp fun</title>
		<link>http://itworks.hu/2009/11/25/regexp-fun/</link>
		<comments>http://itworks.hu/2009/11/25/regexp-fun/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 21:57:31 +0000</pubDate>
		<dc:creator>csak</dc:creator>
				<category><![CDATA[fun]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://itworks.hu/?p=95</guid>
		<description><![CDATA[Reading the post about useful regular expressions,  remembered what my favourite solution is to one of  the questions of the test we give to junior Java developers. The task is to write a method that takes a string as a &#8230; <a href="http://itworks.hu/2009/11/25/regexp-fun/">Read more <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Reading the post about<a href="http://www.mkyong.com/regular-expressions/10-java-regular-expression-examples-you-should-know/"> useful regular expressions</a>,  remembered what my favourite solution is to one of  the questions of the test we give to junior Java developers.</p>
<p>The task is to write a method that takes a string as a parameter and returns the acronym of the string in uppercase made up of the first letters of the words in the string. The acronym must ignore the words &#8220;the&#8221;, &#8220;of&#8221; and &#8220;and&#8221;.</p>
<p>The usual solutions are either to sequentially step through the string (Yuck!) or split it up or use a StringTokenizer class. The people usually overlook the fact, that the input strings can be padded with whitespace, or contain multiple spaces, and they usually ignore, that the keywords that are to be omitted might be found on the begining of a valid word. Thus my test &#8221; United   States of Andorra&#8221; string breaks most of the methods.  The ones who have time to write the answer down, usually forget to return the value from the method, or to change it to uppercase and sometimes even ignore that it should be a method to start with! This is my favourite question, as it can really show how the applicant can handle stressful situations.</p>
<p>I was tired after several interviews one day and tried to come up with the  shortest possible solution. Naturally it contains regular expressions.</p>
<p>My solution looked  something like this (OK I just reproduced it for the sake of the article, using nano and javac, so it might have overlooked flaws in it):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Acronym <span style="color: #009900;">&#123;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> toAcronym<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> str<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> str.<span style="color: #006633;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.
                        <span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(THE|OF|AND)(<span style="color: #000099; font-weight: bold;">\\</span>W+|$)&quot;</span>,<span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>.
                        <span style="color: #006633;">replaceAll</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(<span style="color: #000099; font-weight: bold;">\\</span>w)<span style="color: #000099; font-weight: bold;">\\</span>w*<span style="color: #000099; font-weight: bold;">\\</span>W*&quot;</span>,<span style="color: #0000ff;">&quot;$1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>args.<span style="color: #006633;">length</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>toAcronym<span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://itworks.hu/2009/11/25/regexp-fun/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

