diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-06-15 00:16:54 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-15 00:16:54 +0100 |
| commit | 16eeabc6a58bba3b993fc60cf33f9dc098d3fd8c (patch) | |
| tree | 3e5e5d806887d437697120cb80d1029fb1f5bdf5 | |
| parent | 739a8bec8d2238958c3bced57912fdc63c364651 (diff) | |
| parent | faa6b718fd5e2ef6c8ddbfc2bd790770f4a9bdd2 (diff) | |
| download | perlweeklychallenge-club-16eeabc6a58bba3b993fc60cf33f9dc098d3fd8c.tar.gz perlweeklychallenge-club-16eeabc6a58bba3b993fc60cf33f9dc098d3fd8c.tar.bz2 perlweeklychallenge-club-16eeabc6a58bba3b993fc60cf33f9dc098d3fd8c.zip | |
Merge pull request #12176 from adamcrussell/challenge-325
initial commit
| -rw-r--r-- | challenge-325/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-325/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-325/adam-russell/perl/ch-1.pl | 45 | ||||
| -rw-r--r-- | challenge-325/adam-russell/perl/ch-2.pl | 31 | ||||
| -rw-r--r-- | challenge-325/adam-russell/prolog/ch-1.p | 28 | ||||
| -rw-r--r-- | challenge-325/adam-russell/prolog/ch-2.p | 16 |
6 files changed, 122 insertions, 0 deletions
diff --git a/challenge-325/adam-russell/blog.txt b/challenge-325/adam-russell/blog.txt new file mode 100644 index 0000000000..5438ac105b --- /dev/null +++ b/challenge-325/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2025/06/12 diff --git a/challenge-325/adam-russell/blog1.txt b/challenge-325/adam-russell/blog1.txt new file mode 100644 index 0000000000..d0605fdf80 --- /dev/null +++ b/challenge-325/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2025/06/14 diff --git a/challenge-325/adam-russell/perl/ch-1.pl b/challenge-325/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..843e1bc065 --- /dev/null +++ b/challenge-325/adam-russell/perl/ch-1.pl @@ -0,0 +1,45 @@ + + use v5.40; + + sub consecutive_one_r{ + my($i, $consecutive, $max_consecutive) = @_; + my $x; + unless(@{$i} == 0){ + $x = pop @{$i}; + if($x == 0){ + $$max_consecutive = $$consecutive if $$consecutive > $$max_consecutive; + $$consecutive = 0; + } + if($x == 1){ + $$consecutive++; + } + consecutive_one_r($i, $consecutive, $max_consecutive); + } + elsif(@{$i} == 1){ + $x = pop @{$i}; + if($x == 0){ + $$max_consecutive = $$consecutive if $$consecutive > $$max_consecutive; + } + if($x == 1){ + $$consecutive++; + $$max_consecutive = $$consecutive if $$consecutive > $$max_consecutive; + } + consecutive_one_r($i, $consecutive, $max_consecutive); + } + } + + + sub consecutive_one{ + my(@i) = @_; + my($consecutive, $max_consecutive) = (0, 0); + consecutive_one_r(\@i, \$consecutive, \$max_consecutive); + return $max_consecutive; + } + + +MAIN:{ + say consecutive_one(0, 1, 1, 0, 1, 1, 1); + say consecutive_one(0, 0, 0, 0); + say consecutive_one(1, 0, 1, 0, 1, 1); +} + diff --git a/challenge-325/adam-russell/perl/ch-2.pl b/challenge-325/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..e8b8fd9e8b --- /dev/null +++ b/challenge-325/adam-russell/perl/ch-2.pl @@ -0,0 +1,31 @@ + + use v5.40; + + sub search_lower{ + my($prices, $price, $lower) = @_; + if(@{$prices} > 0){ + my $next_price = shift @{$prices}; + search_lower($prices, $price, $lower) unless $next_price <= $price; + $$lower = $next_price if $next_price <= $price; + } + } + + + sub calculate_lowest_prices{ + my @prices = @_; + my @lowest = (); + for my $i (0 .. @prices - 1){ + my $lower = 0; + search_lower [@prices[$i + 1 .. @prices - 1]], $prices[$i], \$lower; + push @lowest, $prices[$i] - $lower; + } + return @lowest; + } + + +MAIN:{ + say join q/, /, calculate_lowest_prices 8, 4, 6, 2, 3; + say join q/, /, calculate_lowest_prices 1, 2, 3, 4, 5; + say join q/, /, calculate_lowest_prices 7, 1, 1, 5; +} + diff --git a/challenge-325/adam-russell/prolog/ch-1.p b/challenge-325/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..ed1d11fcf8 --- /dev/null +++ b/challenge-325/adam-russell/prolog/ch-1.p @@ -0,0 +1,28 @@ + + + consecutive_ones(Consecutive), [Consecutive] --> [Consecutive]. + consecutive_ones(C, Consecutive), [Consecutive] --> [C]. + + + count_ones(Input) --> consecutive_ones(C, Consecutive), + {Input = [H|T], + H == 1, + [Count, Maximum] = C, + succ(Count, Count1), + ((Count1 > Maximum, Consecutive = [Count1, Count1]); + (Consecutive = [Count1, Maximum])) + }, + count_ones(T). + count_ones(Input) --> consecutive_ones(C, Consecutive), + {Input = [H|T], + H == 0, + [_, Maximum] = C, + Consecutive = [0, Maximum]}, + count_ones(T). + count_ones([]) --> []. + + + consecutive_ones(L, MaximumConsecutive):- + phrase(count_ones(L), [[0, 0]], [Output]), !, + [_, MaximumConsecutive] = Output. + diff --git a/challenge-325/adam-russell/prolog/ch-2.p b/challenge-325/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..333c7ea72d --- /dev/null +++ b/challenge-325/adam-russell/prolog/ch-2.p @@ -0,0 +1,16 @@ + + + next_smallest([], _, 0). + next_smallest([H|_], Price, H):- + H =< Price, !. + next_smallest([H|T], Price, LowestPrice):- + H > Price, + next_smallest(T, Price, LowestPrice). + + + compute_lowest([], []). + compute_lowest([H|T], [LowestPrice|LowestPrices1]):- + compute_lowest(T, LowestPrices1), + next_smallest(T, H, Discount), + LowestPrice is H - Discount. + |
