简体   繁体   English

尝试从urlify问题中编写Java代码以破解python中的编码采访

[英]Trying to write the java code from the urlify problem in cracking the coding interview in python

I took the Java code from cracking the coding interview for the urlify problem (1.3): 我从破解urlify问题(1.3)的编码访谈中获取了Java代码:

URLify: Write a method to replace all spaces in a string with '%20'. URLify:编写一种方法以'%20'替换字符串中的所有空格。 You may assume that the string has sufficient space at the end to hold the additional characters, and that you are given the "true" length of the string. 您可以假定字符串的末尾有足够的空间来容纳其他字符,并且假定字符串的长度为“ true”。 (Note: if implementing in Java, please use a character array so that you can perform this operation in place.) (注意:如果使用Java实现,请使用字符数组,以便可以就地执行此操作。)

EXAMPLE

Input: "Mr John Smith , 13 输入:“约翰·史密斯先生,13岁

Output: "Mr%2eJohn%2eSmith" 输出:“ Mr%2eJohn%2eSmith”

I'm having some issues with the converted code. 我在转换后的代码中遇到了一些问题。 Here is my python code: 这是我的python代码:

def urlify(str, trueLength):
    spaceCount = 0
    index = 0
    i = 0
    for i in range(0, trueLength):
        if str[i] == ' ':
            spaceCount += 1
        print(spaceCount, "spaceCount")
    index = trueLength + spaceCount * 2
    if (trueLength < len(str)):
        str[trueLength] = '\0'
    for i in range(trueLength, 0):
        if str[i] == ' ':
            str[index - 1] = '0'
            str[index - 2] = '2'
            str[index - 3] = '%'
            index = index - 3
        else:
            str[index - 1] = str[i]
            index = index - 1


print(urlify("Mr John Smith     ", 13))

I think one of the issues is 我认为问题之一是

str[trueLength] = '\0'

I'm not sure what else could be an issue. 我不确定还有什么问题。 I'm also a little confused about the two lines 我对这两行也有些困惑

if (trueLength < len(str)):
        str[trueLength] = '\0'

so if someone could explain those lines, that would be awesome. 因此,如果有人可以解释这些内容,那将非常棒。 I just wanted to fully understand Gayle's solution. 我只是想完全了解盖尔的解决方案。


Code I found: 我发现的代码:

def urlify(string, length):
'''function replaces single spaces with %20 and removes trailing spaces'''
new_index = len(string)

for i in reversed(range(length)):
    if string[i] == ' ':
        # Replace spaces
        string[new_index - 3:new_index] = '%20'
        new_index -= 3
    else:
        # Move characters
        string[new_index - 1] = string[i]
        new_index -= 1

return string

Shorten code(more Pythonic way): 缩短代码(更多Python方式):

def urlify(string, real_length):
 return string[:real_length].replace(' ', '%20')

Explanation: 说明:

string[:real_length]
# This limits strings to real length, in your case to 13. This will remove unnecessary end of the string. 
.replace(' ', '%20')
# This replaces every space with '%20'.

About your code: 关于您的代码:

  1. In Python, 'str' is reserved word. 在Python中,“ str”是保留字。 Do not use it. 不要使用它。

  2. In Python, you can't change strings. 在Python中,您无法更改字符串。 You must create new one. 您必须创建一个新的。 String item assignment is not supported. 不支持字符串项目分配。 Your code is completely based on item assignment. 您的代码完全基于项目分配。 You should instead create new string and add characters to it. 您应该改为创建新字符串并向其中添加字符。

  3. This code is really messed up. 这段代码真的搞砸了。 It is very hard to understand, you should find easier solution. 这很难理解,您应该找到更简单的解决方案。

Your code and logic optimized: 您的代码和逻辑已优化:

def urlify(string, trueLength):
    new_string = ''
    for i in range(0, trueLength):
        if string[i] == ' ':
            new_string=new_string+'%20'
        else:
            new_string=new_string+string[i]
    return new_string

My guess, justing trying to understand the code you want to understand: 我的猜测是,为了尝试理解您想要理解的代码:

Note that: 注意:

you are given the "true" length of the string. 您将获得字符串的“ true”长度。

So the trueLength can be shorter than the len(str) , and you should handle this case in your code. 因此, trueLength可以比len(str)短,您应该在代码中处理这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM