VehiclesFashionRecipesBlogsHuntTravelsSportFunHandmadeITEducation
Mini-Games
x

x
zakruti.com » IT - Software » freeCodeCamp.org
Condensing Sentences - The Daily Programmer #319

Condensing Sentences - The Daily Programmer #319

FBTwitterReddit

video description

Rating: 4.0; Vote: 1
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
Date: 2022-03-14

Comments and reviews: 4


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

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 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'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