aboutsummaryrefslogtreecommitdiff
path: root/challenge-133/iangoodnight/perl/ch-1.pl
blob: 4a2fa4e2fe07db4f32c42dc0d096dd231ef032e0 (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
#!/usr/bin/perl
# ch-1.pl

=begin comment

 * https://theweeklychallenge.org/blog/perl-weekly-challenge-133/
 *
 * Task 1 > Integer Square Root
 * ============================
 *
 * You are given a positive integer `$N`.
 *
 * Write a script to calculate the integer square root of the given number.
 *
 * Please avoid using built-in function.  Find out more about it here
 * (https://en.wikipedia.org/wiki/Integer_square_root).
 *
 * Examples
 * --------
 *
 * Input: $N = 10
 * Output: 3
 *
 * Input: $N = 27
 * Output: 5
 *
 * Input: $N = 85
 * Output: 9
 *
 * Input: $N = 101
 * Output: 10

=end comment
=cut

use strict;
use warnings;
use utf8;
use open ':std', ':encoding(UTF-8)';
use Term::ANSIColor;

################################################################################
# Our PWC Solution
################################################################################

sub return_int_sqr_root {
  my $input = shift;
  chomp $input;

  # Validate input
  $input =~ s{
    ^\s+ # Leading whitespace
    |
    \s+$ # Trailing whitespace
  }{}gmx;
  if ( !$input =~ /^\d+$/m ) {
    return;
  }

  # Crawl through squares starting with 0
  my $i = 0;
  while ( $i * $i <= $input ) {
    $i++;
  }

  # Return the highest passing number
  return --$i;
}

################################################################################
# Utilities
################################################################################

sub print_banner {
  my $message  = 'Integer Square Root Calculator';
  my $border   = q{=} x length $message;
  my $empty    = q{};
  my @elements = ( $empty, $border, $message, $border, $empty, $empty );

  return print color('cyan'), join( "\n", @elements ), color('reset');
}

################################################################################
# REPL
################################################################################

sub main {
  my $prompt =
      'Enter a positive integer (or, type "'
    . color('yellow') . 'exit'
    . color('reset')
    . '" to quit)> ';

  my $retry =
      'Try again? ('
    . color('yellow') . 'y'
    . color('reset') . q{/}
    . color('yellow') . 'n'
    . color('reset') . ')> ';

  print_banner();
  print $prompt;

  my $complete = 0;

  while ( my $line = <> ) {
    chomp $line;
    $line =~ s{
      ^\s+ # Remove leading whitespace
      |
      \s+$ # Remove trailing whitespace
    }{}gmx;

    if ( $line eq 'exit' || $line eq 'n' ) {
      print "Goodbye.\n";
      return 1;
    }
    if ( $line eq 'y' ) {
      $complete = 0;
      print $prompt;
      next;
    }
    if ( $line =~ m/^\d+$/m && !$complete ) {
      print 'Integer square root: ';
      print color('yellow'), return_int_sqr_root($line), color('reset'), "\n";
    }
    elsif ($complete) {
      print "\n", $retry;
    }
    else {
      print "Arguments must be positive integers only.\n";
    }

    $complete = 1;
    print $retry;
  }
  return 1;
}

main();