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 ++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 challenge-052/user-person/python/ch-1.py (limited to 'challenge-052/user-person/python/ch-1.py') 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() -- 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.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'challenge-052/user-person/python/ch-1.py') 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