[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

golf: tabs to bullets

Martin DeMello

3/25/2008 9:03:00 PM

Given:
an outline list, with each line indented by a series of tabs
a list of bullets (assume a circular list, for simplicity)

Write a function that replaces every tab with two spaces, except for
the last one, which is replaced by a bullet.

example:

input file:
foo
\t bar
\t \t baz
\t \t \t hello
\t \t world

bullets: %w(* - o x)

output (view in fixed width):
foo
* bar
- baz
o hello
- world

martin

17 Answers

Martin DeMello

3/25/2008 9:15:00 PM

0

On Tue, Mar 25, 2008 at 2:02 PM, Martin DeMello <martindemello@gmail.com> wrote:
> Given:
> an outline list, with each line indented by a series of tabs
> a list of bullets (assume a circular list, for simplicity)
>
> Write a function that replaces every tab with two spaces, except for
> the last one, which is replaced by a bullet.

Code is worth a thousand descriptions:

#!/usr/bin/ruby

file = ARGV[0]
MARKERS = "*-ox".split(//)

def bullet(n)
MARKERS[n % MARKERS.length]
end

IO.foreach(file) {|line|
a = line.gsub(/^\t+/, "")
n = line.length - a.length - 1
replace = n == -1 ? "" : " "*(n)+ bullet(n) + " "
out = line.gsub(/^\t+/, replace)
puts out
}

martin

James Gray

3/25/2008 9:17:00 PM

0

On Mar 25, 2008, at 4:02 PM, Martin DeMello wrote:

> Given:
> an outline list, with each line indented by a series of tabs
> a list of bullets (assume a circular list, for simplicity)
>
> Write a function that replaces every tab with two spaces, except for
> the last one, which is replaced by a bullet.
>
> example:
>
> input file:
> foo
> \t bar
> \t \t baz
> \t \t \t hello
> \t \t world
>
> bullets: %w(* - o x)
>
> output (view in fixed width):
> foo
> * bar
> - baz
> o hello
> - world

I didn't really golf it, but my answer is:

#!/usr/bin/env ruby -wKU

input = <<END_INPUT
foo
\t bar
\t \t baz
\t \t \t hello
\t \t world
END_INPUT

bullets = %w[* - o x]
input.gsub!(/^(\t ?)+/) do |indent|
tabs = indent.count("\t") - 1
" " * tabs + bullets[tabs] + " "
end

puts input

__END__

James Edward Gray II

Martin DeMello

3/25/2008 9:40:00 PM

0

On Tue, Mar 25, 2008 at 2:16 PM, James Gray <james@grayproductions.net> wrote:
>
> I didn't really golf it, but my answer is:

You have a point - I wasn't looking for golf so much as for neat
and/or offbeat approaches to the problem of counting the tabs.

martin

David A. Black

3/25/2008 9:49:00 PM

0

Hi --

On Wed, 26 Mar 2008, Martin DeMello wrote:

> On Tue, Mar 25, 2008 at 2:16 PM, James Gray <james@grayproductions.net> wrote:
>>
>> I didn't really golf it, but my answer is:
>
> You have a point - I wasn't looking for golf so much as for neat
> and/or offbeat approaches to the problem of counting the tabs.

Darn, I took you literally :-)

def t(s)b='*-ox';s.map{|l|c=l.count("\t")
(c-1).times{l.sub!(/\t/,' ')}
l.sub(/\t/,b[c-1,1])}end

t(some_string)

(Thanks to JEG2 for reminding me of count, which I had hypercorrected
in my mind into thinking only existed in 1.9 because of Array#count
:-)


David

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.r... for details and updates!

Joel VanderWerf

3/25/2008 10:00:00 PM

0


A slightly different approach (count the tabs once!), starting from
James's code:

input = <<END_INPUT
foo
\t bar
\t \t baz
\t \t \t hello
\t \t world
END_INPUT

bullets = %w[* - o x]

h = Hash.new {|h, k|
bullet = bullets[(k.count("\t")-1) % bullets.size]
h[k] = k.gsub(/(\t )(?=\t )/, " ").sub(/\t/, bullet)
}

input.gsub!(/^(\t ?)+/) do |indent|
h[indent]
end

puts input

__END__

foo
* bar
- baz
o hello
- world

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Gordon Thiesfeld

3/25/2008 10:12:00 PM

0

Similar to Joel's approach...

#bullets.rb
h={}
%w(\ * - o x).each_with_index{|b, i| h["\t" * i]= " " * i + "#{b} " }
puts gets.gsub(/^(\t*)/){ h[$1]} until $stdin.eof?

C:\ruby\scripts\>cat input_file | ruby bullets.rb
foo
* bar
- baz
o hello
- world

Gordon Thiesfeld

3/25/2008 10:25:00 PM

0

On Tue, Mar 25, 2008 at 5:11 PM, Gordon Thiesfeld <gthiesfeld@gmail.com> wrote:
> Similar to Joel's approach...
>
> #bullets.rb
> h={}
> %w(\ * - o x).each_with_index{|b, i| h["\t" * i]= " " * i + "#{b} " }
> puts gets.gsub(/^(\t*)/){ h[$1]} until $stdin.eof?
>
> C:\ruby\scripts\>cat input_file | ruby bullets.rb
>
>
> foo
> * bar
> - baz
> o hello
> - world
>


Except it was broken. I'll try it again.

#bullets.rb
h={''=>''}
%w(* - o x).each_with_index{|b, i| h["\t" * (i+1)]= "#{' ' * i}#{b} " }
puts gets.gsub(/^(\t*)/){ h[$1] } until $stdin.eof?

Joel VanderWerf

3/25/2008 10:30:00 PM

0

Joel VanderWerf wrote:
> h[k] = k.gsub(/(\t )(?=\t )/, " ").sub(/\t/, bullet)

a little more robust:

h[k] = k.gsub(/(\t ?)(?=\t)/, " ").sub(/\t/, bullet)

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

ara.t.howard

3/25/2008 10:36:00 PM

0


On Mar 25, 2008, at 3:02 PM, Martin DeMello wrote:

> Given:
> an outline list, with each line indented by a series of tabs
> a list of bullets (assume a circular list, for simplicity)
>
> Write a function that replaces every tab with two spaces, except for
> the last one, which is replaced by a bullet.
>
> example:
>
> input file:
> foo
> \t bar
> \t \t baz
> \t \t \t hello
> \t \t world
>
> bullets: %w(* - o x)

cfp2:~ > cat a.rb
DATA.read.gsub(%r/(?:\t\ ?)+/){|s|' '*(n=s.count("\t")-1)+%w(* - o x)
[n]+' '}.display

__END__
foo
bar
baz
hello
world


cfp2:~ > ruby a.rb
foo
* bar
- baz
o hello
- world



a @ http://draw...
--
sleep is the best meditation.
h.h. the 14th dalai lama




David A. Black

3/25/2008 10:58:00 PM

0

Hi --

On Wed, 26 Mar 2008, ara howard wrote:

>
> On Mar 25, 2008, at 3:02 PM, Martin DeMello wrote:
>
>> Given:
>> an outline list, with each line indented by a series of tabs
>> a list of bullets (assume a circular list, for simplicity)
>>
>> Write a function that replaces every tab with two spaces, except for
>> the last one, which is replaced by a bullet.
>>
>> example:
>>
>> input file:
>> foo
>> \t bar
>> \t \t baz
>> \t \t \t hello
>> \t \t world
>>
>> bullets: %w(* - o x)
>
> cfp2:~ > cat a.rb
> DATA.read.gsub(%r/(?:\t\ ?)+/){|s|' '*(n=s.count("\t")-1)+%w(* - o x)[n]+'
> '}.display

I don't think that does the two-space thing:

david-blacks-macbook:hacking dblack$ cat ara.rb
<<EOM.gsub(%r/(?:\t\ ?)+/){|s|' '*(n=s.count("\t")-1)+%w(* - o x)[n]+'
'}.display
\t\t\ta
\t\tb
\t\t\t\tc
EOM
david-blacks-macbook:hacking dblack$ ruby ara.rb
o a
- b
x c

Then again, mine didn't either....


David

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.r... for details and updates!