diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-02-25 07:08:24 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-02-25 07:08:24 +0000 |
| commit | 29292a8a8192db4df994839fe7cd7c18dadab0e0 (patch) | |
| tree | e4027ca55acdd1457447a3ad5ba1fe3c0461f0dc /challenge-101 | |
| parent | 74e3fda714a9155f2d14e8453e6bc2440f4c01c9 (diff) | |
| download | perlweeklychallenge-club-29292a8a8192db4df994839fe7cd7c18dadab0e0.tar.gz perlweeklychallenge-club-29292a8a8192db4df994839fe7cd7c18dadab0e0.tar.bz2 perlweeklychallenge-club-29292a8a8192db4df994839fe7cd7c18dadab0e0.zip | |
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-101')
| -rw-r--r-- | challenge-101/ulrich-rieke/raku/ch-1.raku | 99 | ||||
| -rw-r--r-- | challenge-101/ulrich-rieke/raku/ch-2.raku | 77 |
2 files changed, 176 insertions, 0 deletions
diff --git a/challenge-101/ulrich-rieke/raku/ch-1.raku b/challenge-101/ulrich-rieke/raku/ch-1.raku new file mode 100644 index 0000000000..a674de36c4 --- /dev/null +++ b/challenge-101/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,99 @@ +use v6 ; + +sub enterArray( ) { + say "Enter an array, items separated by blanks!" ; + my $line = $*IN.get ; + my @array = $line.split( /\s+/ ) ; + return @array ; +} + +my @array = enterArray( ) ; +#we fill the array with this value which is not in @array ; +#this denotes that the field in the array wasn't visited yet ; +my $stopvalue = prompt "Enter stop value!" ; +my $len = @array.elems ; +my $dima = floor( sqrt( $len ) ) ; +if ( $len %% $dima ) { +} +else { + repeat { + $dima-- ; + } until ( $len %% $dima ) ; +} +my $dimb = $len div $dima ; +my @spiralarray ; #will hold the items in a spiral +#we fill @spiralarray with the stop value first +for (0 .. $dima - 1 ) { + my $row ; + for (0 .. $dimb - 1 ) { + $row.push( $stopvalue ) ; + } + @spiralarray.push( $row ) ; +} +my @directions = ("right" , "up", "left" , "down" ) ; +my $currentIndex = 0 ;#here we start in the array +my $row = $dima - 1 ;#we fill the last row of the spiral array +my $col = 0 ;#we start at column 0 , in the blocks, we use ++$col! +my $diri = 0 ; #index into @directions, we begin with a rightwards move +while ( $currentIndex < $len - 1) { + if ( @directions[ $diri ] eq "right" ) { + while (($col < $dimb - 1) and (@spiralarray[ $row ][$col + 1] + == $stopvalue) and ($currentIndex < $len - 1 )) { + @spiralarray[$row][ $col++ ] = @array[$currentIndex++] ; + } + if ( $currentIndex == $len - 1 ) { + if ( $col <= $dimb - 1 and @spiralarray[ $row ][$col] == + $stopvalue ) { + @spiralarray[$row][$col] = @array[ $currentIndex ] ; + } + } + } + elsif ( @directions[ $diri ] eq "down" ) { + while ( ($row < $dima - 1) and (@spiralarray[ $row + 1 ][ $col ] == + $stopvalue ) and ( $currentIndex < $len - 1 ) ) { + @spiralarray[ $row++][ $col ] = @array[$currentIndex++] ; + } + if ( $currentIndex == $len - 1 ) { + if ( $row <= $dima - 1 and @spiralarray[ $row ][ $col ] == + $stopvalue ) { + @spiralarray[$row][$col] = @array[ $currentIndex] ; + } + } + } + elsif ( @directions[ $diri ] eq "up" ) { + while ( ($row > 0 ) and ( @spiralarray[ $row - 1][$col] == + $stopvalue ) and ( $currentIndex < $len - 1 )) { + @spiralarray[$row--][$col] = @array[$currentIndex++] ; + } + if ( $currentIndex == $len - 1 ) { + if ( $row >= 0 and @spiralarray[ $row ][ $col ] == + $stopvalue ) { + @spiralarray[ $row ][ $col ] = @array[ $currentIndex] ; + } + } + } + elsif ( @directions[ $diri ] eq "left" ) { + while (($col > 0 ) and (@spiralarray[ $row ][$col - 1 ] == + $stopvalue ) and ( $currentIndex < $len - 1 ) ) { + @spiralarray[$row][$col--] = @array[$currentIndex++] ; + } + if ( $currentIndex == $len - 1 ) { + if ( $col >= 0 and @array[ $row ][ $col ] == $stopvalue ) { + @spiralarray[$row][$col] = @array[ $currentIndex ] ; + } + } + } + $diri++ ; + $diri = $diri % 4 ; +} +my @sorted = @array.sort( {~$^b.chars <=> ~$^a.chars} ) ; +my $maxwidth = @sorted[0].chars ; +for (0 .. $dima - 1 ) -> $i { + for ( 0 .. $dimb - 1 ) -> $j { + my $num = @spiralarray[$i][$j] ; + my $width = $num.Str.chars ; + print ' ' x ( $maxwidth - $width + 1) ; + print $num ; + } + print "\n" ; +} diff --git a/challenge-101/ulrich-rieke/raku/ch-2.raku b/challenge-101/ulrich-rieke/raku/ch-2.raku new file mode 100644 index 0000000000..9ab076efa6 --- /dev/null +++ b/challenge-101/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,77 @@ +use v6 ; + +sub enterPoints( ) { + my @points ; + for (1 .. 3) { + say "Enter point coordinates, separated by a blank!" ; + my $line = $*IN.get ; + my @coordinates = $line.split( /\s+/ ) ; + @points.push( [|@coordinates] ) ; + } + return @points ; +} + +sub findEquation( [$x1, $y1] , [$x2, $y2] ) { + my $m ; + my $b ; + if ( $x1 != $x2 ) { + $m = ($y1 -$y2) / ($x1 - $x2) ; + } + if ( $m.defined ) { + $b = $y1 - $m * $x1 ; + } + if ( $m.defined and $b.defined ) { + return ( $m, $b ) ; + } + else { + return ( ) ; + } +} + +my @points = enterPoints( ) ; +my $output = 0 ; +#a point lies inside the triangle if we can add 2 vectors which +#are parallel to BA and to CA so that the ratios of the vectors to +#BA ( w1 ) and to CA (w2) are both positive and add up to a number +#not larger than 1 +#point P is inside the triangle if the following equation holds true: +#P = A + w1( B - A ) + w2( C - A ) +#we need 2 equations to find w1 and w2, and we use the corresponding +#equation for the x and y coordinate for that. +#by solving this by hand, we get this for w1: +#w1 = (Ax(Cy -Ay) + (Py -Ay )(Cx - Ax)- Px(Cy -Ay )) / +# ((By -Ay )( Cx - Ax ) - (Bx -Ax ) (Cy -Ay)) +#w2 can be obtained by entering w1 into: +#w2 = (Py -Ay -w1(By -Ay)) / (Cy -Ay ) +my $w1 = (@points[0][0] * ( @points[2][1] - @points[0][1] ) + + (0 - @points[0][1] ) * (@points[2][0] - @points[0][0] )) / + ((@points[1][1] - @points[0][1]) - (@points[1][0] - @points[0][0] ) + * (@points[2][1] - @points[0][1] )) ; +my $w2 = (-@points[0][1] - $w1 * (@points[1][1] - @points[0][1])) / + (@points[2][1] - @points[0][1] ) ; +if ( $w1 >= 0 and $w2 >= 0 and ($w1 + $w2) <= 1 ) { + $output = 1 ; +} +else {#it might be on the edge and not inside ! + my @equations ; + my @result = findEquation( @points[0] , @points[1] ) ; + if ( @result ) { + @equations.push( [|@result] ) ; + } + @result = findEquation( @points[1] , @points[2] ) ; + if ( @result ) { + @equations.push( [|@result] ) ; + } + @result = findEquation( @points[0] , @points[2] ) ; + if ( @result ) { + @equations.push( [|@result] ) ; + } + for @equations -> $res { +#we have to check y = mx + b ; with y = 0 and x = 0, only b == 0 is required + if ( $res[1] == 0 ) { #it's on the edge! + $output = 1 ; + last ; + } + } +} +say $output ; |
