Reducing the complexity of a python 2 code [closed]
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
-2
down vote
favorite
I have the following piece of code. But, the code is time costly and the for loops are also not very effective. Is there any way, I can make this piece of my code more better by changing the syntax.
frequency_list =
frequency_probability_dict =
for k, v in substitution_table.items():
if len(v) > 1:
print 'length above 1::', k, len(v), v
frequency_list = v
frequency_sum = 0
for tup in frequency_list:
frequency_sum += tup[1]
key_list =
prob_list =
key_prob_list =
for tup1 in frequency_list:
key = tup1[0]
prob = tup1[1] / frequency_sum
key_list.append(key)
prob_list.append(prob)
key_prob_list.append(key_list)
key_prob_list.append(prob_list)
frequency_probability_dict[k] = key_prob_list
#print "&&&&",frequency_probability_dict
encrypted_attack_payload = ""
for ch in attack_payload:
if ch in frequency_probability_dict.keys():
probability_keys = frequency_probability_dict[ch][0]
probability_values = frequency_probability_dict[ch][1]
encrypted_attack_payload = encrypted_attack_payload +
np.random.choice(probability_keys, 1, p=probability_values)[0]
else:
encrypted_attack_payload = encrypted_attack_payload + substitution_table[ch][0][0]
attack_payload_list = list(attack_payload)
result = list(encrypted_attack_payload)
#build xor_table
for idx, val in enumerate(attack_payload_list):
xor_table.append(chr(ord(val) ^ ord(result[idx])))
and Lines 84 to 110 in substitution.py-
substitution_table =
i=0
for tup in sorted_attack_frequency:
substitution_table[tup[0]] = [sorted_artificial_frequency[i]]
i=i+1
#2nd loop for the remaining elements in artificial: starting from m+1
remianing_frequencies =
for num in range(i, len(sorted_artificial_frequency)):
remianing_frequencies.append(sorted_artificial_frequency[num])
for tup in remianing_frequencies:
temp_dict =
for tup1 in sorted_attack_frequency:
key = tup1[0]
value = tup1[1]
char_sub_list = substitution_table[key]
value2=0
for tup2 in char_sub_list:
value2 = value2 + tup2[1]
value = value/value2
temp_dict[key] = value
max_key = max(temp_dict.iteritems(), key=operator.itemgetter(1))[0]
substitution_table[max_key].append(tup)
Any suggestion will be highly appreciated.
I know that for loop works per operations and are heavy and time costly, so I just want to make it less complex and faster.
python object-oriented python-2.7
closed as unclear what you're asking by Mathias Ettinger, Ludisposed, yuri, Ben Steffan, Stephen Rauch Jul 29 at 20:07
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |Â
up vote
-2
down vote
favorite
I have the following piece of code. But, the code is time costly and the for loops are also not very effective. Is there any way, I can make this piece of my code more better by changing the syntax.
frequency_list =
frequency_probability_dict =
for k, v in substitution_table.items():
if len(v) > 1:
print 'length above 1::', k, len(v), v
frequency_list = v
frequency_sum = 0
for tup in frequency_list:
frequency_sum += tup[1]
key_list =
prob_list =
key_prob_list =
for tup1 in frequency_list:
key = tup1[0]
prob = tup1[1] / frequency_sum
key_list.append(key)
prob_list.append(prob)
key_prob_list.append(key_list)
key_prob_list.append(prob_list)
frequency_probability_dict[k] = key_prob_list
#print "&&&&",frequency_probability_dict
encrypted_attack_payload = ""
for ch in attack_payload:
if ch in frequency_probability_dict.keys():
probability_keys = frequency_probability_dict[ch][0]
probability_values = frequency_probability_dict[ch][1]
encrypted_attack_payload = encrypted_attack_payload +
np.random.choice(probability_keys, 1, p=probability_values)[0]
else:
encrypted_attack_payload = encrypted_attack_payload + substitution_table[ch][0][0]
attack_payload_list = list(attack_payload)
result = list(encrypted_attack_payload)
#build xor_table
for idx, val in enumerate(attack_payload_list):
xor_table.append(chr(ord(val) ^ ord(result[idx])))
and Lines 84 to 110 in substitution.py-
substitution_table =
i=0
for tup in sorted_attack_frequency:
substitution_table[tup[0]] = [sorted_artificial_frequency[i]]
i=i+1
#2nd loop for the remaining elements in artificial: starting from m+1
remianing_frequencies =
for num in range(i, len(sorted_artificial_frequency)):
remianing_frequencies.append(sorted_artificial_frequency[num])
for tup in remianing_frequencies:
temp_dict =
for tup1 in sorted_attack_frequency:
key = tup1[0]
value = tup1[1]
char_sub_list = substitution_table[key]
value2=0
for tup2 in char_sub_list:
value2 = value2 + tup2[1]
value = value/value2
temp_dict[key] = value
max_key = max(temp_dict.iteritems(), key=operator.itemgetter(1))[0]
substitution_table[max_key].append(tup)
Any suggestion will be highly appreciated.
I know that for loop works per operations and are heavy and time costly, so I just want to make it less complex and faster.
python object-oriented python-2.7
closed as unclear what you're asking by Mathias Ettinger, Ludisposed, yuri, Ben Steffan, Stephen Rauch Jul 29 at 20:07
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
5
What does this code do? What problem are you trying to solve?
â Ludisposed
Jul 29 at 18:07
add a comment |Â
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I have the following piece of code. But, the code is time costly and the for loops are also not very effective. Is there any way, I can make this piece of my code more better by changing the syntax.
frequency_list =
frequency_probability_dict =
for k, v in substitution_table.items():
if len(v) > 1:
print 'length above 1::', k, len(v), v
frequency_list = v
frequency_sum = 0
for tup in frequency_list:
frequency_sum += tup[1]
key_list =
prob_list =
key_prob_list =
for tup1 in frequency_list:
key = tup1[0]
prob = tup1[1] / frequency_sum
key_list.append(key)
prob_list.append(prob)
key_prob_list.append(key_list)
key_prob_list.append(prob_list)
frequency_probability_dict[k] = key_prob_list
#print "&&&&",frequency_probability_dict
encrypted_attack_payload = ""
for ch in attack_payload:
if ch in frequency_probability_dict.keys():
probability_keys = frequency_probability_dict[ch][0]
probability_values = frequency_probability_dict[ch][1]
encrypted_attack_payload = encrypted_attack_payload +
np.random.choice(probability_keys, 1, p=probability_values)[0]
else:
encrypted_attack_payload = encrypted_attack_payload + substitution_table[ch][0][0]
attack_payload_list = list(attack_payload)
result = list(encrypted_attack_payload)
#build xor_table
for idx, val in enumerate(attack_payload_list):
xor_table.append(chr(ord(val) ^ ord(result[idx])))
and Lines 84 to 110 in substitution.py-
substitution_table =
i=0
for tup in sorted_attack_frequency:
substitution_table[tup[0]] = [sorted_artificial_frequency[i]]
i=i+1
#2nd loop for the remaining elements in artificial: starting from m+1
remianing_frequencies =
for num in range(i, len(sorted_artificial_frequency)):
remianing_frequencies.append(sorted_artificial_frequency[num])
for tup in remianing_frequencies:
temp_dict =
for tup1 in sorted_attack_frequency:
key = tup1[0]
value = tup1[1]
char_sub_list = substitution_table[key]
value2=0
for tup2 in char_sub_list:
value2 = value2 + tup2[1]
value = value/value2
temp_dict[key] = value
max_key = max(temp_dict.iteritems(), key=operator.itemgetter(1))[0]
substitution_table[max_key].append(tup)
Any suggestion will be highly appreciated.
I know that for loop works per operations and are heavy and time costly, so I just want to make it less complex and faster.
python object-oriented python-2.7
I have the following piece of code. But, the code is time costly and the for loops are also not very effective. Is there any way, I can make this piece of my code more better by changing the syntax.
frequency_list =
frequency_probability_dict =
for k, v in substitution_table.items():
if len(v) > 1:
print 'length above 1::', k, len(v), v
frequency_list = v
frequency_sum = 0
for tup in frequency_list:
frequency_sum += tup[1]
key_list =
prob_list =
key_prob_list =
for tup1 in frequency_list:
key = tup1[0]
prob = tup1[1] / frequency_sum
key_list.append(key)
prob_list.append(prob)
key_prob_list.append(key_list)
key_prob_list.append(prob_list)
frequency_probability_dict[k] = key_prob_list
#print "&&&&",frequency_probability_dict
encrypted_attack_payload = ""
for ch in attack_payload:
if ch in frequency_probability_dict.keys():
probability_keys = frequency_probability_dict[ch][0]
probability_values = frequency_probability_dict[ch][1]
encrypted_attack_payload = encrypted_attack_payload +
np.random.choice(probability_keys, 1, p=probability_values)[0]
else:
encrypted_attack_payload = encrypted_attack_payload + substitution_table[ch][0][0]
attack_payload_list = list(attack_payload)
result = list(encrypted_attack_payload)
#build xor_table
for idx, val in enumerate(attack_payload_list):
xor_table.append(chr(ord(val) ^ ord(result[idx])))
and Lines 84 to 110 in substitution.py-
substitution_table =
i=0
for tup in sorted_attack_frequency:
substitution_table[tup[0]] = [sorted_artificial_frequency[i]]
i=i+1
#2nd loop for the remaining elements in artificial: starting from m+1
remianing_frequencies =
for num in range(i, len(sorted_artificial_frequency)):
remianing_frequencies.append(sorted_artificial_frequency[num])
for tup in remianing_frequencies:
temp_dict =
for tup1 in sorted_attack_frequency:
key = tup1[0]
value = tup1[1]
char_sub_list = substitution_table[key]
value2=0
for tup2 in char_sub_list:
value2 = value2 + tup2[1]
value = value/value2
temp_dict[key] = value
max_key = max(temp_dict.iteritems(), key=operator.itemgetter(1))[0]
substitution_table[max_key].append(tup)
Any suggestion will be highly appreciated.
I know that for loop works per operations and are heavy and time costly, so I just want to make it less complex and faster.
python object-oriented python-2.7
asked Jul 29 at 18:02
Ayman
1
1
closed as unclear what you're asking by Mathias Ettinger, Ludisposed, yuri, Ben Steffan, Stephen Rauch Jul 29 at 20:07
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Mathias Ettinger, Ludisposed, yuri, Ben Steffan, Stephen Rauch Jul 29 at 20:07
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, itâÂÂs hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
5
What does this code do? What problem are you trying to solve?
â Ludisposed
Jul 29 at 18:07
add a comment |Â
5
What does this code do? What problem are you trying to solve?
â Ludisposed
Jul 29 at 18:07
5
5
What does this code do? What problem are you trying to solve?
â Ludisposed
Jul 29 at 18:07
What does this code do? What problem are you trying to solve?
â Ludisposed
Jul 29 at 18:07
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
5
What does this code do? What problem are you trying to solve?
â Ludisposed
Jul 29 at 18:07