diff options
| author | User Person <usermanperson+github@gmail.com> | 2020-03-22 13:29:58 -0400 |
|---|---|---|
| committer | User Person <usermanperson+github@gmail.com> | 2020-03-22 13:29:58 -0400 |
| commit | e059b73c28f82cd3fe73cc40ba15c1bb94f4d06e (patch) | |
| tree | 470a1ab2f6fff22787b933dc650a869825c5697e | |
| parent | 5d9836b8a90e55a5b07f488c2ae018fe925feb47 (diff) | |
| download | perlweeklychallenge-club-e059b73c28f82cd3fe73cc40ba15c1bb94f4d06e.tar.gz perlweeklychallenge-club-e059b73c28f82cd3fe73cc40ba15c1bb94f4d06e.tar.bz2 perlweeklychallenge-club-e059b73c28f82cd3fe73cc40ba15c1bb94f4d06e.zip | |
User-person's solutions for challenge 52.
| -rwxr-xr-x | challenge-052/user-person/perl/ch-1.pl | 75 | ||||
| -rwxr-xr-x | challenge-052/user-person/perl/ch-2.pl | 147 | ||||
| -rwxr-xr-x | challenge-052/user-person/python/ch-1.py | 70 | ||||
| -rwxr-xr-x | challenge-052/user-person/python/ch-2.py | 128 |
4 files changed, 420 insertions, 0 deletions
diff --git a/challenge-052/user-person/perl/ch-1.pl b/challenge-052/user-person/perl/ch-1.pl new file mode 100755 index 0000000000..acf198b2c4 --- /dev/null +++ b/challenge-052/user-person/perl/ch-1.pl @@ -0,0 +1,75 @@ +#!/usr/bin/env perl + +########################################################################### +# script name: ch-1.pl # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-052/ # +# # +# Stepping Numbers # +# # +# Write a script to accept two numbers between 100 and 999. It should # +# then print all Stepping Numbers between them. # +# # +# A number is called a stepping number if the adjacent digits have a # +# difference of 1. For example, 456 is a stepping number but 129 is not. # +# # +########################################################################### + +use strict; +use warnings; +use FindBin; + +my @step = (); +my $UPPER_LIMIT = 1000; +my $LOWER_LIMIT = 99; + +for (my $i = 1; $i < 8; ++$i) { + push @step, ($i * 100) + ( $i + 1 ) * 10 + ($i + 2); +} + +if ( + scalar @ARGV != 2 + + or $ARGV[0] !~ m|\A\d{3}\Z| + or $ARGV[0] < $LOWER_LIMIT + or $ARGV[0] > $UPPER_LIMIT + + or $ARGV[1] !~ m|\A\d{3}\Z| + or $ARGV[1] < $LOWER_LIMIT + or $ARGV[1] > $UPPER_LIMIT + + ) { + print STDERR "$FindBin::Script requires 2 arguments between 100 and 999.\n"; + exit(1); +} + +my ($min, $max) = (0, 0); + +if ($ARGV[0] < $ARGV[1]) { + $min = $ARGV[0]; + $max = $ARGV[1]; +} elsif ($ARGV[1] < $ARGV[0]) { + $min = $ARGV[1]; + $max = $ARGV[0]; +} else { + $max = $min = $ARGV[0]; +} + +my $commaFlag = 0; + +exit if $step[0] > $max or $step[-1] < $min; + +LOOP: +foreach (@step) { + if ($_ >= $min and $_ <= $max) { + print ", " if $commaFlag; + print "$_"; + $commaFlag = 1; + } elsif ($commaFlag) { + last LOOP; + } +} + +print "\n" if $commaFlag; diff --git a/challenge-052/user-person/perl/ch-2.pl b/challenge-052/user-person/perl/ch-2.pl new file mode 100755 index 0000000000..cba45b6864 --- /dev/null +++ b/challenge-052/user-person/perl/ch-2.pl @@ -0,0 +1,147 @@ +#!/usr/bin/env perl + +########################################################################### +# script name: ch-2.pl # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-052/ # +# # +# Lucky Winner # +# Suppose there are following coins arranged on a table in a line in # +# random order # +# # +# 1, 50p, 1p, 10p, 5p, 2, 2p # +# # +# Suppose you are playing against the computer. Player can only pick one # +# coin at a time from either ends. Find out the lucky winner, who has # +# the larger amounts in total?20p, order. # +# # +########################################################################### + +# Coins total 3.88. 2 > 1.88 . If a player gets the L2 coin, they win the game. + +# Given sufficiently intelligent players, whoever gets the first turn wins the game. + +use strict; +use warnings; +use diagnostics; + +use List::Util qw/shuffle/; +use Term::ReadLine; + +my $term = Term::ReadLine->new('input'); +$term->ornaments(00,00,00,00); + +my @coins = qw{ 1p 2p 5p 10p 20p 50p L1 L2 }; + +@coins = shuffle @coins; + +my %coinVal = ( '1p' => 0.01, '2p' => 0.02, '5p' => 0.05, '10p' => 0.10, + '20p' => 0.20, '50p' => 0.50, 'L1' => 1.00, 'L2' => 2.00 ); + +my %bank = ( 'player' => 0, 'computer' => 0 ); + +my $turn = int rand 2; + +sub l2Index { + my $ret = -1; + LOOP: + for (my $i = 0; $i <= $#coins; ++$i) { + if ($coins[$i] eq 'L2') { + $ret = $i; + last LOOP; + } + } + return $ret; +} + +sub takeCoin { + my $choice = shift; + my $who = shift; + + if ($choice eq 'f') { + $bank{$who} += $coinVal{ shift @coins }; + } else { # "If you ain't first, you're last." + $bank{$who} += $coinVal{ pop @coins }; + } + + printf "%s: L %.2f\n\n", $who, $bank{$who}; +} + +sub playerChoice { + my $fl = ""; + my $loop = 1; + + my $prompt = "Type 'f' to choose the first coin. Type 'l' to choose the last coin. Type 'q' to quit:\n" ; + print $prompt if scalar @coins > 1; + + ILOOP: + while ($loop--) { + + if ( scalar @coins == 1) { # Don't ask when there's only one choice. + takeCoin('f','player'); + next ILOOP; + } + + my $fl = $term->readline('> '); + + if ($fl eq 'f' or $fl eq 'l') { + takeCoin($fl,'player'); + + } elsif ($fl eq 'q') { + exit; + + } else { + print "Invalid choice.\n"; + ++$loop; + } + } +} + +sub chooseGreater { + $coinVal{$coins[0]} > $coinVal{$coins[-1]} ? takeCoin('f','computer') : takeCoin('l','computer'); +} + +sub computerChoice { + # Grabs L2 off the end when available + # Doesn't grab the item before L2 to free it up for player to win. + # Otherwise, grabs whichever end is greater. + # It doesn't always get the highest points, but it wins when that's possible. + + my $ind = l2Index(); + + if (scalar @coins == 3) { # Without this statement computer always chooses last (third) + chooseGreater(); # when protecting L2 ( e.g. [first], L2, [last] ) + # even if first is greater. + } else { + + if ($ind == 0 or $ind == $#coins-1 ) { + takeCoin('f','computer'); + + } elsif ( $ind == $#coins or $ind == 1) { + takeCoin('l','computer'); + + } else { + chooseGreater(); + } + } +} + +while (scalar @coins) { + print "@coins\n"; + + if ( $turn ) { + playerChoice(); + --$turn; + } else { + computerChoice(); + ++$turn; + } +} + +if ( $bank{computer} > $bank{player} ) { + printf "Computer Wins. Computer: L %.2f Player: L %.2f\n\n", $bank{computer}, $bank{player}; +} else { + printf "Player Wins. Player: L %.2f Computer: L %.2f\n\n", $bank{player}, $bank{computer}; +} diff --git a/challenge-052/user-person/python/ch-1.py b/challenge-052/user-person/python/ch-1.py new file mode 100755 index 0000000000..b7a0685098 --- /dev/null +++ b/challenge-052/user-person/python/ch-1.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 + +########################################################################### +# script name: ch-1.py # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-052/ # +# # +# Stepping Numbers # +# # +# Write a script to accept two numbers between 100 and 999. It should # +# then print all Stepping Numbers between them. # +# # +# A number is called a stepping number if the adjacent digits have a # +# difference of 1. For example, 456 is a stepping number but 129 is not. # +# # +########################################################################### + +import os +import re +import sys + +step = [] +UPPER_LIMIT = 1000 +LOWER_LIMIT = 99 + +for i in range(1,8): + step.append( (i * 100) + ((i+1) * 10) + (i+2) ) + +errorString = os.path.basename(sys.argv[0]) + ' requires 2 arguments between 100 and 999.' + +if len(sys.argv) != 3: + print(errorString) + exit(1) + +args = [int(arg) for arg in sys.argv[1:]] + +for arg in args: + if arg < LOWER_LIMIT or arg > UPPER_LIMIT: + print(errorString) + exit(1) + +max = min = 0 + +if args[0] < args[1]: + min = args[0] + max = args[1] +elif args[1] < args[0]: + min = args[1] + max = args[0] +else: + max = min = args[0] + +commaFlag = False + +if step[0] > max or step[-1] < min: + exit(0) + +for num in step: + if num >= min and num <= max: + if commaFlag: + print(', ', end='') + print(num, end='') + commaFlag = True + elif commaFlag: + break + +if commaFlag: + print() diff --git a/challenge-052/user-person/python/ch-2.py b/challenge-052/user-person/python/ch-2.py new file mode 100755 index 0000000000..da402bb7db --- /dev/null +++ b/challenge-052/user-person/python/ch-2.py @@ -0,0 +1,128 @@ +#!/usr/bin/env python3 + +########################################################################### +# script name: ch-2.py # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-052/ # +# # +# Lucky Winner # +# Suppose there are following coins arranged on a table in a line in # +# random order # +# # +# 1, 50p, 1p, 10p, 5p, 2, 2p # +# # +# Suppose you are playing against the computer. Player can only pick one # +# coin at a time from either ends. Find out the lucky winner, who has # +# the larger amounts in total?20p, order. # +# # +########################################################################### + +# Coins total 3.88. 2 > 1.88 . If a player gets the L2 coin they win the game. + +# Given sufficiently intelligent players, whoever gets the first turn wins the game. + +import random +import readline + +coins = [ '1p', '2p', '5p', '10p', '20p', '50p', 'L1', 'L2' ] + +random.shuffle(coins) + +coinVal = { '1p' : 0.01, '2p' : 0.02, '5p' : 0.05, '10p' : 0.10, \ + '20p' : 0.20, '50p' : 0.50, 'L1' : 1.00, 'L2' : 2.00 } + +bank = { 'player' : 0, 'computer' : 0 } + +turn = random.choice([True,False]) + +def l2Index(): + ret = -1 + for i in range(len(coins)-1): + if coins[i] == 'L2': + ret = i + break + return ret + +def takeCoin(choice,who): + if choice == 'f': + bank[who] += coinVal[ coins.pop(0) ] + else: # "If you ain't first, you're last." + bank[who] += coinVal[ coins.pop(-1) ] + + print(who + ': ' + '{:.2f}'.format(bank[who]) + '\n') + +def playerChoice(): + fl = '' + loop = True + + prompt = "Type 'f' to choose the first coin. Type 'l' to choose the last coin. Type 'q' to quit:" ; + if len(coins) > 1: + print(prompt) + + while loop: + + loop = False + + if len(coins) == 1: # Don't ask when there's only one choice. + takeCoin( 'f', 'player') + continue + + fl = input('> ') + + if fl == 'f' or fl == 'l': + takeCoin( fl, 'player') + elif fl == 'q': + exit() + else: + print('Invalid choice') + loop = True + +def chooseGreater(): + if coinVal[ coins[0] ] > coinVal[ coins[-1] ] : + takeCoin('f','computer') + else: + takeCoin('l','computer') + +def computerChoice(): + + # Grabs L2 off the end when available + # Doesn't grab the item before L2 to free it up for player to win. + # Otherwise, grabs whichever end is greater. + # It doesn't always get the highest points, but it wins when that's possible. + + ind = l2Index() + + if len(coins) == 3: # Without this statement computer always chooses last (third) + chooseGreater() # when protecting L2 ( e.g. [first], L2, [last] ) + # even if first is greater. + else: + + if ind == 0 or ind == len(coins)-2: + takeCoin('f','computer') + + elif ind == len(coins)-1 or ind == 1: + takeCoin('l','computer') + + else: + chooseGreater() + +while len(coins): + for c in coins: + print(c,end=' ') + print() + + if turn: + playerChoice() + turn = False + else: + computerChoice() + turn = True + +if bank['computer'] > bank['player']: + print('Computer Wins. Computer: L ' + '{:.2f}'.format(bank['computer']) \ + + ' Player: L ' + '{:.2f}'.format(bank['player']) + '\n') +else: + print('Player Wins. Player: L ' + '{:.2f}'.format(bank['player']) \ + + ' Computer: L ' + '{:.2f}'.format(bank['computer']) + '\n') |
