Skip to content

Commit

Permalink
Solution of 100 Doors problem in Perl & Erlang
Browse files Browse the repository at this point in the history
  • Loading branch information
upasana-me committed Jul 18, 2015
1 parent 1932ffb commit 3cb38e5
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
75 changes: 75 additions & 0 deletions 100_Doors_Problem/Erlang/Sweet-kid/doors_100.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
-module(doors_100).
-compile(export_all).
-import(lists).

main() ->
%% false represents a closed door & true represents an open door
Doors = lists:duplicate(100, false),
Result = toggle_doors( Doors, 0 ),
io:format("Open doors:~n"),
print_open_doors( Result, 1),
OpenDoorsIndex = [ X*X || X <- lists:seq( 1, 10) ],
TestPassed = test_open_doors( Result, 1, OpenDoorsIndex ),
if TestPassed == true ->
io:format("~nTest passed~n");
true ->
io:format("~nTest failed~n")
end.

test_open_doors(_, 101, _) ->
true;

test_open_doors(Result, CurrentIndex, OpenDoorsIndex) ->
IsAnOpenDoor = lists:member( CurrentIndex, OpenDoorsIndex ),
%% index for nth should be >=1
Door = lists:nth( CurrentIndex, Result ),
if IsAnOpenDoor == true ->
if Door == true ->
test_open_doors( Result, CurrentIndex + 1, OpenDoorsIndex );
true ->
io:format("test failed for ~p~n", [CurrentIndex]),
false
end;
true ->
if Door == false ->
test_open_doors( Result, CurrentIndex + 1, OpenDoorsIndex );
true ->
io:format("test failed for ~s~n", [CurrentIndex]),
false
end
end.

print_list([], _) ->
io:format("~n");
print_list([H|T], N) ->
io:format("~p ", [H]),
if (N rem 10) == 0 ->
io:format("~n"),
print_list(T, N+1);
true ->
print_list(T, N+1)
end.

print_open_doors([], _) ->
io:format("~n");
print_open_doors([H|T], N) ->
if H == true ->
io:format("~p ",[N]),
print_open_doors(T, N+1);
true ->
print_open_doors(T, N+1)
end.

toggle_doors( Doors, 100 ) -> Doors;
toggle_doors( Doors, Turn) ->
toggle_doors( toggle_doors_per_turn( Doors, 1, Turn), Turn + 1 ).

toggle_doors_per_turn( Doors, Current, Turn ) ->
if Current*(Turn+1) > 100 ->
Doors;
true ->
ToggledDoors = lists:sublist( Doors, Current*(Turn + 1) - 1 ) ++
[ not lists:nth( Current*(Turn + 1) , Doors) ] ++
lists:sublist( Doors, Current*(Turn + 1) + 1, length( Doors ) - Current*(Turn + 1) + 1 ),
toggle_doors_per_turn( ToggledDoors, Current + 1, Turn )
end.
52 changes: 52 additions & 0 deletions 100_Doors_Problem/Perl/Sweet-kid/100_Doors.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env perl

use strict;
use warnings;

use POSIX;

sub toggle_doors {
my ($doors, $start) = @_;
for( my $i = 1; ($start*$i - 1) < 100; $i++ ) {
$doors->[ $start*$i - 1 ] = !$doors->[ $start*$i - 1 ];
}
}

sub print_open_doors {
my ($doors) = @_;
print "Open doors:\n";
for( my $i = 0; $i < 100; $i++ ) {
if( $doors->[ $i ] ) {
print ($i + 1);
print " ";
}
}
print "\n";
}

sub test_doors {
my $doors = shift;

my @expected = (0) x 100 ;

for( my $i = 1; $i <= 10; $i++ ) {
$expected[ ($i*$i) - 1 ] = 1;
}

for( my $i = 0; $i <= $#expected; $i++ ) {
if( $doors->[ $i ] != $expected[ $i ] ) {
die "test failed for index $i\n";
}
}

print "\nAll tests passed\n\n";
}

my @doors = ( 0 ) x 100;
for( my $i = 1; $i <= 100; $i++ ) {
toggle_doors( \@doors, $i );
}

print_open_doors( \@doors );
test_doors( \@doors );

0 comments on commit 3cb38e5

Please sign in to comment.