gpt4 book ai didi

javascript - 将 JavaScript 代码转换为 Python

转载 作者:行者123 更新时间:2023-11-28 05:49:12 25 4
gpt4 key购买 nike

这是java脚本代码,它是一个具有2个功能的解码器:
getOffset,以及主函数asdf

我想解码此字符串:LqMWJQzZYUWJQpEcYGII26XombQQoqnQQQQoqnqgoqnQQQQoqnQQQQoqnQQQQoqnqgoqnQQQQoqnQQuuuQQoqnQQQQoqnxioqnQQQQoqnQQMJCSVcCgnQQoqnQQQQoqnxioqnQQQQoqnQQnia QQoqnQQQQoqnqgoqnQQQQoqnQQWJQzhYAUoVMTAdAT33nvX3nB

对此:

/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)

但它给了我未解码的字符串:LqMWJQzZYUWJQpEcYGII26XombQQoqnQQQQoqnqgoqnQQQQoqnQQQQoqnQQQQoqnqgoqnQQQQoqnQQuuuQQoqnQQQQoqnxioqnQQQQoqnQQMJCSVcCgnQQoqnQQQQoqnxioqnQQQQ oqnQQniaQQoqnQQQQoqnqgoqnQQQQoqnQQWJQzhYAUoVMTAdAT33nvX3nB

我的代码有什么问题吗?为什么我没有得到与 javascript 代码相同的结果?

最佳答案

实际上,你的 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环形。这样,两种语言的内在逻辑基本上保持相同。

更改:

 #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]

...

并且..

    ...

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

关于javascript - 将 JavaScript 代码转换为 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38227463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com