One liner challenge

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

One liner challenge

kobetic
[hidden email] wrote:

> Date: March 11, 2010 9:07:23 PM
> From: [hidden email]
> To: "Travis Griggs"<[hidden email]>
> Cc: "VWNC NC"<[hidden email]>
> Subject: [vwnc] One liner challenge
>
> "Travis Griggs"<[hidden email]> wrote:
> > Date: March 11, 2010 8:26:22 PM
> > From: "Travis Griggs"<[hidden email]>
> > To: "VWNC NC"<[hidden email]>
> > Subject: [vwnc] One liner challenge
> >
> > I was looking at someone's ruby/regex code. I could do the equivalent  
> > of what they were doing with the Regex library. But I was curious if  
> > it could be done tersely/elegantly without using regex. Here's the  
> > challenge.
> >
> > Given strings of the form
> >
> > 'This is a (string) with some (parenthetical fields)'
> >
> > turn them into
> >
> > 'This is a (STRING) with some (PARENTHETICAL FIELDS)'
>
> Not exactly a one-liner, but pretty much a literal transcription using Xtreams:
>
> | nesting |
> nesting := 0.
> ('This is a (string) with some (parenthetical fields)' reading
> transforming: [ :in :out || char |
> char := in get.
> char = $( ifTrue: [ nesting := nesting + 1 ].
> char = $) ifTrue: [ nesting := nesting - 1 ].
> out put: (nesting > 0 ifTrue: [ char asUppercase ] ifFalse: [ char ]) ]
> ) rest

Or more straightforward:

| nesting |
nesting := 0.
('This is a (string) with some (parenthetical fields)' reading
        collecting: [ :char |
                char = $( ifTrue: [ nesting := nesting + 1 ].
                char = $) ifTrue: [ nesting := nesting - 1 ].
                nesting > 0 ifTrue: [ char asUppercase ] ifFalse: [ char ] ]
) rest
_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc