Splitting of string at an interval

S

subhabangalore

Dear Group,

I was looking to split a string in a particular interval, like,

If I have a string,
string="The Sun rises in the east of our earth"

I like to see it as,
words=["The Sun","rises in","in the","east of","our earth"]

If any one of the learned members can kindly suggest.

Regards,
Subhabrata.
 
D

Dave Angel

Dear Group,

I was looking to split a string in a particular interval, like,

If I have a string,
string="The Sun rises in the east of our earth"

Are you asserting that there is nothing but letters and whitespace in
the string, and that any amount of consecutive whitespace may be
considered a blank?
I like to see it as,
words=["The Sun","rises in","in the","east of","our earth"]

Those aren't words, they're phrases. But more importantly, you're
somehow doubling the word "in" before parsing.
If any one of the learned members can kindly suggest.

split it into a list, use slices to divide that into even and odd
numbered words. Use zip to combine those two list together, and then
combine the resultant tuples with a space between.
 
S

Steven D'Aprano

Dear Group,

I was looking to split a string in a particular interval, like,

If I have a string,
string="The Sun rises in the east of our earth"

I like to see it as,
words=["The Sun","rises in","in the","east of","our earth"]

If any one of the learned members can kindly suggest.


Like every programming problem, the solution is to break it apart into
small, simple steps that even a computer can follow.


1) Split the string into words at whitespace.

words = string.split()


2) Search for the word "in", and if found, duplicate it.

tmp = []
for word in words:
if word == 'in':
tmp.append('in')
tmp.append(word)

words = tmp


3) Take the words two at a time.

phrases = [words[i:i+2] for i in range(0, len(words), 2)]


4) Join the phrases into strings.

phrases = [' '.join(phrase) for phrase in phrases]
 
C

Chris Angelico

Like every programming problem, the solution is to break it apart into
small, simple steps that even a computer can follow.
...

5) Shortcut the whole thing, since the problem was underspecified, by
using a literal.

words = ["The Sun", "rises in", "in the", "east of", "our earth"]

*dive for cover against rotten tomatoes*

ChrisA
 
R

Roy Smith

Chris Angelico said:
Like every programming problem, the solution is to break it apart into
small, simple steps that even a computer can follow.
...

5) Shortcut the whole thing, since the problem was underspecified, by
using a literal.

words = ["The Sun", "rises in", "in the", "east of", "our earth"]

*dive for cover against rotten tomatoes*

Seems like the right solution to me.

For a while, I was rabidly(*) into TDD (Test Driven Development). The
cycle I was using was, "Write a specification of a behavior, write a
(failing) test for that behavior, then write the least possible amount
of code to make the test pass. Lather, Rinse, Repeat, Ship"

The "least possible" part is important. It makes sure the cycles stay
short (ideally, just a few minutes), and that you don't write any code
for which you don't have tests. If you buy into that plan, then I see
nothing wrong with your suggested solution.

(*) I still believe in TDD, but I don't practice it quite as
enthusiastically as I used to. Which probably means my code isn't as
good as it used to be.
 
A

Arnaud Delobelle

For a while, I was rabidly(*) into TDD (Test Driven Development). The
cycle I was using was, "Write a specification of a behavior, write a
(failing) test for that behavior, then write the least possible amount
of code to make the test pass. Lather, Rinse, Repeat, Ship"

The "least possible" part is important. It makes sure the cycles stay
short (ideally, just a few minutes), and that you don't write any code
for which you don't have tests.

The least amount of code is often also not the best in terms of time
or space complexity. Does this mean you have to write tests for time
and space complexity as well? That's interesting, but I don't know of
tools to help do that (time complexity seems easy enough, but space
complexity seems tougher to me).
 
R

Roy Smith

The least amount of code is often also not the best in terms of time
or space complexity. Does this mean you have to write tests for time
and space complexity as well? That's interesting, but I don't know of
tools to help do that (time complexity seems easy enough, but space
complexity seems tougher to me).


If space and time complexity are important, then you need to write a test for those things. If you have no test for them, then it's not important and you shouldn't worry about it. At least according to the TDD catechism :)

From a somewhat less radical point of view, the first thing you want to do is get the code to produce correct results. Once you've got that (and a fully comprehensive test suite to prove it), then you can move on to making it more efficient, and your test suite serves as protection against behavior regressions.

And, yes, I agree that testing for time and space complexity are not trivial, because making accurate, repeatable, and isolated measurements of those things is often surprisingly complicated. I can't help point out, however, that if your initial implementation is to have your code return a constant, it's pretty likely to be an optimum solution in both time and space :)
 
C

Chris Angelico

I can't help point out, however, that if your initial implementation is to have your code return a constant, it's pretty likely to be an optimum solution in both time and space :)

Likely, but not certain.

# 1
def fifty_stars():
return "**************************************************"

# 2
fifty_stars=lambda "*"*50

Okay, that's just getting 2AM stupid now. :)

ChrisA
 
T

Tim Chase

I see the top 10 entries are all written in Perl. I suppose this
says something.

About the capabilities of Perl for writing such code, or about the
drinking habits of Perl programmers? :)

Or-about-how-perl-drives-you-to-drink'ly yours,

-tkc
 
S

Steven D'Aprano

I see the top 10 entries are all written in Perl. I suppose this says
something.


When I write my own programming language, it will include a one-character
built-in command to perform 99 bottles of beer, just so my language will
always be the winner.

In fact, I may make it a bare . so that not only will it be the shortest
program, but also the smallest program in terms of number of non-white
pixels.
 
A

Andrew Berg

In fact, I may make it a bare . so that not only will it be the shortest
program, but also the smallest program in terms of number of non-white
pixels.
Until someone implements it in Whitespace.
 
C

Chris Angelico

When I write my own programming language, it will include a one-character
built-in command to perform 99 bottles of beer, just so my language will
always be the winner.

In fact, I may make it a bare . so that not only will it be the shortest
program, but also the smallest program in terms of number of non-white
pixels.

Don't be too specific, Steven. Also include a one-character built-in
to emit the program's own source, and another to echo "hello, world"
to standard output. And one to increment the accumulator, just for
completeness.

Who knows, it might already exist!

http://esolangs.org/wiki/Cliff_L._Biffle

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top