aboutsummaryrefslogtreecommitdiff
path: root/challenge-050/user-person
diff options
context:
space:
mode:
authoruser-person <60802990+user-person@users.noreply.github.com>2020-03-08 15:42:51 -0400
committerGitHub <noreply@github.com>2020-03-08 15:42:51 -0400
commit635adb076fcefbe49e5aba9f017132e0c480972d (patch)
tree06a2e6e080cbaafbbb010cb12b626b3bcde33de3 /challenge-050/user-person
parent6b9b40509bc743c8c2edd318284861e8e92e93de (diff)
downloadperlweeklychallenge-club-635adb076fcefbe49e5aba9f017132e0c480972d.tar.gz
perlweeklychallenge-club-635adb076fcefbe49e5aba9f017132e0c480972d.tar.bz2
perlweeklychallenge-club-635adb076fcefbe49e5aba9f017132e0c480972d.zip
Create ch-2.pl
Diffstat (limited to 'challenge-050/user-person')
-rw-r--r--challenge-050/user-person/perl/ch-2.pl67
1 files changed, 67 insertions, 0 deletions
diff --git a/challenge-050/user-person/perl/ch-2.pl b/challenge-050/user-person/perl/ch-2.pl
new file mode 100644
index 0000000000..7abfedc00e
--- /dev/null
+++ b/challenge-050/user-person/perl/ch-2.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/env perl
+
+###########################################################################
+# script name: ch-2.pl #
+# #
+# https://github.com/user-person #
+# #
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-050/ #
+# #
+# Noble Integer #
+# You are given a list, @L, of three or more random integers between 1 #
+# and 50. A Noble Integer is an integer N in @L, such that there are #
+# exactly N integers greater than N in @L. Output any Noble Integer #
+# found in @L, or an empty list if none were found. #
+# #
+# An interesting question is whether or not there can be multiple Noble #
+# Integers in a list. #
+# #
+# For example, #
+# #
+# Suppose we have list of 4 integers [2, 6, 1, 3]. #
+# #
+# Here we have 2 in the above list, known as Noble Integer, since there #
+# are exactly 2 integers in the list i.e.3 and 6, which are greater than #
+# 2. #
+# #
+# Therefore the script would print 2. #
+# #
+###########################################################################
+
+use strict;
+use warnings;
+
+sub nobleInt {
+ my @ints = sort { $a <=> $b } @_;
+ my %seen = ();
+ for (my $i = 0; $i <= $#ints; ++$i) {
+ my $count = 0;
+ MOV:
+ for (my $j = 0; $j <= $#ints; ++$j) {
+ next MOV if $i == $j;
+ ++$count if $ints[$i] < $ints[$j];
+ }
+ $seen{ $ints[$i] }++ if $count == $ints[$i];
+ }
+ return keys %seen;
+}
+
+my $input = "[2, 6, 1, 3]";
+
+if (@ARGV) {
+ $input = "@ARGV"
+} else {
+ print "default input: $input\n";
+}
+
+$input =~ s{[][, ]+}{ }g;
+$input =~ s{\A\s+|\s+\Z}{};
+my @L = split m{ }, $input;
+
+my @nobleInteger = nobleInt @L;
+
+if ( @nobleInteger ){
+ print "Noble integer: $nobleInteger[0]\n";
+} else {
+ print "No noble integer.\n";
+}