I'm coding in python and I have an iterator that iterates through all the possible combinations of a 45 character string. I am generating them one at a time and I have found that it generates from the smallest possible combination which is 0000000..0000 to 0000000..0001. I want to set my own mapping to this so that it can generate from my own specified mapping. I want to do this by setting each value of the combinations to a number so I can set the smallest number to the first iteration.
My code is:
class MyIterator:
def __init__(self, hex_string, repeat, mapping_function=None):
self.hex_string = hex_string
self.repeat = repeat
self.mapping_function = mapping_function
if self.mapping_function:
self.hex_string = self.mapping_function(self.hex_string)
self.combinations = itertools.product(self.hex_string, repeat=repeat)
self.current = fastFunction(self.combinations, None)
def __iter__(self):
return self
def modify_speed(self):
pass
def __next__(self):
if self.current is not None:
private_key = ''.join(self.current)
self.current = fastFunction(self.combinations, None)
return private_key
else:
raise StopIteration
def custom_mapping(hex_string):
pass
iterator = MyIterator("0123456789abcdef", 45, mapping_function=custom_mapping)
for string in iterator:
account = onlyFunction(string)
# do something with the account
I want to set the custom mapping in the custom_mapping function. I want the mapping to map each character in the iterator variable to a number like 0,1,2 etc. that will allow the iterator to generate from whatever is set to 0 to the last character.
Here's the solution in my opinion.
def custom_mapping(hex_string):
# Define the custom mapping here
char_to_number = {
'0': 3, '1': 1, '2': 4, '3': 2, '4': 0,
'5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15
}
sorted_chars = sorted(hex_string, key=lambda char: char_to_number[char])
return ''.join(sorted_chars)
sorted()
function to sort the characters according to the custom order defined in the char_to_number
dictionary. Why i use the lambda function documentation