The weekly challenge 230 - Task 1: Separate Digits
1 #!/usr/bin/perl 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-230/#TASK1 3 # 4 # Task 1: Separate Digits 5 # ======================= 6 # 7 # You are given an array of positive integers. 8 # 9 # Write a script to separate the given array into single digits. 10 # 11 ## Example 1 12 ## 13 ## Input: @ints = (1, 34, 5, 6) 14 ## Output: (1, 3, 4, 5, 6) 15 # 16 ## Example 2 17 ## 18 ## Input: @ints = (1, 24, 51, 60) 19 ## Output: (1, 2, 4, 5, 1, 6, 0) 20 # 21 ############################################################ 22 ## 23 ## discussion 24 ## 25 ############################################################ 26 # 27 # Just split the integers one by one and collect the result. 28 29 use strict; 30 use warnings; 31 32 separate_digits(1, 34, 5, 6); 33 separate_digits(1, 24, 51, 60); 34 35 sub separate_digits { 36 my @ints = @_; 37 print "Input: (" . join(", ", @ints) . ")\n"; 38 my @output = (); 39 foreach my $i (@ints) { 40 push @output, split //, $i; 41 } 42 print "Output: (" . join(", ", @output) . ")\n"; 43 }