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”
Tag: Perl 6 programming language
๐ 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”