Thursday, October 09, 2008

Realizing my mistakes : Phone Interview Question and Comments

Today as I managed my blog settings, I realized two mistakes.

So let me first fix these two mistakes before you guys point out more.

1. Monitoring comments on my blog.

The comments are now monitored and I have set up email notifications for any comments posted. I apologize for not doing this earlier and I intend to reply to any comment posted. Your email address on the comment would be appreciated.

2. Lame code

Let me not repeat what I said in the post. Basically, you take source code with function calls as input and properly format it. For example,

foo (bar ( new Point(x, graph.getY()) )); should be formatted as

foo ( bar ( new Point ( x, graph.getY ( ) ) ) ) ;

Look at my earlier lame solution. Thanks to someone's comment, I realized the code I mentioned there works but is not readable and forget about maintainability. But at that time, I took the statement "i dont  care how you write the code" in the problem description too seriously. This time, again I write the same code but its better. And yet I am not using Regular Expressions. Someday I would be a great Reg Ex guy! The new code is

public static void RunSnippet()
{
string code = "foo (bar ( new Point(x, graph.getY()) ));"
;
code = Replace(code,
'('
);
code = Replace(code,
')'
);
Console.WriteLine(code);
}
public static string Replace(string code,char
ch)
{
string
[] splits = code.Split(ch);
string fCode=string
.Empty;
foreach(string split in
splits)
fCode+=
string.Format("{0} {1} "
,split,ch);
fCode = fCode.Substring(
0,fCode.Length-3
);
return
fCode.Trim();
}

No comments: