The Weekly Challenge: Week 186

 The Weekly Challenge is a site that delivers two challenges each week. This was originally for just Perl and Raku (Raku is Perl 6), but in week 18 someone submitted a Python solution, and now you can submit any language. Note that solutions for week 186 are open until 10/16/2022. You can contribute by solving and putting the solution on GitHub. Now I will show you my solutions.

Task 1

 Task 1 is as follows:

You are given two list @a and @b of same size.

Create a subroutine sub zip(@a, @b) that merge the two list as shown in the example below.

Example

Input:  @a = qw/1 2 3/; @b = qw/a b c/;

Output: zip(@a, @b) should return qw/1 a 2 b 3 c/;

        zip(@b, @a) should return qw/a 1 b 2 c 3/;

Using list comprehensions in Python makes this quite easy:

def z(a, b): # note the different name to avoid recursion

    return [j for i in zip(a, b) for j in i]

JavaScript requires map, but if the arrays are not the same size, array b will be ignored, or if a is larger, the indices without b values are filled with undefined, although this is not a problem:

function zip(a, b) {

    return a.map((x, i) => [x, b[i]]).flat();

}

And now we have APL. Yes, an array-oriented language. And look at the size of this solution...

zip ← ,.,

Yes. That's the solution. Want an explanation? Here it goes.

The ,function stands for concatenate.

The . operator stands for inner product, and it's like a dot product, but you can change the operations, in which case both are concatenate, which results in a[1],b[1],a[2],b[2], so on, so forth.

Conclusion

 I haven't solved task 2 and never will, because you need to turn UNICODE INTO ASCII, so yeah, bye (and also don't ctrl+c ctrl+v my solutions)!

Comentários