📘 Finding duplicate texts using Perl 6

Find duplicate fragments in the same text. This task was dictated by the practical need when I realised that I used the same phrases in different parts of the text of this book. Some of them, like Hello, World!, are unavoidable, but it would be a great help to find the rest. Here is the full … Continue reading “📘 Finding duplicate texts using Perl 6”

📘 Finding the longest palindrome using Perl 6

Find the longest palindromic substring in the given string. The main idea behind the solution is to scan the string with a window of varying width. In other words, starting from a given character, test all the substrings of any length possible at that position. For the string $string, this is how the loops can be … Continue reading “📘 Finding the longest palindrome using Perl 6”

📘 Palindrome test using Perl 6

Check if the entered string is palindromic. A palindrome is a string that can be read from both ends: left to right or right to left. First, start with the simple case when the string contains only letters. (Thus, spaces and punctuation do not affect anything.) In Task 5, Reverse a string, the flipmethod is used to … Continue reading “📘 Palindrome test using Perl 6”

📘 Anagram test using Perl 6

Tell if the two words are anagrams of each other. Anagrams are words or phrases that are built out of the same letters. We start with checking words only. my $a = prompt(‘First word > ‘);my $b = prompt(‘Second word > ‘);say normalize($a) eq normalize($b)     ?? ‘Anagrams.’ !! ‘Not anagrams.’;sub normalize($word) {    return $word.split(”).sort.join(”);} The words, stored … Continue reading “📘 Anagram test using Perl 6”

📘 Finding the longest common substring using Perl 6

Find the longest common substring in the given two strings. Let us limit ourselves with finding only the first longest substring. If there are more common substrings of the same length, then the rest are ignored. There are two loops (see also Task 17, The longest palindrome) over the first string ($a), and they use the indexmethod … Continue reading “📘 Finding the longest common substring using Perl 6”

📘 Finding the most frequent word using Perl 6

Find the most frequent word in the given text. To find the most frequent word, you need first to find all the words in the text.  This can be done via the global regex m:g/(\w+)/ or by using the comb method. The method returns a list of all the matched substrings. In the following example of solving … Continue reading “📘 Finding the most frequent word using Perl 6”

📘 Plural endings using Perl 6

Put a noun in the correct form—singular or plural—depending on the number next to it. In program outputs, it is often required to print some number followed by a noun, for example: 10 files copied If there is only one file, then the phrase should be ‘1 file copied’ instead. Let’s see how Perl 6 … Continue reading “📘 Plural endings using Perl 6”

📘 Caesar cipher using Perl 6

Encode a message using the Caesar cipher technique. The Caesar code is a simple method of transcoding the letters of the message so that each letter is replaced with the letter that occurs in the alphabet N positions earlier or later. For example, if N is 4, then the letter e becomes a, f is transformed to b, etc. … Continue reading “📘 Caesar cipher using Perl 6”

📘 DNA-to-RNA transcription using Perl 6

Convert the given DNA sequence to a compliment RNA. We’ll not dig deep into the biology aspect of the problem. For us, it is important that the DNA is a string containing the four letters A, C, G, and T, and the RNA is a string of A, C, G, and U. The transformation from … Continue reading “📘 DNA-to-RNA transcription using Perl 6”

📘 Generating random passwords using Perl 6

Generate a random string that can be used as a password. One of the possible solutions looks like this: say (‘0’ .. ‘z’).pick(15).join(”); The pickmethod with no arguments takes a random element from the range of the ASCII charactersbetween 0and z. In the above example, calling pick(15) selects 15 different characters, which are then joined together using the join method. … Continue reading “📘 Generating random passwords using Perl 6”