From cc83fef900af85964861dd02f6fc6ce55ec6dce9 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Sun, 4 Oct 2020 17:12:05 -0400 Subject: solutions for challenge 080 --- challenge-080/adam-russell/blog.txt | 1 + challenge-080/adam-russell/cxx/ch-1.cxx | 25 +++++++++++++++++++++ challenge-080/adam-russell/cxx/ch-2.cxx | 30 ++++++++++++++++++++++++++ challenge-080/adam-russell/perl/ch-1.pl | 31 ++++++++++++++++++++++++++ challenge-080/adam-russell/perl/ch-2.pl | 37 ++++++++++++++++++++++++++++++++ challenge-080/adam-russell/prolog/ch-1.p | 24 +++++++++++++++++++++ challenge-080/adam-russell/prolog/ch-2.p | 36 +++++++++++++++++++++++++++++++ 7 files changed, 184 insertions(+) create mode 100644 challenge-080/adam-russell/blog.txt create mode 100644 challenge-080/adam-russell/cxx/ch-1.cxx create mode 100644 challenge-080/adam-russell/cxx/ch-2.cxx create mode 100644 challenge-080/adam-russell/perl/ch-1.pl create mode 100644 challenge-080/adam-russell/perl/ch-2.pl create mode 100644 challenge-080/adam-russell/prolog/ch-1.p create mode 100644 challenge-080/adam-russell/prolog/ch-2.p 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 +#include +#include +/* +* 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 numbers (n, n + 4); + for(int i = n[0]; i < n[3]; i++){ + std::vector::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 +/* +* 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 -- cgit