diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-03-27 18:53:15 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-03-27 18:53:15 +0200 |
| commit | 81d0737ace1c412703eb9b8d6f31883a63f0ead9 (patch) | |
| tree | 5012c7343a1d8c7d0d70ed5425ac0122c0e5f217 /challenge-044 | |
| parent | 8915a66de2cb2a724aee5e55ddfc15580cfdf1d5 (diff) | |
| download | perlweeklychallenge-club-81d0737ace1c412703eb9b8d6f31883a63f0ead9.tar.gz perlweeklychallenge-club-81d0737ace1c412703eb9b8d6f31883a63f0ead9.tar.bz2 perlweeklychallenge-club-81d0737ace1c412703eb9b8d6f31883a63f0ead9.zip | |
Challenges 043 044 LK Perl Python
Diffstat (limited to 'challenge-044')
| -rw-r--r-- | challenge-044/lubos-kolouch/perl/ch-1.pl | 28 | ||||
| -rw-r--r-- | challenge-044/lubos-kolouch/perl/ch-2.pl | 20 | ||||
| -rw-r--r-- | challenge-044/lubos-kolouch/python/ch-1.py | 20 | ||||
| -rw-r--r-- | challenge-044/lubos-kolouch/python/ch-2.py | 16 |
4 files changed, 84 insertions, 0 deletions
diff --git a/challenge-044/lubos-kolouch/perl/ch-1.pl b/challenge-044/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..f76b375086 --- /dev/null +++ b/challenge-044/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,28 @@ +use strict; +use warnings; + +sub insert_operators { + my ( $str, $target ) = @_; + my @results; + helper( $str, $target, "", \@results ); + return @results; +} + +sub helper { + my ( $str, $target, $expr, $results ) = @_; + if ( $str eq "" ) { + if ( eval($expr) == $target ) { + push @$results, $expr; + } + return; + } + for my $i ( 1 .. length($str) ) { + my $left = substr( $str, 0, $i ); + my $right = substr( $str, $i ); + helper( $right, $target, "$expr+$left", $results ); + helper( $right, $target, "$expr-$left", $results ); + } +} + +my @results = insert_operators( "123456789", 100 ); +print "$_\n" for @results; diff --git a/challenge-044/lubos-kolouch/perl/ch-2.pl b/challenge-044/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..75a6d5429b --- /dev/null +++ b/challenge-044/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,20 @@ +use strict; +use warnings; + +my $goal = 200; # Target amount +my $current = 1; # Current amount +my $moves = 0; # Number of moves + +while ( $current < $goal ) { + + # Check if doubling the current amount gets us closer to the goal + if ( $current * 2 <= $goal - $current - 1 ) { + $current *= 2; + } + else { + $current += 1; + } + $moves += 1; +} + +print "To reach $goal, you need $moves moves.\n"; diff --git a/challenge-044/lubos-kolouch/python/ch-1.py b/challenge-044/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..4e58bd9a26 --- /dev/null +++ b/challenge-044/lubos-kolouch/python/ch-1.py @@ -0,0 +1,20 @@ +def insert_operators(s, target): + results = [] + + def helper(s, expr): + if not s: + if eval(expr) == target: + results.append(expr) + return + for i in range(1, len(s) + 1): + left, right = s[:i], s[i:] + helper(right, expr + "+" + left) + helper(right, expr + "-" + left) + + helper(s, "") + return results + + +results = insert_operators("123456789", 100) +for r in results: + print(r) diff --git a/challenge-044/lubos-kolouch/python/ch-2.py b/challenge-044/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..4ad8efde17 --- /dev/null +++ b/challenge-044/lubos-kolouch/python/ch-2.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +goal = 200 # Target amount +current = 1 # Current amount +moves = 0 # Number of moves + +while current < goal: + # Check if doubling the current amount gets us closer to the goal + if current * 2 <= goal - current - 1: + current *= 2 + else: + current += 1 + moves += 1 + +print("To reach $", goal, ", you need", moves, "moves.") |
