-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,17 @@ | ||
package BinarySearchTree; | ||
|
||
use Moo; | ||
use strict; | ||
use warnings; | ||
use experimental qw<signatures postderef postderef_qq>; | ||
use Feature::Compat::Class; | ||
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.26 on ubuntu-latest
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.26 on ubuntu-latest
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.22 on ubuntu-latest
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.22 on ubuntu-latest
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.24 on ubuntu-latest
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.24 on ubuntu-latest
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.30 on ubuntu-latest
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.30 on ubuntu-latest
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.38 on ubuntu-latest
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.38 on ubuntu-latest
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.28 on ubuntu-latest
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.28 on ubuntu-latest
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.40 on ubuntu-latest
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.40 on ubuntu-latest
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.32 on ubuntu-latest
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.32 on ubuntu-latest
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.20 on ubuntu-latest
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.20 on ubuntu-latest
Check failure on line 4 in exercises/practice/binary-search-tree/.meta/solutions/lib/BinarySearchTree.pm GitHub Actions / Perl 5.36 on ubuntu-latest
|
||
|
||
package BinarySearchTree::Node { | ||
use Moo; | ||
no warnings qw<experimental::signatures>; | ||
|
||
has data => ( | ||
is => 'ro', | ||
); | ||
has [qw<left right>] => ( | ||
is => 'rw', | ||
); | ||
|
||
sub set ( $self, $data ) { | ||
if ( $data > $self->data ) { | ||
if ( $self->right ) { | ||
$self->right->set($data); | ||
} | ||
else { | ||
$self->right( BinarySearchTree::Node->new( data => $data ) ); | ||
} | ||
} | ||
elsif ( $self->left ) { | ||
$self->left->set($data); | ||
} | ||
else { | ||
$self->left( BinarySearchTree::Node->new( data => $data ) ); | ||
} | ||
} | ||
}; | ||
class BinarySearchTree; | ||
|
||
has root => ( | ||
is => 'rw', | ||
); | ||
field $root : param; | ||
|
||
sub add ( $self, $data ) { | ||
$self->root->set($data); | ||
method add ($data) { | ||
$root->set($data); | ||
} | ||
|
||
sub sort ($self) { | ||
method sort () { | ||
my @sorted; | ||
my $sub; | ||
$sub = sub { | ||
|
@@ -55,8 +26,31 @@ sub sort ($self) { | |
$sub->( $node->right ); | ||
} | ||
}; | ||
$sub->( $self->root ); | ||
$sub->($root); | ||
return [@sorted]; | ||
} | ||
|
||
class BinarySearchTree::Node { | ||
field $data : reader : param; | ||
field $left : reader; | ||
field $right : reader; | ||
|
||
method set ($new_data) { | ||
if ( $new_data > $data ) { | ||
if ($right) { | ||
$right->set($new_data); | ||
} | ||
else { | ||
$right = BinarySearchTree::Node->new( data => $new_data ); | ||
} | ||
} | ||
elsif ($left) { | ||
$left->set($new_data); | ||
} | ||
else { | ||
$left = BinarySearchTree::Node->new( data => $new_data ); | ||
} | ||
} | ||
} | ||
|
||
1; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,23 @@ | ||
package BinarySearchTree; | ||
|
||
use v5.40; | ||
use Moo; | ||
|
||
package BinarySearchTree::Node { | ||
use Moo; | ||
use experimental qw<class>; | ||
|
||
has data => ( | ||
is => 'ro', | ||
); | ||
has [qw<left right>] => ( | ||
is => 'rw', | ||
); | ||
}; | ||
class BinarySearchTree; | ||
|
||
has root => ( | ||
is => 'rw', | ||
); | ||
field $root : param; | ||
|
||
sub add ($self) { | ||
method add () { | ||
|
||
# $self->root contains the initial node. | ||
# $root contains the initial node. | ||
} | ||
|
||
sub sort ($self) { | ||
method sort () { | ||
return []; | ||
} | ||
|
||
class BinarySearchTree::Node { | ||
field $data : reader : param; | ||
field $left : reader; | ||
field $right : reader; | ||
} | ||
|
||
1; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,34 @@ | ||
package Clock; | ||
|
||
use Moo; | ||
use strict; | ||
use warnings; | ||
use experimental qw<signatures postderef postderef_qq>; | ||
use Feature::Compat::Class; | ||
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.26 on ubuntu-latest
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.26 on ubuntu-latest
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.22 on ubuntu-latest
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.22 on ubuntu-latest
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.24 on ubuntu-latest
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.24 on ubuntu-latest
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.30 on ubuntu-latest
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.30 on ubuntu-latest
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.38 on ubuntu-latest
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.38 on ubuntu-latest
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.28 on ubuntu-latest
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.28 on ubuntu-latest
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.40 on ubuntu-latest
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.40 on ubuntu-latest
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.32 on ubuntu-latest
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.32 on ubuntu-latest
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.20 on ubuntu-latest
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.20 on ubuntu-latest
Check failure on line 4 in exercises/practice/clock/.meta/solutions/lib/Clock.pm GitHub Actions / Perl 5.36 on ubuntu-latest
|
||
|
||
class Clock; | ||
|
||
use POSIX (); | ||
use POSIX qw<floor>; | ||
|
||
has [qw(hour minute)] => ( | ||
is => 'rwp', | ||
default => 0, | ||
); | ||
field $hour : reader : param = 0; | ||
field $minute : reader : param = 0; | ||
|
||
sub time ($self) { | ||
return sprintf '%02d:%02d', $self->hour, $self->minute; | ||
method time { | ||
return sprintf '%02d:%02d', $hour, $minute; | ||
} | ||
|
||
sub add_minutes ( $self, $amount ) { | ||
$self->_set_minute( $self->minute + $amount ); | ||
return $self->BUILD; | ||
method add_minutes ($amount) { | ||
return $self->_set_time( $hour * 60 + $minute + $amount ); | ||
} | ||
|
||
sub subtract_minutes ( $self, $amount ) { | ||
method subtract_minutes ($amount) { | ||
return $self->add_minutes( -$amount ); | ||
} | ||
|
||
sub BUILD ( $self, @ ) { | ||
$self->_set_hour( ( $self->hour + POSIX::floor( $self->minute / 60 ) ) % 24 ); | ||
$self->_set_minute( $self->minute % 60 ); | ||
ADJUST { | ||
$self->_set_time( $hour * 60 + $minute ); | ||
} | ||
|
||
method _set_time ($amount) { | ||
$hour = floor( $amount / 60 ) % 24; | ||
$minute = $amount % 60; | ||
return $self; | ||
} | ||
|
||
|