Perl Weekly Challenge 149: Fibonacci Digit Sum and Largest Square

These are some answers to the Week 149 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 January 30, 2022 at 24:00). 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: Fibonacci Digit Sum

Given an input $N, generate the first $N numbers for which the sum of their digits is a Fibonacci number.

Example:

f(20)=[0, 1, 2, 3, 5, 8, 10, 11, 12, 14, 17, 20, 21, 23, 26, 30, 32, 35, 41, 44]

Fibonacci Digit Sum in Raku

We first populate a Set (for fast look-up) with the Fibonacci numbers up to 1000. Note that we could choose a much smaller maximum value, but it doesn’t cost much and it will work with very large sequences.

Alles in Ordnung

Perl returns its hash values in a random order. Since 5.14 or so, the random order changes every time. So if you loop over your hash values, you get a different ordering each time.

for my $k (keys %hash) { }

No problem you say, I'll use sort to order my keys.

for my $k (sort keys %hash) { }

But what if you want to use a non-default order, like case-insensitive? Easy-peasy you claim.

for my $k (sort {uc ($a) cmp uc ($b)} keys %hash) { }

Now here's my problem. I'm using XS to loop through the hash, and I want to sort the keys in the hash according to the user's preference.

First Perl Steering Council elected to replace Pumpking position

The most important Perl Governance poll yet has concluded. The position of Pumpking is no more, and is instead now held by a shared 3 person Steering Council, as defined in perlgov.pod: https://github.com/Perl/perl5/pull/18357/files

To quote RJBS:

Perl has two well-defined bodies involved in its governance: a core team of a few dozen and a steering council of three people. The core team sets the rules of Perl governance, votes on membership of the two groups, and delegates substantial decision making power to the steering council. The steering council has broad authority to make decisions about the development of the Perl language, the interpreter, and all other components, systems and processes that result in new releases of the language interpreter. Right now, the core team has 25 members, although this may change over time.

The new Perl Steering Council is now:

RJBS + Neil Bowers + Sawyer X

Thanks to all of them for stepping up and donating their efforts to the Perl community. :)

Further information on these matters can be found on the p5p mailing list and the public perl core mailing lists.

Perl weekly challenge 92

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

Spoiler Alert: This weekly challenge deadline is due in a few days (December 27, 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.

I'm not a great blogger - but I will try and explain my solutions to the Perl weekly challenge each week. I always try and look for interesting solutions to the problems at hand.

Part 1

I started with what I thought was the best approach - keeping a hash of the letters in both words and if they don't match the required pattern then to return 0 otherwise return 1. But this looked rather messy. There must be a simpler, more elegant solution.

I thought how would I compare the words, and "normalisation" sprang to mind:

Perl Weekly Challenge 148: Eban Numbers and Cardano Triplets

These are some answers to the Week 148 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 January 23, 2022 at 24:00). 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: Eban Numbers

Write a script to generate all Eban Numbers <= 100.

*An Eban number is a number that has no letter ‘e’ in it when the number is spelled in English (American or British).

Example:

2, 4, 6, 30, 32 are the first 5 Eban numbers.

The task asks us to list the Eban integers smaller than or equal to 100. To start with, 100 (”one hundred”) has two ‘e’, so it is not an Eban number. So we can limit our search to all Eban Numbers < 100, so that we can limit our search to integers with one or two digits.

The Persuaders!

The Persuaders! is a particularly silly 1970s action TV series which lasted for only one season. Tony Curtis plays Danny Wilde to Roger Moore's Lord Brett Sinclair. Tony is the funny guy and Roger is the straight man. After the Persuaders, Roger Moore went on to become James Bond, and Tony Curtis went on to date Debee Ashby.

12d45aab-504d-4560-9a8b-a2d30c692f39.jpg

Although it wasn't a hit in America, The Persuaders actually became more popular in translation than in the original. One of the running gags is American Danny (Tony Curtis) making fun of snooty, toffee-nosed, upper-crust "your lordship" Roger Moore. But since the translated versions were for people who didn't know anything about British or American people, the translators invented new dialogue unrelated to the original, which ended up being more entertaining for the audience than the English dialogue. Which has nothing to do with Perl but here we go.

Perl dying? Well now I don't care

It is a bit of a long story how I got burned by bad perl internal politics.

For many years I wanted images in Pod. And many others wanted too. And of course, each time I raised this in lists and on facebook, an answer was, if you want it, go and write it yourself. I would tell that myself, the classic "patches are welcome". Until one day I said, well, now, why actually not, right? Especially that I do have experience in creating and actively using images in pod using various hacks, such as direct inclusion of html with images, and even writing a standalone POD viewer capable of showing said images.

The Twelve Days of Dancer, 2020 Edition

Need some joy this holiday season? Head over to the Dancer Advent Calendar for this year's Twelve Days of Dancer. With a little help from our friends, we've put together a 12-day mini advent calendar featuring some great software built in Dancer, some helpful tips and tricks, new features, and more.

Thanks for being such a great community, and for your continued support of Dancer! Wishing you the happiest of holiday seasons!

CromeDome (on behalf of the Dancer Core Developers)

Perl Weekly Challenge 147: Truncatable Primes and Pentagon Numbers

These are some answers to the Week 147 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 January 16, 2022 at 24:00). 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: Truncatable Prime

Write a script to generate first 20 left-truncatable prime numbers in base 10.

In number theory, a left-truncatable prime is a prime number which, in a given base, contains no 0, and if the leading left digit is successively removed, then all resulting numbers are primes.

Example:

9137 is one such left-truncatable prime since 9137, 137, 37 and 7 are all prime numbers.

Truncatable Prime in Raku

Bill & Ted's Bogus Journey

Most operating systems have a version of libpng, the library for reading and writing the PNG (portable network graphics) image format on them. Unfortunately, though, the libpng is often fairly old.

I wrote a CPAN module which links against libpng, but then trying to get the module tested with CPAN testers, a lot of bugs would happen. It was frustrating because I couldn't work out what was going wrong with the tests unless I could find out what version of libpng was installed on the testing machine.

The solution I came up with in the end was a bogus test file which merely prints the libpng version as its skip_all message. This turned out to be quite effective in working out what is going wrong as various improvements to my PNG module turn out to trip bugs in older versions of libpng.

Strawberryperl.com - https any time soon?

Throwing this one out to the wider community, if anyone can assist in adding https support to strawberryperl.com that'd be great, with browsers and corporate firewalls moving towards a stricter mode of operation. An existing issue is here, with some history for those interested. Thanks in advance.

CY's Brute-Force Take on Task 2 of PWC#089

If you want to challenge yourself on programming, especially on Perl and/or Raku, go to https://perlweeklychallenge.org, code the latest challenge, submit codes on-time (via GitHub or email).



(In a rush, sorry for the "raw" style this week.)


Perl Weekly Challenge 146: Prime Numbers and Fraction Tree

These are some answers to the Week 146 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 January 9, 2022 at 24:00). 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: 10001st Prime Number

Write a script to generate the 10001st prime number.

10001st Prime Number in Raku

Raku has a fast is-prime subroutine or method, using the Miller-Rabin algorithm, to test whether a integer is prime. We can just generate an infinite (lazy) list of prime numbers and look for the 10000st one.

use v6;

my @primes = grep { .is-prime }, (1, 2, 3, -> $a { $a + 2} ...Inf);
say @primes[10001 - 1];  # Subtract 1 because the array starts at 0

This script displays the following output:

Development community that we can see to development of Perl Part1 - C language specification creating group

The Perl core team seems to be looking for resources to help in language development.

Currently, it seems the material in python.

perlgov: the perl governance document

I feel that Perl and Python cultures are quite different.

It's also based on an improvised document in 2019 when the Python reader quit.

I can understand how envious we are when we see Python attracting users(although
I'm watching a lot of cheating at the same time).

On the other hand Perl has long been a conservative culture.

So, we can refer to the methods used by developers of languages, operating systems and tools that also have a conservative mindset.

The first thing I would like us to refer to is the method used by the group that creates C language specifications.

C language specification creating group

C89, C99, C11

Development of C language has continued some slowly, but functions that users think are lacking have been added.

Specially C99 is added good features I wanted.

It would be worthwhile to ask the C specification group how it was achieved.

I think it's less repulsive and less likely to fail if we adopt a method from a culture similar to Perl rather than a culture opposite to Perl.

Monthly Report - November

Welcome last month of the year 2020

Generally, I always look forward to festive month, December. But I don't expect it to be any different from other months, unfortunately.

With so much going on in my personal life, it is hard to focus on anything. One thing that I really miss these days are personal time. I am constantly working on it with the help of experts in the field. I try to look at the positive side of the life but I can't ignore the fact I am not giving 100% to my pet project The Weekly Challenge. Having said that I must thank the entire team for the support and encouragement in this difficult time. As of today, we entered into the 89th week. I can't wait to see when we get to the 100th week.

CY's Take on PWC#088

If you want to challenge yourself on programming, especially on Perl and/or Raku, go to https://perlweeklychallenge.org, code the latest challenges, submit codes on-time (by GitHub or email).


This blogpost is not in shortage of unanswered questions...

Task 1 Array of Product

sub myproduct {
    my @arr = @_;
    my @ans;
    my $pre_prod = 1;   # short for "previous product"
    push @arr1;
    for my $i (0..$#arr-1) {
        my $entry = $pre_prod;
        $entry *= $arr[$_for ($i+1..$#arr);
        $pre_prod *= $arr[$i];
        push @ans$entry;
    }

    return \@ans;
}
The above, I designed, is a prototype for multiplication (and division, if possible) when it is expensive to do mulitplication, such as matrices. Since I don't know much about those algorithmic knowledge, just leave the codes here for personal future digestion.
---
What I have submitted is an one-liner:
perl -e ' for $j (0..scalar @ARGV-1) {$a = 1eval {$a *= $ARGV[$_if $_ != $jfor (0..scalar @ARGV-1); print "$a "; }' 5 2 1 3 4

Perl Weekly Challenge 145: Palindromes

These are some answers to the Week 145 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 January 2, 2022 at 24:00). 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: Dot Product

This first task of this week’s challenge was covered in this blog post.

Task 2: Palindromic Tree

You are given a string $s.

Write a script to create a Palindromic Tree for the given string.

I found this blog explaining Palindromic Tree in detail.

Example 1:

Input: $s = 'redivider'
Output: r redivider e edivide d divid i ivi v

Example 2:

Input: $s = 'deific'
Output: d e i ifi f c

Example 3:

Drawing a blank with XS

I spent quite a lot of time trying to work out what this error message meant:

Error: Unterminated '#if/#ifdef/#ifndef' in Libpng.xs, line 1328

The first problem here is that line 1328 is the end of the file, so that wasn't a big help.

After spending a lot of time counting #if and #endif statements in the file over and over again, in the end I had the bright idea of looking at the actual XS output, and managed to find the problem. Apologies for quoting it in full here but I can't think of a good way to truncate it:

Perl Weekly Challenge 088

Despite the holiday week here in the U.S., I was able to tackle this week's Perl Weekly Challenge. I have to say that this week's challenge was the most satisfying for me as it allowed me to not only come up with a novel solution (for me!), but it also provided an opportunity for me to leverage two areas of Perl programming that have been a real challenge for me: recursion and references. The deadline to submit solutions for this challenge is fast approaching so if you haven't solved it yourself yet, you may want to come back to this post later.

Task 1

Task #1, "Array of Product", asks the following:

You are given an array of positive integers @N.

Write a script to return an array @M where $M[i] is the product of all elements of @N except the index $N[i].

Example 1:

CY's Take on PWC#087

If you want to challenge yourself on programming, especially on Perl and/or Raku, go to https://perlweeklychallenge.org, code the latest challenges, submit codes on-time (by GitHub or email).

After the long-haul Sudoku Task, this week we come to meet two tiny tasks.

Task 1 Longest Consecutive Sequence

It seems unavoidable for me that we have to sort the input first:

sub long_consec{
    my @list = sort {$a<=>$b@_;
    #...

Then I use a for loop and a temporary list variable @potential_max_opp

    my $max_len = 1;
    my @max_opp;

    my @potential_max_opp = ($list[0]);
    for (1..$#list) { 
        if ($list[$_-1] == $list[$_]-1) {
            push @potential_max_opp$list[$_];
        } else
        {
            if (scalar @potential_max_opp > $max_len) {
                $max_len = scalar @potential_max_opp;
                @max_opp = @potential_max_opp;
            }
            @potential_max_opp = ($list[$_]);
        }
    }

    return \@max_opp;
}

Pretty straight-forward.

Some ideas: There should be some more efficient algorithms, maybe similar to counting sort , if the range of integers is given and the integers are "dense" enough.
---

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.