All permutations of a Windows license key
I need to apply for a Windows 8 upgrade for my laptop, for which I need the Windows 7 license key on the underside of the laptop.
Because Microsoft decided in their infinite wisdom to create license labels that wear off, and I cannot read my license key clearly, it means I can't register my laptop for the windows upgrade offer using an automated process.
By holding the laptop at an angle to the light I have been able to verify most of the code but several of the letters are ambiguous (thanks again Microsoft for using easy to misread characters in your label).
I have the following (obfuscated) license key,
MPP6R-09RXG-2H[8B]MT-[B8]K[HN]M9-V[6G]C8R
where the characters in square brackets are ambiguous, so it is either 8
or B
, B
or 8
, H
or N
, 6
or G
.
Making 16 combinations.
Is it appropriate to generate the possible permutations of this license key using itertools or is there a better way?
I got the correct key with thanks to the contributors. A very convenient way to check if the key is valid is by using the Windows 7 product key checker.
Solution 1:
Disclaimer: Yes, I know that this is not Python code. It just popped into my mind and I simply had to write it down.
The simplest way is the use of shell expansion:
$ echo MPP6R-09RXG-2H{8,B}MT-{B,8}K{H,N}M9-V{6,G}C8R
MPP6R-09RXG-2H8MT-BKHM9-V6C8R
MPP6R-09RXG-2H8MT-BKHM9-VGC8R
MPP6R-09RXG-2H8MT-BKNM9-V6C8R
MPP6R-09RXG-2H8MT-BKNM9-VGC8R
MPP6R-09RXG-2H8MT-8KHM9-V6C8R
MPP6R-09RXG-2H8MT-8KHM9-VGC8R
MPP6R-09RXG-2H8MT-8KNM9-V6C8R
MPP6R-09RXG-2H8MT-8KNM9-VGC8R
MPP6R-09RXG-2HBMT-BKHM9-V6C8R
MPP6R-09RXG-2HBMT-BKHM9-VGC8R
MPP6R-09RXG-2HBMT-BKNM9-V6C8R
MPP6R-09RXG-2HBMT-BKNM9-VGC8R
MPP6R-09RXG-2HBMT-8KHM9-V6C8R
MPP6R-09RXG-2HBMT-8KHM9-VGC8R
MPP6R-09RXG-2HBMT-8KNM9-V6C8R
MPP6R-09RXG-2HBMT-8KNM9-VGC8R
Solution 2:
from itertools import product
for perm in product('8B', 'B8', 'HN', '6G'):
print 'MPP6R-09RXG-2H%sMT-%sK%sM9-V%sC8R' % perm
Solution 3:
Another way to generate the combinations
>>> ['MPP6R-09RXG-2H%sMT-%sK%sM9-V%sC8R' % (a, b, c, d)
... for a in '8B' for b in 'B8' for c in 'HN' for d in '6G']
['MPP6R-09RXG-2H8MT-BKHM9-V6C8R',
'MPP6R-09RXG-2H8MT-BKHM9-VGC8R',
'MPP6R-09RXG-2H8MT-BKNM9-V6C8R',
'MPP6R-09RXG-2H8MT-BKNM9-VGC8R',
'MPP6R-09RXG-2H8MT-8KHM9-V6C8R',
'MPP6R-09RXG-2H8MT-8KHM9-VGC8R',
'MPP6R-09RXG-2H8MT-8KNM9-V6C8R',
'MPP6R-09RXG-2H8MT-8KNM9-VGC8R',
'MPP6R-09RXG-2HBMT-BKHM9-V6C8R',
'MPP6R-09RXG-2HBMT-BKHM9-VGC8R',
'MPP6R-09RXG-2HBMT-BKNM9-V6C8R',
'MPP6R-09RXG-2HBMT-BKNM9-VGC8R',
'MPP6R-09RXG-2HBMT-8KHM9-V6C8R',
'MPP6R-09RXG-2HBMT-8KHM9-VGC8R',
'MPP6R-09RXG-2HBMT-8KNM9-V6C8R',
'MPP6R-09RXG-2HBMT-8KNM9-VGC8R']
>>>
Solution 4:
How about using itertools and functools at the same time?
>>> from operator import mod
>>> from functools import partial
>>> from itertools import product
>>> map(partial(mod, 'MPP6R-09RXG-2H%sMT-%sK%sM9-V%sC8R'), product('8B', 'B8', 'HN', '6G'))
['MPP6R-09RXG-2H8MT-BKHM9-V6C8R', 'MPP6R-09RXG-2H8MT-BKHM9-VGC8R', 'MPP6R-09RXG-2H8MT-BKNM9-V6C8R', 'MPP6R-09RXG-2H8MT-BKNM9-VGC8R', 'MPP6R-09RXG-2H8MT-8KHM9-V6C8R', 'MPP6R-09RXG-2H8MT-8KHM9-VGC8R', 'MPP6R-09RXG-2H8MT-8KNM9-V6C8R', 'MPP6R-09RXG-2H8MT-8KNM9-VGC8R', 'MPP6R-09RXG-2HBMT-BKHM9-V6C8R', 'MPP6R-09RXG-2HBMT-BKHM9-VGC8R', 'MPP6R-09RXG-2HBMT-BKNM9-V6C8R', 'MPP6R-09RXG-2HBMT-BKNM9-VGC8R', 'MPP6R-09RXG-2HBMT-8KHM9-V6C8R', 'MPP6R-09RXG-2HBMT-8KHM9-VGC8R', 'MPP6R-09RXG-2HBMT-8KNM9-V6C8R', 'MPP6R-09RXG-2HBMT-8KNM9-VGC8R']