The Daily WTF posted this neat Key Lock Box problem.
This was actually the first time I had ever seen a key lock box. Essentially, they’re small steel boxes that contain a key and are often affixed near a keyhole (eg., the door handle). Some feature a set of dials that must be aligned with the right code, while others (such as the one pictured) use a pushbutton system to enter the combination.
Being that they’re such low-tech contraptions, the pushbutton key lock boxes don’t quite work like you might expect. For example, one popular brand will unlock if the four-digit code is entered in sequence, regardless of what or how many digits preceded it. That is, if the code is 2-3-4-5, pressing 1-2-3-4-5 will open the box. Taking it a step further, the sequence 4-8-2-9-5-1-4-5 would cover the codes 4-8-2-9 8-2-9-5 9-5-1-4 and 5-1-4-5.
Your exercise for the day: write a function that outputs a sequence numbers which cover all combinations within the sequence.
Input: two variables: an integer that represents the combination code length, and the number of buttons on Output: The output should be a valid sequence that covers all combinations
My solution is the 10,124 character long string:
000000010002000300040005000600070008000900110012001300140015
001600170018001900210022002300240025002600270028002900310032
003300340035003600370038003900410042004300440045004600470048
004900510052005300540055005600570058005900610062006300640065
006600670068006900710072007300740075007600770078007900810082
008300840085008600870088008900910092009300940095009600970098
009901010102010301040105010601070108010901110112011301140115
011601170118011901210122012301240125012601270128012901310132
013301340135013601370138013901410142014301440145014601470148
014901510152015301540155015601570158015901610162016301640165
016601670168016901710172017301740175017601770178017901810182
018301840185018601870188018901910192019301940195019601970198
019902020203020402050206020702080209021102120213021402150216
021702180219022102220223022402250226022702280229023102320233
023402350236023702380239024102420243024402450246024702480249
025102520253025402550256025702580259026102620263026402650266
026702680269027102720273027402750276027702780279028102820283
028402850286028702880289029102920293029402950296029702980299
030303040305030603070308030903110312031303140315031603170318
031903210322032303240325032603270328032903310332033303340335
033603370338033903410342034303440345034603470348034903510352
035303540355035603570358035903610362036303640365036603670368
036903710372037303740375037603770378037903810382038303840385
038603870388038903910392039303940395039603970398039904040405
040604070408040904110412041304140415041604170418041904210422
042304240425042604270428042904310432043304340435043604370438
043904410442044304440445044604470448044904510452045304540455
045604570458045904610462046304640465046604670468046904710472
047304740475047604770478047904810482048304840485048604870488
048904910492049304940495049604970498049905050506050705080509
051105120513051405150516051705180519052105220523052405250526
052705280529053105320533053405350536053705380539054105420543
054405450546054705480549055105520553055405550556055705580559
056105620563056405650566056705680569057105720573057405750576
057705780579058105820583058405850586058705880589059105920593
059405950596059705980599060606070608060906110612061306140615
061606170618061906210622062306240625062606270628062906310632
063306340635063606370638063906410642064306440645064606470648
064906510652065306540655065606570658065906610662066306640665
066606670668066906710672067306740675067606770678067906810682
068306840685068606870688068906910692069306940695069606970698
069907070708070907110712071307140715071607170718071907210722
072307240725072607270728072907310732073307340735073607370738
073907410742074307440745074607470748074907510752075307540755
075607570758075907610762076307640765076607670768076907710772
077307740775077607770778077907810782078307840785078607870788
078907910792079307940795079607970798079908080809081108120813
081408150816081708180819082108220823082408250826082708280829
083108320833083408350836083708380839084108420843084408450846
084708480849085108520853085408550856085708580859086108620863
086408650866086708680869087108720873087408750876087708780879
088108820883088408850886088708880889089108920893089408950896
089708980899090909110912091309140915091609170918091909210922
092309240925092609270928092909310932093309340935093609370938
093909410942094309440945094609470948094909510952095309540955
095609570958095909610962096309640965096609670968096909710972
097309740975097609770978097909810982098309840985098609870988
098909910992099309940995099609970998099911111112111311141115
111611171118111911221123112411251126112711281129113211331134
113511361137113811391142114311441145114611471148114911521153
115411551156115711581159116211631164116511661167116811691172
117311741175117611771178117911821183118411851186118711881189
119211931194119511961197119811991212121312141215121612171218
121912221223122412251226122712281229123212331234123512361237
123812391242124312441245124612471248124912521253125412551256
125712581259126212631264126512661267126812691272127312741275
127612771278127912821283128412851286128712881289129212931294
129512961297129812991313131413151316131713181319132213231324
132513261327132813291332133313341335133613371338133913421343
134413451346134713481349135213531354135513561357135813591362
136313641365136613671368136913721373137413751376137713781379
138213831384138513861387138813891392139313941395139613971398
139914141415141614171418141914221423142414251426142714281429
143214331434143514361437143814391442144314441445144614471448
144914521453145414551456145714581459146214631464146514661467
146814691472147314741475147614771478147914821483148414851486
148714881489149214931494149514961497149814991515151615171518
151915221523152415251526152715281529153215331534153515361537
153815391542154315441545154615471548154915521553155415551556
155715581559156215631564156515661567156815691572157315741575
157615771578157915821583158415851586158715881589159215931594
159515961597159815991616161716181619162216231624162516261627
162816291632163316341635163616371638163916421643164416451646
164716481649165216531654165516561657165816591662166316641665
166616671668166916721673167416751676167716781679168216831684
168516861687168816891692169316941695169616971698169917171718
171917221723172417251726172717281729173217331734173517361737
173817391742174317441745174617471748174917521753175417551756
175717581759176217631764176517661767176817691772177317741775
177617771778177917821783178417851786178717881789179217931794
179517961797179817991818181918221823182418251826182718281829
183218331834183518361837183818391842184318441845184618471848
184918521853185418551856185718581859186218631864186518661867
186818691872187318741875187618771878187918821883188418851886
188718881889189218931894189518961897189818991919192219231924
192519261927192819291932193319341935193619371938193919421943
194419451946194719481949195219531954195519561957195819591962
196319641965196619671968196919721973197419751976197719781979
198219831984198519861987198819891992199319941995199619971998
199922222223222422252226222722282229223322342235223622372238
223922432244224522462247224822492253225422552256225722582259
226322642265226622672268226922732274227522762277227822792283
228422852286228722882289229322942295229622972298229923232324
232523262327232823292333233423352336233723382339234323442345
234623472348234923532354235523562357235823592363236423652366
236723682369237323742375237623772378237923832384238523862387
238823892393239423952396239723982399242424252426242724282429
243324342435243624372438243924432444244524462447244824492453
245424552456245724582459246324642465246624672468246924732474
247524762477247824792483248424852486248724882489249324942495
249624972498249925252526252725282529253325342535253625372538
253925432544254525462547254825492553255425552556255725582559
256325642565256625672568256925732574257525762577257825792583
258425852586258725882589259325942595259625972598259926262627
262826292633263426352636263726382639264326442645264626472648
264926532654265526562657265826592663266426652666266726682669
267326742675267626772678267926832684268526862687268826892693
269426952696269726982699272727282729273327342735273627372738
273927432744274527462747274827492753275427552756275727582759
276327642765276627672768276927732774277527762777277827792783
278427852786278727882789279327942795279627972798279928282829
283328342835283628372838283928432844284528462847284828492853
285428552856285728582859286328642865286628672868286928732874
287528762877287828792883288428852886288728882889289328942895
289628972898289929292933293429352936293729382939294329442945
294629472948294929532954295529562957295829592963296429652966
296729682969297329742975297629772978297929832984298529862987
298829892993299429952996299729982999333333343335333633373338
333933443345334633473348334933543355335633573358335933643365
336633673368336933743375337633773378337933843385338633873388
338933943395339633973398339934343435343634373438343934443445
344634473448344934543455345634573458345934643465346634673468
346934743475347634773478347934843485348634873488348934943495
349634973498349935353536353735383539354435453546354735483549
355435553556355735583559356435653566356735683569357435753576
357735783579358435853586358735883589359435953596359735983599
363636373638363936443645364636473648364936543655365636573658
365936643665366636673668366936743675367636773678367936843685
368636873688368936943695369636973698369937373738373937443745
374637473748374937543755375637573758375937643765376637673768
376937743775377637773778377937843785378637873788378937943795
379637973798379938383839384438453846384738483849385438553856
385738583859386438653866386738683869387438753876387738783879
388438853886388738883889389438953896389738983899393939443945
394639473948394939543955395639573958395939643965396639673968
396939743975397639773978397939843985398639873988398939943995
399639973998399944444445444644474448444944554456445744584459
446544664467446844694475447644774478447944854486448744884489
449544964497449844994545454645474548454945554556455745584559
456545664567456845694575457645774578457945854586458745884589
459545964597459845994646464746484649465546564657465846594665
466646674668466946754676467746784679468546864687468846894695
469646974698469947474748474947554756475747584759476547664767
476847694775477647774778477947854786478747884789479547964797
479847994848484948554856485748584859486548664867486848694875
487648774878487948854886488748884889489548964897489848994949
495549564957495849594965496649674968496949754976497749784979
498549864987498849894995499649974998499955555556555755585559
556655675568556955765577557855795586558755885589559655975598
559956565657565856595666566756685669567656775678567956865687
568856895696569756985699575757585759576657675768576957765777
577857795786578757885789579657975798579958585859586658675868
586958765877587858795886588758885889589658975898589959595966
596759685969597659775978597959865987598859895996599759985999
666666676668666966776678667966876688668966976698669967676768
676967776778677967876788678967976798679968686869687768786879
688768886889689768986899696969776978697969876988698969976998
699977777778777977887789779877997878787978887889789878997979
79887989799879998888888988988899898989999000
I have the codes in 8889 and 9888 in there twice so it’s a tad longer than it needs to be. I think the optimal solution is the De Bruijn Sequence but that involves math I don’t understand.
My solution was written in Python.
def permutations(values, size):
if size == 0: return [[]]
out = []
for v in values:
for perm in permutations(values, size-1):
out.append([v] + perm)
return out
def combinationString(permutations):
out = ''
for permutation in permutations:
tmp = ''.join([str(num) for num in permutation])
if out.find(tmp) == -1:
out += tmp
return out;
def verify(permutations, combinationString):
for permutation in permutations:
tmp = ''.join([str(num) for num in permutation])
m = combinationString.split(tmp)
if len(m) == 1: print ("[ERROR]", tmp, "is missing")
if len(m) > 2: print ("[WARN]", tmp, "is duplicated")
v = [0,1,2,3,4,5,6,7,8,9]
s = 4
p = permutations(v, s)
c = combinationString(p)
verify(p, c)
print("Combination String:", c)
print(len(c))