这是java脚本代码,它是一个具有2个功能的解码器:
getOffset
,主要功能asdf
。
我想解码这个字符串:LqMWJQzZYUWJQpEcYGII26XombQQoqnQQQQoqnqgoqnQQQQoqnQQQQoqnQQQQoqnqgoqnQQQQoqnQQuuuQQoqnQQQQoqnxioqnQQQQoqnQQMJCSVcCgnQQoqnQQQQoqnxioqnQQQQoqnQQniaQQoqnQQQQoqnqgoqnQQQQoqnQQWJQzhYAUoVMTAdAT33nvX3nB
对此:
/ShowUrl-a_partnerKey.1-a_url.http%253A__5F____5F__2F__5F____5F____5F____5F__2F__5F____5F__www__5F____5F__2E__5F____5F__danielnyc__5F____5F__2E__5F____5F__com__5F____5F__2F__5F____5F__-a_urlKey.915edf949f66c826c.html
我想将此(工作)javascript代码转换为python:
var d = "LqMWJQzZYUWJQpEcYGII26XombQQoqnQQQQoqnqgoqnQQQQoqnQQQQoqnQQQQoqnqgoqnQQQQoqnQQuuuQQoqnQQQQoqnxioqnQQQQoqnQQMJCSVcCgnQQoqnQQQQoqnxioqnQQQQoqnQQniaQQoqnQQQQoqnqgoqnQQQQoqnQQWJQzhYAUoVMTAdAT33nvX3nB"
function asdf(d) {
var h = {
"": ["&", "=", "p", "6", "?", "H", "%", "B", ".com", "k", "9", ".html", "n", "M", "r", "www.", "h", "b", "t", "a", "0", "/", "d", "O", "j", "http://", "_", "L", "i", "f", "1", "e", "-", "2", ".", "N", "m", "A", "l", "4", "R", "C", "y", "S", "o", "+", "7", "I", "3", "c", "5", "u", 0, "T", "v", "s", "w", "8", "P", 0, "g", 0],
q: [0, "__3F__", 0, "Photos", 0, "https://", ".edu", "*", "Y", ">", 0, 0, 0, 0, 0, 0, "`", "__2D__", "X", "<", "slot", 0, "ShowUrl", "Owners", 0, "[", "q", 0, "MemberProfile", 0, "ShowUserReviews", '"', "Hotel", 0, 0, "Expedia", "Vacation", "Discount", 0, "UserReview", "Thumbnail", 0, "__2F__", "Inspiration", "V", "Map", ":", "@", 0, "F", "help", 0, 0, "Rental", 0, "Picture", 0, 0, 0, "hotels", 0, "ftp://"],
x: [0, 0, "J", 0, 0, "Z", 0, 0, 0, ";", 0, "Text", 0, "(", "x", "GenericAds", "U", 0, "careers", 0, 0, 0, "D", 0, "members", "Search", 0, 0, 0, "Post", 0, 0, 0, "Q", 0, "$", 0, "K", 0, "W", 0, "Reviews", 0, ",", "__2E__", 0, 0, 0, 0, 0, 0, 0, "{", "}", 0, "Cheap", ")", 0, 0, 0, "#", ".org"],
z: [0, "Hotels", 0, 0, "Icon", 0, 0, 0, 0, ".net", 0, 0, "z", 0, 0, "pages", 0, "geo", 0, 0, 0, "cnt", "~", 0, 0, "]", "|", 0, "tripadvisor", "Images", "BookingBuddy", 0, "Commerce", 0, 0, "partnerKey", 0, "area", 0, "Deals", "from", "//", 0, "urlKey", 0, "'", 0, "WeatherUnderground", 0, "MemberSign", "Maps", 0, "matchID", "Packages", "E", "Amenities", "Travel", ".htm", 0, "!", "^", "G"]
};
var b = "";
for (var a = 0; a < d.length; a++) {
var j = d.charAt(a);
var f = j;
if (h[j] && a + 1 < d.length) {
a++;
f += d.charAt(a)
} else {
j = ""
}
var g = getOffset(d.charCodeAt(a));
if (g < 0 || typeof h[j][g] == "String") {
b += f
} else {
b += h[j][g]
}
}
return b}
function getOffset(a) {
if (a >= 97 && a <= 122) {
return a - 61
}
if (a >= 65 && a <= 90) {
return a - 55
}
if (a >= 48 && a <= 71) {
return a - 48
}
return -1}
asdf(d)
返回(按预期):
/ShowUrl-a_partnerKey.1-a_url.http%253A__5F____5F__2F__5F____5F____5F____5F__2F__5F____5F__www__5F____5F__2E__5F____5F__danielnyc__5F____5F__2E__5F____5F__com__5F____5F__2F__5F____5F__-a_urlKey.915edf949f66c826c.html
这是我尝试过的Python代码:
d = "LqMWJQzZYUWJQpEcYGII26XombQQoqnQQQQoqnqgoqnQQQQoqnQQQQoqnQQQQoqnqgoqnQQQQoqnQQuuuQQoqnQQQQoqnxioqnQQQQoqnQQMJCSVcCgnQQoqnQQQQoqnxioqnQQQQoqnQQniaQQoqnQQQQoqnqgoqnQQQQoqnQQWJQzhYAUoVMTAdAT33nvX3nB"
def asdf(d):
h = {
"" : ["&", "=", "p", "6", "?", "H", "%", "B", ".com", "k", "9", ".html", "n", "M", "r", "www.", "h", "b", "t", "a", "0", "/", "d", "O", "j", "http://", "_", "L", "i", "f", "1", "e", "-", "2", ".", "N", "m", "A", "l", "4", "R", "C", "y", "S", "o", "+", "7", "I", "3", "c", "5", "u", 0, "T", "v", "s", "w", "8", "P", 0, "g", 0],
"q": [0, "__3F__", 0, "Photos", 0, "https://", ".edu", "*", "Y", ">", 0, 0, 0, 0, 0, 0, "`", "__2D__", "X", "<", "slot", 0, "ShowUrl", "Owners", 0, "[", "q", 0, "MemberProfile", 0, "ShowUserReviews", '"', "Hotel", 0, 0, "Expedia", "Vacation", "Discount", 0, "UserReview", "Thumbnail", 0, "__2F__", "Inspiration", "V", "Map", ":", "@", 0, "F", "help", 0, 0, "Rental", 0, "Picture", 0, 0, 0, "hotels", 0, "ftp://"],
"x": [0, 0, "J", 0, 0, "Z", 0, 0, 0, ";", 0, "Text", 0, "(", "x", "GenericAds", "U", 0, "careers", 0, 0, 0, "D", 0, "members", "Search", 0, 0, 0, "Post", 0, 0, 0, "Q", 0, "$", 0, "K", 0, "W", 0, "Reviews", 0, ",", "__2E__", 0, 0, 0, 0, 0, 0, 0, "{", "}", 0, "Cheap", ")", 0, 0, 0, "#", ".org"],
"z": [0, "Hotels", 0, 0, "Icon", 0, 0, 0, 0, ".net", 0, 0, "z", 0, 0, "pages", 0, "geo", 0, 0, 0, "cnt", "~", 0, 0, "]", "|", 0, "tripadvisor", "Images", "BookingBuddy", 0, "Commerce", 0, 0, "partnerKey", 0, "area", 0, "Deals", "from", "//", 0, "urlKey", 0, "'", 0, "WeatherUnderground", 0, "MemberSign", "Maps", 0, "matchID", "Packages", "E", "Amenities", "Travel", ".htm", 0, "!", "^", "G"]
}
b = ""
for a in range(len(d)):
j=d[a]
f=j
list =[]
for key in h:
list.append(key)
if j in list and a+1 < len(d):
a=a+1
f = f+d[a]
else:
j = ""
g = getOffset(ord(d[a]))
if g < 0 or type(h[j][g]) is str:
b = b + f
else:
b = b+str(h[j][g])
print b
return b
def getOffset(a):
if(a >= 97 and a <= 122):
return(a-61)
if(a >= 65 and a <= 90):
return(a-55)
if(a >= 48 and a <=71):
return(a-48)
return(-1)
print asdf(d)
但它让我回到了未解码的字符串:LqMWJQzZYUWJQpEcYGII26XombQQoqnQQQQoqnqgoqnQQQQoqnQQQQoqnQQQQoqnqgoqnQQQQoqnQQuuuQQoqnQQQQoqnxioqnQQQQoqnQQMJCSVcCgnQQoqnQQQQoqnxioqnQQQQoqnQQniaQQoqnQQQQoqnqgoqnQQQQoqnQQWJQzhYAUoVMTAdAT33nvX3nB
我的代码出了什么问题?为什么我没有得到与javascript代码相同的结果?
答案 0 :(得分:0)
实际上,您的Javascript代码有一个错误 - 如果错误得到修复,那么您的JS代码会中断,返回原始字符串,就像您的Python代码一样。
if (g < 0 || typeof h[j][g] == "String")
的行为不符合您的预期:
typeof h[j][g] == "String")
应该是
if (g < 0 || typeof h[j][g] == "string")
(注意未大写的字符串)。
实际上错误的typeof h[j][g] == "String")
实际上相当于
if (g < 0) {...
如果在两个版本的代码中更改该行,则可以在JS中获得所需的结果,并在Python中获得所需的结果(这有另一个错误)。
Javascript版
更改:
var g = getOffset(d.charCodeAt(a));
if (g < 0 || typeof h[j][g] == "String") { #THIS LINE IS INCORRECT
b += f
} else {
b += h[j][g]
}
要:
var g = getOffset(d.charCodeAt(a));
if (g < 0){ # CHANGE THIS LINE
b += f
} else {
b += h[j][g]
}
JS结果:
/ShowUrl-a_partnerKey.1-a_url.http%253A__5F____5F__2F__5F____5F____5F____5F__2F__5F____5F__www__5F____5F__2E__5F____5F__danielnyc__5F____5F__2E__5F____5F__com__5F____5F__2F__5F____5F__-a_urlKey.915edf949f66c826c.html
Python版
变化:
g = getOffset(ord(d[a]))
if g < 0 or type(h[j][g]) is str: # THIS LINE IS INCORRECT
b = b + f
else:
b = b+str(h[j][g])
print b # THIS LINE SHOULD BE REMOVED
致:
g = getOffset(ord(d[a])) # CHANGED THIS LINE
if g < 0:
b = b + f
else:
b = b+str(h[j][g])
#print b # REMOVED THIS LINE
结果:
/ShowUrld-a_partnerKeyN.1-a_url.http%253A__5Fc____5Fc__2F__y5Fc____5Fc____5Fc____5Fc__2F__y5Fc____5Fc__www__5Fc____5Fc__2E__o5Fc____5Fc__danielnyc__5Fc____5Fc__2E__o5Fc____5Fc__com__5Fc____5Fc__2F__y5Fc____5Fc__-a_urlKeyS.915edf949f66c826c.html
以下是最终代码的链接:
最终PYTHON修复
现在,你有另一个Python错误..
使用for a in ..
构造时,您无法在内部更改a
的值,如以下行:a = a + 1
。
嗯,你可以,但这会导致意想不到的结果
每次到达循环的for a in..
顶部时,a
将增加到所拥有的下一个值,无论您暂时的值是多少>在循环中将其更改为 !!
因此,(2, 'M'), (7, 'Z'), ...
没有跳过,你原本打算!
这就是我上面展示的Python结果比你预期的更多字符的原因。
请参阅these SO answers for great explanations and examples,python docs here或this closed SO question。
在这种情况下转换代码的最简单方法是简单地使用while
循环而不是for in
循环。这样,两种语言的内在逻辑基本相同。
CHANGE:
#for a in range(len(d)): ## Remove and Change this to a WHILE loop as below
a = 0 # Manually initialize your loop
print(len(d))
while a < len(d): # Use WHILE loop instead
j = d[a]
...
AND ..
...
g = getOffset(ord(d[a]))
if g < 0 : # Remember to UPDATE This Line (Remove #or type(h[j][g]) is str:)
b = b + f
else:
b = b + str(h[j][g])
#print b # REMOVE this line
a = a + 1 # Manually increment your WHILE loop
return b
FINAL PYTHON CODE on REPL.IT )
最终的PYTHON结果:
/ShowUrl-a_partnerKey.1-a_url.http%253A__5F____5F__2F__5F____5F____5F____5F__2F__5F____5F__www__5F____5F__2E__5F____5F__danielnyc__5F____5F__2E__5F____5F__com__5F____5F__2F__5F____5F__-a_urlKey.915edf949f66c826c.html