9 Replies - 8020 Views - Last Post: 14 April 2015 - 07:52 AM

#1 mike73  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Rervse word order but keep punctuation order

Post icon  Posted 15 March 2015 - 05:01 PM

I was bored and asked a friend to come up with a challenge for me. He came up with an interesting one:

Quote

Given an input String, write a method which will reverse the word order, but leaves all punctuation in its original place.

e.g:
input: Hello, world! How are you?
output: you, are! How world Hello?


The rules were:
* An English sentence is in the form: "An English sentence. Another sentence."
* A sentence begins with a letter and ends with a punctuation mark of some kind.
* Multiple punctuation marks (What?! Ok!!!) can be handled as you feel fit.
* Multiple consecutive white spaces can be treated as a single space), but feel free to tackle them differently if you wish.

You can either go for shortest solution, or an elegant solution.

Good luck!

Is This A Good Question/Topic? 2
  • +

Replies To: Rervse word order but keep punctuation order

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12134
  • View blog
  • Posts: 45,117
  • Joined: 27-December 08

Re: Rervse word order but keep punctuation order

Posted 15 March 2015 - 05:09 PM

Don't forget to post your code using spoiler tags!
Was This Post Helpful? 0
  • +
  • -

#3 mike73  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: Rervse word order but keep punctuation order

Posted 16 March 2015 - 06:44 AM

Here is my initial attemp:
Spoiler


It's a bit messy, so needs some tidying up.
It produces okayish output:

Quote

java WordOrder

Hello, world! How are you?
you, are! How world Hello?

ABCDEFG! HIJKLMOP? QRSTUV: WX; YZ...
YZ! WX? QRSTUV: HIJKLMOP; ABCDEFG...

!!! sTRIng ... Break... ???!!!...,, hmmm
!!! hmmm... Break...???!!!...,, sTRIng


Spaces seem to be the onlything so far that cause any trouble. At least it's a starting point.

This post has been edited by mike73: 16 March 2015 - 06:47 AM

Was This Post Helpful? 1
  • +
  • -

#4 gm04030276  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 06-January 08

Re: Rervse word order but keep punctuation order

Posted 16 March 2015 - 08:57 AM

Version using Python and regular expressions :)/>/>

Spoiler


Outputs

it handle you will How! sentance English an is This?!


This post has been edited by gm04030276: 16 March 2015 - 08:58 AM

Was This Post Helpful? 2
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1567
  • View blog
  • Posts: 3,520
  • Joined: 05-April 11

Re: Rervse word order but keep punctuation order

Posted 16 March 2015 - 09:14 AM

Not the most efficient solution.. Matching against the input twice, but oh well :D
Spoiler

We need more test cases :P

This post has been edited by CasiOo: 16 March 2015 - 09:19 AM
Reason for edit:: Edit: No need for the List ^^

Was This Post Helpful? 1
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,008
  • Joined: 11-December 07

Re: Rervse word order but keep punctuation order

Posted 17 March 2015 - 04:00 PM

Here is a (mostly) recursive version. Test cases included.

Spoiler

Was This Post Helpful? 1
  • +
  • -

#7 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,008
  • Joined: 11-December 07

Re: Rervse word order but keep punctuation order

Posted 17 March 2015 - 04:22 PM

And an iterative one:

Spoiler

This post has been edited by cfoley: 17 March 2015 - 04:23 PM

Was This Post Helpful? 0
  • +
  • -

#8 Fixoncloud  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-April 15

Re: Rervse word order but keep punctuation order

Posted 14 April 2015 - 04:29 AM

View Postcfoley, on 17 March 2015 - 04:22 PM, said:

And an iterative one:

Spoiler


Regarding this code,I tried executing it and got many errors.

This post has been edited by xclite: 14 April 2015 - 06:02 AM
Reason for edit:: Tried to fix quotes

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12134
  • View blog
  • Posts: 45,117
  • Joined: 27-December 08

Re: Rervse word order but keep punctuation order

Posted 14 April 2015 - 06:38 AM

I'm guessing you probably don't have JUnit installed and aren't using it.
Was This Post Helpful? 0
  • +
  • -

#10 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,008
  • Joined: 11-December 07

Re: Rervse word order but keep punctuation order

Posted 14 April 2015 - 07:52 AM

It uses a testing framework called junit. If you just want to try out my function then delete all the junit stuff and write a main method for it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1