[erlang-questions] Programming challenge

Zvi exta7@REDACTED
Mon Feb 18 04:50:15 CET 2008


Hi All,
I just looked on LinkedIn for Erlang jobs (out of curriousity, not that I
looking for a job) and found this one:
http://www.linkedin.com/jobs?viewJob=&jobId=469388&fromSearch=0&sik=1203301840118

Whichbriefly mention Erlang: 
"Comfortable working with at least one dynamically typed scripting language
like Ruby, Python, Erlang or Perl"

Then I saw this:

"Programming challenge: 

Without using any built in date or time functions, write a function or
method that accepts two mandatory arguments. The first argument is a string
of the format "[H]H:MM {AM|PM}" and the second argument is an integer.
Assume the integer is the number of minutes to add to the string. The return
value of the function should be a string of the same format as the first
argument. For example AddMinutes("9:13 AM", 10) would return "9:23 AM". The
exercise isn't meant to be too hard. We just want to see how you code. Feel
free to do it procedurally or in an object oriented way, whichever you
prefer. Use any language you want."

So I decided to write it in Erlang. Bellow is my code. Critique is wellcome.

Zvi.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-module(time).
-author("Zvi").

-export([add/2]).

%% add M minutes to the time string Time
add(Time, M) 
	when is_list(Time) andalso M>=0 -> 
	add(list_to_binary(Time), M);

add(<<HH:2/bytes,$:,MM:2/bytes,$\s,AMPM,$M>>, M) 
	when AMPM=:=$A orelse AMPM=:=$P ->
	MM1 = list_to_integer(binary_to_list(MM)),
	HH1 = list_to_integer(binary_to_list(HH)),
	MM2 = MM1 + M,
	HH2 = HH1 + MM2 div 60,
	MM3 = MM2 rem 60,
	HH3 = HH2 rem 12,
	AMPM2 = toggleAMPM(AMPM, HH2 div 12),
	pad0(HH3)++":"++pad0(MM3)++" "++[AMPM2]++"M";
	
add(<<_H:1/bytes,":",_MM:2/bytes," AM">> = Bin, M) -> add(<<"0",Bin/bytes>>,
M);
add(<<_H:1/bytes,":",_MM:2/bytes," PM">> = Bin, M) -> add(<<"0",Bin/bytes>>,
M).

pad0(N) when N<10 -> "0"++integer_to_list(N);
pad0(N) -> integer_to_list(N).

toggleAMPM($A,0) -> $A;
toggleAMPM($A,1) -> $P;
toggleAMPM($P,0) -> $P;
toggleAMPM($P,1) -> $A.


-- 
View this message in context: http://www.nabble.com/Programming-challenge-tp15538398p15538398.html
Sent from the Erlang Questions mailing list archive at Nabble.com.




More information about the erlang-questions mailing list