Solutions of course at http://courses.cms.caltech.edu/cs11/ in Erlang,C++,C,C#,Python

Python Implementation
-------------------------------------------------------------------
# -- Lab 1 for python
from math import sqrt
from string import upper
def welcome():
 print "Hello world of python \n"

def solve_quadEq (A,B,C):
 try:
 A+B+C
 except:
 print "Please enter all numerics\n"
 return -1
 try:
 SqrPart = ( B * B - 4 * A * C)
 except:
 print " This equation has complex roots "
 return -1
 Ans1 = (-B + SqrPart) / (2 * A)
 Ans2 = (-B - SqrPart) / (2 * A)
 return [Ans1,Ans2]

def fileLen (File):
 try:
 f = open(File,"r")
 except:
 print "Can not open file /n"
 return -1
 i = 0
 while (f.readline()) :
 i += 1
 print "Number of lines is \n"
 f.close()
 return i

def file_to_upper(File,Dest):
 try:
 F = open(File,"r")
 D = open(Dest,"w")
 except:
 print "Error in I/O file \n"
 return -1
 while (F.readline()):
 D.write(upper(F.readline()))
 F.close()
 D.close()
 return 0

Erlang Implementation 

-------------------------------------------------------------------

% -- Lab 1 for python
-module(erlangPythonLAB11).
-compile(export_all).

welcome() ->
 io:format("Hello World of Erlang~n").
solve_quadEq( A, B, C) ->
 try math:sqrt( B * B - 4 * A * C) of
 SqrPart ->
 Ans1 = (-B + SqrPart) / (2 * A),
 Ans2 = (-B - SqrPart) / (2 * A),
 {Ans1,Ans2}
 catch
 error:What -> io:format("Can not happend because of..~s~n",[What])
 end.

fileLen(File) ->
 try file:open(File,read) of
 _ -> {ok,S}= file:open(File,read),
 {ok,Dest}=file:open("Temp.xml",write),
 file_to_upper(S,Dest),
 {ok,S1}= file:open(File,read),
 getLine(S1,0)

catch
 error:What -> io:format("Can not open file ~s~n",[What])
 end.
getLine(S,I) ->
 case
 io:get_line(S,'') of
 eof -> file:close(S),
 {eof,I};
 _A->
 getLine(S,I+1)
 end.
file_to_upper(S,Dest) ->
 case io:get_line(S,'') of
 eof -> file:close(S),
 file:close(Dest),
 ok;
 A ->
 io:format(Dest,"~p,~n",[string:to_upper(A)]),
 file_to_upper(S,Dest)
 end.

Tower of Brahma (MisQuoted as Hanoi) in Erlang

----------------------------------------------------------------------------------

</pre>
-module(erlangPythonLAB24).
-compile(export_all).
move ([],[]) ->
{[],[]};
move (A,[]) ->
LastA = lists:last(A),
A1 = lists:delete(LastA,A),
{A1,[LastA]};
move ([],B) ->
{B1,A}=move(B,[]),
{A,B1};
move(A,B) ->
LastA = lists:last(A),
LastB = lists:last(B),
if LastA
B1=lists:delete(LastB,B),
A1 =lists:append(A,[LastB]),
{A1,B1};
true ->
A1=lists:delete(LastA,A),
B1 =lists:append(B,[LastA]),
{A1,B1}
end.
brahmaTower(1) ->
{[],[],[1]};
brahmaTower(P) ->
A = lists:seq(1,P),
B =[],
C = [],
brahmaTowerH(A,B,C,P,0).
brahmaTowerH([],[],_C,_Nvar,Iter) ->
io:format("Done and number of iterations taken are ~p~n",[Iter]);
brahmaTowerH(A,B,C,Nvar,Iter) ->
{A1,B1}=move(A,B),
{A2,C1} = move (A1,C),
{B2,C2} = move(B1,C1),
io:format("A is: ~p~nB is: ~p~nC is: ~p~n~n",[A2,B2,C2]),
brahmaTowerH(A2,B2,C2,Nvar,Iter+1).

Python Implementation

</pre>
def move(A,B):
 SizeA = len(A)
 SizeB = len(B)
 if SizeA !=0 and SizeB != 0:
 try:
 LastA = A[-1]
 LastB = B[-1]
 except:
 print "Can not access last elements \n"
 return -1
 if LastA<LastB:
 A.append(B.pop())
 return [A,B]
 else:
 B.append(A.pop())
 return [A,B]
 else:
 if SizeA==0:
 if SizeB==0:
 return [[],[]]
 else:
 A.append(B.pop())
 return [A,B]
 else:
 B.append(A.pop())
 return [A,B]

def brahmaTower(N):
 A = range(1,N+1)
 B =[]
 C =[]
 brahmaTowerH(A,B,C,0)

def brahmaTowerH(A,B,C,Iter):
 if len(A) == 0 and len(B) == 0:
 print "Done in ", Iter , "steps \n"
 else:
 [A1,B1] = move(A,B)
 [A2,C1] = move(A1,C)
 [B2,C2] = move(B,C)
 print "A is:",A2, "\n", "B is:",B2,"\n","C is:",C2,"\n"
 brahmaTowerH(A2,B2,C2,Iter+1)

Advertisements