Python [conda env:PY27_Test]

Convert Number To String Sequence

BookMarks

This content is organized almost like a story. Start to finish it demonstrates a bunch of related concepts in Python programming. For later reference, these bookmarks jump to specific sections:

The Problem

This problem was inspired by www.hackerrank.com. The original problem was to take any number N (input by the user), and output a "123...N" result without using any strings to do it. The most elegant solution accepted on www.hackerrank.com is to utilize Python 3 print() function syntax in a single statement that prints range(N) by unpacking it first with an asterick: *.


In [88]:
from __future__ import print_function
    # only need this line for Python 2.7 ... by importing print() we also get support for unpacking within print
    # * for unpacking is not recognized in this context in Python 2.7 normally
    # arguments on print and behavior of print in this example is also Python 3.x which "from _future__" is importing
    
# assume input of 9:
N = 9
print(*range(N+1), sep='', end='')


0123456789

In [476]:
# this larger number test is just for comparison with what follows in the attempt to do this mathematically
N = 113
print(*range(N+1), sep='', end='')


0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113

As a point of curiosity though ... how would we do it using math instead of relying on print tricks which probably convert the numbers to strings under the covers anyway? The basics of the solution is that the code needs to add zeroes to each number in the sequence in the right amount so that added together, they become the "string" of numbers:

    3              1001
   20          10000000
+ 100       99900000000
======    ===============
  123       99910001001

List Comprehensions (Incomplete Solution)

Initially, the problem is approached using a list comprehension to avoid looping. This simple experiment is only a partial solution though:


In [477]:
'''Initial experiment:  use powers of 10, but start from the end so that we get: 1 * 10**N + 2 * 10**N-1 + ...
   This idea fails however as soon as numbers get bigger than 9
   This example outputs the source list generated by range() ahead of the answer just to show it.  '''

def num_to_seqStr(N, showList = True):
    lst = range(1,N+1)
    answer = sum([i*10**(N-i) for i in lst])
    if showList == True:
        print(lst)
    return answer

for i in range(11):
    print("Answer: %s" %num_to_seqStr(i))


[]
Answer: 0
[1]
Answer: 1
[1, 2]
Answer: 12
[1, 2, 3]
Answer: 123
[1, 2, 3, 4]
Answer: 1234
[1, 2, 3, 4, 5]
Answer: 12345
[1, 2, 3, 4, 5, 6]
Answer: 123456
[1, 2, 3, 4, 5, 6, 7]
Answer: 1234567
[1, 2, 3, 4, 5, 6, 7, 8]
Answer: 12345678
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Answer: 123456789
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Answer: 1234567900

Note: At 10, it all goes wrong and the number ends in 7900 instead of 78910. This is because the earlier numbers were not multiplied by sufficient powers of 10 so the numbers will sum together correctly into the final answer.

Solving It With Loops and Incrementers

There are better ways to do all of this, but using loops and incrementers to get the right amount of zeroes for each power of ten encountered establishes a simple baseline for what the program internally needs to do.


In [49]:
def findTens(N):   # find the powers of 10 inside a number
    incrementer = 1
    while True:
        if N - 10**incrementer < 0:
            break
        else:
            incrementer += 1
        
        if incrementer == 100:  
            break   # debugging condition
    return incrementer - 1

In [50]:
findTensTests = [findTens(0),
findTens(7),
findTens(112),
findTens(13),
findTens(1009)]
findTensTests


Out[50]:
[0, 0, 2, 1, 3]

In [410]:
def create_seqNum(N, reverse=False, showWork=False, returnDescr=False, divLength=100):
    '''create_seqNum() --> iput N, and get back a number built from the sequence of 1234...N
Arguments: reverse=True to get the sequence in revers, showWork=True to see numbers that add up to final,
returnDescr=True to print the answer in a sentence as well as returning it as a number.'''
    
    num = 0
    tensIncr = 0
    answer = 0
    Ntens = findTens(N)
    modifier = 0            # modifies counter when increment of 10 occurs
    if reverse == True:     # create range() inputs
        rstart = 1
        rend = N+1
        rinc = 1
    else:
        rstart = N
        rend = 0
        rinc = -1
    for i in range(rstart, rend, rinc):
        itens = findTens(i)
        num = i * 10**tensIncr    # how many zeroes do we need on the end of each num?    
        tensIncr += 1 + itens
        pad = (Ntens - itens)
        
        if showWork == True:
            print(("For %d" + " "*pad + " Add: %d") %(i, num)) 
            
        answer += num
        
    if showWork == True:
        print("#"*divLength)
    
    if showWork == True or returnDescr == True:
        print("Answer: %d" %answer)
        print("#"*divLength)
    
    return answer

In [412]:
print(create_seqNum.__doc__)


create_seqNum() --> iput N, and get back a number built from the sequence of 1234...N
Arguments: reverse=True to get the sequence in revers, showWork=True to see numbers that add up to final,
returnDescr=True to print the answer in a sentence as well as returning it as a number.

In [415]:
for i in [1, 5, 9, 10, 11, 13, 98, 99, 100, 101, 102, 107, 1012]:
    create_seqNum(i, reverse=True, returnDescr=True)
    create_seqNum(i, returnDescr=True)


Answer: 1
####################################################################################################
Answer: 1
####################################################################################################
Answer: 54321
####################################################################################################
Answer: 12345
####################################################################################################
Answer: 987654321
####################################################################################################
Answer: 123456789
####################################################################################################
Answer: 10987654321
####################################################################################################
Answer: 12345678910
####################################################################################################
Answer: 1110987654321
####################################################################################################
Answer: 1234567891011
####################################################################################################
Answer: 13121110987654321
####################################################################################################
Answer: 12345678910111213
####################################################################################################
Answer: 9897969594939291908988878685848382818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321
####################################################################################################
Answer: 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
####################################################################################################
Answer: 999897969594939291908988878685848382818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321
####################################################################################################
Answer: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
####################################################################################################
Answer: 100999897969594939291908988878685848382818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321
####################################################################################################
Answer: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
####################################################################################################
Answer: 101100999897969594939291908988878685848382818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321
####################################################################################################
Answer: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
####################################################################################################
Answer: 102101100999897969594939291908988878685848382818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321
####################################################################################################
Answer: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
####################################################################################################
Answer: 107106105104103102101100999897969594939291908988878685848382818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321
####################################################################################################
Answer: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
####################################################################################################
Answer: 1012101110101009100810071006100510041003100210011000999998997996995994993992991990989988987986985984983982981980979978977976975974973972971970969968967966965964963962961960959958957956955954953952951950949948947946945944943942941940939938937936935934933932931930929928927926925924923922921920919918917916915914913912911910909908907906905904903902901900899898897896895894893892891890889888887886885884883882881880879878877876875874873872871870869868867866865864863862861860859858857856855854853852851850849848847846845844843842841840839838837836835834833832831830829828827826825824823822821820819818817816815814813812811810809808807806805804803802801800799798797796795794793792791790789788787786785784783782781780779778777776775774773772771770769768767766765764763762761760759758757756755754753752751750749748747746745744743742741740739738737736735734733732731730729728727726725724723722721720719718717716715714713712711710709708707706705704703702701700699698697696695694693692691690689688687686685684683682681680679678677676675674673672671670669668667666665664663662661660659658657656655654653652651650649648647646645644643642641640639638637636635634633632631630629628627626625624623622621620619618617616615614613612611610609608607606605604603602601600599598597596595594593592591590589588587586585584583582581580579578577576575574573572571570569568567566565564563562561560559558557556555554553552551550549548547546545544543542541540539538537536535534533532531530529528527526525524523522521520519518517516515514513512511510509508507506505504503502501500499498497496495494493492491490489488487486485484483482481480479478477476475474473472471470469468467466465464463462461460459458457456455454453452451450449448447446445444443442441440439438437436435434433432431430429428427426425424423422421420419418417416415414413412411410409408407406405404403402401400399398397396395394393392391390389388387386385384383382381380379378377376375374373372371370369368367366365364363362361360359358357356355354353352351350349348347346345344343342341340339338337336335334333332331330329328327326325324323322321320319318317316315314313312311310309308307306305304303302301300299298297296295294293292291290289288287286285284283282281280279278277276275274273272271270269268267266265264263262261260259258257256255254253252251250249248247246245244243242241240239238237236235234233232231230229228227226225224223222221220219218217216215214213212211210209208207206205204203202201200199198197196195194193192191190189188187186185184183182181180179178177176175174173172171170169168167166165164163162161160159158157156155154153152151150149148147146145144143142141140139138137136135134133132131130129128127126125124123122121120119118117116115114113112111110109108107106105104103102101100999897969594939291908988878685848382818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321
####################################################################################################
Answer: 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
####################################################################################################

In [421]:
create_seqNum(102, showWork=True)


For 102 Add: 102
For 101 Add: 101000
For 100 Add: 100000000
For 99  Add: 99000000000
For 98  Add: 9800000000000
For 97  Add: 970000000000000
For 96  Add: 96000000000000000
For 95  Add: 9500000000000000000
For 94  Add: 940000000000000000000
For 93  Add: 93000000000000000000000
For 92  Add: 9200000000000000000000000
For 91  Add: 910000000000000000000000000
For 90  Add: 90000000000000000000000000000
For 89  Add: 8900000000000000000000000000000
For 88  Add: 880000000000000000000000000000000
For 87  Add: 87000000000000000000000000000000000
For 86  Add: 8600000000000000000000000000000000000
For 85  Add: 850000000000000000000000000000000000000
For 84  Add: 84000000000000000000000000000000000000000
For 83  Add: 8300000000000000000000000000000000000000000
For 82  Add: 820000000000000000000000000000000000000000000
For 81  Add: 81000000000000000000000000000000000000000000000
For 80  Add: 8000000000000000000000000000000000000000000000000
For 79  Add: 790000000000000000000000000000000000000000000000000
For 78  Add: 78000000000000000000000000000000000000000000000000000
For 77  Add: 7700000000000000000000000000000000000000000000000000000
For 76  Add: 760000000000000000000000000000000000000000000000000000000
For 75  Add: 75000000000000000000000000000000000000000000000000000000000
For 74  Add: 7400000000000000000000000000000000000000000000000000000000000
For 73  Add: 730000000000000000000000000000000000000000000000000000000000000
For 72  Add: 72000000000000000000000000000000000000000000000000000000000000000
For 71  Add: 7100000000000000000000000000000000000000000000000000000000000000000
For 70  Add: 700000000000000000000000000000000000000000000000000000000000000000000
For 69  Add: 69000000000000000000000000000000000000000000000000000000000000000000000
For 68  Add: 6800000000000000000000000000000000000000000000000000000000000000000000000
For 67  Add: 670000000000000000000000000000000000000000000000000000000000000000000000000
For 66  Add: 66000000000000000000000000000000000000000000000000000000000000000000000000000
For 65  Add: 6500000000000000000000000000000000000000000000000000000000000000000000000000000
For 64  Add: 640000000000000000000000000000000000000000000000000000000000000000000000000000000
For 63  Add: 63000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 62  Add: 6200000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 61  Add: 610000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 60  Add: 60000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 59  Add: 5900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 58  Add: 580000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 57  Add: 57000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 56  Add: 5600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 55  Add: 550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 54  Add: 54000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 53  Add: 5300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 52  Add: 520000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 51  Add: 51000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 50  Add: 5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 49  Add: 490000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 48  Add: 48000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 47  Add: 4700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 46  Add: 460000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 45  Add: 45000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 44  Add: 4400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 43  Add: 430000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 42  Add: 42000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 41  Add: 4100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 40  Add: 400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 39  Add: 39000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 38  Add: 3800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 37  Add: 370000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 36  Add: 36000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 35  Add: 3500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 34  Add: 340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 33  Add: 33000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 32  Add: 3200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 31  Add: 310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 30  Add: 30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 29  Add: 2900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 28  Add: 280000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 27  Add: 27000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 26  Add: 2600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 25  Add: 250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 24  Add: 24000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 23  Add: 2300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 22  Add: 220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 21  Add: 21000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 20  Add: 2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 19  Add: 190000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 18  Add: 18000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 17  Add: 1700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 16  Add: 160000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 15  Add: 15000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 14  Add: 1400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 13  Add: 130000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 12  Add: 12000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 11  Add: 1100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 10  Add: 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 9   Add: 9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 8   Add: 80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 7   Add: 700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 6   Add: 6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 5   Add: 50000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 4   Add: 400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 3   Add: 3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 2   Add: 20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
For 1   Add: 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
####################################################################################################
Answer: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
####################################################################################################
Out[421]:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102L

log() To The Rescue for Part Of The Problem

As suggested on Stack Overflow by userID: eruciform, taking the base-10 logarithm of a number should replace the loop logic used in the earlier "findTens()" function. This helps us calculate the powers of 10 in a number to use to help calculate how many zeroes will be needed on each term. This allows the terms to add together into a final that displays them all.


In [51]:
import math                              # needed for log functions
def powOfTens(N):                        # find the powers of 10 inside a number
    return int(math.log10(N))            # converts decimal to whole integer
                                         # int() rounds down no matter how big the decimal
                                         # note: math.log(x, 10) produces floating point rounding errors

In [52]:
# output is of form: (oritinalNumber, powersOfTens)
countTensTest = [(1,    powOfTens(1)),
                 (7,    powOfTens(7)),
                 (112,  powOfTens(112)),
                 (13,   powOfTens(13)),
                 (1009, powOfTens(1009))]
countTensTest


Out[52]:
[(1, 0), (7, 0), (112, 2), (13, 1), (1009, 3)]

In [53]:
listofints = [1,2,3,9,10,11,12,19,99,100,101,102, 999, 1000, 1001, 50102030]

In [54]:
for i in listofints:
    print(i, powOfTens(i), math.log10(i))  # show what we are really calculating: 
                                           # (original, function result, un-modified log)


(1, 0, 0.0)
(2, 0, 0.3010299956639812)
(3, 0, 0.47712125471966244)
(9, 0, 0.9542425094393249)
(10, 1, 1.0)
(11, 1, 1.0413926851582251)
(12, 1, 1.0791812460476249)
(19, 1, 1.2787536009528289)
(99, 1, 1.99563519459755)
(100, 2, 2.0)
(101, 2, 2.0043213737826426)
(102, 2, 2.0086001717619175)
(999, 2, 2.9995654882259823)
(1000, 3, 3.0)
(1001, 3, 3.000434077479319)
(50102030, 7, 7.699855322672388)

Using map(), reduce() and lambda() To Solve It

This code addresses the underlying calculations. It initially tests the code using a number sequence that is not the full 123... range progression of the original problem, but we can tells from the tests that these approaches work. This code is also designed to quickly solve the problem in as few lines as possible (unlike the loop which was also designed to give options on how to show its inner workings).


In [55]:
# source: eruciform on StackOverflow
# note:   powOfTens(x) is just int(math.log10(x))

import math   # to get math.log
listofints = [1,2,3,9,10,11,12,19,99,100,101,102, 999, 1000, 1001, 50102030]
n = reduce(lambda x,y:[x[0]*(10**(y[1]+1))+y[0],0],map(lambda x:[x,powOfTens(x)], listofints))[0]
            # to do this in one line with no external functions, replace powOfTens(x) w/:
            #       int(math.log10(x))
print(n)


123910111219991001011029991000100150102030

In [58]:
# source: eruciform on StackOverflow
# we can also more simply crack this problem with just reduce()
n = reduce(lambda x,y:x*(10**(powOfTens(y)+1))+y,listofints)
            # to do this in one line with no external functions, replace powOfTens(x) w/:
            #       int(math.log10(y))
print(n)


123910111219991001011029991000100150102030

Deconstructing the code from the inside out, this is what it is doing.

map() example with reduce() and lambda()

Starting with the first more complex example using map(), lambda creates an anonymous function that "maps" to all the elements in the list, in this case listofints. The anonymous lambda function applies int(math.log10(x)) to a single x, and then map() "maps" each x in listofints to the lambda function.


In [61]:
listofints = [1,2,3,9,10,11,12,19,99,100,101,102, 999, 1000, 1001, 50102030]
map(lambda x:[x,int(math.log10(x))], listofints)


Out[61]:
[[1, 0],
 [2, 0],
 [3, 0],
 [9, 0],
 [10, 1],
 [11, 1],
 [12, 1],
 [19, 1],
 [99, 1],
 [100, 2],
 [101, 2],
 [102, 2],
 [999, 2],
 [1000, 3],
 [1001, 3],
 [50102030, 7]]

Each sublist now contains [original_number, number_of_tens_in_number]. reduce() "reduces" the list to a single number by applying the lambda function fed into it. It takes each term in the list as function(n1 , n2), then the result goes back into the function as function(result, n3), then function(result2, n4) ... and so on until the entire list is consumed and one answer is spit back.


In [74]:
reduce(lambda x,y:[x[0]*(10**(y[1]+1))+y[0],0],map(lambda x:[x,int(math.log10(x))], listofints))


Out[74]:
[1231012199910010150102030L, 0]

The lambda function being used to reduce it, grabs the first term of of each sublist and multiplies by 10 to the power of the second term of the next sublist + 1 + the next term. The original format of the sublist is preserved by wrapping the whole thing in [] to make it a list that is then used to replace the original [term, number_powers_of_tens] sublist in listofints. This test function can better show what is happening:


In [69]:
# asume this is a subset from a list like this [ ... 999, 1001, 1002, ...]
# after mapping it with powOfTens() or int(math.log10(x)), the first two terms in the sample would look like this:

testVal = [[999, 2], [1001, 3]]  # and it would continue with more terms
testFun = lambda x,y:[x[0]*(10**(y[1]+1))+y[0],0]
testFun(testVal[0], testVal[1])


Out[69]:
[9991001, 0]

In [191]:
# then, as reduce works its way up the list, the answer and the next term feed in like this:
testVal = [[9991001, 2], [1002, 3]]
testFun(testVal[0], testVal[1])


Out[191]:
[99910011002L, 0]

Extracting just the first term is why the whole thing ended with [0] in the original code:

     n = reduce(lambda x,y:[x[0]*(10**(y[1]+1))+y[0],0],map(lambda x:[x,int(math.log(x,10))], listofints))[0]

The original code:


In [79]:
listofints = [1,2,3,9,10,11,12,19,99,100,101,102, 999, 1000, 1001, 50102030]
n = reduce(lambda x,y:x*(10**(int(math.log10(y))+1))+y,listofints)
print(n)


123910111219991001011029991000100150102030

Simpler reduce() and lambda() Example

When the code is simplified to just use use reduce(), now only one lambda function is able to set up the power-of-tens calculation for each number and merge it all into terms needed for reduce(). Picking it apart from the inside, just the lambda logic is demonstrated here:


In [77]:
# for comparison, here is the lambda logic split in two functions from the earlier example
testFun_r = lambda x,y:[x[0]*(10**(y[1]+1))+y[0],0]  # used in outer () to feed into reduce()
testFun_m = lambda x:[x,int(math.log10(x))]          # used in inner () to feed into map()

# for this idea, only one lambda does it all, feeding directly into reduce():
testFun2 = lambda x,y:x*(10**(int(math.log10(y))+1))+y

# test it:
testVal = [999, 1001]
testFun2(testVal[0], testVal[1])


Out[77]:
9991001

In [81]:
# now reduce() applies it across the whole original list: function(n1, n2) = result1, function(result1, n3) = result2 ...
# until it produces a final answer to return ("reducing the list down to one number").

listofints = [1,2,3,10,12,19,99,100,101,50102030]
n = reduce(lambda x,y:x*(10**(int(math.log10(y))+1))+y,listofints)
print(n)


1231012199910010150102030

In [110]:
1*10**(int(math.log10(2))+1)+2


Out[110]:
12

Returning to the original Problem and Eliminating The Loops

After exploring different options, if we are to preserve all the functionality but lose the loops, a solution is chosen that combines reduce() with list comprehensions. Some notes on this solution:

  • this version supports the original 123...N where user passes in N
  • it also supports the listofints used in the quick solutions above
  • showWork now shows what reduce is fusing together to form the final output
  • in accepting either N or listofints it tests the input argument for type. This is not considered good practice, but is kept here to illustrate how to do it. Coding sites generally recommend code be refactored to avoid using type testing within its operation.

In [263]:
# this function presented and tested in earlier sections
# repeated here (unchanged) for conveninece:

import math                              # needed for log functions
def powOfTens(N):                        # find the powers of 10 inside a number
    if N < 1:
        N = 1                            # less than 1 would throw an error, this is a work-around based on
                                         # how this function is used in the code
            
    return int(math.log10(N))            # converts decimal to whole integer
                                         # int() rounds down no matter how big the decimal
                                         # note: math.log(x, 10) produces floating point rounding errors

In [269]:
from __future__ import print_function

def create_seqNumber(N, reverse=False, showWork=False, returnDescr=False, divLength=100):
    '''create_seqNumber() --> iput N, and get back a number built from the sequence of 1234...N
Arguments: reverse=True to get the sequence in revers, showWork=True to see numbers that add up to final,
returnDescr=True to print the answer in a sentence as well as returning it as a number.'''
    
    num = 0
    tensIncr = 0
    answer = 0
        
    if isinstance(N, list):
        if reverse == False:
            listofints = N
        else:
            listofints = N[::-1]
    elif isinstance(N, int):
        Ntens = powOfTens(N)
        if reverse == False:     # create range builder inputs
            rstart = 1
            rend = N+1
            rinc = 1
        else:
            rstart = N
            rend = 0
            rinc = -1
        listofints = range(rstart, rend, rinc)
    else:
        print(type(N))
        raise TypeError("Error: for create_seqNumber(N), N must be a list or an integer.")
        
    answer = reduce(lambda x,y:x*(10**(powOfTens(y)+1))+y,listofints)
    
    if showWork == True:        
        print("Show Work:")
        print("#"*divLength)
                
        worklist = [reduce(lambda x,y:x*(10**(powOfTens(y)+1))+y, listofints[0:2])]
        worklist.append([reduce(lambda a,b:a*(10**(powOfTens(b)+1))+b, [worklist[-1], x]) 
                     for ind, x in enumerate(listofints[2:])])
        
        worklist = [worklist[0]] + worklist[1]
        
#         print("worklist: %s" %worklist)
#         print("worklist[-1]", worklist[-1])

        NpOfT  = powOfTens(worklist[-1])
        NpOfT2 = powOfTens(len(worklist)-1)
        [(x, print(("%d)" + " "*(NpOfT2 - powOfTens(ind)) + " "*(NpOfT - powOfTens(x) + 1) + "%s") %(ind, x)))[0] 
         for ind, x in enumerate(worklist)]
        
        print("#"*divLength)
    
    if showWork == True or returnDescr == True:
        print("Answer: %d" %answer)
        print("#"*divLength)
    
    return answer
Testing with showWork Off

These test cases show functionality that supports forward and reverse lists generated from a single number, or feeding in a custom list and spitting it back in forward or reverse.


In [279]:
create_seqNumber(15, showWork=False, returnDescr=True)


Answer: 123456789101112131415
####################################################################################################
Out[279]:
123456789101112131415L

In [214]:
create_seqNumber(15, reverse=True, showWork=False, returnDescr=True)


Answer: 151413121110987654321
####################################################################################################
Out[214]:
151413121110987654321L

In [215]:
create_seqNumber(102, reverse=False, showWork=False, returnDescr=True)


Answer: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
####################################################################################################
Out[215]:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102L

In [216]:
for i in [1, 5, 9, 10, 11, 13, 98, 99, 100, 101, 102, 107, 1012]:   # returnDescr = False by default
    print("F: %s" %(create_seqNumber(i, reverse=False)))
    print("-----"*25)
    print("R: %s" %(create_seqNumber(i, reverse=True)))
    print("#####"*25)


F: 1
-----------------------------------------------------------------------------------------------------------------------------
R: 1
#############################################################################################################################
F: 12345
-----------------------------------------------------------------------------------------------------------------------------
R: 54321
#############################################################################################################################
F: 123456789
-----------------------------------------------------------------------------------------------------------------------------
R: 987654321
#############################################################################################################################
F: 12345678910
-----------------------------------------------------------------------------------------------------------------------------
R: 10987654321
#############################################################################################################################
F: 1234567891011
-----------------------------------------------------------------------------------------------------------------------------
R: 1110987654321
#############################################################################################################################
F: 12345678910111213
-----------------------------------------------------------------------------------------------------------------------------
R: 13121110987654321
#############################################################################################################################
F: 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
-----------------------------------------------------------------------------------------------------------------------------
R: 9897969594939291908988878685848382818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321
#############################################################################################################################
F: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
-----------------------------------------------------------------------------------------------------------------------------
R: 999897969594939291908988878685848382818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321
#############################################################################################################################
F: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
-----------------------------------------------------------------------------------------------------------------------------
R: 100999897969594939291908988878685848382818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321
#############################################################################################################################
F: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
-----------------------------------------------------------------------------------------------------------------------------
R: 101100999897969594939291908988878685848382818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321
#############################################################################################################################
F: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
-----------------------------------------------------------------------------------------------------------------------------
R: 102101100999897969594939291908988878685848382818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321
#############################################################################################################################
F: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
-----------------------------------------------------------------------------------------------------------------------------
R: 107106105104103102101100999897969594939291908988878685848382818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321
#############################################################################################################################
F: 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
-----------------------------------------------------------------------------------------------------------------------------
R: 1012101110101009100810071006100510041003100210011000999998997996995994993992991990989988987986985984983982981980979978977976975974973972971970969968967966965964963962961960959958957956955954953952951950949948947946945944943942941940939938937936935934933932931930929928927926925924923922921920919918917916915914913912911910909908907906905904903902901900899898897896895894893892891890889888887886885884883882881880879878877876875874873872871870869868867866865864863862861860859858857856855854853852851850849848847846845844843842841840839838837836835834833832831830829828827826825824823822821820819818817816815814813812811810809808807806805804803802801800799798797796795794793792791790789788787786785784783782781780779778777776775774773772771770769768767766765764763762761760759758757756755754753752751750749748747746745744743742741740739738737736735734733732731730729728727726725724723722721720719718717716715714713712711710709708707706705704703702701700699698697696695694693692691690689688687686685684683682681680679678677676675674673672671670669668667666665664663662661660659658657656655654653652651650649648647646645644643642641640639638637636635634633632631630629628627626625624623622621620619618617616615614613612611610609608607606605604603602601600599598597596595594593592591590589588587586585584583582581580579578577576575574573572571570569568567566565564563562561560559558557556555554553552551550549548547546545544543542541540539538537536535534533532531530529528527526525524523522521520519518517516515514513512511510509508507506505504503502501500499498497496495494493492491490489488487486485484483482481480479478477476475474473472471470469468467466465464463462461460459458457456455454453452451450449448447446445444443442441440439438437436435434433432431430429428427426425424423422421420419418417416415414413412411410409408407406405404403402401400399398397396395394393392391390389388387386385384383382381380379378377376375374373372371370369368367366365364363362361360359358357356355354353352351350349348347346345344343342341340339338337336335334333332331330329328327326325324323322321320319318317316315314313312311310309308307306305304303302301300299298297296295294293292291290289288287286285284283282281280279278277276275274273272271270269268267266265264263262261260259258257256255254253252251250249248247246245244243242241240239238237236235234233232231230229228227226225224223222221220219218217216215214213212211210209208207206205204203202201200199198197196195194193192191190189188187186185184183182181180179178177176175174173172171170169168167166165164163162161160159158157156155154153152151150149148147146145144143142141140139138137136135134133132131130129128127126125124123122121120119118117116115114113112111110109108107106105104103102101100999897969594939291908988878685848382818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231302928272625242322212019181716151413121110987654321
#############################################################################################################################

In [217]:
tstLst = [1,2,3,9,10,11,12,59,99,100,101,50102030]
print("F: %s" %(create_seqNumber(tstLst, reverse=False)))
print("-----"*25)
print("R: %s" %(create_seqNumber(tstLst, reverse=True)))
print("#####"*25)


F: 1239101112599910010150102030
-----------------------------------------------------------------------------------------------------------------------------
R: 5010203010110099591211109321
#############################################################################################################################
Testing with showWork On

Code to do this is a bit clumsy and inefficient. Due to how lengthy the output, only a few test cases are shown.


In [277]:
create_seqNumber(3, reverse=False, showWork=True, returnDescr=True)


Show Work:
####################################################################################################
0)  12
1) 123
####################################################################################################
Answer: 123
####################################################################################################
Out[277]:
123

In [278]:
create_seqNumber(13, reverse=True, showWork=True, returnDescr=True)


Show Work:
####################################################################################################
0)   1312
1) 131211
2) 131210
3)  13129
4)  13128
5)  13127
6)  13126
7)  13125
8)  13124
9)  13123
10) 13122
11) 13121
####################################################################################################
Answer: 13121110987654321
####################################################################################################
Out[278]:
13121110987654321L

In [276]:
tstLst = [1,2,3,9,10,11,12,59,99,100,101,50102030]
create_seqNumber(tstLst, reverse=False, showWork=True, returnDescr=True)


Show Work:
####################################################################################################
0)          12
1)         123
2)         129
3)        1210
4)        1211
5)        1212
6)        1259
7)        1299
8)       12100
9)       12101
10) 1250102030
####################################################################################################
Answer: 1239101112599910010150102030
####################################################################################################
Out[276]:
1239101112599910010150102030L

In [272]:
create_seqNumber(1003, reverse=False, showWork=True, returnDescr=True)


Show Work:
####################################################################################################
0)        12
1)       123
2)       124
3)       125
4)       126
5)       127
6)       128
7)       129
8)      1210
9)      1211
10)     1212
11)     1213
12)     1214
13)     1215
14)     1216
15)     1217
16)     1218
17)     1219
18)     1220
19)     1221
20)     1222
21)     1223
22)     1224
23)     1225
24)     1226
25)     1227
26)     1228
27)     1229
28)     1230
29)     1231
30)     1232
31)     1233
32)     1234
33)     1235
34)     1236
35)     1237
36)     1238
37)     1239
38)     1240
39)     1241
40)     1242
41)     1243
42)     1244
43)     1245
44)     1246
45)     1247
46)     1248
47)     1249
48)     1250
49)     1251
50)     1252
51)     1253
52)     1254
53)     1255
54)     1256
55)     1257
56)     1258
57)     1259
58)     1260
59)     1261
60)     1262
61)     1263
62)     1264
63)     1265
64)     1266
65)     1267
66)     1268
67)     1269
68)     1270
69)     1271
70)     1272
71)     1273
72)     1274
73)     1275
74)     1276
75)     1277
76)     1278
77)     1279
78)     1280
79)     1281
80)     1282
81)     1283
82)     1284
83)     1285
84)     1286
85)     1287
86)     1288
87)     1289
88)     1290
89)     1291
90)     1292
91)     1293
92)     1294
93)     1295
94)     1296
95)     1297
96)     1298
97)     1299
98)    12100
99)    12101
100)   12102
101)   12103
102)   12104
103)   12105
104)   12106
105)   12107
106)   12108
107)   12109
108)   12110
109)   12111
110)   12112
111)   12113
112)   12114
113)   12115
114)   12116
115)   12117
116)   12118
117)   12119
118)   12120
119)   12121
120)   12122
121)   12123
122)   12124
123)   12125
124)   12126
125)   12127
126)   12128
127)   12129
128)   12130
129)   12131
130)   12132
131)   12133
132)   12134
133)   12135
134)   12136
135)   12137
136)   12138
137)   12139
138)   12140
139)   12141
140)   12142
141)   12143
142)   12144
143)   12145
144)   12146
145)   12147
146)   12148
147)   12149
148)   12150
149)   12151
150)   12152
151)   12153
152)   12154
153)   12155
154)   12156
155)   12157
156)   12158
157)   12159
158)   12160
159)   12161
160)   12162
161)   12163
162)   12164
163)   12165
164)   12166
165)   12167
166)   12168
167)   12169
168)   12170
169)   12171
170)   12172
171)   12173
172)   12174
173)   12175
174)   12176
175)   12177
176)   12178
177)   12179
178)   12180
179)   12181
180)   12182
181)   12183
182)   12184
183)   12185
184)   12186
185)   12187
186)   12188
187)   12189
188)   12190
189)   12191
190)   12192
191)   12193
192)   12194
193)   12195
194)   12196
195)   12197
196)   12198
197)   12199
198)   12200
199)   12201
200)   12202
201)   12203
202)   12204
203)   12205
204)   12206
205)   12207
206)   12208
207)   12209
208)   12210
209)   12211
210)   12212
211)   12213
212)   12214
213)   12215
214)   12216
215)   12217
216)   12218
217)   12219
218)   12220
219)   12221
220)   12222
221)   12223
222)   12224
223)   12225
224)   12226
225)   12227
226)   12228
227)   12229
228)   12230
229)   12231
230)   12232
231)   12233
232)   12234
233)   12235
234)   12236
235)   12237
236)   12238
237)   12239
238)   12240
239)   12241
240)   12242
241)   12243
242)   12244
243)   12245
244)   12246
245)   12247
246)   12248
247)   12249
248)   12250
249)   12251
250)   12252
251)   12253
252)   12254
253)   12255
254)   12256
255)   12257
256)   12258
257)   12259
258)   12260
259)   12261
260)   12262
261)   12263
262)   12264
263)   12265
264)   12266
265)   12267
266)   12268
267)   12269
268)   12270
269)   12271
270)   12272
271)   12273
272)   12274
273)   12275
274)   12276
275)   12277
276)   12278
277)   12279
278)   12280
279)   12281
280)   12282
281)   12283
282)   12284
283)   12285
284)   12286
285)   12287
286)   12288
287)   12289
288)   12290
289)   12291
290)   12292
291)   12293
292)   12294
293)   12295
294)   12296
295)   12297
296)   12298
297)   12299
298)   12300
299)   12301
300)   12302
301)   12303
302)   12304
303)   12305
304)   12306
305)   12307
306)   12308
307)   12309
308)   12310
309)   12311
310)   12312
311)   12313
312)   12314
313)   12315
314)   12316
315)   12317
316)   12318
317)   12319
318)   12320
319)   12321
320)   12322
321)   12323
322)   12324
323)   12325
324)   12326
325)   12327
326)   12328
327)   12329
328)   12330
329)   12331
330)   12332
331)   12333
332)   12334
333)   12335
334)   12336
335)   12337
336)   12338
337)   12339
338)   12340
339)   12341
340)   12342
341)   12343
342)   12344
343)   12345
344)   12346
345)   12347
346)   12348
347)   12349
348)   12350
349)   12351
350)   12352
351)   12353
352)   12354
353)   12355
354)   12356
355)   12357
356)   12358
357)   12359
358)   12360
359)   12361
360)   12362
361)   12363
362)   12364
363)   12365
364)   12366
365)   12367
366)   12368
367)   12369
368)   12370
369)   12371
370)   12372
371)   12373
372)   12374
373)   12375
374)   12376
375)   12377
376)   12378
377)   12379
378)   12380
379)   12381
380)   12382
381)   12383
382)   12384
383)   12385
384)   12386
385)   12387
386)   12388
387)   12389
388)   12390
389)   12391
390)   12392
391)   12393
392)   12394
393)   12395
394)   12396
395)   12397
396)   12398
397)   12399
398)   12400
399)   12401
400)   12402
401)   12403
402)   12404
403)   12405
404)   12406
405)   12407
406)   12408
407)   12409
408)   12410
409)   12411
410)   12412
411)   12413
412)   12414
413)   12415
414)   12416
415)   12417
416)   12418
417)   12419
418)   12420
419)   12421
420)   12422
421)   12423
422)   12424
423)   12425
424)   12426
425)   12427
426)   12428
427)   12429
428)   12430
429)   12431
430)   12432
431)   12433
432)   12434
433)   12435
434)   12436
435)   12437
436)   12438
437)   12439
438)   12440
439)   12441
440)   12442
441)   12443
442)   12444
443)   12445
444)   12446
445)   12447
446)   12448
447)   12449
448)   12450
449)   12451
450)   12452
451)   12453
452)   12454
453)   12455
454)   12456
455)   12457
456)   12458
457)   12459
458)   12460
459)   12461
460)   12462
461)   12463
462)   12464
463)   12465
464)   12466
465)   12467
466)   12468
467)   12469
468)   12470
469)   12471
470)   12472
471)   12473
472)   12474
473)   12475
474)   12476
475)   12477
476)   12478
477)   12479
478)   12480
479)   12481
480)   12482
481)   12483
482)   12484
483)   12485
484)   12486
485)   12487
486)   12488
487)   12489
488)   12490
489)   12491
490)   12492
491)   12493
492)   12494
493)   12495
494)   12496
495)   12497
496)   12498
497)   12499
498)   12500
499)   12501
500)   12502
501)   12503
502)   12504
503)   12505
504)   12506
505)   12507
506)   12508
507)   12509
508)   12510
509)   12511
510)   12512
511)   12513
512)   12514
513)   12515
514)   12516
515)   12517
516)   12518
517)   12519
518)   12520
519)   12521
520)   12522
521)   12523
522)   12524
523)   12525
524)   12526
525)   12527
526)   12528
527)   12529
528)   12530
529)   12531
530)   12532
531)   12533
532)   12534
533)   12535
534)   12536
535)   12537
536)   12538
537)   12539
538)   12540
539)   12541
540)   12542
541)   12543
542)   12544
543)   12545
544)   12546
545)   12547
546)   12548
547)   12549
548)   12550
549)   12551
550)   12552
551)   12553
552)   12554
553)   12555
554)   12556
555)   12557
556)   12558
557)   12559
558)   12560
559)   12561
560)   12562
561)   12563
562)   12564
563)   12565
564)   12566
565)   12567
566)   12568
567)   12569
568)   12570
569)   12571
570)   12572
571)   12573
572)   12574
573)   12575
574)   12576
575)   12577
576)   12578
577)   12579
578)   12580
579)   12581
580)   12582
581)   12583
582)   12584
583)   12585
584)   12586
585)   12587
586)   12588
587)   12589
588)   12590
589)   12591
590)   12592
591)   12593
592)   12594
593)   12595
594)   12596
595)   12597
596)   12598
597)   12599
598)   12600
599)   12601
600)   12602
601)   12603
602)   12604
603)   12605
604)   12606
605)   12607
606)   12608
607)   12609
608)   12610
609)   12611
610)   12612
611)   12613
612)   12614
613)   12615
614)   12616
615)   12617
616)   12618
617)   12619
618)   12620
619)   12621
620)   12622
621)   12623
622)   12624
623)   12625
624)   12626
625)   12627
626)   12628
627)   12629
628)   12630
629)   12631
630)   12632
631)   12633
632)   12634
633)   12635
634)   12636
635)   12637
636)   12638
637)   12639
638)   12640
639)   12641
640)   12642
641)   12643
642)   12644
643)   12645
644)   12646
645)   12647
646)   12648
647)   12649
648)   12650
649)   12651
650)   12652
651)   12653
652)   12654
653)   12655
654)   12656
655)   12657
656)   12658
657)   12659
658)   12660
659)   12661
660)   12662
661)   12663
662)   12664
663)   12665
664)   12666
665)   12667
666)   12668
667)   12669
668)   12670
669)   12671
670)   12672
671)   12673
672)   12674
673)   12675
674)   12676
675)   12677
676)   12678
677)   12679
678)   12680
679)   12681
680)   12682
681)   12683
682)   12684
683)   12685
684)   12686
685)   12687
686)   12688
687)   12689
688)   12690
689)   12691
690)   12692
691)   12693
692)   12694
693)   12695
694)   12696
695)   12697
696)   12698
697)   12699
698)   12700
699)   12701
700)   12702
701)   12703
702)   12704
703)   12705
704)   12706
705)   12707
706)   12708
707)   12709
708)   12710
709)   12711
710)   12712
711)   12713
712)   12714
713)   12715
714)   12716
715)   12717
716)   12718
717)   12719
718)   12720
719)   12721
720)   12722
721)   12723
722)   12724
723)   12725
724)   12726
725)   12727
726)   12728
727)   12729
728)   12730
729)   12731
730)   12732
731)   12733
732)   12734
733)   12735
734)   12736
735)   12737
736)   12738
737)   12739
738)   12740
739)   12741
740)   12742
741)   12743
742)   12744
743)   12745
744)   12746
745)   12747
746)   12748
747)   12749
748)   12750
749)   12751
750)   12752
751)   12753
752)   12754
753)   12755
754)   12756
755)   12757
756)   12758
757)   12759
758)   12760
759)   12761
760)   12762
761)   12763
762)   12764
763)   12765
764)   12766
765)   12767
766)   12768
767)   12769
768)   12770
769)   12771
770)   12772
771)   12773
772)   12774
773)   12775
774)   12776
775)   12777
776)   12778
777)   12779
778)   12780
779)   12781
780)   12782
781)   12783
782)   12784
783)   12785
784)   12786
785)   12787
786)   12788
787)   12789
788)   12790
789)   12791
790)   12792
791)   12793
792)   12794
793)   12795
794)   12796
795)   12797
796)   12798
797)   12799
798)   12800
799)   12801
800)   12802
801)   12803
802)   12804
803)   12805
804)   12806
805)   12807
806)   12808
807)   12809
808)   12810
809)   12811
810)   12812
811)   12813
812)   12814
813)   12815
814)   12816
815)   12817
816)   12818
817)   12819
818)   12820
819)   12821
820)   12822
821)   12823
822)   12824
823)   12825
824)   12826
825)   12827
826)   12828
827)   12829
828)   12830
829)   12831
830)   12832
831)   12833
832)   12834
833)   12835
834)   12836
835)   12837
836)   12838
837)   12839
838)   12840
839)   12841
840)   12842
841)   12843
842)   12844
843)   12845
844)   12846
845)   12847
846)   12848
847)   12849
848)   12850
849)   12851
850)   12852
851)   12853
852)   12854
853)   12855
854)   12856
855)   12857
856)   12858
857)   12859
858)   12860
859)   12861
860)   12862
861)   12863
862)   12864
863)   12865
864)   12866
865)   12867
866)   12868
867)   12869
868)   12870
869)   12871
870)   12872
871)   12873
872)   12874
873)   12875
874)   12876
875)   12877
876)   12878
877)   12879
878)   12880
879)   12881
880)   12882
881)   12883
882)   12884
883)   12885
884)   12886
885)   12887
886)   12888
887)   12889
888)   12890
889)   12891
890)   12892
891)   12893
892)   12894
893)   12895
894)   12896
895)   12897
896)   12898
897)   12899
898)   12900
899)   12901
900)   12902
901)   12903
902)   12904
903)   12905
904)   12906
905)   12907
906)   12908
907)   12909
908)   12910
909)   12911
910)   12912
911)   12913
912)   12914
913)   12915
914)   12916
915)   12917
916)   12918
917)   12919
918)   12920
919)   12921
920)   12922
921)   12923
922)   12924
923)   12925
924)   12926
925)   12927
926)   12928
927)   12929
928)   12930
929)   12931
930)   12932
931)   12933
932)   12934
933)   12935
934)   12936
935)   12937
936)   12938
937)   12939
938)   12940
939)   12941
940)   12942
941)   12943
942)   12944
943)   12945
944)   12946
945)   12947
946)   12948
947)   12949
948)   12950
949)   12951
950)   12952
951)   12953
952)   12954
953)   12955
954)   12956
955)   12957
956)   12958
957)   12959
958)   12960
959)   12961
960)   12962
961)   12963
962)   12964
963)   12965
964)   12966
965)   12967
966)   12968
967)   12969
968)   12970
969)   12971
970)   12972
971)   12973
972)   12974
973)   12975
974)   12976
975)   12977
976)   12978
977)   12979
978)   12980
979)   12981
980)   12982
981)   12983
982)   12984
983)   12985
984)   12986
985)   12987
986)   12988
987)   12989
988)   12990
989)   12991
990)   12992
991)   12993
992)   12994
993)   12995
994)   12996
995)   12997
996)   12998
997)   12999
998)  121000
999)  121001
1000) 121002
1001) 121003
####################################################################################################
Answer: 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
####################################################################################################
Out[272]:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003L

In [280]:
create_seqNumber('15', showWork=False, returnDescr=True) # demo of TypeError the code raises for wrong input


<type 'str'>
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-280-87ad3c776202> in <module>()
----> 1 create_seqNumber('15', showWork=False, returnDescr=True)

<ipython-input-269-77702f44ceda> in create_seqNumber(N, reverse, showWork, returnDescr, divLength)
     33     else:
     34         print(type(N))
---> 35         raise TypeError("Error: for create_seqNumber(N), N must be a list or an integer.")
     36 
     37     # workList = [i * 10**((i-1) + findTens(i)) for i in range(rstart, rend, rinc)]

TypeError: Error: for create_seqNumber(N), N must be a list or an integer.

For more information on the python functions explored here:


In [ ]:


In [ ]: