Good day,
I am attempting to write a Python script that has a given hexadecimal value, and a given list of strings.
The script randomly samples the list and pulls out 3, 4-string lists. The strings are joined, converted to integers, converted to hexadecimal values, added together and the sum is compared to the given hexadecimal value.
#HexValue:
value = "%X" % 0x2e1087c4
# Character Set:
charset = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '0b', '0c', '0e', '0f', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '1a', '1b', '1c', '1d', '1e', '1f', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '2a', '2b', '2c', '2d', '2e', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '3b', '3c', '3d', '3e', '41', '42', '43', '44', '45', '46', '47', '48', '49', '4a', '4b', '4c', '4d', '4e', '4f', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '5a', '5b', '5c', '5d', '5e', '5f', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '6a', '6b', '6c', '6d', '6e', '6f', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '7a', '7b', '7c', '7d', '7e', '7f']
# Randomly Sample Three, Four-String Lists:
ran1 = random.sample(charset,4)
ran2 = random.sample(charset,4)
ran3 = random.sample(charset,4)
# Join Lists Into Strings:
str1 = ''.join(ran1)
str2 = ''.join(ran2)
str3 = ''.join(ran3)
# Convert Each String To Integer:
int1 = int(str1, 16)
int2 = int(str2, 16)
int3 = int(str3, 16)
# Convert Each Integer to Hexadecimal:
hex1 = "%X" % int1
hex2 = "%X" % int2
hex3 = "%X" % int3
#Add Hexadecimal Values And Compare Equality to HexValue:
while True:
hex1 + hex2 + hex3 == value
print hex1, "+", hex2, "+", hex3, "=", value
I'm running the script in Python 2.6.4 on Windows. This is the first Python script that I've ever written from start to finish.
I've read on these forums that I may be running into something akin to the Travelling Salesman Problem.
My questions are these:
- Should I be using random.sample, which I've read samples without replacement, or should I be using scipy.maxentropy.maxentutils.sample_wr, which samples with replacement?
- As I am a complete newb, should I give up on this script, and try to find another way to find three hex numbers whose sum is a given hex number?