Google Code In

perl5i: ARRAY/HASH->pick to get a random element The Perl Foundation
Status: Closed Time to complete: 48 hrs Mentors: Paul Johnson, Florian Ragwitz, Michael G Schwern Tags: perl5i, Michael G Schwern

See perl5i: ARRAY/HASH->pick to get a random element

Pick N random elements out of a list or hash (in which case its the yet to be defined Pair). Modeled on Perl 6's pick. http://svn.pugscode.org/pugs/docs/Perl6/Spec/S32-setting-library/Containers.pod

method pick ( Int $number = 1 )

pick guarantees not to return the same element twice in a single call.

What should it return? If you just want to pick one it seems silly to return a list or array ref. But I don't want to make an exception for where $number = 1. Maybe $item = @array->pick_one.

If $number is greater than the size of the list, it returns the entire list, shuffled. $number = 0 returns an empty list.

Uploaded Work
File name/URL File size Date submitted
ARRAY.pm 5.4 KB November 26 2011 20:01 UTC
pick.t 354 bytes November 27 2011 19:09 UTC
perl5i.pm 30.0 KB November 27 2011 19:18 UTC
https://github.com/schwern/perl5i/pull/216 n/a December 07 2011 01:25 UTC
Comments
Task Claimed by Prashan Dharmasena November 25 2011 01:54 UTC

I would like to work on this task.

Task Assigned by Michael G Schwern November 25 2011 22:32 UTC

This task has been assigned to Prashan Dharmasena. You have 48 hours to complete this task, good luck!

Note on the task by Michael G Schwern November 26 2011 19:34 UTC

Note that this task contains an open design question because the use case is split.  Often you want to pick a single element, in which case it should return a scalar.  But it can also return a list, in which case you must return a list... but that makes the single element case annoying.


Should it always return a list?  Should it be split into two methods?  Should it return a magic "parcel" that works as both a list and scalar?  Pick one way to implement it and discuss on the issue.

Submitted work... by Prashan Dharmasena November 26 2011 20:03 UTC

So I added a method pick() that always returns a list, whether or not it is empty or only has one element, and a method pick_one that  only returns one element.  

Ready for review by Prashan Dharmasena November 27 2011 19:18 UTC

The work on this task is ready to be reviewed.

No more Work can be submitted by Melange November 27 2011 22:32 UTC

Melange has detected that the deadline has passed and no more work can be submitted. The submitted work should be reviewed.

Deadline extended by Florian Ragwitz November 28 2011 22:36 UTC

The deadline of the task has been extended with 0 days and 12 hours.

Task Needs More Work by Michael G Schwern November 29 2011 02:17 UTC

One of the mentors has sent this task back for more work. Talk to the mentor(s) assigned to this task to satisfy the requirements needed to complete this task, submit your work again and mark the task as complete once you re-submit your work.

Deadline extended by Michael G Schwern November 29 2011 02:17 UTC

The deadline of the task has been extended with 1 days and 0 hours.

Please discuss on Github by Michael G Schwern November 29 2011 02:29 UTC

There's problems with the chosen algorithm.  It's inefficient and not guaranteed to terminate.  Please see the discussion on Github and let's comment on it there.  Work off of the issue/145 branch, I've committed your work and my fixups.  https://github.com/schwern/perl5i/issues/145#issuecomment-2911193

Deadline extended by Michael G Schwern November 30 2011 02:50 UTC

The deadline of the task has been extended with 1 days and 0 hours.

Task Reopened by Melange December 1 2011 17:43 UTC

Melange has detected that the final deadline has passed and it has reopened the task.

Still Prashan's task by Michael G Schwern December 1 2011 19:30 UTC

We let the deadline expire, but Prashan's work is still in review and revisions.  Please nobody else but Prashan claim this task.

Task Claimed by Prashan Dharmasena December 1 2011 22:19 UTC

I would like to work on this task.

Task Assigned by Michael G Schwern December 2 2011 00:52 UTC

This task has been assigned to Prashan Dharmasena. You have 48 hours to complete this task, good luck!

Initial Deadline passed by Melange December 4 2011 00:52 UTC

Melange has detected that the initial deadline has passed and it has set the task status to ActionNeeded. The student has 24 hours to submit the work before the task is reopened and sent back to the pool for other students to claim.

Task Reopened by Melange December 5 2011 00:52 UTC

Melange has detected that the final deadline has passed and it has reopened the task.

Task Claimed by Prashan Dharmasena December 5 2011 00:55 UTC

I would like to work on this task.

Task Assigned by Florian Ragwitz December 5 2011 01:51 UTC

This task has been assigned to Prashan Dharmasena. You have 48 hours to complete this task, good luck!

Ready for review by Prashan Dharmasena December 7 2011 01:25 UTC

The work on this task is ready to be reviewed.

Task Closed by Michael G Schwern December 7 2011 01:26 UTC

Congratulations, this task has been completed successfully.