Lucky and even lucky numbers: Difference between revisions

→‎{{header|Python}}: Add Python example.
(→‎{{header|Perl 6}}: Add Python framework.)
(→‎{{header|Python}}: Add Python example.)
Line 144:
=={{header|Python}}==
The generator
<lang python></lang>from __future__ import print_function
from itertools import islice
import sys, re
 
 
class ArgumentError(Exception):
pass
 
def lgen(even=False, nmax=1000000):
start = 2 if even else 1
n, lst = 1, list(range(start, nmax + 1, 2))
lenlst = len(lst)
yield lst[0]
while n < lenlst and lst[n] < lenlst:
yield lst[n]
n, lst = n + 1, [j for i,j in enumerate(lst, 1) if i % lst[n]]
lenlst = len(lst)
# drain
for i in lst[n:]:
yield i</lang>
 
The argument handler
<lang python></lang>def arghandler(argstring):
match_obj = re.match( r"""(?mx)
(?:
(?P<SINGLE>
(?: ^ (?P<SINGLEL> \d+ ) (?: | \s , \s lucky ) \s* $ )
|(?: ^ (?P<SINGLEE> \d+ ) (?: | \s , \s evenLucky ) \s* $ )
)
|(?P<KTH>
(?: ^ (?P<KTHL> \d+ \s \d+ ) (?: | \s lucky ) \s* $ )
|(?: ^ (?P<KTHE> \d+ \s \d+ ) (?: | \s evenLucky ) \s* $ )
)
|(?P<RANGE>
(?: ^ (?P<RANGEL> \d+ \s -\d+ ) (?: | \s lucky ) \s* $ )
|(?: ^ (?P<RANGEE> \d+ \s -\d+ ) (?: | \s evenLucky ) \s* $ )
)
)""", argstring)
if match_obj:
# Retrieve group(s) by name
SINGLEL = match_obj.group('SINGLEL')
SINGLEE = match_obj.group('SINGLEE')
KTHL = match_obj.group('KTHL')
KTHE = match_obj.group('KTHE')
RANGEL = match_obj.group('RANGEL')
RANGEE = match_obj.group('RANGEE')
if SINGLEL:
j = int(SINGLEL)
assert 0 < j < 10001, "Argument out of range"
print("Single %i'th lucky number:" % j, end=' ')
print( list(islice(lgen(), j-1, j))[0] )
elif SINGLEE:
j = int(SINGLEE)
assert 0 < j < 10001, "Argument out of range"
print("Single %i'th even lucky number:" % j, end=' ')
print( list(islice(lgen(even=True), j-1, j))[0] )
elif KTHL:
j, k = [int(num) for num in KTHL.split()]
assert 0 < j < 10001, "first argument out of range"
assert 0 < k < 10001 and k > j, "second argument out of range"
print("List of %i ... %i lucky numbers:" % (j, k), end=' ')
#print( list(islice(lgen(), j-1, k)) )
for n, luck in enumerate(lgen(), 1):
if n > k: break
if n >=j: print(luck, end = ', ')
print('')
elif KTHE:
j, k = [int(num) for num in KTHE.split()]
assert 0 < j < 10001, "first argument out of range"
assert 0 < k < 10001 and k > j, "second argument out of range"
print("List of %i ... %i even lucky numbers:" % (j, k), end=' ')
#print( list(islice(lgen(even=True), j-1, k)) )
for n, luck in enumerate(lgen(even=True), 1):
if n > k: break
if n >=j: print(luck, end = ', ')
print('')
elif RANGEL:
j, k = [int(num) for num in RANGEL.split()]
assert 0 < j < 10001, "first argument out of range"
assert 0 < -k < 10001 and -k > j, "second argument out of range"
k = -k
print("List of lucky numbers in the range %i ... %i :" % (j, k), end=' ')
for n in lgen():
if n > k: break
if n >=j: print(n, end = ', ')
print('')
elif RANGEE:
j, k = [int(num) for num in RANGEE.split()]
assert 0 < j < 10001, "first argument out of range"
assert 0 < -k < 10001 and -k > j, "second argument out of range"
k = -k
print("List of even lucky numbers in the range %i ... %i :" % (j, k), end=' ')
for n in lgen(even=True):
if n > k: break
if n >=j: print(n, end = ', ')
print('')
else:
raise ArgumentError('''
Error Parsing Arguments!
Expected Arguments of the form (where j and k are integers):
j # Jth lucky number
j , lucky # Jth lucky number
j , evenLucky # Jth even lucky number
#
j k # Jth through Kth (inclusive) lucky numbers
j k lucky # Jth through Kth (inclusive) lucky numbers
j k evenLucky # Jth through Kth (inclusive) even lucky numbers
#
j -k # all lucky numbers in the range j --? |k|
j -k lucky # all lucky numbers in the range j --? |k|
j -k evenLucky # all even lucky numbers in the range j --? |k|
''')
 
if __name__ == '__main__':
arghandler(' '.join(sys.argv[1:]))</lang>
 
{{out}}
<pre># Output when arguments are: 1 20 lucky
<pre></pre>
List of 1 ... 20 lucky numbers: 1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79,
# Output when arguments are: 1 20 evenLucky
List of 1 ... 20 even lucky numbers: 2, 4, 6, 10, 12, 18, 20, 22, 26, 34, 36, 42, 44, 50, 52, 54, 58, 68, 70, 76,
# Output when arguments are: 6000 -6100 lucky
List of lucky numbers in the range 6000 ... 6100 : 6009, 6019, 6031, 6049, 6055, 6061, 6079, 6093,
# Output when arguments are: 6000 -6100 evenLucky
List of even lucky numbers in the range 6000 ... 6100 : 6018, 6020, 6022, 6026, 6036, 6038, 6050, 6058, 6074, 6090, 6092,
# Output when arguments are: 10000
Single 10000'th lucky number: 115591
# Output when arguments are: 10000 , evenLucky
Single 10000'th even lucky number: 111842</pre>
 
=={{header|REXX}}==
Anonymous user