[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[QUIZ] Equation Graphing (#176

Matthew Moss

9/5/2008 2:02:00 PM

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The three rules of Ruby Quiz 2:

1. Please do not post any solutions or spoiler discussion for this
quiz until 48 hours have passed from the time on this message.

2. Support Ruby Quiz 2 by submitting ideas as often as you can! (A
permanent, new website is in the works for Ruby Quiz 2. Until then,
please visit the temporary website at

<http://splatbang.com/rub....

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem
helps everyone on Ruby Talk follow the discussion. Please reply to
the original quiz message, if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


This week's quiz idea provided and written by Martin DeMello.

## Equation Graphing (#176)

Back when the world was shiny and new, and BBC Basic my language of
choice and necessity, one of the fun little programs I wrote was a
simple equation grapher. Two things that made it simple were (i) the
ever-useful EVAL function, and (ii) the almost trivial ability to pick
a colour and a pair of coordinates and put a friendly glowing pixel
onto the screen. Instant gratification writ large. Well, anything that
Basic can do, Ruby should be able to, so your task this week is to
whip out your favourite graphics toolkit or library and write a
program that

1. Asks for a function of one variable, x
2. Asks for the region of the graph to display (xmin, xmax, ymin, ymax)
3. Plots the graph

You can trust the user to input a syntactically correct function, but
don't forget that it might behave badly for certain values of x (e.g.
1/x where x=0). Style points for making the main loop look trivial :)

Here's a neat little online grapher you can use to visually check your
output: <http://www.walterzorn.com/grapher/grapher....



--
Matthew Moss <matthew.moss@gmail.com>

7 Answers

Jesse Brown

9/7/2008 2:23:00 PM

0


> ## Equation Graphing (#176)
...
> You can trust the user to input a syntactically correct function, but
> don't forget that it might behave badly for certain values of x (e.g.
> 1/x where x=0). Style points for making the main loop look trivial :)

I went for style points. :)
1/x where x = 0 is handled nicely too.

graph.rb:
#!/usr/bin/ruby

def usage
puts "USAGE: #{File.basename($0)} <equation> <xmin> <xmax> <ymin>
<ymax>"
exit 42
end

eq = ARGV.shift || usage
usage unless ARGV.length == 4

cmd = "set xr [%f:%f]; set yr [%f:%f]; set tit '%s'; plot %s w li
notitle"

IO.popen("gnuplot -persist", "w") do |plot|
plot.puts cmd % [ARGV.collect { |a| a.to_f }, eq, eq].flatten
end

--
Posted via http://www.ruby-....

brabuhr

9/8/2008 3:05:00 PM

0

On Fri, Sep 5, 2008 at 10:01 AM, Matthew Moss <matthew.moss@gmail.com> wrote:
> whip out your favourite graphics toolkit or library and write a
> program that
>
> 1. Asks for a function of one variable, x
> 2. Asks for the region of the graph to display (xmin, xmax, ymin, ymax)
> 3. Plots the graph

I didn't follow the requirements, but inspired by the quiz did a
little bit using my favorite graphics (none;-):

def grapher xmin, ymin, xmax, ymax, &function
ymax.downto(ymin) do |y|
xmin.upto(xmax) do |x|
begin
print (function.call(x, y) ? '#' : '+')
rescue
print "X"
end
end
print "\n"
end
print "\n"
end

grapher(0, 0, 40, 10){|x,y| x == y}
grapher(0, 0, 40, 10){|x,y| 2 >= y**2 / x}
grapher(0, -5, 60, 10){|x,y| y == (5 + Math.sin(x) * 5).to_i}
grapher(-10, -5, 70, 10){|x,y| y**2 == x}

Output:

++++++++++#++++++++++++++++++++++++++++++
+++++++++#+++++++++++++++++++++++++++++++
++++++++#++++++++++++++++++++++++++++++++
+++++++#+++++++++++++++++++++++++++++++++
++++++#++++++++++++++++++++++++++++++++++
+++++#+++++++++++++++++++++++++++++++++++
++++#++++++++++++++++++++++++++++++++++++
+++#+++++++++++++++++++++++++++++++++++++
++#++++++++++++++++++++++++++++++++++++++
+#+++++++++++++++++++++++++++++++++++++++
#++++++++++++++++++++++++++++++++++++++++

X+++++++++++++++++++++++++++++++++#######
X+++++++++++++++++++++++++++#############
X+++++++++++++++++++++###################
X++++++++++++++++########################
X++++++++++++############################
X++++++++################################
X+++++###################################
X+++#####################################
X+#######################################
X########################################
X########################################

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+##+++++#+++++#+++++##+++++#+++++#+++++#+++++##+++++#+++++#++
+++++++#+++++++#++++++++++#+++++++++++++#++++++++++#+++++++#+
+++++++++#+++#++++++++++++++++++#+#++++++++++++++++++++++#+++
++++++++++++++++++++++++++++#+++++++++#++++++++++++++#+++++++
#++#+++++++++++++++#++++++++++++++++++++++++#++#+++++++++++++
++++++++++++++++++++++#++#+++++++++++++++#+++++++++++++++++++
++++++#+++++++++#+++++++++++++++++++++++++++++++++#+++++++++#
++++++++++#+#++++++++++++++++++#+++#++++++++++++++++++#+#++++
++++#+++++++++++++#++++++++++#+++++++#++++++++++#++++++++++++
+++++#+++++#+++++#+++++##+++++#+++++#+++++##+++++#+++++#+++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#+++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++#++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++#+++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++#++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++#++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++#+++++++++++++++++++++++++++++++++++++++++++++

http://github.com/fjc/rubyquiz/tree/mas...

Martin DeMello

9/10/2008 5:45:00 AM

0

Here's my solution, using _why's wonderful little shoes toolkit. Run like

shoes graph.rb "x*x; -5; 5; 0; 25"

The args are "fn; xmin; xmax; ymin; ymax"

I tried to make the code as straightforward as possible, to recapture
the feel of the old Basic days. A few inevitable complications, due to
shoes being a work in progress: the Grapher class is to work around
scoping quirks, and the range check is due to a bug that doesn't let
the exception handler catch an overflowing Bignum -> long conversion
in the canvas code (it would've been nice to simply draw to a point
off-canvas and let the drawing engine cope). Also shoes's argv
handling code puts the filename in ARGV[0] and doesn't like negative
numbers as plain command line args (they are treated as shoes args),
so a single quoted string was the simplest thing that worked.

#------------------------------------------------------------------------------------------------------
# ARGV[0] is the filename, since we launch via shoes graph.rb args
ARGV.shift
args = ARGV[0].split(";").map {|i| i.chomp}
F = args.shift
Xmin, Xmax, Ymin, Ymax = args.map {|i| i.to_f}

X = Y = 800

XScale = X * 1.0/(Xmax - Xmin)
YScale = Y * 1.0/(Ymax - Ymin)

class Grapher
def at(x,y)
[((x -Xmin)* XScale).to_i, Y - ((y - Ymin) * YScale).to_i] rescue nil
end

def bounded?(x,y)
x && y && 0 <= x && x <= X && 0 <= y && y <= Y
end

def fn(x)
y = eval(F)
end
end

Shoes.app :height => Y, :width => X do
g = Grapher.new
background rgb(255, 255, 255)

fill white
stroke black
strokewidth 1
u, v = nil
Xmin.step(Xmax, (Xmax - Xmin)/(X*1.0)) {|i|
begin
x0, y0 = g.at(u,v)
u, v = i, g.fn(i)
x, y = g.at(u,v)
if g.bounded?(x,y) and g.bounded?(x0,y0)
line(x0, y0, x, y)
end
rescue
end
}
end

Daryl

10/20/2011 1:26:00 AM

0

On 10/19/2011 7:03 PM, Ben wrote:
> On Oct 19, 2:03 pm, "meport"<jjme...@earthlink.net> wrote:
>> I'm sick and tired of calling a company and getting someone in India or Sri
>> Lanka or Pakistan who understands less than 10% of what I'm talking about
>> because they don't speak colloquial American English. I know that American
>> Companies transferred all their jobs overseas because it's cheaper but it's
>> impossible to deal with someone who doesn't speak the same language I do but
>> I HAVE to speak with in order to do business with the company. I refuse to
>> buy Dell anymore because I have to deal with call centers in India. I
>> refuse to do business with City Corp for the same reason. More and more I'm
>> taking the attitude that if an American Company refuses to hire Americans,
>> and we do have 10% official unemployment (but I really think the
>> unemployment figures should be something in the mid to upper teens,
>> percentage wise) that could be significantly eased by bring back the 2 or 3
>> million jobs that American Companies shipped overseas to call centers, then
>> they can do without my business.
>>
>> When I call a company and get someone "over there" I refuse to talk to them
>> and demand to speak to a native American. I don't care one iota if I
>> inconvenience some employee in the Corporate Headquarters who I get
>> transferred to because they're the only ones that the call center can
>> transfer someone to (I just had that experience. I was transferred to some
>> guy in the Corporate Headquarters who was quite indignant because I refused
>> to speak to someone at the call center in India. He was insistent that I
>> talk to the call center. He is going to be more indignant when the Senior
>> Vice President of Corporate Affairs calls him because I call the Senior Vice
>> President of Corporate Affairs and complained about the guy who insisted
>> that I speak with the call center in India. The Internet and Google are
>> wonderful inventions. With enough effort you can find anything.) I don't
>> care one iota if I'm rude or belligerent to the poor schlepp in India or Sri
>> Lanka or where ever and I hurt their "feelings" because I won't speak to
>> them. It's time to bring the jobs home.
>>
>> The bottom line is that we don't have to put up with it anymore. Bring the
>> jobs back here. Refuse to speak to someone in India or Sri Lanka or
>> Pakistan. Demand to speak to a native American and don't even be nice about
>> it. If enough people start demanding that they speak to a fellow American
>> the message just might get through to them.
>>
>> ---
>> meport
>
> The Wallstreet Occupation campaign should demand the resignations of
> ALL U.S. senators and Congressmen/women and POTUS and VP because they
> are responsible for this mess. We need a new election to replace all
> members of Congress, senate, president and VP.

Are you willing to reinstate the 1935 Sherman Act and give the
FTC the power to enforce it again? You can't give greedy
Managers and CEOs the tax breaks and ask them to create jobs. In
2007-8, that was done and what did they do? Pocketed the tax
breaks and shifted even more jobs overseas. It was done in 2003
and guess what, same thing.

Taxes are the answer but do away with the tax breaks as they are
now. Then reward the ones that really are good for the nation
and tax the hell out of those that are not. Yes, some might move
their entire operation overseas. Most won't since the US is the
only real safe haven for their families.

--
http://tvmoviesf...
for free movies and Nostalgic TV. Tons of Military shows and
programs.

BL5511

10/20/2011 1:33:00 AM

0

On Oct 19, 8:17 pm, "Nigel Brooks" <nbro...@msn.com> wrote:
> "Ben" <pb5...@gmail.com> wrote in message
>
> news:31b4fcc0-c62a-455a-9e5e-26b6d01efedc@b10g2000vbc.googlegroups.com...
>
>
>
> > On Oct 19, 2:03 pm, "meport" <jjme...@earthlink.net> wrote:
> >> I'm sick and tired of calling a company and getting someone in India or
> >> Sri
> >> Lanka or Pakistan who understands less than 10% of what I'm talking about
> >> because they don't speak colloquial American English. I know that
> >> American
> >> Companies transferred all their jobs overseas because it's cheaper but
> >> it's
> >> impossible to deal with someone who doesn't speak the same language I do
> >> but
> >> I HAVE to speak with in order to do business with the company. I refuse
> >> to
> >> buy Dell anymore because I have to deal with call centers in India. I
> >> refuse to do business with City Corp for the same reason. More and more
> >> I'm
> >> taking the attitude that if an American Company refuses to hire
> >> Americans,
> >> and we do have 10% official unemployment (but I really think the
> >> unemployment figures should be something in the mid to upper teens,
> >> percentage wise) that could be significantly eased by bring back the 2 or
> >> 3
> >> million jobs that American Companies shipped overseas to call centers,
> >> then
> >> they can do without my business.
>
> >> When I call a company and get someone "over there" I refuse to talk to
> >> them
> >> and demand to speak to a native American. I don't care one iota if I
> >> inconvenience some employee in the Corporate Headquarters who I get
> >> transferred to because they're the only ones that the call center can
> >> transfer someone to (I just had that experience. I was transferred to
> >> some
> >> guy in the Corporate Headquarters who was quite indignant because I
> >> refused
> >> to speak to someone at the call center in India. He was insistent that I
> >> talk to the call center. He is going to be more indignant when the Senior
> >> Vice President of Corporate Affairs calls him because I call the Senior
> >> Vice
> >> President of Corporate Affairs and complained about the guy who insisted
> >> that I speak with the call center in India. The Internet and Google are
> >> wonderful inventions. With enough effort you can find anything.) I don't
> >> care one iota if I'm rude or belligerent to the poor schlepp in India or
> >> Sri
> >> Lanka or where ever and I hurt their "feelings" because I won't speak to
> >> them. It's time to bring the jobs home.
>
> >> The bottom line is that we don't have to put up with it anymore. Bring
> >> the
> >> jobs back here. Refuse to speak to someone in India or Sri Lanka or
> >> Pakistan. Demand to speak to a native American and don't even be nice
> >> about
> >> it. If enough people start demanding that they speak to a fellow American
> >> the message just might get through to them.
>
> >> ---
> >> meport
>
> > The Wallstreet Occupation campaign should demand the resignations of
> > ALL U.S. senators and Congressmen/women and POTUS and VP because they
> > are responsible for this mess. We need a new election to replace all
> > members of Congress, senate, president and VP.
>
> Well Ben old buddy that wouldn't happen.
>
> If everyone resigns there probably wouldn't be too many elections.
> Governors get to fill mid term Senate vacancies in most states, and the
> Presidential succession act would ensure that a non elected cabinet member -
> in this case, Hillary Clinton,  took over as Pres.  Hillary could then
> nominate Bill to be VP - and we'd all be up shit creek.

If POTUS, VP and all congress and senate members resigned, the
successor for the POTUS would not be Hilary Clinton but Secretary of
Treasury. In the 1980s Sec of State Alexander Haig made a mistake when
he claimed that he was in charged when President Reagan was undegone a
surgery caused by the bullets shot by John Hinkley.
Anyway, elected officials are worse than non-elected officials who
replaced them during mid-term. Currently approval rates of Congress
and Senate are only above 10 percent. People are angry of our elected
officials who betrayed them.

Nigel Brooks

10/20/2011 1:59:00 PM

0



"Ben" <pb5511@gmail.com> wrote in message
news:4e968874-a858-48a8-b875-43b392ab4396@u13g2000vbx.googlegroups.com...
> On Oct 19, 8:17 pm, "Nigel Brooks" <nbro...@msn.com> wrote:
>> "Ben" <pb5...@gmail.com> wrote in message
>>
>> news:31b4fcc0-c62a-455a-9e5e-26b6d01efedc@b10g2000vbc.googlegroups.com...
>>
>>
>>
>> > On Oct 19, 2:03 pm, "meport" <jjme...@earthlink.net> wrote:
>> >> I'm sick and tired of calling a company and getting someone in India
>> >> or
>> >> Sri
>> >> Lanka or Pakistan who understands less than 10% of what I'm talking
>> >> about
>> >> because they don't speak colloquial American English. I know that
>> >> American
>> >> Companies transferred all their jobs overseas because it's cheaper but
>> >> it's
>> >> impossible to deal with someone who doesn't speak the same language I
>> >> do
>> >> but
>> >> I HAVE to speak with in order to do business with the company. I
>> >> refuse
>> >> to
>> >> buy Dell anymore because I have to deal with call centers in India. I
>> >> refuse to do business with City Corp for the same reason. More and
>> >> more
>> >> I'm
>> >> taking the attitude that if an American Company refuses to hire
>> >> Americans,
>> >> and we do have 10% official unemployment (but I really think the
>> >> unemployment figures should be something in the mid to upper teens,
>> >> percentage wise) that could be significantly eased by bring back the 2
>> >> or
>> >> 3
>> >> million jobs that American Companies shipped overseas to call centers,
>> >> then
>> >> they can do without my business.
>>
>> >> When I call a company and get someone "over there" I refuse to talk to
>> >> them
>> >> and demand to speak to a native American. I don't care one iota if I
>> >> inconvenience some employee in the Corporate Headquarters who I get
>> >> transferred to because they're the only ones that the call center can
>> >> transfer someone to (I just had that experience. I was transferred to
>> >> some
>> >> guy in the Corporate Headquarters who was quite indignant because I
>> >> refused
>> >> to speak to someone at the call center in India. He was insistent that
>> >> I
>> >> talk to the call center. He is going to be more indignant when the
>> >> Senior
>> >> Vice President of Corporate Affairs calls him because I call the
>> >> Senior
>> >> Vice
>> >> President of Corporate Affairs and complained about the guy who
>> >> insisted
>> >> that I speak with the call center in India. The Internet and Google
>> >> are
>> >> wonderful inventions. With enough effort you can find anything.) I
>> >> don't
>> >> care one iota if I'm rude or belligerent to the poor schlepp in India
>> >> or
>> >> Sri
>> >> Lanka or where ever and I hurt their "feelings" because I won't speak
>> >> to
>> >> them. It's time to bring the jobs home.
>>
>> >> The bottom line is that we don't have to put up with it anymore. Bring
>> >> the
>> >> jobs back here. Refuse to speak to someone in India or Sri Lanka or
>> >> Pakistan. Demand to speak to a native American and don't even be nice
>> >> about
>> >> it. If enough people start demanding that they speak to a fellow
>> >> American
>> >> the message just might get through to them.
>>
>> >> ---
>> >> meport
>>
>> > The Wallstreet Occupation campaign should demand the resignations of
>> > ALL U.S. senators and Congressmen/women and POTUS and VP because they
>> > are responsible for this mess. We need a new election to replace all
>> > members of Congress, senate, president and VP.
>>
>> Well Ben old buddy that wouldn't happen.
>>
>> If everyone resigns there probably wouldn't be too many elections.
>> Governors get to fill mid term Senate vacancies in most states, and the
>> Presidential succession act would ensure that a non elected cabinet
>> member -
>> in this case, Hillary Clinton, took over as Pres. Hillary could then
>> nominate Bill to be VP - and we'd all be up shit creek.
>
> If POTUS, VP and all congress and senate members resigned, the
> successor for the POTUS would not be Hilary Clinton but Secretary of
> Treasury. In the 1980s Sec of State Alexander Haig made a mistake when
> he claimed that he was in charged when President Reagan was undegone a
> surgery caused by the bullets shot by John Hinkley.
> Anyway, elected officials are worse than non-elected officials who
> replaced them during mid-term. Currently approval rates of Congress
> and Senate are only above 10 percent. People are angry of our elected
> officials who betrayed them.

Title 3, United States Code, Section 19

(d)(1) If, by reason of death, resignation, removal from office, inability,
or failure to qualify, there is no President pro tempore to act as President
under subsection (b) of this section, then the officer of the United States
who is highest on the following list, and who is not under disability to
discharge the powers and duties of the office of President shall act as
President:

Secretary of State, Secretary of the Treasury, Secretary of Defense,
Attorney General, Secretary of the Interior, Secretary of Agriculture,
Secretary of Commerce, Secretary of Labor, Secretary of Health and Human
Services, Secretary of Housing and Urban Development, Secretary of
Transportation, Secretary of Energy, Secretary of Education, Secretary of
Veterans Affairs, Secretary of Homeland Security.

Frank Galikanokus

10/20/2011 7:39:00 PM

0

Ben wrote:
>
> On Oct 19, 2:03 pm, "meport" <jjme...@earthlink.net> wrote:
> > I'm sick and tired of calling a company and getting someone in India or Sri
> > Lanka or Pakistan who understands less than 10% of what I'm talking about
> > because they don't speak colloquial American English. I know that American
> > Companies transferred all their jobs overseas because it's cheaper but it's
> > impossible to deal with someone who doesn't speak the same language I do but
> > I HAVE to speak with in order to do business with the company. I refuse to
> > buy Dell anymore because I have to deal with call centers in India. I
> > refuse to do business with City Corp for the same reason. More and more I'm
> > taking the attitude that if an American Company refuses to hire Americans,
> > and we do have 10% official unemployment (but I really think the
> > unemployment figures should be something in the mid to upper teens,
> > percentage wise) that could be significantly eased by bring back the 2 or 3
> > million jobs that American Companies shipped overseas to call centers, then
> > they can do without my business.
> >
> > When I call a company and get someone "over there" I refuse to talk to them
> > and demand to speak to a native American. I don't care one iota if I
> > inconvenience some employee in the Corporate Headquarters who I get
> > transferred to because they're the only ones that the call center can
> > transfer someone to (I just had that experience. I was transferred to some
> > guy in the Corporate Headquarters who was quite indignant because I refused
> > to speak to someone at the call center in India. He was insistent that I
> > talk to the call center. He is going to be more indignant when the Senior
> > Vice President of Corporate Affairs calls him because I call the Senior Vice
> > President of Corporate Affairs and complained about the guy who insisted
> > that I speak with the call center in India. The Internet and Google are
> > wonderful inventions. With enough effort you can find anything.) I don't
> > care one iota if I'm rude or belligerent to the poor schlepp in India or Sri
> > Lanka or where ever and I hurt their "feelings" because I won't speak to
> > them. It's time to bring the jobs home.
> >
> > The bottom line is that we don't have to put up with it anymore. Bring the
> > jobs back here. Refuse to speak to someone in India or Sri Lanka or
> > Pakistan. Demand to speak to a native American and don't even be nice about
> > it. If enough people start demanding that they speak to a fellow American
> > the message just might get through to them.
> >
> > ---
> > meport
>
> The Wallstreet Occupation campaign should demand the resignations of
> ALL U.S. senators and Congressmen/women and POTUS and VP because they
> are responsible for this mess. We need a new election to replace all
> members of Congress, senate, president and VP.

This mess has been in the works for more than 30 years.

And yes, democrats, republicans, randroids and the economic engineers on Wall street are
all to blame.

JAM