📘 Computing leap years in Perl 6

Tell if the given year is leap or common. The algorithm for detecting whether the year is leap includes a few divisibility tests. Take an extract in the pseudocode from Wikipedia: if (year is not divisible by 4) then (it is a common year)else if (year is not divisible by 100) then (it is a leap year)else if (year is not divisible by 400) then (it is a common … Continue reading “📘 Computing leap years in Perl 6”

📘 Datetime arithmetic in Perl 6

Find the difference between the two dates. Add a given number of days to the date. The DateTime class in Perl 6 defines the + and – operators, which can be used in combination with either another DateTime object or with the Duration object. Let us first find the difference between the two given dates: my $date1 = DateTime.new(‘2017-12-31T23:59:50’);my $date2 … Continue reading “📘 Datetime arithmetic in Perl 6”

📘 Formatted date in Perl 6

Print the current date in an even better format. In Perl 6, there is a built-in DateTime class. It is equipped with a few useful methods, so there’s no need to use external modules for many standard tasks. Save the current moment in the $now variable: my $now = DateTime.now; The easiest thing is to print the … Continue reading “📘 Formatted date in Perl 6”

📘 Current date and time in Perl 6

Print current date and time as an epoch and in a human-readable format. In Perl 6, the time function returns the current time as the Unix epoch: say time; The output is something like this: 1495785518. For manipulating dates and times, use the built-in DateTime class: say DateTime.now; The date is now in a more human-readable format, although … Continue reading “📘 Current date and time in Perl 6”

📘 Basic calculator written in Perl 6

Create a program that calculates mathematical operations with two operands, for example: 4 + 5.3 or 7.8 / 3. In this task, we will only limit the solution for the simplest case with only one operation so that there are no issues with the precedence order or parentheses. Let’s first make a solution with regexes … Continue reading “📘 Basic calculator written in Perl 6”

📘 Checking balanced parentheses using Perl 6

Check if the parentheses in a given string are balanced, i. e., whether every opening parenthesis has the corresponding closing one. Let us limit the input strings with the strings containing parentheses () only and no other kinds of brackets {}, [], or <>. The text in between contains only letters and spaces. Empty parentheses are not allowed. Prepare … Continue reading “📘 Checking balanced parentheses using Perl 6”

📘 Decoding Roman numerals using Perl 6

Convert a string, with a Roman number, to a decimal number. The task is opposite to Task 46, Convert to Roman numerals, but let’s use grammars to solve it. The idea is to directly find the sequences of Roman digits that correspond to thousands, hundreds, tens, and ones. For example, as soon as the program sees LXX, … Continue reading “📘 Decoding Roman numerals using Perl 6”

📘 %Templating% engine written in Perl 6

Implement a simple templating engine, which substitutes values in placeholders of the form %name%. The objective is to create a function that takes a template string and a hash with named values and does the substitution. So, let us prepare and pass them to a function. Notice that, in Perl 6, it is possible to pass … Continue reading “📘 %Templating% engine written in Perl 6”

📘 Simple string compressor written in Perl 6

Convert a string containing repeating characters to a string, where each repetition is represented by the character and the number of its copies. For example, the original string abccccdefffffggghhi converts to the compressed string abc4def5g3h2i. my $str = ‘abccccdefffffggghhi’;$str ~~ s:g/        ( (<:alpha>) $0+ )    /{        $0[0] ~ $0.chars    }/;say $str; # abc4def5g3h2i The global replacement finds the parts of the string with … Continue reading “📘 Simple string compressor written in Perl 6”

📘 Pig Latin using Perl 6

Convert the given text to Pig Latin. Pig Latin is a pseudo-language, each word of which is derived from the corresponding English word, following a couple of simple rules: If the word starts with consonant letters (including consonant sounds represented by letter combinations such as qu), move them all to the end of the word. Append … Continue reading “📘 Pig Latin using Perl 6”