Here is the link to the Daily Programmer sub-reddit where this problem was original found: https://www.reddit.com/r/dailyprogrammer/comments/6grwny/20170612_challenge_319_easy_condensing_sentences/
Follow me on twitter: https://twitter.com/CodyLSeibert
Help us improve our videos by taking this short survey here:
https://goo.gl/forms/rustp398xzDYRZoI3
Learn to code for free and get a developer job: https://www.freecodecamp.com
Read hundreds of articles on programming: https://medium.freecodecamp.com
source
really nice explanation ~ thanks!
I am getting confused in my python solution as i am unable to remove next element fro my list of words. pleas help:
def condense(left,right):
for i in range(len(left)):
sub=left[i:]
if right.startswith(sub):
return left +right.replace(sub,'')
return None
def con_sen(sent):
words=sent.split(' ')
for i in range(len(words)-1):
condensed=condense(words[i],words[i+1])
if condensed:
words[i]=condensed
words[i+1]
i-=1
return ' '.join(words)
print(con_sen('no one ever runs so often'))
Can you tell me what editor do you use in this video
Which keyboard do you have? You type so fast… 🙂
How do you get that command line on atom.I just downloaded atom so ill be glad if anyone could help me
what editor is that? if its atom, what package are you using to run it in editor?
I'm not sure it was clear what should happen with an edge case like "leg ego lego", but it seems like it would not be handled correctly if the correct answer is "lego lego". (Ego does not combine with lego…but the combination of leg and ego into lego does combine with lego.) Could you address that?
awesome!
I did mine with reduce:
{
function converter(str) {
let strarr = str.split(" ");
let ans = strarr.reduce(function(l, r) {
for (let i = 0; i < l.length; i++) {
let leftSubstring = l.substring(i);
if (r.startsWith(leftSubstring)) {
return l + r.replace(leftSubstring, '');
}
}
return l + " " + r;
});
return ans;
}
console.log(converter("no one ever runs so often"));
console.log(converter("yellow low dog"));
console.log(converter("the black kettle is hot"));
console.log(converter("hello world"));
}
A good exercise to practice reduce
Here is my jsbin https://jsbin.com/xoqajidofo/edit?js,console
couldn't we have just used regular expressions to solve this ?
You could solve with regex:
const condense = (sentence) => return sentence.replace(/(w+)s+1/g, "$1")
EDIT: Holy shit I just checked the Reddit link, I was about to go like "Move along bois, I got this with my regex" and the most voted solution is exactly the same, except he named things differently (compress and str instead of condense and sentence). I guess we're not alone in this world.
The idea here is that, the regular expression first captures 1 or more alphanumeric characters ((w+)) and stores it as "capture group #1, then it matches 1 or more whitespace characters, then it matches the capture group #1 (1) and the /g flag means that we want to replace all instances of this pattern (1 or more alphanumeric characters followed by any number of spaces followed by the same 1 or more alphanumeric characters of the ending of the previous word); now, the 2nd argument says that all of those instances ought to be replaced by a string that only contains the first capturing group. So for example:
"live verses", the regexp will capture "ve" in "live" and then it will match the only space between them, then it will check to see if all of the previous things are followed by "ve", if that's true, it means that there's a replaceable match and then it replaces "ve ve" (the final match) with "$1" which the replace method will turn into "ve".
I got this one. Pretty bad code and a quick one, but slightly different, so I figured why not share it:) (sorry))
https://gist.github.com/NerveClasp/3e24dc26f627439b42bc6e3579cfe40c
What syntax theme is that, seems like an edited one dark.