my noob "numbering lines" programe. How to improve?

A

Adam Akhtar

Give us hints on how to improve this program. If you want to show your
code off then go ahead but please give us the hints first i.e. what
methods you would use, what you would rewrite in my code etc. Its so
much better for me to go away and try and replicate your thinking than
just reading it.

heres my version

#program to insert numbers infront of lines.
def numbertext(infilename, outfilename)
#for neat formating work out how many lines there are first so we know
how #many digits to set ljust to
totalnumlines = File.readlines(infilename).length
numdigits = 1
x = 1
while (x < totalnumlines)
x = x * 10
numdigits = numdigits + 1
end

linenumber = 0
File.open(outfilename, "w") do |output|
File.foreach(infilename) do |input|
output.write linenumber.to_s.rjust(numdigits) + " " + input
linenumber = linenumber + 1
end
end
end
 
P

Phrogz

Give us hints on how to improve this program. If you want to show your
code off then go ahead but please give us the hints first i.e. what
methods you would use, what you would rewrite in my code etc. Its so
much better for me to go away and try and replicate your thinking than
just reading it.

Here are some suggestions:
* Use the += and *= compound assignment operators
* Use << instead of .write
* "%10s" % str is like rjust, so
* "%#{numdigits}s %s" % [ linenumber, input ]
* Look at each_with_index
* Think about a better way to calculate numdigits; e.g.:
* Math.log10( max ).ceil
* total_lines.to_s.size
 
T

Thomas Adam

Hi --

Give us hints on how to improve this program. If you want to show your
code off then go ahead but please give us the hints first i.e. what
methods you would use, what you would rewrite in my code etc. Its so
much better for me to go away and try and replicate your thinking than
just reading it.

If all you're doing is inserting line numbers in front of the lines in
that file then:

f=File.open("./file").readlines
lines=0
f.each {|content| puts "#{lines += 10}: #{content}" }'

In its crudest form will do that, but note that it doesn't modify
anything, it just dumps the values out. One other approach is to
modify an array of those values "in-place" instead, as in:

f=File.open("./.xsession").readlines
f.each {|e| e.sub!(/^/, "#{f.index(e) + 10}: ")}
puts f

This "cheats" and uses the index of the element to form the line
number - adding it to the start of the string element.

Saving the resultant array "f" to a file, per the second example is
left an exercise to the reader, as is any further cosmetic formatting
to the output.

-- Thomas Adam
 
R

Rob Biedenharn

Give us hints on how to improve this program. If you want to show your
code off then go ahead but please give us the hints first i.e. what
methods you would use, what you would rewrite in my code etc. Its so
much better for me to go away and try and replicate your thinking than
just reading it.

heres my version

#program to insert numbers infront of lines.
def numbertext(infilename, outfilename)
#for neat formating work out how many lines there are first so we
know
how #many digits to set ljust to
totalnumlines = File.readlines(infilename).length
numdigits = 1
x = 1
while (x < totalnumlines)
x = x * 10
numdigits = numdigits + 1
end

Since the representation of numbers doesn't shrink and you already
have totalnumlines (which would typically be total_num_lines), perhaps
you could think of a direct way of finding the number of character
positions that you'd need to reserve for this largest number.
linenumber = 0
File.open(outfilename, "w") do |output|
File.foreach(infilename) do |input|
Too bad you didn't save the lines when you read the file the first
time... then you could have used .each_with_index. You could even use
$. to get the current line number from the file rather than keep your
own explicit counter in linenumber. (Which would be idiomatically
line_number += 1)
output.write linenumber.to_s.rjust(numdigits) + " " + input
linenumber = linenumber + 1
end
end
end
--


All hints, no code. ;-)

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
7

7stud --

totalnumlines = File.readlines(infilename).length
numdigits = 1
x = 1
while (x < totalnumlines)
x = x * 10
numdigits = numdigits + 1

1) Suppose totalnumlines = 3. The last time through your while loop, x
will equal 2--if x is equal to 3, the loop will terminate and not
execute again. So the assignment to x inside the loop will assign 2*10=
20 to x. So why not just do this:

x = (totalnumlines - 1) * 10

There's no need for any loop to calculate x.

2) Once again suppose totalnumlines = 3. That means the loop will
execute twice: the first time when x =1 and the second time when x=2.
When x =3, the loop will terminate before it executes again. Therefore,
numdigits will equal 3, which is the same value as totalnumlines. So
you can just write:

numdigits = totalnumlines
 
T

Thomas Preymesser

numdigits =3D 1
x =3D 1
while (x < totalnumlines)
x =3D x * 10
numdigits =3D numdigits + 1
end


you don't need such a loop. this can be replaced with

numdigits =3D Math.log10(totalnumlines).to_i+1

-Thomas




--=20
Thomas Preymesser
(e-mail address removed)
(e-mail address removed)
B=FCro: 030 - 830 353 88
mobil: 0176 - 75 03 03 04
Privat: 030 - 49 78 37 06
http://thopre.wordpress.com/
http://www.thopre.com/
 
R

Robert Klemme

2008/2/14 said:
Give us hints on how to improve this program. If you want to show your
code off then go ahead but please give us the hints first i.e. what
methods you would use, what you would rewrite in my code etc. Its so
much better for me to go away and try and replicate your thinking than
just reading it.


Here are some suggestions:
* Use the += and *= compound assignment operators
* Use << instead of .write
* "%10s" % str is like rjust, so
* "%#{numdigits}s %s" % [ linenumber, input ]

Or rather

"%*d %s" % [numdigits, linenumber, input ]
printf "%*d %s", numdigits, linenumber, input
* Look at each_with_index
* Think about a better way to calculate numdigits; e.g.:
* Math.log10( max ).ceil
* total_lines.to_s.size

* Look at ARGF and ARFG.lineno

Kind regards

robert
 
W

William James

Give us hints on how to improve this program. If you want to show your
code off then go ahead but please give us the hints first i.e. what
methods you would use, what you would rewrite in my code etc. Its so
much better for me to go away and try and replicate your thinking than
just reading it.

heres my version

#program to insert numbers infront of lines.
def numbertext(infilename, outfilename)
#for neat formating work out how many lines there are first so we know
how #many digits to set ljust to
totalnumlines = File.readlines(infilename).length
numdigits = 1
x = 1
while (x < totalnumlines)
x = x * 10
numdigits = numdigits + 1
end

linenumber = 0
File.open(outfilename, "w") do |output|
File.foreach(infilename) do |input|
output.write linenumber.to_s.rjust(numdigits) + " " + input
linenumber = linenumber + 1
end
end
end

a = IO.readlines('data')
puts (1..a.size).map{|x| x.to_s.rjust(a.size.to_s.size)}.zip(a).
map{|x| x.join ' '}
 
A

Adam Shelly

you don't need such a loop. this can be replaced with

numdigits = Math.log10(totalnumlines).to_i+1
Hint:
why do math to calculate how many digits it takes to represent a
number as a string, when you can just let the String class tell you?
 
T

Thomas Preymesser

why do math to calculate how many digits it takes to represent a
number as a string, when you can just let the String class tell you?


every child can count the digits in a string, but to prove that you have
understood logarithms you need another solution ... ;-)

-Thomas




--=20
Thomas Preymesser
(e-mail address removed)
(e-mail address removed)
B=FCro: 030 - 830 353 88
mobil: 0176 - 75 03 03 04
Privat: 030 - 49 78 37 06
http://thopre.wordpress.com/
http://www.thopre.com/
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top