Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URLify.py #6

Closed
tyrantkhan opened this issue May 4, 2016 · 3 comments
Closed

URLify.py #6

tyrantkhan opened this issue May 4, 2016 · 3 comments

Comments

@tyrantkhan
Copy link

tyrantkhan commented May 4, 2016

I was under the impression that python strings are immutable as well. I tried compiling this code and it did indeed fail. In my own working solution I had to create a separate string and return that.

I took a more forward approach rather than the reverse route because of this:

def urlify(str, str_size):
    url_encoded

    for k,v in enumerate(str):
        if k < str_size:
            #size marks end of string
            if v == ' ':
                url_encoded += '%20'
            else:
                url_encoded += v
        else:
            break

    return url_encoded

@mmermerkaya
Copy link
Collaborator

Yours is also a valid solution and it works, but the question asks for an in-place solution. That's why we used a character array (like the book suggests), and not a separate string.

@tyrantkhan
Copy link
Author

I was actually going to post, i just saw that the unit test does use a character array. thanks.

@asaph
Copy link
Collaborator

asaph commented May 4, 2016

This isn't the first time a non-in-place solution has been suggested as a replacement for this solution. See careercup/CtCI-6th-Edition#31.

mattdinhnguyen pushed a commit to mattdinhnguyen/CtCI-6th-Edition-Python that referenced this issue Jan 22, 2021
PHP Solutions & Unit Tests for Questions 2.6-2.8
mattdinhnguyen pushed a commit to mattdinhnguyen/CtCI-6th-Edition-Python that referenced this issue Jan 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants