Re: Euler problem #22



Andrew Haley <andrew29@xxxxxxxxxxxxxxxxxxxxxxx> wrote:
William James <w_a_x_man@xxxxxxxxx> wrote:

Since this problem takes so little time for the computer to solve,
the amount of time needed to write the program is infinitely more
important. Making the program as fast as possible is simply a
waste of human effort.

A shorter program:

$names = IO.read("names.txt").scan(/[A-Z]+/).sort

sum = 0
$names.each_with_index do |name,i|
sum += i.succ * name.split('').
inject(0){|n,c| n + c[0] - ?A + 1}
end
p sum

Mmm, gforth doesn't like this. I can't think of any Forth system that
would. You wouldn't be posting off-topic, would you?

For each repetitive sort of problem, we eventually build Forth tools
that handle the repetitive part and let us concentrate on the parts that
change. Ruby comes with a set of tools like that. So Ruby could give us
hints about the sort of tools that are useful for the sort of repetitive
problems that Ruby is good at. We might want to have the same tools, or
simplified factored fragments of them, or some different approach. Or we
couild decide that Forth is not good for that sort of thing and use some
language that already provides lots of comfy tools.

A while ago I was looking at how to build a particular string tool, and
he wrote some Ruby code that easily did what I wanted that tool to do,
and then he trivially implemented the tool too -- which looked useless
given the tools Ruby already had. So if I want to do things that
primarily involve manipulating strings then one approach would be to
learn Ruby and use that instead. Maybe it would be easier to adapt Ruby
for the things I use Forth for than the other way around, or find a way
to get them to cooperate closely and then I could use both languages at
the same time and debug my code in both together.

Oh well. We don't mind useing RPN, or a data stack, or weak typing. A
few limitations that don't actually limit us are no big problem. I had
the idea that we could handle strings with no garbage collection if we
used rubber-band memory and accepted a few limitations. The idea is, you
accept as many buffers as you want but at any given time you have one
single extendable buffer that can have an indefinite length. It
currently ends at HERE and the currently-executing routine can extend it
as desired. You can make as many scratch buffers as you want at or
beyond HERE and you can ALLOT the memory for them. You can call other
routines that treat one of your buffers as the one that has indefinite
length. At the end of your routine you can append data to the extendable
string, and -ALLOT to release your scratch buffers, and you're done.
It's a sort of string stack. There aren't any big complications unless
you need to extend two strings with the same routine.

I wrote up an example that used a simple version of this. It worked just
fine. The clf commenters said they didn't want to have to deal with that
limitation. They wanted some more complex string package that would be
easier to use. But you don't find out how easy the simple approach is
until you use it.
.



Relevant Pages

  • Re: Sorting a string...
    ... not sort strings as I expected it would. ... anyone who's sorting a string really just wants to sort the component ... A basic English alphabetic ordering might be an overly simple use-case. ... it is a great example of a Ruby one-liner. ...
    (comp.lang.ruby)
  • Re: VB6 LISTBOX problem
    ... Have a look at the code below, can you do a sort that is faster ... There are of course all sorts of ways of sorting stuff, especially stuf that is "linked together" as in your UDTs, but to prove the following code sorts it in exactly the same way that your own original code does so, by concatenating all three items of each element into a composite string exactly as you are already doing, but instead of dumping those composite strings into a ListBox it dumps them into a VB string array. ... Private Type SAFEARRAY1D ... Dim StPtr As Long, VAs String, pVAs Long ...
    (microsoft.public.vb.general.discussion)
  • Re: using lambda/Proc can prevent a lot of garbage collection
    ... >>> Ruby from excluding unbound variables in closures. ... >> context. ... > needed a string based eval. ... blocks can be used to create local variables (as ...
    (comp.lang.ruby)
  • C Sharp sorting considered superior to C by an order of magnitude
    ... following sort algorithms and the following points. ... its own comparision for the types bool, byte, short, int, long, ... single, double and string. ... private CheckBox CHKdeterminism; ...
    (comp.programming)
  • Re: Cant Figure Out How To Sort On Bind
    ... The sort order is not being passed as an argument, ... with the string "searchCache" to create a key for it, ... 'Assign ColumnOrder to ViewState ... Sub GetDataReader(ByVal ColumnOrder As String) ...
    (microsoft.public.dotnet.framework.aspnet)