aboutsummaryrefslogtreecommitdiff
path: root/challenge-073/jeongoon/perl/ch-2.pl
blob: 0797cde8ee2ad5720eb2ee65e5592ee74eac4996 (plain)
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
#!/usr/bin/env perl
# -*- Mode: cperl; cperl-indent-level:4 tab-width: 8; indent-tabs-mode: nil -*-
# -*- coding: utf-8 -*-

use strict; use warnings;
use boolean qw(:all);

sub unsort ( @ ) { sort { (-1,1)[(rand 1)+0.46] } @_; }
sub map_ssprintf { map { sprintf "%#$_[0]d", $_ } @_[1..$#_]; }

sub gen_smallest_( $$ ) {
    my ( $s, $c ) = @_;         # (s)mallest (c)urrent
    # return true               if we couldn't find the smallest
    #                                   because we found new one
    # return false              if we couldn't find the smallest
    # return current smallest   if we find the one
    if ( isBoolean( $s ) || $s > $c ) {
        true                        # 0         && new smallest
    }
    else {
        ( $s < $c ) ? $s            # smallest  && keep smallest
                    : false         # 0         && keep smallest
    }
}

sub processSmallest ( @ ) {
    my $smallest = boolean( not 'given' );
    map {
        my $current_is_smallest_or_just = gen_smallest_ $smallest, $_;
        isTrue( $current_is_smallest_or_just ) and $smallest = $_;
        isBoolean( $current_is_smallest_or_just ) ? 0 : $smallest;
      } @_;
}

package main;

my $S = shift;

( defined $S and $S > 4 )
  or warn "Size not given: USING ",$S = 9," !!";

$" = ' ';
my ( @A, @S );
my $W = length( 2*$S-1 ); # for sprintf

@S = processSmallest ( @A = unsort $S.. 2*$S-1 );

print "\@A = (${\"}@{[map_ssprintf($W, @A)]}${\"})\n";
print "\@S = (${\"}@{[map_ssprintf($W, @S)]}${\"})\n";