From e059b73c28f82cd3fe73cc40ba15c1bb94f4d06e Mon Sep 17 00:00:00 2001 From: User Person Date: Sun, 22 Mar 2020 13:29:58 -0400 Subject: User-person's solutions for challenge 52. --- challenge-052/user-person/python/ch-1.py | 70 +++++++++++++++++ challenge-052/user-person/python/ch-2.py | 128 +++++++++++++++++++++++++++++++ 2 files changed, 198 insertions(+) create mode 100755 challenge-052/user-person/python/ch-1.py create mode 100755 challenge-052/user-person/python/ch-2.py (limited to 'challenge-052/user-person/python') 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') -- cgit From d5fa188fc2daf2f251673046c1616945ef9c3cb5 Mon Sep 17 00:00:00 2001 From: user-person <60802990+user-person@users.noreply.github.com> Date: Wed, 25 Mar 2020 14:21:09 -0400 Subject: Create ch-1-CORRECTED.py --- challenge-052/user-person/python/ch-1-CORRECTED.py | 81 ++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 challenge-052/user-person/python/ch-1-CORRECTED.py (limited to 'challenge-052/user-person/python') diff --git a/challenge-052/user-person/python/ch-1-CORRECTED.py b/challenge-052/user-person/python/ch-1-CORRECTED.py new file mode 100644 index 0000000000..0fa2fa5bdd --- /dev/null +++ b/challenge-052/user-person/python/ch-1-CORRECTED.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 + +########################################################################### +# script name: ch-1-CORRECTED.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,10): + if i < 8: # UP UP + step.append( (i * 100) + ((i+1) * 10) + (i+2) ) + + if i > 1: # DOWN DOWN + step.append( (i * 100) + ((i-1) * 10) + (i-2) ) + + if i < 9: # UP DOWN + step.append( (i * 100) + ((i+1) * 10) + i ) + + step.append( (i * 100) + ((i-1) * 10) + i ) # DOWN UP + +step.sort() + +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() -- cgit From f9ddd281b0676717a06f9457acfb53a69b0edec5 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 25 Mar 2020 18:37:50 +0000 Subject: - Tidied up solutions by User Person. --- challenge-052/user-person/python/ch-1-CORRECTED.py | 81 ---------------------- challenge-052/user-person/python/ch-1.py | 17 ++++- 2 files changed, 14 insertions(+), 84 deletions(-) delete mode 100644 challenge-052/user-person/python/ch-1-CORRECTED.py (limited to 'challenge-052/user-person/python') diff --git a/challenge-052/user-person/python/ch-1-CORRECTED.py b/challenge-052/user-person/python/ch-1-CORRECTED.py deleted file mode 100644 index 0fa2fa5bdd..0000000000 --- a/challenge-052/user-person/python/ch-1-CORRECTED.py +++ /dev/null @@ -1,81 +0,0 @@ -#!/usr/bin/env python3 - -########################################################################### -# script name: ch-1-CORRECTED.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,10): - if i < 8: # UP UP - step.append( (i * 100) + ((i+1) * 10) + (i+2) ) - - if i > 1: # DOWN DOWN - step.append( (i * 100) + ((i-1) * 10) + (i-2) ) - - if i < 9: # UP DOWN - step.append( (i * 100) + ((i+1) * 10) + i ) - - step.append( (i * 100) + ((i-1) * 10) + i ) # DOWN UP - -step.sort() - -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-1.py b/challenge-052/user-person/python/ch-1.py index b7a0685098..98b642394d 100755 --- a/challenge-052/user-person/python/ch-1.py +++ b/challenge-052/user-person/python/ch-1.py @@ -25,15 +25,26 @@ step = [] UPPER_LIMIT = 1000 LOWER_LIMIT = 99 -for i in range(1,8): - step.append( (i * 100) + ((i+1) * 10) + (i+2) ) +for i in range(1,10): + if i < 8: # UP UP + step.append( (i * 100) + ((i+1) * 10) + (i+2) ) + + if i > 1: # DOWN DOWN + step.append( (i * 100) + ((i-1) * 10) + (i-2) ) + + if i < 9: # UP DOWN + step.append( (i * 100) + ((i+1) * 10) + i ) + + step.append( (i * 100) + ((i-1) * 10) + i ) # DOWN UP + +step.sort() 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: -- cgit