aboutsummaryrefslogtreecommitdiff
path: root/challenge-130/iangoodnight/perl/README.md
blob: 63e359f917859b42cdd7548f0af544ef7267e081 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# Perl Weekly Challenge Club - 130

I started late the last two weeks and finished JavaScript solutions for the PWC
but ran out of time before I finished my Perl Solutions.  Still, I learned a lot
in the attempts, and it was a great excuse to play with some data structures I
wasn't very familiar with.  I was glad to see the Binary Search tree show up
this week after playing with Binary trees last week, as it gave me another
chance to work a little with writing object-oriented perl (if not in my
solutions, at least in framing the problem initially).  Thanks, and great job
with the challenges team PWC!

## Task 1 > Odd Number

You are given an array of positive integers, such that all the numbers appear
an even number of times except one number.

Write a script to find that integer.

**Example 1**

```perl

@input = (2, 5, 4, 4, 5, 5, 2);

$output = 5; # as it appears 3 times in the array whereas all other numbers 2
             # and 4 appear exactly twice
```

**Example 2**

```perl

@input = (1, 2, 3, 4, 3, 2, 1, 4, 4);

$output = 4;

```

### Solution

I'm on a real declarative coding kick, so this is a little more verbose than
necessary, but hopefully, as I do more of these challenges, I'll pick up some
more perlish idioms and shortcuts.

```perl
  
sub reduce_to_odd {
  my $input_ref = shift;
  my %mapped;
  # First, we reduce our input to a hash of values and counts
  foreach my $entry (@$input_ref) {
    # If entry exists, increment count
    if (exists $mapped{$entry}) {
      $mapped{$entry}++;
      next;
    }
    # Else, initialize count
    $mapped{$entry} = 1;
  }
  # Technically, our challenge states that there will only ever be one odd input
  # count, but, just for fun, we'll design it to return all odd counts in case
  # we get improperly validated input.
  my @odd;
  foreach my $key (keys %mapped) {
    # Iterate through, pushing values with odd counts to our `odd` array
    if ($mapped{$key} % 2 != 0) {
      push(@odd, $key);
    }
  }
  my $result_count = scalar @odd;
  # Handle our possible results:
  if ($result_count == 0) {
    # Tricky input, no correct answer.
    return 'No odd counts found';
  }
  if ($result_count == 1) {
    # Input as expected, return correct answer
    return $odd[0];
  }
  # Incorrectly validated input.  Be the bigger person and just return all
  # correct answers as a reference to our `odd` array.
  return \@odd;
}

```

### ch-1.pl

Running `./ch-1.pl` tests our solution against the sample test cases found at
`../test_cases/ch-1/`.  Optionally, `./ch-1.pl` can be run with a path argument,
pointing to a test file or directory of test files (ie: `./ch-1.pl ./test`).
Test files must be formatted as comma separated lists in the format:

```
Input list
expected output

# lines beginning with '#' and empty line are ignored
# a single file can contain multiple tests

1, 2, 2, 3, 3
1

# Whitespace optional

a,a,a,a,b,b,c
c
```

#### Output

Running `./ch-1.pl` without arguments outputs our sample test results:

```
../test_cases/ch-1/case-1.txt
=====================================

Input: 2, 5, 4, 4, 5, 5, 2
Expected: 5
Result: 5
Passed ⚐


../test_cases/ch-1/case-2.txt
=====================================

Input: 1, 2, 3, 4, 3, 2, 1, 4, 4
Expected: 4
Result: 4
Passed ⚐


../test_cases/ch-1/case-3.txt
=====================================

Input: 3, 7, 7, 9, 9, 3, 2
Expected: 2
Result: 2
Passed ⚐

Input: 1, 1, 1, 1, 3
Expected: 3
Result: 3
Passed ⚐


../test_cases/ch-1/case-4.txt
=====================================

Input: 2, 7, 3, 3, 7, 7
Expected: 2, 7
Result: 2, 7
Passed ⚐


../test_cases/ch-1/case-5.txt
=====================================

Input: 1, H, H, H, 1, tree, tree
Expected: H
Result: H
Passed ⚐

Input: 😼, 😽, 😽
Expected: 😼
Result: 😼
Passed ⚐

Input: -2, -2, 10, 10, 10, 10, -1
Expected: -1
Result: -1
Passed ⚐


```

## Task 2 > Binary Search Tree

You are given a tree.  Write a script to find out if the given tree is a
`Binary Seach Tree (BST)`.  According to wikipedia, the definition of BST:

> A binary search tree is a rooted binary tree, whose internal nodes each
> store a key (and optionally, an associated value), and each had two
> distinguished sub-trees, commonly denoted left and right.  The tree
> additionally satisfies the binary seach property: the key in each node is
> greater than or equal to any key stored in the left sub-tree, and less than
> or equal to any key stored in the right sub-tree.  The leaves (final nodes)
> of the tree contain no key and have no structure to distinguish them from
> one another.

**Example 1**

```

Input:

    8
   / \
  5   9
 / \
4   6

Output: 1 as the given tree is a BST.

```

**Example 2**

```

Input:

    5
   / \
  4   7
 / \
3   6

Output: 0 as the given tree is not a BST.

```

### Solution

The real fun here was dealing with an interesting data structure, Binary Search
Tree, writing some object-oriented perl to create test trees, and figuring out a
good way to parse text into binary trees.  I had run into some obstacles
figuring out how to handle writing classes with Perl last week, but I think I am
getting the hang of it now.

```perl

sub is_BST {
  my ($tree, $node, $min, $max) = @_;
  # catch invalid input, reset at root
  if (not ($node || $min || $max)) {
    return 0 unless $tree->{'root'};
    return is_BST($tree, $tree->{'root'});
  }
  # if no node, we've reached the end of the tree.  pass
  if (($min || $max) and not (defined $node)) {
    return 1;
  }
  # Testing conveniences
  my $test = $node->{'data'};
  my $num = $test =~ /^-?\d*\.?\d+$/;
  # exceeds $max.  fail
  if (defined $max) {
    if ($num and $test >= $max) {
      return 0;
    }
    if (not($num) and $test ge $max) {
      return 0;
    }
  }
  # less then $min.  fail
  if (defined $min) {
    if ($num and $test <= $min) {
      return 0;
    }
    if (not($num) and $test le $min) {
      return 0;
    }
  }
  # recurse
  if (
    is_BST($tree, $node->{'left'}, $min, $node->{'data'}) &&
    is_BST($tree, $node->{'right'}, $node->{'data'}, $max)
  ) {
    # pass
    return 1;
  }
  # something unexpected happened, fail for safety
  return 0;
}

```

We are going to test our solution against a `Binary_tree` class that uses a
`Binary_node` class as a helper.

```perl

{
  # Binary_node class to expose `add_right` and `add_left` methods
  package Binary_node;
  # constructor
  sub new {
    my $class = shift;
    my $self = {
      data => shift
    };
    bless $self, $class;
    return $self;
  }

  sub add_left {
    my ($self, $data) = @_; 

    if (exists $self->{'left'}) {
      return 0;
    }
    $self->{'left'} = Binary_node->new($data);
    return $self;
  }

  sub add_right {
    my ($self, $data) = @_;

    if (exists $self->{'right'}) {
      return 0;
    }
    $self->{'right'} = Binary_node->new($data);
    return $self;
  }
}

{
  package Binary_tree;
  # constructor
  sub new {
    my $class = shift;
    my $data = shift;
    my $self = {
      root => undef
    };
    if (defined $data) {
      $self->{'root'} = Binary_node->new($data);
    }
    bless $self, $class;
    return $self;
  }
  # In the case where the Binary_tree is initialized without a root
  sub add_root {
    my ($self, $data) = @_;

    if (defined $data) {
      $self->{'root'} = Binary_node->new($data);
      return $self;
    }
    return 0;
  }
  # Find and return a node to assist in building our tree
  sub find_node {
    my ($self, $match, $tree) = @_;
    unless (defined $tree) {
      $tree = $self->{'root'};
    }
    if ($tree->{'data'} eq $match) {
      return $tree;
    }
    unless ($tree->{'left'} || $tree->{'right'}) {
      return 0;
    }
    return find_node($self, $match, $tree->{'left'}) ||
           find_node($self, $match, $tree->{'right'});
  }
}

```

### ch-2.pl

Running `./ch-2.pl` tests our solution against the sample test cases found at
`../test_cases/ch-2/`.  Optionally, `./ch-2.pl` can be run with a path argument,
pointing to a test file or directory of test files (ie: `./ch-2.pl ./test`). 
Test files are parsed and constructed using our `Binary_tree` class.  Figuring
out a way to parse the input was a lot of fun, but there are probably better 
ways to do it.  Basically, the input is as shown in the challenge description
with nodes attached to leaves with `/` or `\`.  The parser will read trees as
long as their leaves don't overlap their parents.  So:

```
    5
   / \
  4   7
 / \
3   6
```

...parses correctly and

```
         5
    /      \
   4          7
  /   \
3      6

```

...parses, but

```
      5
     / \
    4   7 # overlaps leaf
 / \
3   6

```

...does not.

Test cases are comprised of trees (drawn as shown in the examples above) and 
outputs formatted as:

```
Output: 0
```

...for negative tests and

```
Output: 1
```

...for positive tests.

Test files may contain multiple tests, lines beginning with `#` will be ignored.
See sample tests at `../test_cases/ch-2/` for further examples.

#### Output

Running `./ch-2.pl` without arguments outputs our sample test results:

```

../test_cases/ch-2/case-1.txt
===============================================

Input:
    8
   / \
  5   9
 / \
4   6
Expected: 1
Result: 1 as the given tree is a BST.
Passed ⚐

../test_cases/ch-2/case-2.txt
===============================================

Input:
    5
   / \
  4   7
 / \
3   6
Expected: 0
Result: 0 as the given tree is not a BST
Passed ⚐

../test_cases/ch-2/case-3.txt
===============================================

Input:
    h
   / \
  e   i
 / \
d   f
Expected: 1
Result: 1 as the given tree is a BST.
Passed ⚐

Input:
    e
   / \
  d   g
 / \
c   f
Expected: 0
Result: 0 as the given tree is not a BST
Passed ⚐

../test_cases/ch-2/case-4.txt
===============================================

Input:
      10
     /     \
  7         11
 /  \
6    8