📘 Increasing digits by one using Perl 6

In the given integer number, replace all the digits so that 1 becomes 2, 2 becomes 3, etc., and 9 becomes 0. This task can be approached both mathematically and string-wise. In Perl 6, regexes seem to be the best match. Although a number is an example of the numeric data type, Perl 6 allows … Continue reading “📘 Increasing digits by one using Perl 6”

📘 Separating groups of digits using Perl 6

Put commas between the three-digit groups in a big number. The task is to print an integer number, for example, 1234567890,in the form of 1,234,567,890. Here is a possible solution that uses a lot of Perl 6 facilities: $n ~~ s/<?after \d> (\d ** 3)+ $/{$0.map(‘,’ ~ *).join}/; On the top level, we’ve got a substitution s///. The … Continue reading “📘 Separating groups of digits using Perl 6”

📘 Separating digits and letters using Perl 6

In a given string that contains letters and digits, insert dashes on the borders between the digit and letter sequences. The goal of the task is to convert, for example, the string 6TGT68 to 6-TGT-68. With some modification, this task may be needed for creating the canonical form of car license plates in some countries. There are … Continue reading “📘 Separating digits and letters using Perl 6”

📘 Removing duplicated words using Perl 6

Remove repeated words from froma sentence. Repeated words are most often unintended typing mistakes. In rare cases, though, this is correct like with the word that: He said that that tree is bigger.  Anyway, let us remove the double words ignoring the grammar for now. To find if the word is repeated, a regex with variables can be … Continue reading “📘 Removing duplicated words using Perl 6”

📘 Doubling characters using Perl 6

In a given string, double each alphanumeric character and print the result. Punctuation and spaces should stay untouched. Regexes are very powerful tools for searching and replacing texts. In this task, only the alphanumeric characters are requested to be doubled. The \w character class is the perfect match to find these characters. my $string = ‘Hello, … Continue reading “📘 Doubling characters using Perl 6”

📘 Currency converter written in Perl 6

Parse the string with a currency converting request such as ‘10 EUR in USD’ and print the result. The task of understanding free text is quite complicated. For the currency conversion, we can create a simple regex that matches the most common quires. Let’s ignore the way the exchange rate data are obtained and use … Continue reading “📘 Currency converter written in Perl 6”

📘 Skipping Pod documentation in Perl 6

Create the program that copies the input text and skips the documentation in the Pod style that starts with =begin and ends with =end. Let us take a simple text containing a piece of Pod documentation: # Hello, World!=beginThis program prints a message=endsay ‘Hello, World!’; The program should read it and print everything that is not the … Continue reading “📘 Skipping Pod documentation in Perl 6”

📘 Count words using Perl 6

Count the number of words in a text. Before solving the task, let us assume that by words we mean here a sequence of alphanumeric characters, including the underscore symbol. Here is the solution: my $text = prompt(‘Text> ‘);say $text.comb(/\w+/).elems; Try it on a few test inputs: $perl6 countwords.pl Text> Hello, World;2 The program uses regexes … Continue reading “📘 Count words using Perl 6”

📘 Count vowels in a word using Perl 6

Count the number of vowel letters in the given word. Of course, we will abstract now from the difference between letters and sounds and will only count the number of vowel letters: a, e, i, o, and u. my $word = ‘Hello’;$word ~~ m:g:i/<[aeiou]>/;say $/.elems; Here, the given $word is matched against a regex that contains … Continue reading “📘 Count vowels in a word using Perl 6”

📘 Building the Pascal triangle using Perl 6

Generate the numbers of the Pascal triangle and print them. The Pascal triangle is a sequence of rows of integers. It starts with a single 1 on the top row, and each following row has one number more, starting and ending with 1, while all of the other items are the sums of the two … Continue reading “📘 Building the Pascal triangle using Perl 6”

📘 Building the product table using Perl 6

Generate and print the product table for the values from 1 to 10. The task does not say anything about how to format the output. First, let us print the results as a list with one line per one multiplication. In Perl 6, there is a cross operator X, which operates over lists and creates a … Continue reading “📘 Building the product table using Perl 6”

📘 Counting hash values in Perl 6

Having a hash, count the number of occurrences of each of its values. For example, a hash is a collection mapping a car’s license plate to the colour of the car or a passport number to the name of the street where the person lives. In the first example, the task is to count how … Continue reading “📘 Counting hash values in Perl 6”

📘 Sort hashes by parameter using Perl 6

Sort a list of hashes using data in their values. This task is commonly performed to sort items where the sortable parameter is one of the values in the hash, for example, sorting a list of people by age. my @people = (    {        name => ‘Kevin’, age => 20,    },    . . .    {        name => ‘Amanda’, age => 19,    },);@people.sort({    %^a<age> … Continue reading “📘 Sort hashes by parameter using Perl 6”

📘 How to transpose a matrix in Perl 6

Take a matrix and print its transposed version. A matrix can be represented by nested arrays or lists. For example, here’s a square 2×2 matrix: my @matrix = [1, 2],             [3, 4]; This is how the transposed matrix should look: [[1, 3],[2, 4]] Actually, the outer pair of square brackets, could be added to the initializer of the @matrix variable. … Continue reading “📘 How to transpose a matrix in Perl 6”

📘 Variadic parameters in a sub in Perl 6

Pass a few scalars to a sub and work with them as with an array inside the sub. The task is to take a few scalar parameters and pass them to a single array in the subroutine.  Here is an example of how to do that, prefixing an array name with a star: sub h($sep, … Continue reading “📘 Variadic parameters in a sub in Perl 6”

📘 Passing arrays to subroutines in Perl 6

Pass data, contained in an array, to a subroutine. In Perl 6, an array can be passed to a subroutine as easily as a scalar. You simply define it in a signature and pass it together with other arguments. my @colours = <red green blue>;sub f(@data, $sep) {    @data.join($sep).say;}f(@colours, ‘, ‘); # Prints: red, green, blue The @colours array … Continue reading “📘 Passing arrays to subroutines in Perl 6”

📘 Increasing sequences in Perl 6

Check if the given array contains increasing (or decreasing) numbers. Given the list of numbers in an array, the task is to tell if all of them are sorted in ascending or descending order. Take an array: my @data = 3, 7, 19, 20, 34; In Perl 6, reduction operators offer a very expressive and … Continue reading “📘 Increasing sequences in Perl 6”

📘 Finding minimum and maximum using Perl 6

Find the minimum and the maximum numbers in the given list of integers. Finding the minimum and maximum elements of arrays is extremely easy in Perl 6. For iterable objects, the two methods, min and max, are defined. my @list = 7, 6, 12, 3, 4, 10, 2, 5, 15, 6, 7, 8, 9, 3;say @list.min;say @list.max; For the … Continue reading “📘 Finding minimum and maximum using Perl 6”

📘 Finding unique elements using Perl 6

Print all unique elements of the given array. In Perl 6, objects of the Array type have the unique method. my @a = 2, 3, 7, 4, 5, 5, 6, 2, 10, 7;say @a.unique; The result of running this program is a sequence containing the unique elements: (2 3 7 4 5 6 10). Notice that the values … Continue reading “📘 Finding unique elements using Perl 6”

📘 Counting the number of occurrences in array using Perl 6

Count how many times a particular element appears in the array. Let’s have an array with a list of fruits; some of them are repeated. my @data = <     apple       pear      grape     lemon     peach       apple      banana   grape      pineapple  avocado >; Now, the task is to count how many times the element ‘grape’ appears there. First, extract all the elements to a temporary array … Continue reading “📘 Counting the number of occurrences in array using Perl 6”