aboutsummaryrefslogtreecommitdiff
path: root/challenge-080/andinus/README
blob: c81b89495c7bed9cddfb79da3543341f5f405010 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
                            ━━━━━━━━━━━━━━━
                             CHALLENGE 080

                                Andinus
                            ━━━━━━━━━━━━━━━


Table of Contents
─────────────────

1. Task 1 - Smallest Positive Number Bits
.. 1. Perl
2. Task 2 - Count Candies
.. 1. Perl





1 Task 1 - Smallest Positive Number Bits
════════════════════════════════════════

  You are given unsorted list of integers `@N'.

  Write a script to find out the smallest positive number missing.


1.1 Perl
────────

  • Initial version didn't check for `1', I might have assumed that it
    was accounted for in this line `print "1\n" and exit 0 if
    $sorted[$#sorted] < 1;'.

  • This was pointed out by <https://octodon.social/@polettix> -
    <https://tilde.zone/web/statuses/104981669595493301#>

  • Program: <file:perl/ch-1.pl>

  We take input from `@ARGV', sort it & remove all inputs less than 1.
  We check if the smallest positive number is 1. We filter repeated
  inputs using `%hash'.
  ┌────
  │ my %hash;
  │ %hash = map { $_ => 1 } @ARGV;
  │
  │ my @sorted = sort { $a <=> $b } keys %hash;
  │
  │ # Print 1 if there are no positive numbers in @sorted.
  │ print "1\n" and exit 0 if $sorted[$#sorted] < 1;
  │
  │ while (my $arg = shift @sorted) {
  │     next if $arg < 1;
  │     print "1\n" and exit 0 unless $arg == 1;
  │     last;
  │ }
  └────

  Now we are sure the smallest positive number is not 1 & `@sorted'
  doesn't contain any number less than 2.

  We loop from `2 ... $sorted[$#sorted] + 1' & then over `@sorted'
  array. The first number from the array is dropped if it's equal to
  `$num'. If not then `$num' is the smallest positive number, we print
  it & exit the `MAIN' loop.

  This won't print the smallest positive number if the user passed
  consecutive set of numbers, we just add `print "$num\n"' at the end to
  cover this case.
  ┌────
  │ MAIN: foreach my $num (2 ... $sorted[$#sorted] + 1) {
  │     foreach (@sorted) {
  │         shift @sorted and next MAIN if $num == $_;
  │         print "$num\n" and last MAIN;
  │     }
  │     # Print the last element if it was a continous series of positive
  │     # numbers.
  │     print "$num\n";
  │ }
  └────


2 Task 2 - Count Candies
════════════════════════

  You are given rankings of `@N' candidates.

  Write a script to find out the total candies needed for all
  candidates. You are asked to follow the rules below:

  1. You must given at least one candy to each candidate.
  2. Candidate with higher ranking get more candies than their mmediate
     neighbors on either side.


2.1 Perl
────────

  • Program: <file:perl/ch-2.pl>

  Giving at least one day to all candidates.
  ┌────
  │ my $candies = scalar @ARGV;
  └────

  Handling first & last index, we do this outside the loop to keep it
  simple.
  ┌────
  │ $candies++ if $ARGV[0] > $ARGV[1];
  │ $candies++ if $ARGV[$#ARGV] > $ARGV[$#ARGV - 1];
  └────

  Loop handles rest of the entries.
  ┌────
  │ foreach my $index (1 ... $#ARGV - 1) {
  │     $candies++ if $ARGV[$index] > $ARGV[$index - 1];
  │     $candies++ if $ARGV[$index] > $ARGV[$index + 1];
  │ }
  │
  │ print "$candies\n";
  └────