xqin
2016-06-30 19:17:02 +08:00
javascript
```
DecodeSomething = function(type, str) {
//如果没有传入类型或者字符串(str), 则返回 空数组
if (!type || void 0 === str){
return [];
}
var decode = function(str) {
var b,
c = [],
d = 3,
e = function(a) {//如果字符在 0-9 之间, 则返回它所对应的数字
if(a >= "0" && "9" >= a){
return parseInt(a, 10);
}else{//否则返回 它的 unicode 值 减去 87
return a.charCodeAt(0) - 87;
}
}, f = function(str, index) {
var c = e(str[index]),
d = e(str[index + 1]) & 15,
f = (12 & c) >> 2,
g = (2 & c) > 0,
h = (1 & c) > 0;
return {
playerPair: g,
winner: f,
bankerPair: h,
value: d
}
};
//遍历字符串
for (var index = 0; index < str.length - 1; index += 2){//一次处理两个字符
b = f(str, index);
if(b.winner !== d){//如果 winner 的值 不等于 3
c.push(b);//则添加到 数组 c 里面
}else{
c = [];//否则 清空数组
}
}
return c;//返回数组 c
};
switch (type) {
case "7bal":
case "bal":
return decode(str);
case "rol":
case "rofl":
return str.split(";").map(function(val) {
return {
result: val
}
});
default:
return str
}
}
var a = "60o0749460o07134482";
console.log(JSON.stringify(DecodeSomething("bal", a)));
```
python
```
import string, json
def char2int(s):
if s >= '0' and s <= '9':
return int(s)
else:
return ord(s) - 87
def decode(str):
c = char2int(str[0])
d = char2int(str[1]) & 15
f = (12 & c) >> 2
g = (2 & c) > 0
h = (1 & c) > 0
return {'value': d, 'winner': f, 'playerPair': g, 'bankerPair': h}
def DecodeSomething(type, data):
c = []
if type in ['7bal', 'bal']:
for x in range(0, len(data) - 1, 2):
d = decode(data[x:x+2])
if d['winner'] != 3:
c.append(d)
else:
c = []
return c
elif type in ['rol', 'rofl']:
d = data.split(";")
for x in d:
c.append({"result": x})
return c
else:
return data
print json.dumps(DecodeSomething("bal", "60o0749460o07134482"))
print json.dumps(DecodeSomething("rol", "aa2;asd;42"))
```
以上内容已发到你的 QQ 邮箱(鉴于 V2EX 对于回复的内容里有代码不够友好的原因, 发邮件给你了).