## π Computing Fibonacci numbers using Perl 6

Print the Nth Fibonacci number. Fibonacci numbers are defined by the recurring formula: fn = fn-1 + fn-2. In Perl, you can assign two values at a time (see Task 48, Swap two values). You can use that technique for calculating the next Fibonacci number from the previous two. To bootstrap the algorithm, the two first … Continue reading “π Computing Fibonacci numbers using Perl 6”

## π Computing factorial! using Perl 6

Print the factorial of a given number. By definition, the factorial of a positive integer number N is a product of all the integers numbering from 1 to N, including N. In Perl 6, this can be easily expressed with the use of a reduction operator: my \$n = 5;my \$f = [*] 1 .. \$n;say \$f; The … Continue reading “π Computing factorial! using Perl 6”

## π The number Ο in Perl 6

Print the value of Ο. The value of  is accessible with no additional modules: say Ο; This instruction, not a surprise, prints the desired value: 3.14159265358979 As you may have noticed, a non-ASCII character was used in the code. Perl 6 assumes that the source code is encoded as UTF-8 by default. Instead, a non-Unicode version can be … Continue reading “π The number Ο in Perl 6”

## π 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”

## π Incrementing filenames using Perl 6

Generate a list of filenames like file1.txt, file2.txt, etc. Perl 6 allows incrementing those kinds of filenames directly: my \$filename = ‘file0.txt’;for 1..5 {   \$filename++;    say \$filename;} This program prints the list of consequent filenames: file1.txtfile2.txtfile3.txtfile4.txtfile5.txt Notice that after reaching 9, the e letter from file is incremented. Thus, file9.txt is followed by filf0.txt. To prevent that, add enough zeros in … Continue reading “π Incrementing filenames using Perl 6”

## π Camel case using Perl 6

Create a camel-case identifier from a given phrase. It is a good practice to follow some pattern when choosing names for variables, functions, and classes in any programming language. In Perl 6, identifiers are case-sensitive, and, unlike many other languages, hyphens are allowed. So, variables names like \$max-span or function names like celsius-to-fahrenheit are accepted. In this … Continue reading “π Camel case using Perl 6”

## π Removing blanks from a string using Perl 6

Remove leading, trailing and double spaces from a given string. This task often occurs when you need to clean the user input, such as from web forms, where leading or trailing spaces in, for example, names, are most likely user mistakes and should be removed. Removing double and multiple spaces between words can be solved … Continue reading “π Removing blanks from a string using Perl 6”

## π Reverse a string using Perl 6

Print a string in the reversed order from right to left. Strings, or the objects of the Str class, have the flip method, which does the work: my \$string = ‘Hello, World!’;say \$string.flip; This code prints the desired result: !dlroW ,olleH TheΒ flip routine may be called both as a method on a string and as a stand-alone … Continue reading “π Reverse a string using Perl 6”

## π Finding unique digits using Perl 6

Print unique digits from a given integer number. The task is easily solved if an integer is immediately converted to a string. my \$number = prompt(‘Enter number> ‘);say \$number.comb.unique.sort.join(‘, ‘); The combmethod, called with no arguments, splits the string into separate characters. The method is defined in the Str class; thus, the \$number is converted to the string … Continue reading “π Finding unique digits using Perl 6”

## π String length in Perl 6

Print the length of a string. Perl 6 handles all strings as UTF-8 by default. This is why there is more than one parameter describing the length of the string. In fact, the lengthroutine does not exist, and an attempt to use it issues an error message with some hints to which other methods you can … Continue reading “π String length in Perl 6”

## π Greet a person using Perl 6

Ask a user for their name and greet them by printing βHello, <Name>!β Perl 6 offers a simple promptfunction that performs both actions: prints a prompt and reads the input. So, the program using it may look like this: say ‘Hello, ‘ ~ prompt(‘Enter your name: ‘) ~ ‘!’; The ~operator stands for string concatenation in Perl 6. Donβt … Continue reading “π Greet a person using Perl 6”