Perl Weekly Challenge 75: Coin Sums and Largest Rectangle Histogram

These are some answers to the Week 75 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a few days from now (August 30, 2020). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Coins Sums

You are given a set of coins @C, assuming you have infinite amount of each coin in the set.

Write a script to find how many ways you make sum $S using the coins from the set @C.

Example:

KBOS types

After introducing KBOS I should write about the most fundamental concept in this Perl syntax extension. In fact it's so basic, you could use it even without objects.

Deprecating or Transferring Mojo::ACME

While Mojo::ACME was a fun experiment, it has several shortcomings at this point and I’ve officially stopped using it. If someone is interested in maintaining it, and if I’m sufficiently convinced of your credibility since this is a security module after all, I can hand it over. Otherwise I will be marking it as deprecated soon.

Announcing Zydeco

Technically, I already announced it, but now I've renamed it. MooX::Pression is now called Zydeco.

Moops had a memorable name, and I think the naming really helped it gain a following. MooX::Pression was just meh. So now it's Zydeco. Zydeco is a fun word and pretty short to type. It's a musical genre that blends jazz, blues, and Louisiana French Creole, and it just seemed like a good fit for a module that takes what I feel are some of the coolest features of Perl programming, and blends them together under one syntax.

Also, practising what I preach, Zydeco now has its own website.

http://zydeco.toby.ink/

Perl Weekly Challenge 74: Majority Element and FNR Character

These are some answers to the Week 74 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a few hours from now. This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Majority Element

You are given an array of integers of size $N.

Write a script to find the majority element. If none found then print -1.

Majority element in the list is the one that appears more than floor(size_of_list/2).

Example 1: Input: @A = (1, 2, 2, 3, 2, 4, 2) Output: 2, as 2 appears 4 times in the list which is more than floor(7/2).

Example 2: Input: @A = (1, 3, 1, 2, 4, 5) Output: -1 as none of the elements appears more than floor(6/2).

Majority Element in Raku

av_fetch can return NULL

If you create an array by inserting values, in the following way,

$thing{key}[10] = 1;

and then don't populate the rest of the array, a call to av_fetch in the array to retrieve values lower than the tenth one may return a NULL value.

I found this out the hard way, by segmentation faults returned from the following dereference:

https://metacpan.org/source/BKB/JSON-Create-0.24/json-create-perl.c#L1021

The important thing to do here is not to rely on av_fetch to not return nulls. I fixed the problem in JSON::Create version 0.25:

https://metacpan.org/source/BKB/JSON-Create-0.25/json-create-perl.c#L1036

I chose to populate the output JSON with the JSON value "null" if there a NULL value is returned by av_fetch:

https://metacpan.org/source/BKB/JSON-Create-0.25/json-create-perl.c#L1044

Struggle getting PDL book example to work on Windows 10

Encouraged by Enkidu I decided to write a little about my unsuccessful trial to run PDL Book example on Windows. It all started in December 2019 as I decided to learn Perl and looked for a way to create SVM (Support Vector Machine) model with it as I am able to do similar tasks in R or Python. As a side effect and to my surprise I have found that there exists such a thing as Perl Data Language (PDL) that has many modules suited for data science.

Intrigued by PDL I have installed Strawberry Perl 5.30.1.1 PDL edition. I followed first steps from PDL book and tried to install PDL::Graphics::Simple. Just ran typical installation with cpan and got an error:

Egad not more PAWs posts :(

Well back on my PAWS run again. This one might be a rather short series as I am really just looking at one Action in the Kinesis API 'SubscribeToShard'. There is an open bug for this one up on github https://github.com/pplu/aws-sdk-perl/issues/371 and one I think I can fix up fairly eaisy.

First things first, a little word on Kinesis. Well in short it touted as a very scalable real time data-stream thingy that sings dances and basically makes you line much better. Myself I do not havea use for it but it is part of the system and there is a bug so in I go.

I first had to set things up on the AWS server side with some permission etc the usualal srtuff I also had to run a number of command top build up my Kineses system to a point where I can actually use the 'SubscribeToShard'

Perl Weekly Challenge 73: Min Sliding Window and Smallest Neighbor

These are some answers to the Week 73 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a few days from now (on Aug. 16, 2020). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Min Sliding Window

You are given an array of integers @A and sliding window size $S.

Write a script to create an array of min from each sliding window.

Example:

Important Changes in YAML::PP v0.019

During the SUSE Hackweek 19 I found time to fix some bugs and make important changes in YAML::PP.

Some of these changes might break code, but I expect this will be rare.

As I see more and more CPAN modules using YAML::PP, I decided to make these changes as soon as possible.

I will explain all changes and the reasons.

Introducing KBOS

Starting even before Moose, we (in the Perl 5 world) have a plethora of Modules extending the syntax of the language with Perl 6 and more in mind. The following article sums up not only my 2 and a half cents on the subject but also an attempt to implement it. It should be of interest to anybody thinking about programming in general.

MooX::Pression — now much faster

The test suite for MooX::Pression used to run in 79 seconds on my laptop. Now it's at 10 seconds.

And no, I didn't cut out any tests — I switched from using Keyword::Declare to a combination of Keyword::Simple and PPR. (Keyword::Declare is a wrapper around Keyword::Simple and PPR, but I found out by using them directly, I could massively improve compile-time speed.)

MooX::Pression allows you to build classes and roles with multimethods, types, method signatures, and sweet, sweet, sugary syntax…

Perl Weekly Challenge 72: One-Liners for Trailing Zeros and Line Ranges

These are some answers to the Week 72 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a few hours. This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Since both tasks in this week challenge are quite simple, I decided to use only one-liners to solve each task, both in Raku and in Perl.

Task 1: Trailing Zeros

You are given a positive integer $N (<= 10).

Write a script to print number of trailing zeroes in $N!.

Example 1:

Input: $N = 10
Output: 2 as $N! = 3628800 has 2 trailing zeroes

Example 2

Input: $N = 7
Output: 1 as $N! = 5040 has 1 trailing zero

Example 3:

AgoraCart "Route 66" Version Released

AgoraCart, a website shopping cart application built with Perl, was recently released to the public in November 2019. This release marks the first major version release for the f/OSS (free open source software) version of AgoraCart in about a decade.

I have avoided any spotlight in the Perl community after negative experiences early on but at the urging of Gabor Szabo over at PerlMaven.com, I realized that I should not care if I am not the normal Perl community member/developer. As a result, announcements on Perl type groups was skipped until now. So here's to new beginnings.

To Hardcode, or Not to Hardcode: That Is the (Unit) Test-ion

In my last blog post, there was a bit of a discussion in the comments about whether data in unit tests should be hardcoded or not.  Tom Metro scored the last point with this comment:

We always coach developers to use hard coded test data to the extent practical. When writing tests you have to unlearn a lot of DRY principles. We tolerate a lot more repetition, and factor it out sparingly.

and then I said that this really required a longer discussion than blog post comments would allow.  This, then, is that longer discussion.

So, first of all, let me agree whole-heartedly with Tom’s final statement there: “We tolerate a lot more repetition, and factor it out sparingly.” Absolutely good advice.  The only problem is that, as I noted previously on my Other Blog, we humans have a tendency to want to simplify things in general, and rules and “best practices” in particular.  I don’t think that Tom was saying “always hardcode everything in your unit tests!” ... but I think some people will read it that way nonetheless.  I would like to make the argument that this is a more nuanced topic, and hopefully present some reasons why you want to consider this question very carefully.1

Perl Weekly Challenge 046: Cryptic Message & Is the Room Open?

Cryptic Message

The communication system of an office is broken and message received are not completely reliable. To send message Hello, it ended up sending these following:
H x l 4 !
c e - l o
z e 6 l g
H W l v R
q 9 m # o

Similarly another day we received a message repeatedly like below:

P + 2 l ! a t o
1 e 8 0 R $ 4 u
5 - r ] + a > /
P x w l b 3 k \
2 e 3 5 R 8 y u
< ! r ^ ( ) k 0

Write a script to decrypt the above repeated message (one message repeated 6 times).

Even without reading the hint, the idea seems clear: for each column, the output should consist of its most frequent character. As usually, to count frequency, we’ll use a hash. To find the most frequent one, we’ll use max from List::Util.

Perl Weekly Challenge 71: Peak Elements and Trim Linked List

These are some answers to the Week 71 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Task 1: Peak Elements

You are given positive integer $N (>1).

Write a script to create an array of size $N with random unique elements between 1 and 50.

In the end it should print peak elements in the array, if found.

An array element is called peak if it is bigger than it’s neighbour.

Example 1:

Array: [ 18, 45, 38, 25, 10, 7, 21, 6, 28, 48 ]
Peak: [ 48, 45, 21 ]

Example 2:

Array: [ 47, 11, 32, 8, 1, 9, 39, 14, 36, 23 ]
Peak: [ 47, 32, 39, 36 ]

The specification somewhat lacks precision, but the examples are clear enough to clarify.

Peak Elements in Raku

Increasing Perl's Visibility

Perl has some really great community websites. But a drawback to this is that the Perl community is centred around a few domain names, which means that it isn't as visible as some other languages. Most projects use github for development and CPAN for distribution, and outside those sites, they don't have much online presence.

One thing I think might help spread Perl around the web would be if different Perl projects had their own websites.

PDL: Episode VI - a New Book

The title is clickbait. I ran short of time this week and am ~~recycling~~^Wconsolidating comments, replies and thoughts. Let's talk about Books!

I would love a new PDL Book. One that's completely different from the original to maximize the surface of engagement to a new audience. As a "sequel", It would have the advantage of being able to refer the reader to the first book for longer explanations and be able to jump right into how to solve significant problems. brian d foy has just finished his Mojolicious book, so I bet he's got loads of free time on his hands. (although I remember him in the middle of writing it in 2018, so you may have to wait a bit)

Preallocating scalars

I'm using the fabulous FFI::Platypus to interface to a C routine which uses caller-allocated buffers to return data. While FFI::Platypus transparently translates Perl arrays to C arrays and back, the buffers are used only to return data, so I only need the C-to-Perl conversion and not the Perl-to-C conversion.

The first step is to efficiently allocate a buffer of a given size in Perl (the last step, converting the retuned data in the buffer to Perl, is done straightforwardly with unpack).

If you do your due diligence, you'll find a link to an old PerlMonks post, which provides the following recipe:

my $str;
vec( $str, $length, 8 ) = 0;
$str = '';

About blogs.perl.org

blogs.perl.org is a common blogging platform for the Perl community. Written in Perl with a graphic design donated by Six Apart, Ltd.