From 3cb38e50045d89ec19e2128d4ad1ca508e9e30ba Mon Sep 17 00:00:00 2001 From: Upasana Date: Sun, 19 Jul 2015 02:24:28 +0530 Subject: [PATCH] Solution of 100 Doors problem in Perl & Erlang --- .../Erlang/Sweet-kid/doors_100.erl | 75 +++++++++++++++++++ 100_Doors_Problem/Perl/Sweet-kid/100_Doors.pl | 52 +++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 100_Doors_Problem/Erlang/Sweet-kid/doors_100.erl create mode 100644 100_Doors_Problem/Perl/Sweet-kid/100_Doors.pl diff --git a/100_Doors_Problem/Erlang/Sweet-kid/doors_100.erl b/100_Doors_Problem/Erlang/Sweet-kid/doors_100.erl new file mode 100644 index 00000000..ce8d23ba --- /dev/null +++ b/100_Doors_Problem/Erlang/Sweet-kid/doors_100.erl @@ -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. diff --git a/100_Doors_Problem/Perl/Sweet-kid/100_Doors.pl b/100_Doors_Problem/Perl/Sweet-kid/100_Doors.pl new file mode 100644 index 00000000..dc097cb7 --- /dev/null +++ b/100_Doors_Problem/Perl/Sweet-kid/100_Doors.pl @@ -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 ); +