Is it possible to use an alternative to if/else statements in repl python 3.7? - python-3.7

Is there a better solution in python for making large strings of if/else statements for a random input using import random on version 3.7? I feel like it's a bit counterproductive to write the same if else statements for each input. If not, are there any ways to make my code more efficient?
if G.casefold() == "a":
Boss_Health = Boss_Health - A
print("user dealt", A, "damage with A")
print("Boss health:", Boss_Health)
print("Health:", Health)
print("Bosses turn")
print("")
G = random.randint(1, 20)
if G == 1:
Boss_L = Boss_L + Boss_A
print("Boss_L has been upgraded by", Boss_A, "and now deals",
Boss_L, "damage")
print("Boss Health:", Boss_Health)
print("Health:", Health)
print("users turn")
str(G)
G = input()
if G == 2:
Boss_Health = Boss_Health + Boss_B
print("Boss healed", Boss_B, "hp to Boss")
print("Boss Health:", Boss_Health)
print("Health:", Health)
print("users turn")
str(G)
G = input()
if G == 3:
Boss_M = Boss_M + Boss_B
print("Boss_M has been upgraded by", Boss_B, "and now deals",
Boss_M, "damage")
print("Boss Health:", Boss_Health)
print("Health:", Health)
print("users turn")
str(G)
G = input()

Usually, the first bit to look for is where there is duplication of code. In your program, I saw the following lines entered repeatedly:
print("Boss Health:", Boss_Health)
print("Health:", Health)
print("users turn")
str(G)
G = input()
Those would be a candidate to group into some type of function. Realizing that the value of "G" gets either a manual entry or a random entry, following is a version of your code with repeated code pulled out and placed into a separate function.
import random
def Vitals(b_h, h, rndm = None): # Separate function to handle repeated statements
print("Boss health:", b_h)
print("Health:", h)
print("Bosses turn")
print("")
z = " "
if rndm == "gen":
z = random.randint(1, 20)
else:
z = input("Enter your choice: ")
return z
def game_loop():
A = 1
G = " "
Boss_Health = 10
Boss_A = 2
Boss_B = 2
Boss_L = 1
Health = 20
while True:
if G.casefold() == "a":
Boss_Health = Boss_Health - A
print("user dealt", A, "damage with A")
G = Vitals(Boss_Health, Health, "gen")
if G == 1:
Boss_L = Boss_L + Boss_A
print("Boss_L has been upgraded by", Boss_A, "and now deals", Boss_L, "damage")
G = Vitals(Boss_Health, Health)
if G == 2:
Boss_Health = Boss_Health + Boss_B
print("Boss healed", Boss_B, "hp to Boss")
G = Vitals(Boss_Health, Health)
if G == 3:
Boss_M = Boss_M + Boss_B
print("Boss_M has been upgraded by", Boss_B, "and now deals", Boss_M, "damage")
G = Vitals(Boss_Health, Health)
if G == "q" or G == "Q":
break
G = Vitals(Boss_Health, Health)
return
game_loop()
Not having your full program, I improvised to create a user input loop to test this out. But the net effect is a shorter program that cuts down on repeated code which also reduces the chance of some inconsistencies creeping into the various blocks of code that utilize the function.
Give that a try.

Related

Using zero_grad() after loss.backward(), but still receives RuntimeError: "Trying to backward through the graph a second time..."

Below is my implementation of a2c using PyTorch. Upon learning about backpropagation in PyTorch, I have known to zero_grad() the optimizer after each update iteration. However, there is still a RunTime error on second-time backpropagation.
def torchworker(number, model):
worker_env = gym.make("Taxi-v3").env
max_steps_per_episode = 2000
worker_opt = optim.Adam(lr=5e-4, params=model.parameters())
p_history = []
val_history = []
r_history = []
running_reward = 0
episode_count = 0
under = 0
start = time.time()
for i in range(2):
state = worker_env.reset()
episode_reward = 0
penalties = 0
drop = 0
print("Episode {} begins ({})".format(episode_count, number))
worker_env.render()
criterion = nn.SmoothL1Loss()
time_solve = 0
for _ in range(1, max_steps_per_episode):
#worker_env.render()
state = torch.tensor(state, dtype=torch.long)
action_probs = model.forward(state)[0]
critic_value = model.forward(state)[1]
val_history.append((state, critic_value[0]))
# Choose action
action = np.random.choice(6, p=action_probs.detach().numpy())
p_history.append(torch.log(action_probs[action]))
# Apply chosen action
state, reward, done, _ = worker_env.step(action)
r_history.append(reward)
episode_reward += reward
time_solve += 1
if reward == -10:
penalties += 1
elif reward == 20:
drop += 1
if done:
break
# Update running reward to check condition for solving
running_reward = (running_reward * (episode_count) + episode_reward) / (episode_count + 1)
# Calculate discounted returns
returns = deque(maxlen=3500)
discounted_sum = 0
for r in r_history[::-1]:
discounted_sum = r + gamma * discounted_sum
returns.appendleft(discounted_sum)
# Calculate actor losses and critic losses
loss_actor_value = 0
loss_critic_value = 0
history = zip(p_history, val_history, returns)
for log_prob, value, ret in history:
diff = ret - value[1]
loss_actor_value += -log_prob * diff
ret_tensor = torch.tensor(ret, dtype=torch.float32)
loss_critic_value += criterion(value[1], ret_tensor)
loss = loss_actor_value + 0.1 * loss_critic_value
print(loss)
# Update params
loss.backward()
worker_opt.step()
worker_opt.zero_grad()
# Log details
end = time.time()
episode_count += 1
if episode_count % 1 == 0:
worker_env.render()
if running_reward > -50: # Condition to consider the task solved
under += 1
if under > 5:
print("Solved at episode {} !".format(episode_count))
break
I believe there may be something to do with the architecture of my AC model, so I also include it here for reference.
class ActorCriticNetwork(nn.Module):
def __init__(self, num_inputs, num_hidden, num_actions):
super(ActorCriticNetwork, self).__init__()
self.embed = nn.Embedding(500, 10)
self.fc1 = nn.Linear(10, num_hidden * 2)
self.fc2 = nn.Linear(num_hidden * 2, num_hidden)
self.c = nn.Linear(num_hidden, 1)
self.fc3 = nn.Linear(num_hidden, num_hidden)
self.a = nn.Linear(num_hidden, num_actions)
def forward(self, x):
out = F.relu(self.embed(x))
out = F.relu(self.fc1(out))
out = F.relu(self.fc2(out))
critic = self.c(out)
out = F.relu(self.fc3(out.detach()))
actor = F.softmax(self.a(out), dim=-1)
return actor, critic
Would you please tell me what the mistake here is? Thank you in advance.
SOLVED: I forgot to clear the history of probabilities, action-values and rewards after iterations. It is clear why that would cause the issue, as the older elements would cause propagating through old dcgs.

Why doesn't function loop, loop on input?

I want to use the function serial_circuit with three different inputs (1,2,3). The function is suppose to accept multiple inputs from user until
user hits return, end program and sums up all inputs. The program only takes one input and display it.
def serial_circuit(num1):
num = 0
while(True):
try:
num += num1
except:
break
return num
print("1.Solve for serial resistance: ")
print("2.Solve for serial coils: ")
print("3.Solve for parallel capacitors: ")
choice = input("Enter choice: ")
if choice == '1':
num1 = float(input("Enter resistor value: "))
num = serial_circuit(num1)
print(f"Total resistance = {(num)} ohms")
elif choice == '2':
num1 = float(input("Enter coil value: "))
num = serial_circuit(num1)
print(f"Total inductance = {(num)} henrys")
elif choice == '3':
num1 = float(input("Enter capacitor value: "))
num = serial_circuit(num1)
print(f"Total capacitance = {(num):.6f} farads")
One of your problems is that you have the return statement inside the loop - notice your indentation - during the first iteration of the loop the last thing that will happen within the loop is returning from the serial_circuit function.
Another problem is that you are asking for the input only once - outside of the loop.
Take a look at this solution:
def serial_circuit(text):
num = 0
finishedEntering = False
while(not finishedEntering):
try:
received_input = input(text)
if received_input == "":
finishedEntering = True
else:
num1 = float(received_input)
num += num1
except:
break
return num
print("1.Solve for serial resistance: ")
print("2.Solve for serial coils: ")
print("3.Solve for parallel capacitors: ")
choice = input("Enter choice: ")
if choice == '1':
num = serial_circuit("Enter resistor value or hit enter to finish: ")
print(f"Total resistance = {(num)} ohms")
elif choice == '2':
num = serial_circuit("Enter coil value or hit enter to finish: ")
print(f"Total inductance = {(num)} henrys")
elif choice == '3':
num = serial_circuit("Enter capacitor value or hit enter to finish: ")
print(f"Total capacitance = {(num):.6f} farads")

QBasic/QB64: How to clean up an IF THEN IF "ladder"?

Not sure if I'm using the right terms here, but for whatever reason, QBasic doesn't understand something along the lines of "x = y = z". It's limited to two.
To fix that, I did this:
IF sum(1) = sum(2) THEN
IF sum(2) = sum(3) THEN
IF sum(3) = sum2(1) THEN
IF sum2(1) = sum2(2) THEN
IF sum2(2) = sum2(3) THEN
IF sum2(3) = sum3 THEN
IF sum3 = sum4 THEN
PRINT "This is a Lo Shu Square, with all sums equaling"; sum(1)
ELSE
PRINT "This is not a Lo Shu Square."
END IF
END IF
END IF
END IF
END IF
END IF
END IF
END
Definitely works, but something tells me there's a simpler way to have it check if all of the sums equal the same. Any suggestions?
It would work if all comparisons where put on one line separated by AND like this:
REM code to shrink IFTHEN ladder:
IF sum(1) = sum(2) AND sum(2) = sum(3) AND sum(3) = sum2(1) AND sum2(1) = sum2(2) AND sum2(2) = sum2(3) AND sum2(3) = sum3 AND sum3 = sum4 THEN
PRINT "This is a Lo Shu Square, with all sums equaling"; sum(1)
ELSE
PRINT "This is not a Lo Shu Square."
END IF
END
You could also code the logic into a loop:
DIM testvals(8)
testvals(0) = sum(1)
testvals(1) = sum(2)
testvals(2) = sum(3)
testvals(3) = sum2(1)
testvals(4) = sum2(2)
testvals(5) = sum2(3)
testvals(6) = sum3
testvals(7) = sum4
DO
FOR i = 1+LBOUND(testvals) TO UBOUND(testvals)
IF testvals(i-1) <> testvals(i) THEN
PRINT "This is not a Lo Shu square."
EXIT DO
END IF
NEXT
PRINT "This is a Lo Shu square, with all sums equaling"; sum(1)
LOOP WHILE 1 = 0
This has a couple of benefits:
It's easier to spot a typo if you change the code.
You can always add and remove test values in similar cases. In this case, there's no need to do such a thing, but in some other cases, it may be beneficial to simply type testvals(8) = value and change the 8 to a 9 in the DIM line.
It short-circuits the comparison, meaning if the first condition is false, it stops checking and says it's not a Lo Shu Square, similar to a tower of IF-THEN-ELSE statements (where every ELSE is PRINT "This is not a Lo Shu square.") QB64's AND operator evaluates both operands, even if the first operand is 0 or another "false" value. This can be much faster, though you likely won't notice a difference in this case.
On the other hand, it does have some drawbacks:
It's an unusual pattern in QB64 to not use AND in a case like this. In fact, this is one very good reason why AND exists.
You can just as easily remove test values that are combined using AND with no need to renumber the items in the testvals array or change its dimensions.
Even if you have many test values, it's often better to write a small program that generates the IF a AND b AND c AND ... THEN ... END IF block yourself (or something similar to your IF-THEN tower to retain short-circuit behavior) and paste the output into your program's code where it's needed.
A simpler way to check arrays:
testvals(1) = sum(1)
testvals(2) = sum(2)
testvals(3) = sum(3)
testvals(4) = sum2(1)
testvals(5) = sum2(2)
testvals(6) = sum2(3)
testvals(7) = sum3
testvals(8) = sum4
FOR i = 1 TO 7
IF testvals(i) <> testvals(i + 1) THEN
f = -1
EXIT FOR
END IF
NEXT
IF f THEN
PRINT "This is not a Lo Shu square."
ELSE
PRINT "This is a Lo Shu square, with all sums equaling"; sum(1)
END IF
A simpler way to check a loop:
testvals(1) = sum(1)
testvals(2) = sum(2)
testvals(3) = sum(3)
testvals(4) = sum2(1)
testvals(5) = sum2(2)
testvals(6) = sum2(3)
testvals(7) = sum3
testvals(8) = sum4
FOR i = 1 TO 7
IF testvals(i) <> testvals(i + 1) THEN
PRINT "This is not a Lo Shu square."
END
END IF
NEXT
PRINT "This is a Lo Shu square, with all sums equaling"; sum(1)
Consolidate the logic into a function:
FUNCTION isLoShuSquare (sums() AS DOUBLE)
isLoShuSquare = 1
DIM i AS INTEGER
FOR i = 0 TO UBOUND(sums) - 1
IF sums(i) <> sums(i + 1) THEN
isLoShuSquare = 0
EXIT FOR
END IF
NEXT i
END FUNCTION
Then load the array and pass it to the function:
DIM sums(7) AS DOUBLE
DIM i AS INTEGER
i = 0
sums(i) = sum(1): i = i + 1
sums(i) = sum(2): i = i + 1
sums(i) = sum(3): i = i + 1
sums(i) = sum2(1): i = i + 1
sums(i) = sum2(2): i = i + 1
sums(i) = sum2(3): i = i + 1
sums(i) = sum3: i = i + 1
sums(i) = sum4
PRINT isLoShuSquare(sums())
Another way to check an array in a loop in a function:
DIM sums(8) AS DOUBLE
sums(1) = sum(1)
sums(2) = sum(2)
sums(3) = sum(3)
sums(4) = sum2(1)
sums(5) = sum2(2)
sums(6) = sum2(3)
sums(7) = sum3
sums(8) = sum4
IF isLoShuSquare(sums()) = 0 THEN
PRINT "This is not a Lo Shu square."
ELSE
PRINT "This is a Lo Shu square, with all sums equaling"; sum(1)
END IF
END
FUNCTION isLoShuSquare (sums() AS DOUBLE)
isLoShuSquare = -1
FOR i = 1 TO UBOUND(sums) - 1
IF sums(i) <> sums(i + 1) THEN
isLoShuSquare = 0
EXIT FUNCTION
END IF
NEXT
END FUNCTION

Google Translate TTS API blocked

Google implemented a captcha to block people from accessing the TTS translate API https://translate.google.com/translate_tts?ie=UTF-8&q=test&tl=zh-TW. I was using it in my mobile application. Now, it is not returning anything. How do I get around the captcha?
Add the qualifier '&client=tw-ob' to the end of your query.
https://translate.google.com/translate_tts?ie=UTF-8&q=test&tl=zh-TW&client=tw-ob
This answer no longer works consistently. Your ip address will be blocked by google temporarily if you abuse this too much.
there are 3 main issues:
you must include "client" in your query string (client=t seems to work).
(in case you are trying to retrieve it using AJAX) the Referer of the HTTP request must be https://translate.google.com/
"tk" field changes for every query, and it must be populated with a matching hash:
tk = hash(q, TKK), where q is the text to be TTSed, and TKK is a var in the global scope when you load translate.google.com: (type 'window.TKK' in the console). see the hash function at the bottom of this reply (calcHash).
to summarize:
function generateGoogleTTSLink(q, tl, tkk) {
var tk = calcHash(q, tkk);
return `https://translate.google.com/translate_tts?ie=UTF-8&total=1&idx=0&client=t&ttsspeed=1&tl=${tl}&tk=${tk}&q=${q}&textlen=${q.length}`;
}
generateGoogleTTSLink('ciao', 'it', '410353.1336369826');
// see definition of "calcHash" in the bottom of this comment.
=> to get your hands on a TKK, you can open Google Translate website, then type "TKK" in developer tools' console (e.g.: "410353.1336369826").
NOTE that TKK value changes every hour, and so, old TKKs might get blocked at some point, and refreshing it may be necessary (although so far it seems like old keys can work for a LONG time).
if you DO wish to periodically refresh TKK, it can be automated pretty easily, but not if you're running your code from the browser.
you can find a full NodeJS implementation here:
https://github.com/guyrotem/google-translate-server.
it exposes a minimal TTS API (query, language), and is deployed to a free Heroku server, so you can test it online if you like.
function shiftLeftOrRightThenSumOrXor(num, opArray) {
return opArray.reduce((acc, opString) => {
var op1 = opString[1]; // '+' | '-' ~ SUM | XOR
var op2 = opString[0]; // '+' | '^' ~ SLL | SRL
var xd = opString[2]; // [0-9a-f]
var shiftAmount = hexCharAsNumber(xd);
var mask = (op1 == '+') ? acc >>> shiftAmount : acc << shiftAmount;
return (op2 == '+') ? (acc + mask & 0xffffffff) : (acc ^ mask);
}, num);
}
function hexCharAsNumber(xd) {
return (xd >= 'a') ? xd.charCodeAt(0) - 87 : Number(xd);
}
function transformQuery(query) {
for (var e = [], f = 0, g = 0; g < query.length; g++) {
var l = query.charCodeAt(g);
if (l < 128) {
e[f++] = l; // 0{l[6-0]}
} else if (l < 2048) {
e[f++] = l >> 6 | 0xC0; // 110{l[10-6]}
e[f++] = l & 0x3F | 0x80; // 10{l[5-0]}
} else if (0xD800 == (l & 0xFC00) && g + 1 < query.length && 0xDC00 == (query.charCodeAt(g + 1) & 0xFC00)) {
// that's pretty rare... (avoid ovf?)
l = (1 << 16) + ((l & 0x03FF) << 10) + (query.charCodeAt(++g) & 0x03FF);
e[f++] = l >> 18 | 0xF0; // 111100{l[9-8*]}
e[f++] = l >> 12 & 0x3F | 0x80; // 10{l[7*-2]}
e[f++] = l & 0x3F | 0x80; // 10{(l+1)[5-0]}
} else {
e[f++] = l >> 12 | 0xE0; // 1110{l[15-12]}
e[f++] = l >> 6 & 0x3F | 0x80; // 10{l[11-6]}
e[f++] = l & 0x3F | 0x80; // 10{l[5-0]}
}
}
return e;
}
function normalizeHash(encondindRound2) {
if (encondindRound2 < 0) {
encondindRound2 = (encondindRound2 & 0x7fffffff) + 0x80000000;
}
return encondindRound2 % 1E6;
}
function calcHash(query, windowTkk) {
// STEP 1: spread the the query char codes on a byte-array, 1-3 bytes per char
var bytesArray = transformQuery(query);
// STEP 2: starting with TKK index, add the array from last step one-by-one, and do 2 rounds of shift+add/xor
var d = windowTkk.split('.');
var tkkIndex = Number(d[0]) || 0;
var tkkKey = Number(d[1]) || 0;
var encondingRound1 = bytesArray.reduce((acc, current) => {
acc += current;
return shiftLeftOrRightThenSumOrXor(acc, ['+-a', '^+6'])
}, tkkIndex);
// STEP 3: apply 3 rounds of shift+add/xor and XOR with they TKK key
var encondingRound2 = shiftLeftOrRightThenSumOrXor(encondingRound1, ['+-3', '^+b', '+-f']) ^ tkkKey;
// STEP 4: Normalize to 2s complement & format
var normalizedResult = normalizeHash(encondingRound2);
return normalizedResult.toString() + "." + (normalizedResult ^ tkkIndex)
}
// usage example:
var tk = calcHash('hola', '409837.2120040981');
console.log('tk=' + tk);
// OUTPUT: 'tk=70528.480109'
You can also try this format :
pass q= urlencode format of your language
(In JavaScript you can use the encodeURI() function & PHP has the rawurlencode() function)
pass tl = language short name (suppose bangla = bn)
Now try this :
https://translate.google.com.vn/translate_tts?ie=UTF-8&q=%E0%A6%A2%E0%A6%BE%E0%A6%95%E0%A6%BE+&tl=bn&client=tw-ob
First, to avoid captcha, you have to set a proper user-agent like: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0"
Then to not being blocked you must provide a proper token ("tk" get parameter) for each single request.
On the web you can find many different kind of scripts that try to calculate the token after a lot of reverse engineering...but every time the big G change the algorithm you're stuck again, so it's much easier to retrieve your token just observing in deep similar requests to translate page (with your text in the url).
You can read the token time by time grepping "tk=" from the output of this simple code with phantomjs:
"use strict";
var page = require('webpage').create();
var system = require('system');
var args = system.args;
if (args.length != 2) { console.log("usage: "+args[0]+" text"); phantom.exit(1); }
page.onConsoleMessage = function(msg) { console.log(msg); };
page.onResourceRequested = function(request) { console.log('Request ' + JSON.stringify(request, undefined, 4)); };
page.open("https://translate.google.it/?hl=it&tab=wT#fr/it/"+args[1], function(status) {
if (status === "success") { phantom.exit(0); }
else { phantom.exit(1); }
});
so in the end you can get your speech with something like:
wget -U "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0"
"http://translate.google.com/translate_tts?ie=UTF-8&tl=it&tk=52269.458629&q=ciao&client=t" -O ciao.mp3
(token are probably time based so this link may not work tomorrow)
I rewrote Guy Rotem's answer in Java, so if you prefer Java over Javascript, feel free to use:
public class Hasher {
public long shiftLeftOrRightThenSumOrXor(long num, String[] opArray) {
long result = num;
int current = 0;
while (current < opArray.length) {
char op1 = opArray[current].charAt(1); // '+' | '-' ~ SUM | XOR
char op2 = opArray[current].charAt(0); // '+' | '^' ~ SLL | SRL
char xd = opArray[current].charAt(2); // [0-9a-f]
assertError(op1 == '+'
|| op1 == '-', "Invalid OP: " + op1);
assertError(op2 == '+'
|| op2 == '^', "Invalid OP: " + op2);
assertError(('0' <= xd && xd <= '9')
|| ('a' <= xd && xd <='f'), "Not an 0x? value: " + xd);
int shiftAmount = hexCharAsNumber(xd);
int mask = (op1 == '+') ? ((int) result) >>> shiftAmount : ((int) result) << shiftAmount;
long subresult = (op2 == '+') ? (((int) result) + ((int) mask) & 0xffffffff)
: (((int) result) ^ mask);
result = subresult;
current++;
}
return result;
}
public void assertError(boolean cond, String e) {
if (!cond) {
System.err.println();
}
}
public int hexCharAsNumber(char xd) {
return (xd >= 'a') ? xd - 87 : Character.getNumericValue(xd);
}
public int[] transformQuery(String query) {
int[] e = new int[1000];
int resultSize = 1000;
for (int f = 0, g = 0; g < query.length(); g++) {
int l = query.charAt(g);
if (l < 128) {
e[f++] = l; // 0{l[6-0]}
} else if (l < 2048) {
e[f++] = l >> 6 | 0xC0; // 110{l[10-6]}
e[f++] = l & 0x3F | 0x80; // 10{l[5-0]}
} else if (0xD800 == (l & 0xFC00) &&
g + 1 < query.length() && 0xDC00 == (query.charAt(g + 1) & 0xFC00)) {
// that's pretty rare... (avoid ovf?)
l = (1 << 16) + ((l & 0x03FF) << 10) + (query.charAt(++g) & 0x03FF);
e[f++] = l >> 18 | 0xF0; // 111100{l[9-8*]}
e[f++] = l >> 12 & 0x3F | 0x80; // 10{l[7*-2]}
e[f++] = l & 0x3F | 0x80; // 10{(l+1)[5-0]}
} else {
e[f++] = l >> 12 | 0xE0; // 1110{l[15-12]}
e[f++] = l >> 6 & 0x3F | 0x80; // 10{l[11-6]}
e[f++] = l & 0x3F | 0x80; // 10{l[5-0]}
}
resultSize = f;
}
return Arrays.copyOf(e, resultSize);
}
public long normalizeHash(long encondindRound2) {
if (encondindRound2 < 0) {
encondindRound2 = (encondindRound2 & 0x7fffffff) + 0x80000000L;
}
return (encondindRound2) % 1_000_000;
}
/*
/ EXAMPLE:
/
/ INPUT: query: 'hola', windowTkk: '409837.2120040981'
/ OUTPUT: '70528.480109'
/
*/
public String calcHash(String query, String windowTkk) {
// STEP 1: spread the the query char codes on a byte-array, 1-3 bytes per char
int[] bytesArray = transformQuery(query);
// STEP 2: starting with TKK index,
// add the array from last step one-by-one, and do 2 rounds of shift+add/xor
String[] d = windowTkk.split("\\.");
int tkkIndex = 0;
try {
tkkIndex = Integer.valueOf(d[0]);
}
catch (Exception e) {
e.printStackTrace();
}
long tkkKey = 0;
try {
tkkKey = Long.valueOf(d[1]);
}
catch (Exception e) {
e.printStackTrace();
}
int current = 0;
long result = tkkIndex;
while (current < bytesArray.length) {
result += bytesArray[current];
long subresult = shiftLeftOrRightThenSumOrXor(result,
new String[] {"+-a", "^+6"});
result = subresult;
current++;
}
long encondingRound1 = result;
//System.out.println("encodingRound1: " + encondingRound1);
// STEP 3: apply 3 rounds of shift+add/xor and XOR with they TKK key
long encondingRound2 = ((int) shiftLeftOrRightThenSumOrXor(encondingRound1,
new String[] {"+-3", "^+b", "+-f"})) ^ ((int) tkkKey);
//System.out.println("encodingRound2: " + encondingRound2);
// STEP 4: Normalize to 2s complement & format
long normalizedResult = normalizeHash(encondingRound2);
//System.out.println("normalizedResult: " + normalizedResult);
return String.valueOf(normalizedResult) + "."
+ (((int) normalizedResult) ^ (tkkIndex));
}
}

Encoding - What is this function?

I'm porting and updating an old app and I came across this function. I'd like to know more about it, but I don't actually know what it's called. I'm assuming it has a popular name. Does anyone know?
This version is in Python, although it was originally in Java.
def encode(msg): # msg is a string
msg_len = len(msg)
j = (msg_len + 6) / 7
k = 0
cbytesOutput = [ctypes.c_byte(0)]*(msg_len + j) # return is msg length + j bytes long
for l in xrange(j):
i1 = l * 8
j1 = i1
byte0 = ctypes.c_byte(-128)
byte1 = ctypes.c_byte(1)
k1 = 0
while k1 < 7 and k < msg_len:
byte2 = ctypes.c_byte(ord(msg[k]))
if (byte2.value & 0xffffff80) != 0:
byte0 = ctypes.c_byte(byte0.value | byte1.value)
j1 += 1
cbytesOutput[j1] = ctypes.c_byte(byte2.value | 0xffffff80)
byte1 = ctypes.c_byte(byte1.value << 1)
k += 1
k1 += 1
cbytesOutput[i1] = byte0
return cbytesOutput
Any comments on the algorithm in general? I'm thinking of replacing it. Profiler says it's the worst function in the entire app (it's 60% of the time on the slowest code path) and it bloats the data as well.
Thanks