/
ch-1.pl
62 lines (40 loc) · 1.11 KB
/
ch-1.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!perl
use strict;
use warnings FATAL => qw(all);
# Challenge:
# You are given two positive numbers $N and $S.
# Write a script to list all positive numbers having exactly $N digits where sum of all digits equals to $S.
# Example
# Input:
# $N = 2
# $S = 4
# Output:
# 13, 22, 31, 40
use Getopt::Long;
use Pod::Usage;
use List::Util qw(first sum);
my $message_text = <<"OUT";
Usage: script flags string.
Flags:
--d digits in the number.
--s sum of all digits in the number.
--h read this message
OUT
my %opts = (digits => 2, sum_of_digits => 4,);
GetOptions( \%opts,
'digits|d=i',
'sum_of_digits|s=i',
'help|h'
) or die pod2usage($message_text);
die pod2usage($message_text) unless $opts{digits} and $opts{sum_of_digits};
my $min = '1' . '0' x ($opts{digits} - 1);
my $max = '9' . '9' x ($opts{digits} - 1);
my $num = $min;
while ($num <= $max )
{
last if substr($num, 0, 1) > $opts{sum_of_digits};
my @digits = split(//,$num);
$num++ and next if first { $_ > $opts{sum_of_digits} } @digits;
print $num, $/ if sum(@digits) == $opts{sum_of_digits};
$num++
}