PWC 230

PWC 230

Two easy challenges this week. 

Challenge 1 (Separate digits)

We are given an array of positive integers say @int, and asked to return another array in which the elements of @int are split into their individual digits. Thus (1,23,456) should give (1,2,3,4,5,6).

One easy way to do this in Perl 5 is to combine the elements of @int into a string using 'join', and then split the string into individual characters using split.

Here is the key code snippet:

    split //, join '', @_;

Challenge 2 (Count words)

We are given an array of strings @words say, and a single string called $prefix say. We are asked to count how many of the strings in the array @words start with $prefix.

This is easy to do with grep. Here is the key snippet:

     scalar grep /^$prefix/, @words;

Comments

Popular posts from this blog

PWC 227

PWC 234

PWC 249