๐Ÿ“˜ Computing the average value of an array using Perl 6

Find the average value of the given array of numbers. Calculating the average value of an array has two subtasksโ€”calculate the sum and divide it by the size of the array. So, one of the solutions can look like this: my @data = 7, 11, 34, 50, 200;say sum(@data) / @data; Here, theย sum built-in function … Continue reading “๐Ÿ“˜ Computing the average value of an array using Perl 6”

๐Ÿ“˜ Sum of the elements of an array using Perl 6

Find the sum of the elements of an array of integers. There is an array of integers: my @a = (4, 6, 8, 1, 0, 58, 1, 34, 7, 4, 2); There is no need to explicitly iterate over the elements to calculate the sum of its elements. Rather use the reduction operator: say [+] @a; … Continue reading “๐Ÿ“˜ Sum of the elements of an array using Perl 6”

๐Ÿ“˜ Exclusion of two arrays in Perl 6

From the given two arrays, find the elements of the first array which do not appear in the second one. Take two arbitrary arrays of integers: my @a = 2, 5, 7, 8, 10;my @b = 1, 3, 5, 7, 9; The program should print 2, 8, and 10. Here is a possible solution: my … Continue reading “๐Ÿ“˜ Exclusion of two arrays in Perl 6”

๐Ÿ“˜ Adding up two arrays in Perl 6

Take two arrays and create a new one whose elements are the sums of the corresponding items of the initial arrays. At first, we assume that the arrays are of the same length. In this case, the easiest way to solve the task is to use a meta-operator: my @a = 10..20;my @b = 30..40;my … Continue reading “๐Ÿ“˜ Adding up two arrays in Perl 6”

๐Ÿ“˜ Incrementing array elements in Perl 6

Increment each element in an array. In Perl 6, there is no need to explicitly iterate over an array to apply some operation to each element. Use a hyper-operator: @data>>++; Let us try it on a small array: my @data = 1..10;@data>>++;say @data; # [2 3 4 5 6 7 8 9 10 11] Theย >>++ operator is … Continue reading “๐Ÿ“˜ Incrementing array elements in Perl 6”

๐Ÿ“˜ How to randomise an array in Perl 6

Shuffle the elements of an array in random order. Arrays in Perl 6 have theย pick method, which does the work. my @a = 1..20;say@a.pick(@a); A possible output of the program looks like this: (4 18 10 15 14 8 2 11 3 12 1 6 9 19 13 7 16 17 20 5) Theย pick method … Continue reading “๐Ÿ“˜ How to randomise an array in Perl 6”

๐Ÿ“˜ How to rotate a list in Perl 6

Move all elements of an array N positions to the left or to the right. Array is a data type in Perl 6 that offers theย rotate method, which does exactly what is needed. It takes an argument that tells the length and direction of the rotation. my @a = (1, 3, 5, 7, 9, 11, … Continue reading “๐Ÿ“˜ How to rotate a list in Perl 6”

๐Ÿ“˜ How to reverse a list in Perl 6

Print the given list in reverse order. Start with an array of integer numbers. my @a = (10, 20, 30, 40, 50); To reverse the array, call the reversemethod on it. say @a.reverse; This line prints the required result: (50 40 30 20 10) Notice that the initial array stays unchanged. The reversemethod creates a new sequence … Continue reading “๐Ÿ“˜ How to reverse a list in Perl 6”

๐Ÿ“˜ Swap two values in Perl 6

Swap the values of two variables. In Perl 6, there is no need to use temporary variables to swap the values of two variables. Just use the lists on both sides of the equation: ($b, $a) = ($a, $b); Alternatively, call theย reverse method and assign the result back to the values: ($a, $b).=reverse; Consider the … Continue reading “๐Ÿ“˜ Swap two values in Perl 6”

๐Ÿ“˜ Spelling numbers using Perl 6

Write an integer number below one million in words. Human languages have many inconsistencies, especially in the most frequent constructs. Spelling numbers seems to be a simple task, but due to a number of small differences, the resulting program is quite big. The program is listed on the next page. Letโ€™s discuss the algorithm first. … Continue reading “๐Ÿ“˜ Spelling numbers using Perl 6”

๐Ÿ“˜ Convert to Roman numerals using Perl 6

Convert an integer number to a Roman numerals string. Roman numbers are not a direct translation of the decimal system. In this task, we assume that the number is not morethan 3999, which is the maximum a regular Roman number can reach. Letโ€™s use the algorithm that keeps the table of pre-calculated sequences of Roman letters … Continue reading “๐Ÿ“˜ Convert to Roman numerals using Perl 6”

๐Ÿ“˜ Compose the largest number using Perl 6

Given the list of integers, compose the largest possible number by concatenating them. This task requires working with the same data as with numbers and strings. To compose the largest possible number out of a list of integers, we need to reorder them so that the largest numbers come first. The easiest way to achieve … Continue reading “๐Ÿ“˜ Compose the largest number using Perl 6”

๐Ÿ“˜ Bit counter written in Perl 6

Count the number of bits set to 1 in a binary representation of a positive integer number. There are two approaches to this task: either treat the binary sequence as a string or count real bits in the machine representation of the number. In Perl, both approaches are fine. We restrict ourselves to the positive … Continue reading “๐Ÿ“˜ Bit counter written in Perl 6”

๐Ÿ“˜ Computing the sum of digits using Perl 6

Calculate the sum of digits of a given number. The solution of this task is based on the splitmethod that you can call on any object that is convertible to strings. The following code takes an integer number and splits it into separate characters, one per digit. Then the reduction operator adds up all the digits, … Continue reading “๐Ÿ“˜ Computing the sum of digits using Perl 6”

๐Ÿ“˜ Presenting integers as binary, octal, and hex using Perl 6

Print a given integer number in the binary, octal, and hexadecimal representations. On an integer object, call the basemethod with the corresponding number: say 42.base(2); # 101010say 42.base(8);  # 52say 42.base(16); # 2A Alternatively, use theย fmt method, which is defined for integers and accepts the formatting string in theย printfformat: my $int = 42;say $int.fmt(‘Hex: %x’); # Hex: 2asay $int.fmt(‘Oct: %o’); # … Continue reading “๐Ÿ“˜ Presenting integers as binary, octal, and hex using Perl 6”

๐Ÿ“˜ Converting binary to integer using Perl 6

Convert a binary number to a decimal integer. Perl is good at its ease of switching between numerical and string representation of the same data. This task is an example where this feature can be used. The idea is to take a binary number, treat it as a string, prepend the 0bprefix indicating the binary value, … Continue reading “๐Ÿ“˜ Converting binary to integer using Perl 6”

๐Ÿ“˜ โ€˜Guess the numberโ€™ in Perl 6

Write a program that generates a random integer number 0 through 10 and asks the user to guess it, saying if the entered value is too small or too big. First, a random number needs to be generated. In Perl 6, the randroutine can be called on an integer object, and it returns a random floating-point … Continue reading “๐Ÿ“˜ โ€˜Guess the numberโ€™ in Perl 6”

๐Ÿ“˜ All Unicode digits in Perl 6

Print all Unicode digits. Perl 6 has the best support of Unicode among the modern programming languages. When talking about digits, it is worth remembering that the Unicode standard marks as digits much more than the regular ten characters used in English, for example. Let us iterate over the whole range of codepoints and select … Continue reading “๐Ÿ“˜ All Unicode digits in Perl 6”

๐Ÿ“˜ Implementing the Monte Carlo method using Perl 6

Calculate the area of a circle and the volume of a sphere of radius 1 using the Monte Carlo method. The Monte Carlo method is a statistical method of calculating data whose formula is not known. The idea is to generate a big number of random numbers and see how many of them satisfy the … Continue reading “๐Ÿ“˜ Implementing the Monte Carlo method using Perl 6”

๐Ÿ“˜ Working with polar coordinates in Perl 6

Convert the Cartesian coordinates to polar and backward. Polar coordinates are a convenient way of representing points on a surface with the two values: distance from the centre of coordinates and the angle between the vector and the pole axis. The conversion formulae between the Cartesian and polar systems,which is valid for positiveย x andย y, are … Continue reading “๐Ÿ“˜ Working with polar coordinates in Perl 6”