• Post Reply Bookmark Topic Watch Topic
  • New Topic

Extracting the strings  RSS feed

 
David Freed
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello dear all,
would you please advice me to write these code?

example input (#federer #great #game )->
output : [federer] [great][game], [federer, game],[federer,great], [game,great], [federer, game,great]

Thank you in advance.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use String.split() to separate out the tokens.
You then just need to work out an algorithm to combine them. Think how you would do it manually first. Once you've worked out the steps it shouldn't be too difficult to translate it to code.

Things to think about that aren't obvious from the example
Will there always be three tokens or can there be more or less ?
I notice for the last two you've reversed the order of game and great. Is that a requirement or a typo ?
 
David Freed
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you,
i don't need to split, the strings already in the list.
yes if [game,news], then we also need [news,game]
 
David Freed
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
they can be from 0 to n
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Freed wrote:yes if [game,news], then we also need [news,game]

Also ? So are you saying that the output in your example is not complete ?
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like a good candidate for recursive programming.
 
David Freed
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im so sorry, no we don't need it, one way is enough, the example is complete

Joanne Neal wrote:
David Freed wrote:yes if [game,news], then we also need [news,game]

Also ? So are you saying that the output in your example is not complete ?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so the first step is always the same. Figure out how YOU would do it. Better yet, figure out the instructions you would give someone ELSE, that then details exactly what is needed, how to accomplish it, and accounts for every scenario.

Writing the code is the EASY part. The hard part is the figuring out what you need to code.

So, can you explain to a ten year old child how they should go about creating the list of strings you need?
 
David Freed
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote it in this way,I will test it now :
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what you are looking for is called permutations.

Pseudocode:

 
David Freed
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all,

I wrote it in this way and it works, is it efficient? or very basic algorith?

 
David Freed
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it actually doesn't work for the lists of more than 3 , im really tired.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Freed wrote:Thank you all,

I wrote it in this way and it works, is it efficient? or very basic algorith?



Are you sure that works? It appears to get contiguous subsets, but I don't see how it would get [federer, game]. Here's my version of your code:



And here's the output:


Ignoring the empty set, you will get 2^n-1 output sets from an initial set of size n. In the list above, there are only six, not the expected seven, because "federer game" is missing.
 
David Freed
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whay swap? do you mean add?
does it work for the lists more than 3 members?
Knute Snortum wrote:I think what you are looking for is called permutations.

Pseudocode:

 
David Freed
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I implemented this psuedo code, but it doesn't work, would you pelase correct it?



 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ItDoesntWorkIsUseless
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Freed wrote:Hello All,
I implemented this psuedo code, but it doesn't work, would you pelase correct it?

We don't really do that here. We will help you figure out what needs to be done, but nobody will (or should) give you the solution. That's just how we prefer to work.

Read the link Joanne posted. Remember - people are giving up their time to help you. you should do everything you can to make it easy for them to help you. If you could post a complete program that someone here could take, compile, and run, that's easier than looking at your code and trying to reverse engineer the code needed to make this run.

So, give us more info, and you are more likely to get the help you need.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I'd do at this point: write a general method that takes a list and returns every permutation of that list. Send it:

{ a, b, c }

and you should get back

{ {a}, {b}, {c}, {a, b}, {b, c}, {a, c}, {a, b, c} }

Use my pseudo code to write it, or Google "permutations algorithm". Get that working, then tackle your full program. Post what you get. If it doesn't work, tell us exactly what's happening.

Good luck!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!