Perl weekly challenge 97

Here are solutions to this weeks challenges from the Perl Weekly Challenge.

You can find my full code on Github

Challenge 1

You are given string $S containing alphabets A .. Z only and a number $N . Write a script to encrypt the given string $S using Caesar Cipher with left shift of size $N .

Solution


sub caesar {
  return $_[0] =~ s{([A-Z])}{chr 65+(-65-$_[1]+ord$1)%26}regex;
}

This is a simple one liner - but has some neat features - other than using "regex" for the switches, although most are important...

  • r - return value rather than substitute in original string
  • e - evaluate replace rather than use string
  • g - repeat over all characters
  • x - not needed (comments in match) - but looks good!

In the evaled replacement code - there is some clever ordering of values to reduce the need for brackets...

Perl Weekly Challenge 153: Left Factorials and Factorions

These are some answers to the Week 153 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 February 27, 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: Left Factorials

Write a script to compute Left Factorials of 1 to 10. Please refer OEIS A003422 for more information.

Expected Output:

1, 2, 4, 10, 34, 154, 874, 5914, 46234, 409114

The task specification unfortunately lacks a precise definition of left factorials. Looking at OEIS A003422, I found we could use the following recursive definition:

a(0) = 0
a(1) = 1
a(n) = n*a(n - 1) - (n - 1)*a(n - 2)

Let us Have a Productive Year of 2021

pusheen.jpg

Bye, 2020

There are 28 entries on this blog in the Year 2020.

In February, I started to join "The Weekly Challenge"(PWC in short, as initially it was called "Perl Weekly Challenge" while the name "Perl6" hadn't been replaced by Raku). Then I slowly involved in the Perl community as a beginner.

At first, my codes were messy! I have forgotten from where I heard of Perl Best Practices(by Damian Conway; btw, I have to revisit it again), but have been trying to adopt some of its pieces of advice as many as possible. And then I read more codes and decided to maintain my code more modularized and structured; in addition, I have learnt to use the unit testing package in Perl (thanks to Perl Monks).

Annual Report - 2020

Lets wipe out the memory of 2020

Am I bitter about 2020?

No, not at all. I like to find happiness in every little things in life. It is an art that I am still learning. I am writing this as a part of my monthly routine sharing last month activities. However this is special as it also has overall annual report of the year 2020.

I am embarassed looking back what I had planned at the start of the year.

Do I have any plan for 2021?

No way, I am not going to repeat my mistake. For a change, I want to see how I deal with my life without any plan.

I'm Making Headway Now

Last January there was a post on reddit which claimed that my module JSON::Parse was not only failing some of the JSON Test Suite tests, but also crashing on one of them. Anyway I should have got around to doing something about it sooner, but here are my conclusions.

First of all there was a crash on one of the files, which went something like this: [{"":[{"":[{"", repeated about 100,000 times. (The actual file is here if you really want to see it.) Investigating it using a LInode, I found that after 80,000 open brackets the stack was overflowing, causing the crash to occur. If I added a printf in the midst of my code the printf would cause the stack overflow, so it wasn't actually due to my code but just because the stack size seems to be quite small on Linux.

Perl Weekly Challenge 151: Binary tree Depth

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

Task 1: Binary Tree Depth

You are given binary tree.

Write a script to find the minimum depth.

The minimum depth is the number of nodes from the root to the nearest leaf node (node without any children).

Example 1:

Input: '1 | 2 3 | 4 5'

                1
               / \
              2   3
             / \
            4   5

Output: 2

Example 2:

Perl weekly challenge 093

This is a Raku answer for the 093 Perl Weekly Challenge . This is also my first post.

Exercise 1

Using a similar approach than James Curtis-Smith , the solution looks at points with equal slope to see if they are in a straight line. Being less literate in Raku, using classes help me to organize coding ideas.

This exercise gives me the opportunity to work with the type BagHash. The highest value of the slopes stored in a BagHash gives the number of points in a straight line. Happily, the first example in Raku documentation is for a Class Point, an example reused in this solution.

Perl weekly challenge 93

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

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

Not sure on the correct way to do this... I first looked at all the pairs of points - and looked to see what the direction was between the points. For those where the y-coordinates are different you can just use (x1-x2)/(y1-y2) to represent the slope. We also that flipping the order of the points gives the same value. For those canses where y is the same - the slop can be represented by an "infinity value" in my case i use "-"

Misusing newSVpv

I managed to cause another set of obscure bugs by misusing newSVpv. The goal of this code is to split the RGB and the alpha (transparent) part of a PNG image, for the use of the PDF::Builder module on CPAN.

Here the SV in newSVpv is "scalar value", and the "pv" means "string". My code says this:

sv = newSVpv ("", len);

and it causes the crash on Solaris and other operating systems because the above statement is a bad idea. What happens is that Perl copies "len" bytes from my string, where "len" might be a very large number, for a large PNG image, but I've given it the string "" to copy from. So Perl tries to copy from uninitialised, or possibly even inaccessible, parts of the computer's memory. I found out my error only after almost giving up, by using valgrind to look for memory errors.

The correct version of this is

Perl Weekly Challenge 150: Fibonacci Words and Square Free Integers

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 February 6, 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 Words

You are given two strings having same number of digits, $a and $b.

Write a script to generate Fibonacci Words by concatenation of the previous two strings. Finally print 51st digit of the first term having at least 51 digits.

Example:

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:

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 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.

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)

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.

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.

From a Reflection on The Weekly Challenge 092 Task 1

Update: Ben Bullock has provided script for my question.
See the comment section. Thanks Ben.

---------------------

Happy New Year!


Today's data is about

$ perl pwc092-1_isomorphic.pl '茫茫人海' '夜夜笙歌'
0

--

Seeing others' post, I have a short reflection on dated The Weekly Challenge #092 Task 1 (statements / recap ), but may lead to a hike towards a hill in Perl.

Task statement:

TASK #1 › Isomorphic Strings Submitted by: Mohammad S Anwar

You are given two strings $A and $B.

Write a script to check if the given strings are Isomorphic. Print 1 if they are otherwise 0.
Example 1:

Input: $A = "abc"; $B = "xyz"
Output: 1

Example 2:

Input: $A = "abb"; $B = "xyy"
Output: 1

Example 3:

Input: $A = "sum"; $B = "add"
Output: 0


My unsatisfactory code (distaste due to the two subroutines &verify_pattern and &learn_pattern are almost the same).

Suddenly today I want to try out whether the Unicode support is direct; sadly, no:

For a single character:

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.

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.