[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[SUMMARY] Numbers Can Be Words (#133

James Gray

8/9/2007 1:33:00 PM

We all know this problem isn't tough at all. Myself and several others solved
it with a one-liner. We all enjoy a good one-liner, right?

Actually, I built this solution before the quiz was posted and when I shared it
with Morton, he, very politely, mentioned the G-word. The fact is that I wasn't
really trying to "golf" though. I was using a strategy of problem solving I
call Thinking With The Command-Line. Let me take you through my process to my
solution and beyond to show your what I mean.

First, it's important to remember that Ruby has a lot of command-line switches
that help with these quick tasks. It's not a sin to use these tools. They make
short work of jobs like this because the easy things should be easy. You're not
golfing when you use these, you're just telling Ruby this is a quick job and you
trust her to handle the details on this one.

Let's start with the basics. Obviously, we want to read over the dictionary and
print some words in it. Let's begin with just that much:

$ ruby -pe 1 /usr/share/dict/words

The -p switch asks Ruby to wrap your program in a read and print loop over the
files given as arguments (or STDIN). That was really all I wanted, so I just
needed a program to be wrapped. That's where -e comes in. It let's you give
the code on the command-line and I provided the most trivial code I could think
of. It does nothing of course. It just gives Ruby something to wrap and let's
her do all of the work for me.

OK, so I'm now printing the dictionary, but I really want to print just some
words of the dictionary. I need to introduce some conditional that only prints
when I say it's OK to do so. For that, we move to -p's twin -n and actually
resort to writing a little code:

$ ruby -ne 'print if true' /usr/share/dict/words

The -n switch gives us the same loop around our code, just minus the print()
statement. This lets me choose when I want to print() something.

The read loops that Ruby creates for us always stick the current line in $_. By
default, that exactly what print() spits out.

Great. That's about half of this task. Now I just need the if condition and
I'm done. Before I figure that out though, let's examine one other command-line
switch. I want to set a base for the code to use. It's true that I could just
drop a number in the code and change it as needed, but it would be better if the
number was separate. Ruby has a shortcut switch for that too:

$ ruby -se 'p $base' -- -base=14

The -s switch adds some rudimentary variable processing to switches passes to
the program. Note that I said switches passed to the program, not to Ruby. I
used the -- switch above to end Ruby's switch processing and switch into the
program context. You can then see that the switch just sets a global for us.
That's fine for our purposes.

That means all we need is a Regexp that selects the words we want. I came up
with:

/\A[\d\s#{("a".."z").to_a.join[0...($base.to_i - 10)]}]+\Z/i

That's really just one big character class describing the accepted characters.
The Ruby code inside it creates a String of the alphabet and pulls enough
letters off the front of it to match the current base. Note that we also allow
for digits and the whitespace that will be at the end of each line.

If we put all of that together, we pretty much have my solution:

$ ruby -sne
'print if $_ =~ /\A[\d\s#{("a".."z").to_a.join[0...($base.to_i - 10)]}]+\Z/'
-- -base=12 /usr/share/dict/words

If you're not found of the Regexp, we could remove it. That involves two
changes:

1. Convert our base into an Array of acceptable characters. We only want
such code to run one time, so we will place it in a BEGIN { ... } block.
2. Bring the characters in as an Array so that we can ease the testing of
letters. Ruby's has switches for that too. We can use -a to split()
the line of input and -F to provide the pattern to split() on. We will
also add -l to remove the line ending for us.

Here's the code:

$ ruby -slap
-F'\b|\B'
-e 'BEGIN { $hex = ("a".."z").to_a.first($base.to_i - 10) }'
-e 'next unless $F.all? { |l| $hex.include? l.downcase }'
-- -base=14 /usr/share/dict/words

Note that I snuck in another change in addition to those described. I switched
back to -p and just skipped word that aren't numbers.

The trick in this version is that -a causes each line of input to be split()
into the variable $F. I also added the -F switch with a pattern that will match
between each character to control how the split() works.

Of course, we could argue the point of if this is still a one-liner since I'm
now passing Ruby two lines of code, but I try not to loose a lot of sleep over
such things.

A final weakness of this solution is that it doesn't finish processing as soon
as it could. We can easily add that if you can tolerate one more line:

$ ruby -slap
-F'\b|\B'
-e 'BEGIN { $hex = ("a".."z").to_a.first($base.to_i - 10) }'
-e 'break unless $hex.include? $F.first.downcase'
-e 'next unless $F.all? { |l| $hex.include? l.downcase }'
-- -base=14 /usr/share/dict/words

Now might be the right time to consider putting all of this in a file,
especially if we wanted to do more with it. I'm done though, so I'll leave that
as an exercise for the interested reader.

My thanks to all who showed how easy this can really be.

Tomorrow we're back to simulations and drawing pretty pictures...

3 Answers

James Gray

8/9/2007 1:38:00 PM

0

On Aug 9, 2007, at 8:32 AM, Ruby Quiz wrote:

> We all know this problem isn't tough at all.

My apologies for the repeated post.

James Edward Gray II


The Peeler

2/14/2011 7:33:00 PM

0

On Mon, 14 Feb 2011 18:50:03 +0100, Dumb Heini, the Dutch resident Nazi
troll of sci and scj, wrote:

>>> One wonders whether in the future, investigators will conclude that the
>>> Dutch government has misbehaved against the Jews once again by failing to
>>> pay for their protection and that of their institutions, which is also,
>>> clearly, a public duty.
>>>
>>
>> Where is the link for this article? Do you support the antisemitism of
>> the islamonazi baboons?
>>
>
> it is not only the jews who are suffering from the moroccan scum but also
> the native dutch women and you females.
> why do you think my famiily left amsterdam 20 myears ago?

Cowardly dodging his question again, you disgusting craven Dutch Nazi pig?
<G>


--
Dumb Dutch Nazi Heini commenting on his misery: "si9nce we are talking to
each other my mood has improved"
Message-ID: <DPgVo.27315$My1.20853@newsfe16.iad>

Tilly

2/14/2011 9:03:00 PM

0

"Dug" <andxornot@gggmail.com> wrote in message
news:ijbpov$var$1@news.eternal-september.org...
> "heinrich" <heinrich@ruhrgasnet.de> wrote in message
> news:jg46p.21371$4L4.7897@newsfe11.iad...
>> The Dutch government continues to refuse to pay for the security of
>> its threatened Jewish citizens. The Jewish community has stated on
>> various occasions that it is the only group in society which cannot
>> organize activities without including guards or other security
>> measures.
>>
>>
>>
>>
>>
>>
>> The other week during a debate in a parliamentary commission on the
>> current anti-Semitism in the Netherlands, several MPs asked the Dutch
>> government to pay for the Jews' security. However, Minister of
>> Security and Justice Ivo Opstelten stuck to the position of his
>> predecessors saying that security is the responsibility of the Jewish
>> community itself and if necessary, of the local authorities. The
>> latter are rarely willing to pay for these expenses.
>>
>>
>>
>> The Dutch Parliament has existed for almost 200 years. The first
>> plenary meeting ever on contemporary anti-Semitism took place last
>> June after a series of articles in the media about anti-Semitic
>> incidents. One article in the prominent daily NRC Handelsblad was
>> titled: "Anti-Semitism is more than an incident. It is normal." The
>> fact that a second parliamentary meeting was called for a few months
>> later is a sign that nothing has been solved.
>>
>>
>>
>> The main targets of anti-Semitic harassment in public are a small
>> number of Jews who are recognizably attired as such. In past weeks,
>> several newspapers have reported that Rabbi Raph Evers, the head of
>> the Dutch Jewish Seminary, no longer travels on public transportation
>> because of the harassment he encounters there. He walks out on the
>> street as little as possible. Chief Rabbi Binyomin Jacobs is also
>> frequently insulted in public. His home is fitted with an alarm
>> system which links him directly to the police. Jacobs says 35 year
>> ago, no one insulted him.
>>
>>
>>
>> What were not mentioned in the Dutch media are the experiences of
>> those Jews who had been repeatedly harassed and now live elsewhere.
>> One Dutch Jewish youngster living in Jerusalem told me that while he
>> was a student at Amsterdam University, he earned a living working in
>> a supermarket in the center of town. He wore a small yarmulke. He was
>> insulted at work on the average twice a week with calls like:
>> "cancer-Jew" of "Hamas Hamas, Jews to the gas." All of this
>> harassment came from customers of Moroccan ethnicity.
>>
>>
>>
>> I also interviewed another youngster now living in Jerusalem, who
>> wears ultra-Orthodox garb. A few months ago he visited his parents in
>> the Netherlands. When he changed trains after arriving from Belgium
>> at the Dutch border station Rozendaal, he was immediately shouted at
>> in English by a man with a heavy Dutch accent: "You killed Jesus." At
>> his arrival in Arnhem, where he exited the station through the back
>> door, he was insulted as well. The same happened when he left from
>> the same station. During his stay in The Netherlands he hardly left
>> his parents' home.
>>
>>
>>
>> Non-selective immigration policy
>> Only limited parts of the Dutch Jewish community encounter
>> substantial anti-Semitism. Besides the aforementioned harassment of
>> recognizably dressed Jews, it impacts mainly on Jewish children in
>> schools and through insults in the workplace. The Jewish community
>> now explicitly blames the disproportionately large role in
>> anti-Semitic incidents of Muslims who account for six percent of the
>> population. The most problematic elements come from the Moroccan, and
>> to a lesser extent, Turkish community.
>>
>>
>>
>>
>> Over the past decades, Dutch governments have non-selectively allowed
>> in 1.6 million non-Western immigrants, about 10% of the population.
>> Of these, one million originate in Muslim countries where
>> anti-Semitism is far stronger than in the Netherlands. It is thus not
>> surprising that the percentage of anti-Semites among these immigrants
>> is substantially higher than among the local Dutch population. One
>> could put it differently: the Dutch government has for many years
>> followed an anti-Semitism-promoting immigration policy.
>>
>>
>>
>> It would be desirable to undertake a detailed poll on anti-Semitism
>> among Muslim immigrants as compared to the autochthonous population.
>> One could then also analyze to what extent this anti-Semitism is
>> fueled by families, mosques, schools, friends, or foreign incitement.
>> This would be very useful in the battle against one of the main
>> sources of anti-Semitism. Such an investigation would be, however,
>> far too big a betrayal of Dutch taboos.
>>
>>
>>
>> Last week's parliamentary debate had its origin in the media
>> discussion engendered by my book "The Decay: Jews in a Rudderless
>> Netherlands." It quoted senior Dutch politician and former EU
>> commissioner Frits Bolkestein stating that recognizable Jews should
>> advise their children to leave for the United States or Israel.
>>
>>
>>
>> However, the book did not focus on anti-Semitism. It had two major
>> themes. The first one, that Jews have a symbolic importance in the
>> Netherlands which goes far beyond the real importance of the
>> community, and the second was that by watching the interaction of the
>> Jewish community and Dutch society at large, one obtains a prism on
>> many aspects of the functioning and problems of Dutch society.
>>
>>
>>
>> The recent discussions and debates have borne these two points out
>> once more. Minister Opstelten stated that there would be zero
>> tolerance for anti-Semitic incidents and registration of incidents at
>> school. However, the Dutch police are unlikely to be able to become
>> more efficient in the coming months. It is also unlikely that
>> recognizable Jews can soon walk unhindered in certain parts of the
>> country in the near future.
>>
>>
>>
>> Approximately 10 years ago, there were major inquiries and debates in
>> Dutch society about government failures of post-war restitution of
>> Jewish assets looted in the Holocaust. One of the many criticisms
>> about the Dutch government's handling of the situation was that it
>> had wrongly charged the surviving Jews for the administrative
>> services involved in returning what was looted from them because the
>> Dutch government could not protect them during the occupation.
>>
>>
>>
>>
>>
>> A commission of inquiry said that this was a public duty and thus
>> needed to be provided free of charge.
>>
>>
>>
>> One wonders whether in the future, investigators will conclude that
>> the Dutch government has misbehaved against the Jews once again by
>> failing to pay for their protection and that of their institutions,
>> which is also, clearly, a public duty.
>>
>
> Where is the link for this article? Do you support the antisemitism of
> the islamonazi baboons?
>

It's a Ynet article ,word for word.