Showing posts with label Python-Ruby. Show all posts
Showing posts with label Python-Ruby. Show all posts

Tuesday, October 21, 2008

Google DevFest Vietnam

"Hi,

Thanks for registering for Google DevFest Vietnam! We're looking forward to meeting you there. Be sure to bring your laptops, power chords, questions, and enthusiasm.

The event details are as follows:

Registration will begin at 1pm on November 4th. Please join us at that time.
The event will be hosted at Ho Chi Minh University of Technology, in the A5 conference room.

Attached is a map with directions to the Ho Chi Minh University of Technology.

See you soon!

Developer Relations, Google Inc."


khà khà, cái Hackathon này sẽ vui đây :D

Friday, October 17, 2008

Interview Question: FizzBuzz

Đây là 1 câu hỏi phỏng vấn dạng simple programming test (không dùng computer), nhằm loại bớt những người dự tuyển tuyên bố là thông thạo về 1 ngôn ngữ nào đó (nhưng thực tế rất ít viết code).

Theo tôi dạng phỏng vấn này khá hiệu quả, bởi nó cho thấy ứng viên có lập trình bằng ngôn ngữ này trong vòng 3 tháng gần đây, và cũng phần nào phản ánh suy nghĩ, phong cách lập trình của người viết.
Ít ra cũng không thuộc dạng stupid-interview-questions (cái này khi nào có dịp sẽ bàn kỹ hơn) .


Đề bài:
Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".
(Viết 1 chương trình in các con số từ 1 đến 100. Tuy nhiên ở những con số là bội số của 3 thì in ra "Fizz", bội số của 5 thì in ra "Buzz", bội số của cả 3 và 5 thì in ra "FizzBuzz" thay cho các con số đó.)


Dĩ nhiên là There's More Than One Way To Do It (TMTOWTDI, đọc là "tim-toady") , câu nói yêu thích của các lập trình viên Perl.


Ở đây tôi nêu lên vài cách giải mà theo tôi là ổn (if I were the interviewer) :

* Đơn giản: (KISS, YAGNI)
Code đơn giản không có nghĩa là code ngắn nhất, hay là phải dùng những phần tử cơ bản nhất, mà là dễ dàng đáp ứng ĐỦ yêu cầu (kết quả đúng ngay từ lần đầu chạy), trong khi trình bày ngắn gọn và dễ hiểu.

(Python)

for i in range(1, 100):
if (i % 3 == 0) and (i % 5 == 0):
print “FizzBuzz”
elif i % 3 == 0:
print “Fizz”
elif i % 5 == 0:
print “Buzz”
else:
print i

(Ruby)

1.upto(100) do |number|
if number % 15 == 0 then
puts 'FizzBuzz'
elsif number % 5 == 0 then
puts 'Fizz'
elsif number % 15 == 0 then
puts 'Buzz'
else
puts number
end
end

(Lisp)

(for (i 1 100)
(if
(= 0 (% i 15)) (set 'i "FizzBuzz")
(= 0 (% i 5)) (set 'i "Buzz")
(= 0 (% i 3)) (set 'i "Fizz"))
(println i))


* Dễ dùng lại và mở rộng: (DRY, DRW)
VD nếu như sửa Blah là bội của 4 (thay vì Fizz của 3), hoặc thêm vào FizzBuzzBlah là bội của 60 (3x4x5) , thì liệu code bạn viết lại có đỡ tốn công và vẫn giữ được các ưu điểm (dễ hiểu, chạy nhanh, ...) hay không?

(Ruby)

a = nil, 'fizz', 'buzz', 'fizzbuzz'
1.upto(100) { |a[0]| puts a[(a[0]%3 == 0 ? 1 : 0) + (a[0]%5 == 0 ? 2 : 0)] }

(Ruby - extended)
a = nil, 'Fizz', 'Buzz', 'FizzBuzz', 'Blah', 'FizzBlah', 'BlahBuzz', 'FizzBuzzBlah'
1.upto(100) { |a[0]| puts a[(a[0]%3 == 0 ? 1 : 0) + (a[0]%5 == 0 ? 2 : 0) + (a[0]%4 == 0 ? 4 : 0)] }



* Dễ nhìn, thanh thoát: (Refactoring, DRY)
Thường sau khi viết đạt yêu cầu KISS ta sẽ refactor code để củng cố YAGNI và DRY , cũng để code dễ nhìn và dễ hiểu hơn, mặc dù về performance hoặc line-of-code thì có thể không tối ưu lắm.

(Java - verbose)
public class FizzBuzz {
public static void main(String[] args) {

for(int i = 1; i <= 100; i++) {
if (((i % 3) == 0) && ((i % 5) == 0))
System.out.println("fizzbuzz");
else if ((i % 3) == 0)
System.out.println("fizz");
else if ((i % 5) == 0)
System.out.println("buzz");
else System.out.println(i);
}
//System.out.println();
}
}


(Java - shorter)

public class FizzBuzz {
public static void main(String[] args) {

for(int i = 1; i <= 100; i++) {
System.out.printf("%s\n",
i%15!=0?i%5!=0?i%3!=0 ? i :"Fizz":"Buzz":"FizzBuzz" );
}
}
}



CÒN VÀI KIỂU CODE KHÁC, THAM KHẢO CHO VUI:
(chứ phỏng vấn thiệt mà tìm tòi hướng này chắc là ít nhất cũng cả tiếng cho 1 bài cỡ FizzBuzz)

* Cực ngắn, gõ ít nhất: (one-liner, Perl-Golf)
Bạn phải đam mê lập trình thực sự mới tham gia được loại này. Cái kiểu one-liner này xuất phát từ Perl, do sự linh động, uyển chuyển và mạnh mẽ của Perl trong xử lý text của Perl. Bây giờ các lập trình viên ngôn ngữ hậu duệ của Perl (như Python, Ruby) cũng bắt đầu thích trò brainstorming này :D

(Python)
for i in range(1,101):print”FizzBuzz”[i*i%3*4:8–-i**4%5]or i

(Ruby)
1.upto(?d){|i|puts"FizzBuzz"[4&a=i*i%3*5,9-a-i**4%5]||i}

(Perl)
print$_%3?$_%5?$_:"":Fizz,$_%5?"":Buzz,"\n"for 1..100;



* Phức tạp hóa, làm rối rắm: (complicated, obfuscated)

(Csharp)
using System;

namespace FizzBuzz
{
internal class Program
{
private static void Main()
{
IFormatProvider formatProvider = new FizzBuzzFormatter();

for (int i = 1; i < = 100; i++)
Console.WriteLine(String.Format(formatProvider, “{0:FB}”, i));

Console.ReadLine();
}
}

internal class FizzBuzzFormatter : ICustomFormatter, IFormatProvider
{
public string Format(string format, object arg, IFormatProvider formatProvider)
{
if (format == null)
return String.Format(”{0}”, arg);

if (format.StartsWith(”FB”) && arg is int)
{
int val = (int) arg;
bool mod3 = val%3 == 0;
bool mod5 = val%5 == 0;

if (!mod3 && !mod5)
return arg.ToString();

string s = String.Empty;

if (mod3)
s += "Fizz";

if (mod5)
s += "Buzz";

return s;
}

return String.Format(”{0:” + format + “}”, arg);
}

public object GetFormat(Type formatType)
{
if (formatType == typeof (ICustomFormatter))
return this;
else
return null;
}
}
}


(Csharp - longer)
public abstract class Factory
{
public string ToString(char[] chars)
{
return new string(chars);
}
}

public class FizzFactory : Factory
{
private const char F = 'F';
private const char i = 'i';
private const char z = 'z';

public bool isFizz(int input)
{
if (input == 3) return true;
else if (input == 6) return true;
else if (input == 9) return true;
else if (input == 12) return true;
else if (input == 15) return true;
else if (input == 18) return true;
else if (input == 21) return true;
else if (input == 24) return true;
else if (input == 28) return true;
else if (input == 30) return true;
else if (input == 33) return true;
else if (input == 36) return true;
else if (input == 39) return true;
else if (input == 42) return true;
else if (input == 45) return true;
else if (input == 48) return true;
else if (input == 51) return true;
else if (input == 54) return true;
else if (input == 57) return true;
else if (input == 60) return true;
else if (input == 63) return true;
else if (input == 66) return true;
else if (input == 69) return true;
else if (input == 72) return true;
else if (input == 75) return true;
else if (input == 78) return true;
else if (input == 81) return true;
else if (input == 84) return true;
else if (input == 87) return true;
else if (input == 90) return true;
else if (input == 93) return true;
else if (input == 96) return true;
else if (input == 99) return true;
else return false;
}

public string GetFizz()
{
return base.ToString(new char[4] { F, i, z, z });
}
}

public class BuzzFactory : Factory
{
private const char B = 'B';
private const char u = 'u';
private const char z = 'z';

public bool isBuzz(int input)
{
if (input == 5) return true;
else if (input == 10) return true;
else if (input == 15) return true;
else if (input == 20) return true;
else if (input == 25) return true;
else if (input == 30) return true;
else if (input == 35) return true;
else if (input == 40) return true;
else if (input == 45) return true;
else if (input == 50) return true;
else if (input == 55) return true;
else if (input == 60) return true;
else if (input == 65) return true;
else if (input == 70) return true;
else if (input == 75) return true;
else if (input == 80) return true;
else if (input == 85) return true;
else if (input == 90) return true;
else if (input == 95) return true;
else if (input == 100) return true;
else return false;
}

public string GetBuzz()
{
return base.ToString(new char[4] { B, u, z, z });
}
}

public delegate void FizzBuzzWriter(string input);
public class Looper
{
private FizzFactory Fizz;
private BuzzFactory Buzz;
public event FizzBuzzWriter OnFizzBuzz;

public Looper(Factory fizzFact, Factory buzzFact)
{
Fizz = (FizzFactory) fizzFact;
Buzz = (BuzzFactory) buzzFact;
}

public void execute(int start, int finish)
{
for(int i = start; i<=finish; i++)
{
string val = String.Empty;
if (Fizz.isFizz(i)) val += Fizz.GetFizz();
if (Buzz.isBuzz(i)) val += Buzz.GetBuzz();
if (val == String.Empty) val = i.ToString();

if (OnFizzBuzz != null) OnFizzBuzz(val);
}
}

}

public class TheFizzBuzzProgram
{
public static void main(string[] args)
{
Looper l = new Looper(new FizzFactory(), new BuzzFactory());
l.OnFizzBuzz += new FizzBuzzWriter(l_OnFizzBuzz);
l.execute(1,100);
}

static void l_OnFizzBuzz(string input)
{
Console.Write(input + Environment.NewLine);
}
}



* Hiệu quả, chạy nhanh nhất: (performance, effectiveness)
Chưa test, ai có kết quả test vài trường hợp thì post lên thử nhé ^_^



Search vòng vòng Internet còn thấy thêm khá nhiều implementation với nhiều ngôn ngữ nữa, có thể kể JavaScript, ASP, Pascal, C++, VB, Groovy, Prolog, SmallTalk, Scheme, Haskell, Erlang, COBOL, bash, batch, ... thậm chí cả SQL (yay!), XSL (wow!), hợp ngữ IL (cool!), ASM86 trên Windows lẫn Linux (cooler!) , ASM51 (w00t!), etc ; nhưng nhìn chung mấy đoạn mã ở trên là đủ tiêu biểu rồi :D

Have fun !

Tuesday, October 14, 2008

Future programming language?

According to TIOBE, Java is the most popular programming language now.

Nevertheless, we can see new waves: Python, Ruby, Groovy, Scala, F#, Arc, ... And the others are still rolling .




Which one do you think will be dominant in 2012 - 2013 ?

Sunday, October 12, 2008

Tips for Unit Test

Today, I would like to recommend 12 tips for Unit Testing to every developer :) .
(A good case study for Unit Test is the Craftsman story, you can read the Vietnamese translation here: http://diendantinhoc.net/?cat=se_craftsman )



Unit Testing is one of the pillars of Agile Software Development. First introduced by Kent Beck, unit testing has found its way into the hearts and systems of many organizations. Unit tests help engineers reduce the number of bugs, hours spent on debugging, and contribute to healthier, more stable software.

In this post we look at a dozen unit testing tips that software engineers can apply, regardless of their programming language or environment.



1/ Unit Test to Manage Your Risk


2/ Write a Test Case Per Major Component


3/ Create Abstract Test Case and Test Utilities


4/ Write Smart Tests


5/ Set up Clean Environment for Each Test


6/ Use Mock Objects To Test Effectively


7/ Refactor Tests When You Refactor the Code


8/ Write Tests Before Fixing a Bug


9/ Use Unit Tests to Ensure Performance


10/ Create Tests for Concurrent Code


11/ Run Tests Continuously


12/ Have Fun Testing!


Probably the most important tip is to have fun. When I first encountered unit testing, I was sceptical and thought it was just extra work. But I gave it a chance, because smart people who I trusted told me that it's very useful.

Unit testing puts your brain into a state which is very different from coding state. It is challenging to think about what is a simple and correct set of tests for this given component.

Once you start writing tests, you'd wonder how you ever got by without them. To make tests even more fun, you can incorporate pair programming. Whether you get together with fellow engineers to write tests or write tests for each other's code, fun is guaranteed. At the end of the day, you will be comfortable knowing your system really works because your tests pass.



(Full credit of this article belongs to Alex Iskold and ReadWriteWeb)

(^o^)

Tuesday, October 07, 2008

Windows post-installation notes

For some reason I had to reinstall Windows, and I'd like to keep a minimal backup with good software. It would be interesting to find out what applications you consider essential when doing a complete re-format. So I meditated and tried to pick the best ones. Here were my 'TO DO' list:

1/ Resize the C: partition to 4 GB (to be exact, 4000.5 MB )
2/ Install Windows XP Pro :-)
3/ Install NIC driver, and setup Internet connection
4/ Install AVG (AntiVir and Avast are good, either)
5/ Start scanning, and install other drivers (not reboot yet)
6/ Setup domain/workgroup, map network drives/printers
7/ Prepare folders ('D:\usr' instead of %ProgramFiles%, 'D:\home\ninja' instead of %UserProfile%, ... )
8/ Customize the Taskbar, Start Menu, Power settings.

9/ Install Firefox 2 (FF3 sucks) and some plugins (DownThemAll, ...)
10/ Install 7-zip (alternative: IZarc)
11/ Install Unikey (tool for typing Vietnamese)
12/ Install Adobe flash player
13/ Install VLC (MPlayer is another option)
14/ Install K-lite codecs
15/ Install Foxit Reader
16/ Install StarDict
17/ Install XnView (is Picasa better? )
18/ Install Comical
19/ Install Notepad++
20/ Install FreeCommander
21/ Reboot and make a mini backup

22/ Install ImgBurn (or CDBurnerXP)
23/ Install Audacity
24/ Install CamStudio
25/ Install JRE
26/ Install OpenOffice.org (next time maybe Lotus Symphony or KingSoft)
27/ Install OpenProj
28/ Install Free Download Manager
29/ Install ThunderBird
30/ Install Skype
31/ Install Pidgin
32/ Install KVIrc
33/ Install uTorrent (Vuze is a good alternative one)
34/ Install eMule
35/ Customize Desktop, reboot and make a lite backup

36/ Install Cygwin
37/ Install VIM (Vi IMproved)
38/ Install JDK
39/ Install XAMPP
40/ Install Eclipse (perhaps EasyEclipse? )
41/ Install TortoiseSVN (not Git yet)
42/ Install WinSCP and Putty
43/ Install HeidiSQL
44/ Install DBDesigner
45/ Install ArgoUML
46/ Install Dev-Cpp
47/ Install FreeMind
48/ Install VMWare
49/ Install SQLite
50/ Install PosgreSQL
51/ Install Eclipse plugins (CheckStyle, Subversive, Aptana, PHPeclipse, ...) , Mozilla plugins (for FireFox, ThunderBird)
52/ Reboot and make teh backup !

Yeahhh, it's time for games installation !
And maybe more (GIMP, Python, Ruby, Rails, Symfony, Django, ... ) ^_^


What are your essentials after a Windows re-format?