๐Ÿ“˜ 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”

๐Ÿ“˜ Hello, World! in Perl 6

Print โ€˜Hello, World!โ€™ There are two built-in functions in Perl 6 to print to the console: printand say. Both print their arguments, but the say routine additionally ends the output with a newline character. So, the quickest solution is to use sayand pass a string with no newlines: say ‘Hello, World!’ Another solution is to use print and include the \n character … Continue reading “๐Ÿ“˜ Hello, World! in Perl 6”