Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution for prob17 #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions categories/99-problems/P17-sdondley.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use v6;

=begin pod

=TITLE P17 - Split a list into two parts; the length of the first part is given.

=AUTHOR Ryan Connelly

=head1 Example

> say split-list(('a' xx 10).list, 8).perl;
(["a", "a", "a", "a", "a", "a", "a", "a"], ["a", "a"]).list

=head1 Notes

Rather than looping over every array elment which is wasteful especially for
large arrays, this solution returns two array slices from the original array.
In an edge case when the number of elements desired in the first returned array
is greater than or equal to the size of the original array, the original array
gets returned.

=end pod

sub bisect(@array, $num) {
when @array.elems <= $num { @array };
[ @array[0..^$num] ], [ @array[$num..*-1] ];
}

say bisect [1, 2, 3, 4, 5], 3;


# vim: expandtab shiftwidth=4 ft=perl6