Комментарии:
Extremely helpful to have a human explain through the often confusingHackerRank interface.
ОтветитьSo glad I found your channel! This is exactly the kind of video I've been looking for. Great to see an example of what you'd do in a real interview setting and how you talk through all your questions/uncertainties. In my interview prep that's what I've been really curious about, what does it actually look like to solve a problem in real time - glad I'm not the only one asking myself a lot of questions and changing my mind as I go!
ОтветитьI think what is so valuable about this video is that you still expressed confidence in figuring out the problem even when you felt unsure about how something work (e.g. regex). I think how I would approach is talking through the entire problem before coding while I am walking through the problem. Regardless, this video demonstrated break downs of thought process which I found valuable. Kudos!
Ответитьyou can use the ASCII values when checking the charecters like this:
function printTokens(s) {
const tokens = [];
let token = '';
s.split('').forEach(char => {
if ((char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z')) {
token += char;
} else if(token){
tokens.push(token);
token = '';
}
});
console.log(tokens.length);
tokens.forEach(token => console.log(token));
}
printTokens("He is a very very good boy, isn't he?")
Thank you, I have an interview in 30 minutes. wish me luck!
ОтветитьSeriously? No string.match()?
ОтветитьReally enjoyed watching this, I think its great that you talk through options for one problem. Keep em coming !!
ОтветитьPlease do more of these!
ОтветитьMy solution:
const sample = "He is a very very good boy, ins't he?";
const tokenise = (str) => {
let output = str.split(/[\s!,?._'@]/).filter((w) => w.length > 0);
output.unshift(output.length);
return output.join("\n");
};
console.log(tokenise(sample));
Quokka playing tricks on you. (I would just disable it been how weird it behaves and how distracting it gets printing errors as you type and producing fake outputs) There are double spaces as it should be as shown on the red letters are the bottom after the word boy. One solution that comes to my mind not using regex would be to use replaceAll that way you can even replace all double spaces with just 1 space, and do a trim also.
ОтветитьSomething like this will work:
str.split(/[ !@,?]+/g).forEach(phrase => phrase !== "" && console.log(phrase))
May i know the theme name you are using for Syntax colors?
Ответитьconst str = "He is a very very good boy, isn't he?"
const tokens = str.split(/[^A-Za-z]/).filter(x => x != "")
console.log(tokens.length)
tokens.forEach(x => console.log(x))
Great video, lot's of learning here.
Ответитьconst tokenize = s => (s = s.split(/[ !,?._'@]/).filter(Boolean), s.unshift(s.length), s);
tokenize("He is a very very good boy, isn't he?").forEach(console.log);
Here's my solution (I don't program in JS, and I'm not using Regex, as you didn't use too):
solution = (string) => [...string].map(char => ['!', ',', '?', '.', '_', '\'', '@'].includes(char) ? ' ' : char).join('').split(' ').filter(char => char !== '')
This is a great video, breaking down the thought process on solving these problems. I like the way you went from for loops to more modern using .map() I am looking forward to more.
ОтветитьConvert string to array use the destructuring operator..... const stringArray = [...stringVariable]
ОтветитьI'll post my answer before watching. Harder than it looks.
function res(string) {
const split = string.split(/[\W\d]/g)
console.log(split.length.toString())
split.map((r) => r != '' && console.log(r))
}
res("How are you today, isn't it a lovely day, better than the last 3 days?")
Personally I would read everything once and then come with questions and read it again, as many of your questions are answered there, like if tokens can be of length 1. Not sure neither if refactoring right away adds any value, if any it makes you wonder, why didn't you wrote it "the effective way" to begin with.
ОтветитьHi James, I have skipped the video this time after few minutes. Consider learning regexp as that is a good tool which you can use everywhere (frontend, backend, sql, searching in text files, converting texts). When you master regexps then you will have another tool in your hands, which will save time :) I do press a like button for a raw approach and a courage to share it as a video :)
Ответитьconst inputString = "He is a very very good boy, isn't he?";
function splitTokens(inputString) {
let tokens = inputString.split(/[!,.@'?\s]+/);
return tokens.filter(token => token !== "");
}
function printTokens(tokenArr) {
console.log(tokenArr.length);
for (let token of tokenArr) {
console.log(token);
}
}
printTokens(splitTokens(inputString));
awesome
ОтветитьRegex is great, it takes a little bit to understand but is worth the effort to learn.
ОтветитьTkank for this video could you make a serie of this, is very interesting. Regards from México
Ответитьconst solution = str => {
const strArr = str
.replace(/[!,?._'@]/g, ' ')
.replace(' ', ' ')
.trim()
.split(' ');
console.log(strArr.length);
strArr.forEach(str => console.log(`${str}\n`));
};
solution(`He is a very very good boy, isn't he?`);
Probably missing something (newbie), but it worked with examples I tested with.
Two things I personally would’ve done differently. One is that I tend to use backticks to encompass strings when it’s convenient to avoid issues with literal strings containing single or double quotes; circumvents annoying, cumbersome-looking escapes. The second is that I probably would’ve tried replacing the special characters by doing a forEach loop on the special characters array and running a replaceAll on the source string for each. Great vid! These sorts of tests put developers on the spot, which can be uncomfortable, for sure. But I think it’s great to show the process and how you personally work through it.
ОтветитьBest 🥷🤞
ОтветитьMy only comment is you didn’t verify that the solution was still correct after using the Set approach. I was waiting for you to show the full output at the end because “very” appears twice in the input so did that still come out correctly?
ОтветитьDid I miss an instruction not to use RegEx to split the string? const tokens = s.split(/[ !.,_'@?]+/) in JavaScript.
ОтветитьHere is a way using a regex in split:
const input = "He is a very very good boy, isn't he?";
const tokens = input.split(/[ !,?._'@]+/).filter(s => (s.length > 0));
const output = `${tokens.length}\n${tokens.join("\n")}`;
console.log(output);
This is absolutely great. Could you do more JavaScript interview prep videos?
ОтветитьVery instructive video. I think you did it well on your first try but you just forgot to add blank space in your special character variable. Maybe I’m wrong i don’t have the experience you have. 😉
ОтветитьAnother approach:
const specialChars = new Set(['!', ' ',',', '?', '.', '_', '\'', '@'])
const printTokens = (str, specialChars) => {
let tempArray = [];
let tempString = '';
for (let i = 0; i < str.length; i++) {
const letter = str[i];
if (!specialChars.has(letter)) {
tempString += letter
} else if (letter!='') {
tempArray.push(tempString)
tempString = '';
}
}
return(tempArray.filter(t=>t));
}
let filtered = printTokens("He is a very good boy, isn't he?", specialChars)
console.log(filtered)
Personally I would have used a single loop and pushed items to an array of arrays. Each time a special character occurs I would start a new array. It would be slightly more efficient because split, join, and filter all imply another loop. Good video though.
ОтветитьThanks <3
ОтветитьWhat is the difference between instance and object??🤔
ОтветитьHackerman James
Ответить