aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-04 22:26:09 +0100
committerGitHub <noreply@github.com>2020-10-04 22:26:09 +0100
commitc6d2624274ad79aa421c79853b888b6785f22ef1 (patch)
tree8baeff1c68fe2041e1990651f0b4197ab1989d11
parent7a840bf970054c0104b39d6eaf0fb1cfeea21d44 (diff)
parentcc83fef900af85964861dd02f6fc6ce55ec6dce9 (diff)
downloadperlweeklychallenge-club-c6d2624274ad79aa421c79853b888b6785f22ef1.tar.gz
perlweeklychallenge-club-c6d2624274ad79aa421c79853b888b6785f22ef1.tar.bz2
perlweeklychallenge-club-c6d2624274ad79aa421c79853b888b6785f22ef1.zip
Merge pull request #2448 from adamcrussell/challenge-080
solutions for challenge 080
-rw-r--r--challenge-080/adam-russell/blog.txt1
-rw-r--r--challenge-080/adam-russell/cxx/ch-1.cxx25
-rw-r--r--challenge-080/adam-russell/cxx/ch-2.cxx30
-rw-r--r--challenge-080/adam-russell/perl/ch-1.pl31
-rw-r--r--challenge-080/adam-russell/perl/ch-2.pl37
-rw-r--r--challenge-080/adam-russell/prolog/ch-1.p24
-rw-r--r--challenge-080/adam-russell/prolog/ch-2.p36
7 files changed, 184 insertions, 0 deletions
diff --git a/challenge-080/adam-russell/blog.txt b/challenge-080/adam-russell/blog.txt
new file mode 100644
index 0000000000..4b31d71a01
--- /dev/null
+++ b/challenge-080/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/2020/10/04#pwc080
diff --git a/challenge-080/adam-russell/cxx/ch-1.cxx b/challenge-080/adam-russell/cxx/ch-1.cxx
new file mode 100644
index 0000000000..c13f1c8c43
--- /dev/null
+++ b/challenge-080/adam-russell/cxx/ch-1.cxx
@@ -0,0 +1,25 @@
+#include <iostream>
+#include <algorithm>
+#include <vector>
+/*
+* You are given an unsorted list of integers @N.
+* Write a script to find out the smallest positive number missing.
+*/
+int least_missing(int n[]){
+ std::sort (n, n + 4);
+ std::vector<int> numbers (n, n + 4);
+ for(int i = n[0]; i < n[3]; i++){
+ std::vector<int>::iterator itr = std::find(numbers.begin(), numbers.end(), i);
+ if (itr == std::end(numbers)) {
+ if(i > 0)
+ return i;
+ }
+ }
+ return -1;
+}
+
+int main(int argc, char** argv){
+ int N[4] = {5, 2, -2, 0};
+ int i = least_missing(N);
+ std::cout << "the least positive missing number is " << i << std::endl;
+} \ No newline at end of file
diff --git a/challenge-080/adam-russell/cxx/ch-2.cxx b/challenge-080/adam-russell/cxx/ch-2.cxx
new file mode 100644
index 0000000000..f3e26491c9
--- /dev/null
+++ b/challenge-080/adam-russell/cxx/ch-2.cxx
@@ -0,0 +1,30 @@
+#include <iostream>
+/*
+* 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:
+* a) You must given at least one candy to each candidate.
+* b) Candidate with higher ranking get more candies than their immediate
+* neighbors on either side.
+*/
+int count_candies(int candidates[]){
+ int candies = 4;
+ for(int i = 0; i < 3; i++){
+ if((i - 1) >= 0){
+ if(candidates[i] > candidates[i - 1]){
+ candies++;
+ }
+ }
+ if((i + 1) < 4){
+ if(candidates[i] > candidates[i + 1]){
+ candies++;
+ }
+ }
+ }
+ return candies;
+}
+int main(int argc, char** argv){
+ int N[4] = {1, 4, 3, 2};
+ int i = count_candies(N);
+ std::cout << "the number of candies is " << i << std::endl;
+} \ No newline at end of file
diff --git a/challenge-080/adam-russell/perl/ch-1.pl b/challenge-080/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..2b1d643b9b
--- /dev/null
+++ b/challenge-080/adam-russell/perl/ch-1.pl
@@ -0,0 +1,31 @@
+use strict;
+use warnings;
+##
+# You are given an unsorted list of integers @N.
+# Write a script to find out the smallest positive number missing.
+##
+sub least_missing{
+ my(@numbers) = @_;
+ @numbers = sort @numbers;
+ for my $i ($numbers[0] .. $numbers[@numbers - 1]){
+ my @a = grep { $_ == $i } @numbers;
+ return $i if(!@a && $i > 0);
+ }
+ return undef;
+}
+
+MAIN:{
+ my @N;
+ @N = (5, 2, -2, 0);
+ my $least_missing = least_missing(@N);
+ print "The least mising number from (" .
+ join(",", @N) . ") is $least_missing\n";
+ @N = (1, 8, -1);
+ $least_missing = least_missing(@N);
+ print "The least mising number from (" .
+ join(",", @N) . ") is $least_missing\n";
+ @N = (2, 0, -1);
+ $least_missing = least_missing(@N);
+ print "The least mising number from (" .
+ join(",", @N) . ") is $least_missing\n";
+} \ No newline at end of file
diff --git a/challenge-080/adam-russell/perl/ch-2.pl b/challenge-080/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..84a4f4d2ea
--- /dev/null
+++ b/challenge-080/adam-russell/perl/ch-2.pl
@@ -0,0 +1,37 @@
+use strict;
+use warnings;
+##
+# 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:
+# a) You must given at least one candy to each candidate.
+# b) Candidate with higher ranking get more candies than their immediate
+# neighbors on either side.
+##
+sub count_candies{
+ my(@candidates) = @_;
+ my $candies = @candidates;
+ for my $i (0 .. (@candidates - 1)){
+ if($candidates[$i - 1]){
+ $candies++ if $candidates[$i] > $candidates[$i - 1];
+ }
+ if($candidates[$i + 1]){
+ $candies++ if $candidates[$i] > $candidates[$i + 1];
+ }
+ }
+ return $candies;
+}
+
+
+MAIN:{
+ my @N;
+ my $number_candies;
+ @N = (1, 2, 2);
+ $number_candies = count_candies(@N);
+ print "The number of candies for (" .
+ join(",", @N) . ") is $number_candies\n";
+ @N = (1, 4, 3, 2);
+ $number_candies = count_candies(@N);
+ print "The number of candies for (" .
+ join(",", @N) . ") is $number_candies\n";
+} \ No newline at end of file
diff --git a/challenge-080/adam-russell/prolog/ch-1.p b/challenge-080/adam-russell/prolog/ch-1.p
new file mode 100644
index 0000000000..14b5a5bb6e
--- /dev/null
+++ b/challenge-080/adam-russell/prolog/ch-1.p
@@ -0,0 +1,24 @@
+/*
+* You are given an unsorted list of integers N.
+* Write a script to find out the smallest positive number missing.
+*/
+n([2, 0, -1]).
+
+missing_smallest([H|T], X):-
+ H >= 0,
+ last(T, Last),
+ between(H, Last, X),
+ \+ member(X, [H|T]),
+ !.
+missing_smallest([_|T], X):-
+ missing_smallest(T, X).
+
+
+main:-
+ n(N),
+ sort(N, Sorted),
+ missing_smallest(Sorted, Missing),
+ writeln(Missing),
+ halt.
+
+
diff --git a/challenge-080/adam-russell/prolog/ch-2.p b/challenge-080/adam-russell/prolog/ch-2.p
new file mode 100644
index 0000000000..b0152bdc7f
--- /dev/null
+++ b/challenge-080/adam-russell/prolog/ch-2.p
@@ -0,0 +1,36 @@
+/*
+* 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:
+* a) You must given at least one candy to each candidate.
+* b) Candidate with higher ranking get more candies than their immediate
+* neighbors on either side.
+*/
+n([1, 2, 2]).
+%n([1, 4, 3, 2]).
+
+candies(N, Candies):-
+ length(N, CandiesAccum),
+ candies(N, CandiesAccum, Candies).
+candies([], Candies, Candies).
+candies([H0, H1|[]], CandiesAccum, Candies):-
+ H0 > H1,
+ C is CandiesAccum + 1,
+ candies([], C, Candies).
+candies([_, _|[]], CandiesAccum, Candies):-
+ candies([], CandiesAccum, Candies).
+candies([H0, H1|T], CandiesAccum, Candies):-
+ H0 > H1,
+ C is CandiesAccum + 1,
+ candies([H1|T], C, Candies).
+candies([H0, H1|T], CandiesAccum, Candies):-
+ H1 > H0,
+ C is CandiesAccum + 1,
+ candies([H1|T], C, Candies).
+
+main:-
+ n(N),
+ candies(N, C),
+ writeln(C),
+ halt.
+ \ No newline at end of file