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”
Category: Chapter 4. Regexes and Grammars
๐ 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”
๐ 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”