Compare the two non-integer values approximately. Comparing non-integer numbers, which are represented as floating-point numbers is often a task that requires approximate comparison. In Perl 6, there is theย =~= operator, called theย approximately-equal operator, which checks if its operands are close enough to each other. say 1/1000 =~= 1/1001; # Falsesay 1/1E20 =~= 1 / (1E20 + … Continue reading “๐ Compare numbers approximately using Perl 6”
Category: Raku
๐ Checking odd and even numbers in Perl 6
Print the first ten odd numbers. Print the first ten even numbers. Odd numbers are those that have a remainder after division by 2. This fact can be directly exploited in filtering the numbers and printing only those that match this definition. .say if $_ % 2 for 1 .. 20; To print even numbers, … Continue reading “๐ Checking odd and even numbers in Perl 6”
๐ Computing powers of two using Perl 6
Print the first ten powers of two. The naรฏve loop for calculating powers of two can be created similar to the solution of the Task 22, Print squares: say 2 ** $_ for 0..9; It prints the values 1, 2, 4, etc. up to 512. In Perl 6, thereโs another way of generating sequences withย the defined … Continue reading “๐ Computing powers of two using Perl 6”
๐ Print squares using Perl 6
Print the squares of the numbers from 1 to 10. To print the squares for a range of numbers, a loop is required. In Perl 6, if the body of the loop is simple, you can use the postfix notation: say $_ ** 2 for 1..10; Theย $_ variable is a loop variable, which receives the … Continue reading “๐ Print squares using Perl 6”
๐ 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”