aboutsummaryrefslogtreecommitdiff
path: root/challenge-052/user-person/python
diff options
context:
space:
mode:
authorUser Person <usermanperson+github@gmail.com>2020-03-22 13:29:58 -0400
committerUser Person <usermanperson+github@gmail.com>2020-03-22 13:29:58 -0400
commite059b73c28f82cd3fe73cc40ba15c1bb94f4d06e (patch)
tree470a1ab2f6fff22787b933dc650a869825c5697e /challenge-052/user-person/python
parent5d9836b8a90e55a5b07f488c2ae018fe925feb47 (diff)
downloadperlweeklychallenge-club-e059b73c28f82cd3fe73cc40ba15c1bb94f4d06e.tar.gz
perlweeklychallenge-club-e059b73c28f82cd3fe73cc40ba15c1bb94f4d06e.tar.bz2
perlweeklychallenge-club-e059b73c28f82cd3fe73cc40ba15c1bb94f4d06e.zip
User-person's solutions for challenge 52.
Diffstat (limited to 'challenge-052/user-person/python')
-rwxr-xr-xchallenge-052/user-person/python/ch-1.py70
-rwxr-xr-xchallenge-052/user-person/python/ch-2.py128
2 files changed, 198 insertions, 0 deletions
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')