Find longest word in a string: are any of these algorithms good?
I'm trying to find the longest word in a given text, but it has to be done "old school": without using split, enumerate, etc. (I'm using Python but what I'm looking for is more of a pseudocode or a general algorithm). I'm struggling with the last word (or the last character).
As a precondition, there are no leading or trailing spaces.
So I came up with two options that do the job but I feel like they're breaking some best practices:
Option 1
longestWord=""
lenlongestWord=0
currentWord=""
for i in range(len(text)):
if text[i]==" " or i==len(text)-1:
if i==len(text)-1:
currentWord+=text[i]
if len(currentWord) > lenlongestWord:
longestWord=currentWord
lenlongestWord=len(currentWord)
currentWord=""
else:
currentWord+=text[i]
print("Longest word: ", longestWord)
From this one I dislike this part (but I had to do it or I would be missing the last word in the string:
if i==len(text)-1:
currentWord+=text[i]
because I'm duplicating the concatenation, both in the "if" block and the "else" block.
Option 2:
text+=" "
longestWord=""
lenlongestWord=0
currentWord=""
for i in range(len(text)):
if text[i]==" ":
if len(currentWord) > lenlongestWord:
longestWord=currentWord
lenlongestWord=len(currentWord)
currentWord=""
else:
currentWord+=text[i]
print("Longest word: ", longestWord)
What I don't like from this one is the fact that I'm manipulating the original text by adding a trailing whitespace so I can read it as a word separator.
Is there a third option I'm missing? Or maybe one of these two is not as bad practice as I believe?
strings
|
show 3 more comments
I'm trying to find the longest word in a given text, but it has to be done "old school": without using split, enumerate, etc. (I'm using Python but what I'm looking for is more of a pseudocode or a general algorithm). I'm struggling with the last word (or the last character).
As a precondition, there are no leading or trailing spaces.
So I came up with two options that do the job but I feel like they're breaking some best practices:
Option 1
longestWord=""
lenlongestWord=0
currentWord=""
for i in range(len(text)):
if text[i]==" " or i==len(text)-1:
if i==len(text)-1:
currentWord+=text[i]
if len(currentWord) > lenlongestWord:
longestWord=currentWord
lenlongestWord=len(currentWord)
currentWord=""
else:
currentWord+=text[i]
print("Longest word: ", longestWord)
From this one I dislike this part (but I had to do it or I would be missing the last word in the string:
if i==len(text)-1:
currentWord+=text[i]
because I'm duplicating the concatenation, both in the "if" block and the "else" block.
Option 2:
text+=" "
longestWord=""
lenlongestWord=0
currentWord=""
for i in range(len(text)):
if text[i]==" ":
if len(currentWord) > lenlongestWord:
longestWord=currentWord
lenlongestWord=len(currentWord)
currentWord=""
else:
currentWord+=text[i]
print("Longest word: ", longestWord)
What I don't like from this one is the fact that I'm manipulating the original text by adding a trailing whitespace so I can read it as a word separator.
Is there a third option I'm missing? Or maybe one of these two is not as bad practice as I believe?
strings
4
I'm not really sure this is on-topic here. But regarding your first approach - if you don't like concatenating so much, why are you bothering with remembering the current longest word, rather than its start and end position?
– Ordous
15 hours ago
1
Note that your examples should resetcurrentWord
when the find a space, rather than when they mark something as longer
– Caleth
15 hours ago
Thanks @Caleth it was a typo. Fixed it :)
– Floella
15 hours ago
2
You're essentially just reimplementing string.split. How is that really accomplishing anything with the artificial requirement not to use it?
– whatsisname
12 hours ago
2
Let me give you some doesn't-answer-the-question advice: Just about every language's standard library has functions for doing these things. They're well-solved problems and often available in source form. If you want to know how they've been implemented by the more-experinced, study them.
– Blrfl
12 hours ago
|
show 3 more comments
I'm trying to find the longest word in a given text, but it has to be done "old school": without using split, enumerate, etc. (I'm using Python but what I'm looking for is more of a pseudocode or a general algorithm). I'm struggling with the last word (or the last character).
As a precondition, there are no leading or trailing spaces.
So I came up with two options that do the job but I feel like they're breaking some best practices:
Option 1
longestWord=""
lenlongestWord=0
currentWord=""
for i in range(len(text)):
if text[i]==" " or i==len(text)-1:
if i==len(text)-1:
currentWord+=text[i]
if len(currentWord) > lenlongestWord:
longestWord=currentWord
lenlongestWord=len(currentWord)
currentWord=""
else:
currentWord+=text[i]
print("Longest word: ", longestWord)
From this one I dislike this part (but I had to do it or I would be missing the last word in the string:
if i==len(text)-1:
currentWord+=text[i]
because I'm duplicating the concatenation, both in the "if" block and the "else" block.
Option 2:
text+=" "
longestWord=""
lenlongestWord=0
currentWord=""
for i in range(len(text)):
if text[i]==" ":
if len(currentWord) > lenlongestWord:
longestWord=currentWord
lenlongestWord=len(currentWord)
currentWord=""
else:
currentWord+=text[i]
print("Longest word: ", longestWord)
What I don't like from this one is the fact that I'm manipulating the original text by adding a trailing whitespace so I can read it as a word separator.
Is there a third option I'm missing? Or maybe one of these two is not as bad practice as I believe?
strings
I'm trying to find the longest word in a given text, but it has to be done "old school": without using split, enumerate, etc. (I'm using Python but what I'm looking for is more of a pseudocode or a general algorithm). I'm struggling with the last word (or the last character).
As a precondition, there are no leading or trailing spaces.
So I came up with two options that do the job but I feel like they're breaking some best practices:
Option 1
longestWord=""
lenlongestWord=0
currentWord=""
for i in range(len(text)):
if text[i]==" " or i==len(text)-1:
if i==len(text)-1:
currentWord+=text[i]
if len(currentWord) > lenlongestWord:
longestWord=currentWord
lenlongestWord=len(currentWord)
currentWord=""
else:
currentWord+=text[i]
print("Longest word: ", longestWord)
From this one I dislike this part (but I had to do it or I would be missing the last word in the string:
if i==len(text)-1:
currentWord+=text[i]
because I'm duplicating the concatenation, both in the "if" block and the "else" block.
Option 2:
text+=" "
longestWord=""
lenlongestWord=0
currentWord=""
for i in range(len(text)):
if text[i]==" ":
if len(currentWord) > lenlongestWord:
longestWord=currentWord
lenlongestWord=len(currentWord)
currentWord=""
else:
currentWord+=text[i]
print("Longest word: ", longestWord)
What I don't like from this one is the fact that I'm manipulating the original text by adding a trailing whitespace so I can read it as a word separator.
Is there a third option I'm missing? Or maybe one of these two is not as bad practice as I believe?
strings
strings
edited 15 hours ago
Floella
asked 16 hours ago
FloellaFloella
18617
18617
4
I'm not really sure this is on-topic here. But regarding your first approach - if you don't like concatenating so much, why are you bothering with remembering the current longest word, rather than its start and end position?
– Ordous
15 hours ago
1
Note that your examples should resetcurrentWord
when the find a space, rather than when they mark something as longer
– Caleth
15 hours ago
Thanks @Caleth it was a typo. Fixed it :)
– Floella
15 hours ago
2
You're essentially just reimplementing string.split. How is that really accomplishing anything with the artificial requirement not to use it?
– whatsisname
12 hours ago
2
Let me give you some doesn't-answer-the-question advice: Just about every language's standard library has functions for doing these things. They're well-solved problems and often available in source form. If you want to know how they've been implemented by the more-experinced, study them.
– Blrfl
12 hours ago
|
show 3 more comments
4
I'm not really sure this is on-topic here. But regarding your first approach - if you don't like concatenating so much, why are you bothering with remembering the current longest word, rather than its start and end position?
– Ordous
15 hours ago
1
Note that your examples should resetcurrentWord
when the find a space, rather than when they mark something as longer
– Caleth
15 hours ago
Thanks @Caleth it was a typo. Fixed it :)
– Floella
15 hours ago
2
You're essentially just reimplementing string.split. How is that really accomplishing anything with the artificial requirement not to use it?
– whatsisname
12 hours ago
2
Let me give you some doesn't-answer-the-question advice: Just about every language's standard library has functions for doing these things. They're well-solved problems and often available in source form. If you want to know how they've been implemented by the more-experinced, study them.
– Blrfl
12 hours ago
4
4
I'm not really sure this is on-topic here. But regarding your first approach - if you don't like concatenating so much, why are you bothering with remembering the current longest word, rather than its start and end position?
– Ordous
15 hours ago
I'm not really sure this is on-topic here. But regarding your first approach - if you don't like concatenating so much, why are you bothering with remembering the current longest word, rather than its start and end position?
– Ordous
15 hours ago
1
1
Note that your examples should reset
currentWord
when the find a space, rather than when they mark something as longer– Caleth
15 hours ago
Note that your examples should reset
currentWord
when the find a space, rather than when they mark something as longer– Caleth
15 hours ago
Thanks @Caleth it was a typo. Fixed it :)
– Floella
15 hours ago
Thanks @Caleth it was a typo. Fixed it :)
– Floella
15 hours ago
2
2
You're essentially just reimplementing string.split. How is that really accomplishing anything with the artificial requirement not to use it?
– whatsisname
12 hours ago
You're essentially just reimplementing string.split. How is that really accomplishing anything with the artificial requirement not to use it?
– whatsisname
12 hours ago
2
2
Let me give you some doesn't-answer-the-question advice: Just about every language's standard library has functions for doing these things. They're well-solved problems and often available in source form. If you want to know how they've been implemented by the more-experinced, study them.
– Blrfl
12 hours ago
Let me give you some doesn't-answer-the-question advice: Just about every language's standard library has functions for doing these things. They're well-solved problems and often available in source form. If you want to know how they've been implemented by the more-experinced, study them.
– Blrfl
12 hours ago
|
show 3 more comments
3 Answers
3
active
oldest
votes
String concatenation can be slow. If you just want to go "old school", and speed is important, then forget storing the current word, and continually concatenating letters onto it.
Just store the start of the current word, as an index into the array, and the start of the longest word. Store the length of the current word, and the length of the longest.
Whenever the length of the current word exceeds the length of the (previous) longest, update the start and length of the longest.
At the end, you will have the start index and length of the longest word. You can just extract that word from the string.
PS: There's an assumption in your code that words are always separated by spaces, and that there's no other form of punctuation. Is this deliberate?
add a comment |
How about the straightforward approach?
string = 'abc defg hij klm ytrda nopasdas'
st, n = 0, 0 # start and length of longest word
m = 0 # start of current word
# instead of enumerate, you can just increment i in the loop
for i, x in enumerate(string):
if x == ' ':
if i > m+n:
st = m
n = i-st
m = i+1
# this is the special case for the last word
if i > m+n:
st = m
n = i-st+1
print(string[st:st+n])
This will not copy strings and only go through your string once.
If you did this in C you would probably use pointers. For C++ you would use iterators - sadly, in Python, these are not as powerful.
New contributor
2
I think the "straightforward approach" would usesplit
...
– BlueRaja - Danny Pflughoeft
8 hours ago
Thesplit
approach would be extremely slow and wasteful for any string of non-trivial length. I wouldn't dare describe it as “straightforward” at all because of all the pointless work that will have to be done in the background.
– Patrice Levesque
58 mins ago
add a comment |
If you feel like trying recursion, you could give this a go. This is by no means elegant, and it does assume that your string is separated by a single space between words, but it's pretty quick. Other considerations you'll need to take into account is how you break ties (in this case, unless a word is strictly longer, it goes with the first one it finds). I've also built a helper function to recursively build the next word in the string.
def buildWord(string):
if(len(string) == 0 or string[0]==' '):
return "";
else:
return string[0] + buildWord(string[1:]);
def findLongest(string, word = ""):
#stop if the string is empty
if(len(string) <= 0):
return word;
#build the next word in the string
curWord = buildWord(string);
#check the current longest word
longestWord = curWord if len(curWord) > len(word) else word;
#keep checking
return findLongest(string[len(curWord)+1:], longestWord);
print(findLongest("this is a string with a very long antediluvian word in it"));
New contributor
How is this different to OPs Option 1, except done as recursion, rather than iteration? In fact, since this is tail recursion, it's trivial to show that it does the exact same thing.
– Ordous
11 hours ago
Other than the fact that it removes the duplicate concatenation code, which is what he was concerned about, I suppose it isn't different.
– GnoveltyGnome
11 hours ago
Except it still does letter-by-letter concatenation of each word inbuildWord
?
– Ordous
11 hours ago
It does concatenation once, in one place. In option 1, it does concatenation once, in two places (in both the if and else statement). The goal here wasn't to remove concatenation entirely (and indeed, without using split, there's only a few options for that - one of which is in the other answer provided by Simon B). I was trying to address the duplicate code issue from Option 1, and I suppose I could have been more clear about that.
– GnoveltyGnome
11 hours ago
1
Sorry, you are right! I misread the question and thought OPs problem was unnecessary work, not code duplication. Your variant does solve that.
– Ordous
9 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "131"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f388394%2ffind-longest-word-in-a-string-are-any-of-these-algorithms-good%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
String concatenation can be slow. If you just want to go "old school", and speed is important, then forget storing the current word, and continually concatenating letters onto it.
Just store the start of the current word, as an index into the array, and the start of the longest word. Store the length of the current word, and the length of the longest.
Whenever the length of the current word exceeds the length of the (previous) longest, update the start and length of the longest.
At the end, you will have the start index and length of the longest word. You can just extract that word from the string.
PS: There's an assumption in your code that words are always separated by spaces, and that there's no other form of punctuation. Is this deliberate?
add a comment |
String concatenation can be slow. If you just want to go "old school", and speed is important, then forget storing the current word, and continually concatenating letters onto it.
Just store the start of the current word, as an index into the array, and the start of the longest word. Store the length of the current word, and the length of the longest.
Whenever the length of the current word exceeds the length of the (previous) longest, update the start and length of the longest.
At the end, you will have the start index and length of the longest word. You can just extract that word from the string.
PS: There's an assumption in your code that words are always separated by spaces, and that there's no other form of punctuation. Is this deliberate?
add a comment |
String concatenation can be slow. If you just want to go "old school", and speed is important, then forget storing the current word, and continually concatenating letters onto it.
Just store the start of the current word, as an index into the array, and the start of the longest word. Store the length of the current word, and the length of the longest.
Whenever the length of the current word exceeds the length of the (previous) longest, update the start and length of the longest.
At the end, you will have the start index and length of the longest word. You can just extract that word from the string.
PS: There's an assumption in your code that words are always separated by spaces, and that there's no other form of punctuation. Is this deliberate?
String concatenation can be slow. If you just want to go "old school", and speed is important, then forget storing the current word, and continually concatenating letters onto it.
Just store the start of the current word, as an index into the array, and the start of the longest word. Store the length of the current word, and the length of the longest.
Whenever the length of the current word exceeds the length of the (previous) longest, update the start and length of the longest.
At the end, you will have the start index and length of the longest word. You can just extract that word from the string.
PS: There's an assumption in your code that words are always separated by spaces, and that there's no other form of punctuation. Is this deliberate?
answered 15 hours ago
Simon BSimon B
4,37511327
4,37511327
add a comment |
add a comment |
How about the straightforward approach?
string = 'abc defg hij klm ytrda nopasdas'
st, n = 0, 0 # start and length of longest word
m = 0 # start of current word
# instead of enumerate, you can just increment i in the loop
for i, x in enumerate(string):
if x == ' ':
if i > m+n:
st = m
n = i-st
m = i+1
# this is the special case for the last word
if i > m+n:
st = m
n = i-st+1
print(string[st:st+n])
This will not copy strings and only go through your string once.
If you did this in C you would probably use pointers. For C++ you would use iterators - sadly, in Python, these are not as powerful.
New contributor
2
I think the "straightforward approach" would usesplit
...
– BlueRaja - Danny Pflughoeft
8 hours ago
Thesplit
approach would be extremely slow and wasteful for any string of non-trivial length. I wouldn't dare describe it as “straightforward” at all because of all the pointless work that will have to be done in the background.
– Patrice Levesque
58 mins ago
add a comment |
How about the straightforward approach?
string = 'abc defg hij klm ytrda nopasdas'
st, n = 0, 0 # start and length of longest word
m = 0 # start of current word
# instead of enumerate, you can just increment i in the loop
for i, x in enumerate(string):
if x == ' ':
if i > m+n:
st = m
n = i-st
m = i+1
# this is the special case for the last word
if i > m+n:
st = m
n = i-st+1
print(string[st:st+n])
This will not copy strings and only go through your string once.
If you did this in C you would probably use pointers. For C++ you would use iterators - sadly, in Python, these are not as powerful.
New contributor
2
I think the "straightforward approach" would usesplit
...
– BlueRaja - Danny Pflughoeft
8 hours ago
Thesplit
approach would be extremely slow and wasteful for any string of non-trivial length. I wouldn't dare describe it as “straightforward” at all because of all the pointless work that will have to be done in the background.
– Patrice Levesque
58 mins ago
add a comment |
How about the straightforward approach?
string = 'abc defg hij klm ytrda nopasdas'
st, n = 0, 0 # start and length of longest word
m = 0 # start of current word
# instead of enumerate, you can just increment i in the loop
for i, x in enumerate(string):
if x == ' ':
if i > m+n:
st = m
n = i-st
m = i+1
# this is the special case for the last word
if i > m+n:
st = m
n = i-st+1
print(string[st:st+n])
This will not copy strings and only go through your string once.
If you did this in C you would probably use pointers. For C++ you would use iterators - sadly, in Python, these are not as powerful.
New contributor
How about the straightforward approach?
string = 'abc defg hij klm ytrda nopasdas'
st, n = 0, 0 # start and length of longest word
m = 0 # start of current word
# instead of enumerate, you can just increment i in the loop
for i, x in enumerate(string):
if x == ' ':
if i > m+n:
st = m
n = i-st
m = i+1
# this is the special case for the last word
if i > m+n:
st = m
n = i-st+1
print(string[st:st+n])
This will not copy strings and only go through your string once.
If you did this in C you would probably use pointers. For C++ you would use iterators - sadly, in Python, these are not as powerful.
New contributor
New contributor
answered 10 hours ago
michi7x7michi7x7
1211
1211
New contributor
New contributor
2
I think the "straightforward approach" would usesplit
...
– BlueRaja - Danny Pflughoeft
8 hours ago
Thesplit
approach would be extremely slow and wasteful for any string of non-trivial length. I wouldn't dare describe it as “straightforward” at all because of all the pointless work that will have to be done in the background.
– Patrice Levesque
58 mins ago
add a comment |
2
I think the "straightforward approach" would usesplit
...
– BlueRaja - Danny Pflughoeft
8 hours ago
Thesplit
approach would be extremely slow and wasteful for any string of non-trivial length. I wouldn't dare describe it as “straightforward” at all because of all the pointless work that will have to be done in the background.
– Patrice Levesque
58 mins ago
2
2
I think the "straightforward approach" would use
split
...– BlueRaja - Danny Pflughoeft
8 hours ago
I think the "straightforward approach" would use
split
...– BlueRaja - Danny Pflughoeft
8 hours ago
The
split
approach would be extremely slow and wasteful for any string of non-trivial length. I wouldn't dare describe it as “straightforward” at all because of all the pointless work that will have to be done in the background.– Patrice Levesque
58 mins ago
The
split
approach would be extremely slow and wasteful for any string of non-trivial length. I wouldn't dare describe it as “straightforward” at all because of all the pointless work that will have to be done in the background.– Patrice Levesque
58 mins ago
add a comment |
If you feel like trying recursion, you could give this a go. This is by no means elegant, and it does assume that your string is separated by a single space between words, but it's pretty quick. Other considerations you'll need to take into account is how you break ties (in this case, unless a word is strictly longer, it goes with the first one it finds). I've also built a helper function to recursively build the next word in the string.
def buildWord(string):
if(len(string) == 0 or string[0]==' '):
return "";
else:
return string[0] + buildWord(string[1:]);
def findLongest(string, word = ""):
#stop if the string is empty
if(len(string) <= 0):
return word;
#build the next word in the string
curWord = buildWord(string);
#check the current longest word
longestWord = curWord if len(curWord) > len(word) else word;
#keep checking
return findLongest(string[len(curWord)+1:], longestWord);
print(findLongest("this is a string with a very long antediluvian word in it"));
New contributor
How is this different to OPs Option 1, except done as recursion, rather than iteration? In fact, since this is tail recursion, it's trivial to show that it does the exact same thing.
– Ordous
11 hours ago
Other than the fact that it removes the duplicate concatenation code, which is what he was concerned about, I suppose it isn't different.
– GnoveltyGnome
11 hours ago
Except it still does letter-by-letter concatenation of each word inbuildWord
?
– Ordous
11 hours ago
It does concatenation once, in one place. In option 1, it does concatenation once, in two places (in both the if and else statement). The goal here wasn't to remove concatenation entirely (and indeed, without using split, there's only a few options for that - one of which is in the other answer provided by Simon B). I was trying to address the duplicate code issue from Option 1, and I suppose I could have been more clear about that.
– GnoveltyGnome
11 hours ago
1
Sorry, you are right! I misread the question and thought OPs problem was unnecessary work, not code duplication. Your variant does solve that.
– Ordous
9 hours ago
add a comment |
If you feel like trying recursion, you could give this a go. This is by no means elegant, and it does assume that your string is separated by a single space between words, but it's pretty quick. Other considerations you'll need to take into account is how you break ties (in this case, unless a word is strictly longer, it goes with the first one it finds). I've also built a helper function to recursively build the next word in the string.
def buildWord(string):
if(len(string) == 0 or string[0]==' '):
return "";
else:
return string[0] + buildWord(string[1:]);
def findLongest(string, word = ""):
#stop if the string is empty
if(len(string) <= 0):
return word;
#build the next word in the string
curWord = buildWord(string);
#check the current longest word
longestWord = curWord if len(curWord) > len(word) else word;
#keep checking
return findLongest(string[len(curWord)+1:], longestWord);
print(findLongest("this is a string with a very long antediluvian word in it"));
New contributor
How is this different to OPs Option 1, except done as recursion, rather than iteration? In fact, since this is tail recursion, it's trivial to show that it does the exact same thing.
– Ordous
11 hours ago
Other than the fact that it removes the duplicate concatenation code, which is what he was concerned about, I suppose it isn't different.
– GnoveltyGnome
11 hours ago
Except it still does letter-by-letter concatenation of each word inbuildWord
?
– Ordous
11 hours ago
It does concatenation once, in one place. In option 1, it does concatenation once, in two places (in both the if and else statement). The goal here wasn't to remove concatenation entirely (and indeed, without using split, there's only a few options for that - one of which is in the other answer provided by Simon B). I was trying to address the duplicate code issue from Option 1, and I suppose I could have been more clear about that.
– GnoveltyGnome
11 hours ago
1
Sorry, you are right! I misread the question and thought OPs problem was unnecessary work, not code duplication. Your variant does solve that.
– Ordous
9 hours ago
add a comment |
If you feel like trying recursion, you could give this a go. This is by no means elegant, and it does assume that your string is separated by a single space between words, but it's pretty quick. Other considerations you'll need to take into account is how you break ties (in this case, unless a word is strictly longer, it goes with the first one it finds). I've also built a helper function to recursively build the next word in the string.
def buildWord(string):
if(len(string) == 0 or string[0]==' '):
return "";
else:
return string[0] + buildWord(string[1:]);
def findLongest(string, word = ""):
#stop if the string is empty
if(len(string) <= 0):
return word;
#build the next word in the string
curWord = buildWord(string);
#check the current longest word
longestWord = curWord if len(curWord) > len(word) else word;
#keep checking
return findLongest(string[len(curWord)+1:], longestWord);
print(findLongest("this is a string with a very long antediluvian word in it"));
New contributor
If you feel like trying recursion, you could give this a go. This is by no means elegant, and it does assume that your string is separated by a single space between words, but it's pretty quick. Other considerations you'll need to take into account is how you break ties (in this case, unless a word is strictly longer, it goes with the first one it finds). I've also built a helper function to recursively build the next word in the string.
def buildWord(string):
if(len(string) == 0 or string[0]==' '):
return "";
else:
return string[0] + buildWord(string[1:]);
def findLongest(string, word = ""):
#stop if the string is empty
if(len(string) <= 0):
return word;
#build the next word in the string
curWord = buildWord(string);
#check the current longest word
longestWord = curWord if len(curWord) > len(word) else word;
#keep checking
return findLongest(string[len(curWord)+1:], longestWord);
print(findLongest("this is a string with a very long antediluvian word in it"));
New contributor
edited 12 hours ago
New contributor
answered 12 hours ago
GnoveltyGnomeGnoveltyGnome
1194
1194
New contributor
New contributor
How is this different to OPs Option 1, except done as recursion, rather than iteration? In fact, since this is tail recursion, it's trivial to show that it does the exact same thing.
– Ordous
11 hours ago
Other than the fact that it removes the duplicate concatenation code, which is what he was concerned about, I suppose it isn't different.
– GnoveltyGnome
11 hours ago
Except it still does letter-by-letter concatenation of each word inbuildWord
?
– Ordous
11 hours ago
It does concatenation once, in one place. In option 1, it does concatenation once, in two places (in both the if and else statement). The goal here wasn't to remove concatenation entirely (and indeed, without using split, there's only a few options for that - one of which is in the other answer provided by Simon B). I was trying to address the duplicate code issue from Option 1, and I suppose I could have been more clear about that.
– GnoveltyGnome
11 hours ago
1
Sorry, you are right! I misread the question and thought OPs problem was unnecessary work, not code duplication. Your variant does solve that.
– Ordous
9 hours ago
add a comment |
How is this different to OPs Option 1, except done as recursion, rather than iteration? In fact, since this is tail recursion, it's trivial to show that it does the exact same thing.
– Ordous
11 hours ago
Other than the fact that it removes the duplicate concatenation code, which is what he was concerned about, I suppose it isn't different.
– GnoveltyGnome
11 hours ago
Except it still does letter-by-letter concatenation of each word inbuildWord
?
– Ordous
11 hours ago
It does concatenation once, in one place. In option 1, it does concatenation once, in two places (in both the if and else statement). The goal here wasn't to remove concatenation entirely (and indeed, without using split, there's only a few options for that - one of which is in the other answer provided by Simon B). I was trying to address the duplicate code issue from Option 1, and I suppose I could have been more clear about that.
– GnoveltyGnome
11 hours ago
1
Sorry, you are right! I misread the question and thought OPs problem was unnecessary work, not code duplication. Your variant does solve that.
– Ordous
9 hours ago
How is this different to OPs Option 1, except done as recursion, rather than iteration? In fact, since this is tail recursion, it's trivial to show that it does the exact same thing.
– Ordous
11 hours ago
How is this different to OPs Option 1, except done as recursion, rather than iteration? In fact, since this is tail recursion, it's trivial to show that it does the exact same thing.
– Ordous
11 hours ago
Other than the fact that it removes the duplicate concatenation code, which is what he was concerned about, I suppose it isn't different.
– GnoveltyGnome
11 hours ago
Other than the fact that it removes the duplicate concatenation code, which is what he was concerned about, I suppose it isn't different.
– GnoveltyGnome
11 hours ago
Except it still does letter-by-letter concatenation of each word in
buildWord
?– Ordous
11 hours ago
Except it still does letter-by-letter concatenation of each word in
buildWord
?– Ordous
11 hours ago
It does concatenation once, in one place. In option 1, it does concatenation once, in two places (in both the if and else statement). The goal here wasn't to remove concatenation entirely (and indeed, without using split, there's only a few options for that - one of which is in the other answer provided by Simon B). I was trying to address the duplicate code issue from Option 1, and I suppose I could have been more clear about that.
– GnoveltyGnome
11 hours ago
It does concatenation once, in one place. In option 1, it does concatenation once, in two places (in both the if and else statement). The goal here wasn't to remove concatenation entirely (and indeed, without using split, there's only a few options for that - one of which is in the other answer provided by Simon B). I was trying to address the duplicate code issue from Option 1, and I suppose I could have been more clear about that.
– GnoveltyGnome
11 hours ago
1
1
Sorry, you are right! I misread the question and thought OPs problem was unnecessary work, not code duplication. Your variant does solve that.
– Ordous
9 hours ago
Sorry, you are right! I misread the question and thought OPs problem was unnecessary work, not code duplication. Your variant does solve that.
– Ordous
9 hours ago
add a comment |
Thanks for contributing an answer to Software Engineering Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f388394%2ffind-longest-word-in-a-string-are-any-of-these-algorithms-good%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
I'm not really sure this is on-topic here. But regarding your first approach - if you don't like concatenating so much, why are you bothering with remembering the current longest word, rather than its start and end position?
– Ordous
15 hours ago
1
Note that your examples should reset
currentWord
when the find a space, rather than when they mark something as longer– Caleth
15 hours ago
Thanks @Caleth it was a typo. Fixed it :)
– Floella
15 hours ago
2
You're essentially just reimplementing string.split. How is that really accomplishing anything with the artificial requirement not to use it?
– whatsisname
12 hours ago
2
Let me give you some doesn't-answer-the-question advice: Just about every language's standard library has functions for doing these things. They're well-solved problems and often available in source form. If you want to know how they've been implemented by the more-experinced, study them.
– Blrfl
12 hours ago