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

chapter01/1.3 - URLify/urlify.js #20

Open
jarkin13 opened this issue Sep 19, 2017 · 4 comments
Open

chapter01/1.3 - URLify/urlify.js #20

jarkin13 opened this issue Sep 19, 2017 · 4 comments

Comments

@jarkin13
Copy link

jarkin13 commented Sep 19, 2017

For this problem, why wouldn't you do something along the lines of:

var uglify = function(input) {
  input = input.trim();
  var url = input.split(' ').join('%20');
  return url;
}

I know it says that it provides with the true length of the string, but would something a long these lines not be acceptable?

@skawaguchi
Copy link

You could also use Regex. But the exercise could also be to see how you would build this out in a more general way. I assumed that this was simple for the reason to see how would would modularize your code and go with a faster runtime, rather than to focus on how you would manipulate strings in a language that didn’t have these tools.

const charMap = {
   : %20
};

function replaceCharacter(str, targetChar, replacementChar) {
  const charRegex = new RegExp(targetChar, ‘gi’);

  return str.replace(charRegex, replacementChar);
}

function urlify(url) {
  let modifiedURL = url.trim();

  for(let charToReplace in charMap) {
    const replacementChar = charMap[charToReplace];

    modifiedURL = replaceCharacter(modifiedURL, charToReplace, replacementChar);
  }
  return modifiedURL;
}

Of course, if we are pretending there are no language features that trivialize the problem or we need to account for space (maybe the target device is embedded with insanely small resources), then we could do something along the lines of the existing code or the solution from the book.

Basically it seems your interviewer can make it as hard as they want to, so we should probably consider all cases.

@liz-ete
Copy link

liz-ete commented Oct 3, 2017

Apparently there is an issue with this solution because when you run it the result it returns is missing some characters: Mr%20hohn%20ith%20.
Also without using any JS specific methods/properties you could still simplify this even more.

@skawaguchi @jarkin13 The thing that I don't like about using specific javascript methods/properties, specially if you can do without them, is that if your interviewer asks you about the Big O for your solution, unless you know what's happening behind curtains, you won't be able to provide an answer.

@Elaniobro
Copy link

I came here to write, you could simply use regex, I do like your solution @jarkin13, I did not think to use trim();

const URLify = (string) => string.replace(/[ \t]+$/g, '').replace(/[ ]/g, '%20');

@AmitJoki
Copy link

I think the following is a general solution and that which is more elegant.

var urlify = function(str, length) {
  var strArr = [];
  for(var i = 0; i < length; i++) {
     strArr[i] = str[i] == ' ' ? '%20' : str[i]; 
  }
  return strArr.join('');
};

I've submitted a PR for the same.

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

5 participants