diff options
| author | Adam Russell <ac.russell@live.com> | 2021-08-29 01:58:09 -0400 |
|---|---|---|
| committer | Adam Russell <ac.russell@live.com> | 2021-08-29 01:58:09 -0400 |
| commit | 83d1adfe7a27e3cfcce8cb68b19c49548ee819ca (patch) | |
| tree | 13b2b83f6ef46d372b18ebafe7a2faba8b2feeff /challenge-127 | |
| parent | df35ff0ae1f97a768f4365ac33aebffc0d331a6d (diff) | |
| download | perlweeklychallenge-club-83d1adfe7a27e3cfcce8cb68b19c49548ee819ca.tar.gz perlweeklychallenge-club-83d1adfe7a27e3cfcce8cb68b19c49548ee819ca.tar.bz2 perlweeklychallenge-club-83d1adfe7a27e3cfcce8cb68b19c49548ee819ca.zip | |
solutions and blog links for challenge 127
Diffstat (limited to 'challenge-127')
| -rw-r--r-- | challenge-127/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-127/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-127/adam-russell/cxx/ch-1.cxx | 32 | ||||
| -rw-r--r-- | challenge-127/adam-russell/cxx/ch-2.cxx | 46 | ||||
| -rw-r--r-- | challenge-127/adam-russell/perl/ch-1.pl | 24 | ||||
| -rw-r--r-- | challenge-127/adam-russell/perl/ch-2.pl | 31 | ||||
| -rw-r--r-- | challenge-127/adam-russell/prolog/ch-1.p | 15 | ||||
| -rw-r--r-- | challenge-127/adam-russell/prolog/ch-2.p | 31 |
8 files changed, 181 insertions, 0 deletions
diff --git a/challenge-127/adam-russell/blog.txt b/challenge-127/adam-russell/blog.txt new file mode 100644 index 0000000000..ca8ec12c18 --- /dev/null +++ b/challenge-127/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2021/08/29 diff --git a/challenge-127/adam-russell/blog1.txt b/challenge-127/adam-russell/blog1.txt new file mode 100644 index 0000000000..e8675c674d --- /dev/null +++ b/challenge-127/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2021/08/29 diff --git a/challenge-127/adam-russell/cxx/ch-1.cxx b/challenge-127/adam-russell/cxx/ch-1.cxx new file mode 100644 index 0000000000..1081bb652d --- /dev/null +++ b/challenge-127/adam-russell/cxx/ch-1.cxx @@ -0,0 +1,32 @@ +#include <vector> +#include <iostream> +#include <algorithm> +/* + * You are given two sets with unique numbers. + * Write a script to figure out if they are disjoint. +*/ +class DisjointSets{ + public: + bool disjoint(std::vector<int>, std::vector<int>); +}; + +bool DisjointSets::disjoint(std::vector<int> v0, std::vector<int> v1){ + bool found = false; + std::for_each(v0.begin(), v0.end(), + [&v1, &found](int &n){ + if(std::find(v1.begin(), v1.end(), n) != v1.end()) + found = true; + } + ); + return !found; +} + +int main(int argc, char** argv){ + std::vector<int> v0 {1, 2, 5, 3, 4}; + std::vector<int> v1 {4, 6, 7, 8, 9}; + DisjointSets ds; + std::cout << ds.disjoint(v0, v1) << std::endl; + std::vector<int> v2 {1, 3, 5, 7, 9}; + std::vector<int> v3 {0, 2, 4, 6, 8}; + std::cout << ds.disjoint(v2, v3) << std::endl; +} diff --git a/challenge-127/adam-russell/cxx/ch-2.cxx b/challenge-127/adam-russell/cxx/ch-2.cxx new file mode 100644 index 0000000000..3dabc1ca4a --- /dev/null +++ b/challenge-127/adam-russell/cxx/ch-2.cxx @@ -0,0 +1,46 @@ +#include <vector> +#include <iostream> +#include <algorithm> +/* + * You are given a list of intervals. Write a script + * to determine conflicts between the intervals. +*/ +class ConflictIntervals{ + public: + std::vector<std::vector<int>> conflicts(std::vector<std::vector<int>>); +}; + +std::vector<std::vector<int>> ConflictIntervals::conflicts(std::vector<std::vector<int>> v){ + std::vector<std::vector<int>> conflicts; + std::sort(v.begin(), v.end(), + [](const std::vector<int> &a, const std::vector<int> &b) { + return a[1] < b[1]; + } + ); + for(int i = v.size() - 1; i >= 0; i--){ + for (int j = 0; j < i; j++){ + if(v[i][0] >= v[j][0] && v[i][0] <= v[j][1]){ + conflicts.push_back(v[i]); + break; + } + } + } + + return conflicts; +} + +int main(int argc, char** argv){ + std::vector<std::vector<int>> intervals = {{1, 4}, {3, 5}, {6, 8}, {12, 13}, {3, 20}}; + ConflictIntervals ci; + std::vector<std::vector<int>> conflicts = ci.conflicts(intervals); + for(int i=0; i < conflicts.size(); i++){ + std::cout << "[" << conflicts[i][0] << ", " << conflicts[i][1] << "]" << " "; + } + std::cout << std::endl; + intervals = {{3, 4}, {5, 7}, {6, 9}, {10, 12}, {13, 15}}; + conflicts = ci.conflicts(intervals); + for(int i=0; i < conflicts.size(); i++){ + std::cout << "[" << conflicts[i][0] << ", " << conflicts[i][1] << "]" << " "; + } + std::cout << std::endl; +} diff --git a/challenge-127/adam-russell/perl/ch-1.pl b/challenge-127/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..2771942173 --- /dev/null +++ b/challenge-127/adam-russell/perl/ch-1.pl @@ -0,0 +1,24 @@ +use strict; +use warnings; +## +# You are given two sets with unique numbers. +# Write a script to figure out if they are disjoint. +## +use boolean; + +sub disjoint{ + my($list1, $list2) = @_; + my @a = map { my $n = $_; grep $n == $_ , @{$list2} } @{$list1}; + return boolean(@a == 0);#boolean() used for better stringification +} + + +MAIN:{ + my(@S1, @S2); + @S1 = (1, 2, 5, 3, 4); + @S2 = (4, 6, 7, 8, 9); + print disjoint(\@S1, \@S2) . "\n"; + @S1 = (1, 3, 5, 7, 9); + @S2 = (0, 2, 4, 6, 8); + print disjoint(\@S1, \@S2) . "\n"; +} diff --git a/challenge-127/adam-russell/perl/ch-2.pl b/challenge-127/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..45e703af1d --- /dev/null +++ b/challenge-127/adam-russell/perl/ch-2.pl @@ -0,0 +1,31 @@ +use strict; +use warnings; +## +# You are given a list of intervals. Write a script +# to determine conflicts between the intervals. +## +sub conflicts{ + my @intervals = @_; + my @conflicts; + @intervals = sort { $a->[1] <=> $b->[1] } @intervals; + { + my $interval = pop @intervals; + my($i, $j) = @{$interval}; + for $interval (@intervals){ + my($m, $n) = @{$interval}; + do { unshift @conflicts, [$i, $j]; last } if $i >= $m && $i <= $n; + } + redo if @intervals; + } + return @conflicts; +} + +MAIN:{ + my(@Intervals); + @Intervals = ([1, 4], [3, 5], [6, 8], [12, 13], [3, 20]); + map { print "[" . join(", ", @{$_}) . "] " } conflicts(@Intervals); + print "\n"; + @Intervals = ([3, 4], [5, 7], [6, 9], [10, 12], [13, 15]); + map { print "[" . join(", ", @{$_}) . "] " } conflicts(@Intervals); + print "\n"; +} diff --git a/challenge-127/adam-russell/prolog/ch-1.p b/challenge-127/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..a24a644b91 --- /dev/null +++ b/challenge-127/adam-russell/prolog/ch-1.p @@ -0,0 +1,15 @@ +:-initialization(main). + +conflicts(List0, List1):- + member(X, List0), + member(X, List1). + +disjoint(List0, List1):- + \+ conflicts(List0, List1). + +main:- + ((disjoint([1, 2, 5, 3, 4], [4, 6, 7, 8, 9]), write(1)); (write(0))), + nl, + ((disjoint([1, 3, 5, 7, 9], [0, 2, 4, 6, 8]), write(1)); (write(0))), + nl, + halt. diff --git a/challenge-127/adam-russell/prolog/ch-2.p b/challenge-127/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..1e06eaee6e --- /dev/null +++ b/challenge-127/adam-russell/prolog/ch-2.p @@ -0,0 +1,31 @@ +:-initialization(main). + +conflicts(Intervals, Conflicts):- + conflicts(Intervals, Conflicts, []). +conflicts([_|[]], Conflicts, ConflictsAccum):- + sort(ConflictsAccum, Conflicts). +conflicts([H|T], Conflicts, ConflictsAccum):- + last(T, Last), + append(Intervals, [Last], T), + append([H], Intervals, CompareIntervals), + comparisons(Last, CompareIntervals, HasConflicts), + append(ConflictsAccum, HasConflicts, ConflictsAccumUpdated), + conflicts(CompareIntervals, Conflicts, ConflictsAccumUpdated). + +comparisons(Interval, CompareIntervals, Conflicts):- + comparisons(Interval, CompareIntervals, Conflicts, []). +comparisons(_, [], Conflicts, Conflicts). +comparisons(Interval, [[H0|T0]|T], Conflicts, ConflictsAccum):- + [I|J] = Interval, + I >= H0, + I =< T0, + comparisons([I|J], T, Conflicts, [Interval|ConflictsAccum]). +comparisons([I|J], [_|T], Conflicts, ConflictsAccum):- + comparisons([I|J], T, Conflicts, ConflictsAccum). + +main:- + conflicts([[1, 4], [3, 5], [6, 8], [12, 13], [3, 20]], Conflicts0), + write(Conflicts0), nl, + conflicts([[3, 4], [5, 7], [6, 9], [10, 12], [13, 15]], Conflicts1), + write(Conflicts1), nl, + halt. |
