
Condensing Sentences - The Daily Programmer #319
video description
Date: 2022-03-14
Related videos
Comments and reviews: 4
KozmicLuis
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-.
reply
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-.
reply
vigilantezack
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-));
-
reply
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-));
-
reply
Gurmeet
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'))
reply
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'))
reply
J.D.
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?
reply
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?
reply
Add a review, comment
Other channel videos















