diff options
| -rw-r--r-- | challenge-224/adam-russell/prolog/ch-2.p | 6 | ||||
| -rw-r--r-- | challenge-244/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-244/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-244/adam-russell/javascript/ch-1.js | 23 | ||||
| -rw-r--r-- | challenge-244/adam-russell/javascript/ch-2.js | 31 | ||||
| -rw-r--r-- | challenge-244/adam-russell/perl/ch-1.pl | 20 | ||||
| -rw-r--r-- | challenge-244/adam-russell/perl/ch-2.pl | 21 | ||||
| -rw-r--r-- | challenge-244/adam-russell/prolog/ch-1.p | 8 | ||||
| -rw-r--r-- | challenge-244/adam-russell/prolog/ch-2.p | 9 | ||||
| -rw-r--r-- | challenge-244/adam-russell/r/ch-1.r | 21 | ||||
| -rw-r--r-- | challenge-244/adam-russell/r/ch-2.r | 29 |
11 files changed, 167 insertions, 3 deletions
diff --git a/challenge-224/adam-russell/prolog/ch-2.p b/challenge-224/adam-russell/prolog/ch-2.p index c6642222bf..b19502f568 100644 --- a/challenge-224/adam-russell/prolog/ch-2.p +++ b/challenge-224/adam-russell/prolog/ch-2.p @@ -26,16 +26,16 @@ additive_number(Sequence) --> Sequence, \+ C = []}, digit_sequence(A), digit_sequence(B), digit_sequence(C), { is_additive_sequence(A, B, C), - T = []}. + T = [], !}. digit_sequence([]) --> []. digit_sequence([D]) --> digit(D). digit_sequence([D|T]) --> digit(D), digit_sequence(T). -digit(D) --> [D], { D #>= 48, D #=< 57 }. +digit(D) --> [D], { between(0, 9, X), number_codes(X, [D]) }. is_additive_sequence(A, B, C) :- number_codes(NA, A), number_codes(NB, B), number_codes(NC, C), - NC #= NA + NB.
\ No newline at end of file + NC is NA + NB.
\ No newline at end of file diff --git a/challenge-244/adam-russell/blog.txt b/challenge-244/adam-russell/blog.txt new file mode 100644 index 0000000000..74f0cf8653 --- /dev/null +++ b/challenge-244/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2023/11/26
\ No newline at end of file diff --git a/challenge-244/adam-russell/blog1.txt b/challenge-244/adam-russell/blog1.txt new file mode 100644 index 0000000000..22df6a5bb0 --- /dev/null +++ b/challenge-244/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2023/11/26
\ No newline at end of file diff --git a/challenge-244/adam-russell/javascript/ch-1.js b/challenge-244/adam-russell/javascript/ch-1.js new file mode 100644 index 0000000000..35d7790240 --- /dev/null +++ b/challenge-244/adam-russell/javascript/ch-1.js @@ -0,0 +1,23 @@ +class Ch1{ + countSmaller(integers){ + let r = []; + let i = integers; + integers.forEach((x) => { + let smallerCount = integers.map((y) => {return y < x}). + reduce((accumulator, currentValue) => { + return accumulator + currentValue}, 0); + r.push(smallerCount); + }); + return r; + } +} +let ch1 = new Ch1(); +console.log( + ch1.countSmaller([8, 1, 2, 2, 3]) +); +console.log( + ch1.countSmaller([6, 5, 4, 8]) +); +console.log( + ch1.countSmaller([2, 2, 2]) +);
\ No newline at end of file diff --git a/challenge-244/adam-russell/javascript/ch-2.js b/challenge-244/adam-russell/javascript/ch-2.js new file mode 100644 index 0000000000..36732a7e4f --- /dev/null +++ b/challenge-244/adam-russell/javascript/ch-2.js @@ -0,0 +1,31 @@ +class Ch2{ + int2bits(x){ + let bits = []; + while(x > 0){ + let b = x & 1; + bits.push(b); + x = x >> 1; + } + return(bits) + } + + groupHero(integers){ + let group_hero = []; + let i = integers; + for(let i = 1; i < 2**integers.length; i++){ + let hero = []; + let b = this.int2bits(i); + for(let j = 0; j < b.length; j++){ + if(b[j] == 1) + hero.push(integers[j]); + } + group_hero.push(Math.max(...hero)**2 * Math.min(...hero)); + } + return group_hero.reduce((accumulator, currentValue) => { + return accumulator + currentValue}, 0); + } +} +let ch2 = new Ch2(); +console.log( + ch2.groupHero([2, 1, 4]) +);
\ No newline at end of file diff --git a/challenge-244/adam-russell/perl/ch-1.pl b/challenge-244/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..b280e63130 --- /dev/null +++ b/challenge-244/adam-russell/perl/ch-1.pl @@ -0,0 +1,20 @@ +use v5.38; +## +# You are given an array of integers. +# Write a script to calculate the number of integers smaller than the +# integer at each index. +## +sub count_smaller{ + my @integers = @_; + my @integers_sorted = sort {$a <=> $b} @integers; + return map { + my $x = $_; + (grep { $integers[$x] == $integers_sorted[$_]} 0 .. @integers_sorted - 1)[0]; + } 0 .. @integers - 1; +} + +MAIN:{ + say join q/, /, count_smaller qw/8 1 2 2 3/; + say join q/, /, count_smaller qw/6 5 4 8/; + say join q/, /, count_smaller qw/2 2 2/; +}
\ No newline at end of file diff --git a/challenge-244/adam-russell/perl/ch-2.pl b/challenge-244/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..f25ce673e9 --- /dev/null +++ b/challenge-244/adam-russell/perl/ch-2.pl @@ -0,0 +1,21 @@ +use v5.38; +## +# You are given an array of integers representing the strength. +# Write a script to return the sum of the powers of all possible +# combinations; power is defined as the square of the largest number in +# a sequence, multiplied by the smallest. +## +sub group_hero{ + my @group = @_; + my $group_hero = 0; + do{ + my $indices = $_; + my @hero = sort {$a <=> $b} @group[grep{vec($indices, $_, 1) == 1} 0 .. @group - 1]; + $group_hero += ($hero[@hero - 1]**2 * $hero[0]); + } for 1 .. 2**@group - 1; + return $group_hero; +} + +MAIN:{ + say group_hero qw/2 1 4/; +}
\ No newline at end of file diff --git a/challenge-244/adam-russell/prolog/ch-1.p b/challenge-244/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..15738a67ec --- /dev/null +++ b/challenge-244/adam-russell/prolog/ch-1.p @@ -0,0 +1,8 @@ +smaller([], _, 0). +smaller([H|Integers], X, Y):- + smaller(Integers, X, Y0), + ((X > H, succ(Y0, Y)); + (X =< H, Y = Y0)). + +count_smaller(Integers, CountSmaller):- + maplist(smaller(Integers), Integers, CountSmaller).
\ No newline at end of file diff --git a/challenge-244/adam-russell/prolog/ch-2.p b/challenge-244/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..b9d47495f0 --- /dev/null +++ b/challenge-244/adam-russell/prolog/ch-2.p @@ -0,0 +1,9 @@ +group_hero(Group, GroupHero):- + findall(Hero, ( + sublist(SubList, Group), + max_list(SubList, Maximum), + min_list(SubList, Minimum), + Hero #= Maximum**2 * Minimum + ), Heroes), + sum_list(Heroes, GroupHero). +
\ No newline at end of file diff --git a/challenge-244/adam-russell/r/ch-1.r b/challenge-244/adam-russell/r/ch-1.r new file mode 100644 index 0000000000..421eac4ebb --- /dev/null +++ b/challenge-244/adam-russell/r/ch-1.r @@ -0,0 +1,21 @@ +ch_1 <- function(){ + structure(list(), class = "ch_1") +} + +count_smaller <- function(self, l){ + UseMethod("count_smaller", self) +} + +count_smaller.ch_1 <- function(self, integers) { + r <- c() + for (i in 1:(length(integers))) { + x <- integers[i] + r[length(r) + 1] <- length(which(integers < x)) + } + return(r) +} + +ch_1 <- ch_1() +print(count_smaller(ch_1, c(8, 1, 2, 2, 3))) +print(count_smaller(ch_1, c(6, 5, 4, 8))) +print(count_smaller(ch_1, c(2, 2, 2)))
\ No newline at end of file diff --git a/challenge-244/adam-russell/r/ch-2.r b/challenge-244/adam-russell/r/ch-2.r new file mode 100644 index 0000000000..d3c49d11fc --- /dev/null +++ b/challenge-244/adam-russell/r/ch-2.r @@ -0,0 +1,29 @@ +ch_2 <- function(){ + structure(list(), class = "ch_2") +} + +int2bits <- function(x){ + bits <- c() + while(x > 0){ + b <- bitwAnd(x, 1) + bits[length(bits) + 1] <- b + x <- bitwShiftR(x, 1) + } + return(bits) +} + +group_hero <- function(self, l){ + UseMethod("group_hero", self) +} + +group_hero.ch_2 <- function(self, group) { + group_hero <- 0 + for (i in 1:(2**length(group) - 1)) { + hero <- group[which(int2bits(i) == 1)] + group_hero = group_hero + max(hero)**2 * min(hero) + } + return(group_hero) +} + +ch_2 <- ch_2() +print(group_hero(ch_2, c(2, 1, 4))) |
