Divide and detect
Weekly challenge 230 — 14 August 2023
Week 230: 14 Aug 2023
You are given an array of positive integers. Write a script to separate the given array into single digits.
Example 1 Input: @ints = (1, 34, 5, 6) Output: (1, 3, 4, 5, 6) Example 2 Input: @ints = (1, 24, 51, 60) Output: (1, 2, 4, 5, 1, 6, 0)
This task is an easy one-liner with the use of join()
and split()
.
First we join the supplied array into a single string of digits, and then split them
into individual digits. Voilà!
#!/usr/bin/perl use v5.16; # The Weekly Challenge - 2023-08-14 use utf8; # Week 230 task 1 - Separate digits use strict; # Peter Campbell Smith use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge separate_digits(1, 34, 5, 6); separate_digits(123, 9876, 2, 99999, 0); sub separate_digits { say qq[\nInput: (] . join(', ', @_) . ')'; say qq[Output: (] . join(', ', split('', join('', @_))) . ')';; }
Input: (1, 34, 5, 6) Output: (1, 3, 4, 5, 6) Input: (123, 9876, 2, 99999, 0) Output: (1, 2, 3, 9, 8, 7, 6, 2, 9, 9, 9, 9, 9, 0)
Any content of this website which has been created by Peter Campbell Smith is in the public domain