Simple lambda question

hello, I am newbie for ruby.
these days i am forcusing learning about ruby languages.
and I wonder and hope ruby will be simply answer below quiz just one
line.
below is python code.

Python 2.3.4 (#1, Nov 10 2004, 13:08:40) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.

a=0
while a*a < 10000:

... if a*a > 1000:
... s = str(a*a)
... if s[0]==s[1] and s[2]==s[3]:
... print a, a*a
... a = a+1
...
88 7744

quiz is about number.
and ruby have lambda func. so I hope easly solution make.
anybody wellknown ruby user, help and solve above quiz.
Thank you~!

···

--
Posted via http://www.ruby-forum.com/\.

Hyun Oh K. wrote in post #1105634:

below is python code.

[snip]

and ruby have lambda func. so I hope easly solution make.
anybody wellknown ruby user, help and solve above quiz.

You don't need lambdas to do this. If you want to start simply, why not
just translate the python directly to ruby?

$ irb
irb:001:0> a=0
=> 0
irb:002:0> while a*a < 10000
irb:003:1> if a*a > 1000
irb:004:2> s = (a*a).to_s
irb:005:2> if s[0]==s[1] and s[2]==s[3]
irb:006:3> puts "#{a}, #{a*a}"
irb:007:3> end
irb:008:2> end
irb:009:1> a = a+1
irb:010:1> end
88, 7744
=> nil

The next idiomatic step would be to use ruby's more functional block
methods. As far as I understand it these aren't actually lambdas as
such, but I guess that's what you were talking about. You can also
inline the inner-most `if' pretty easily as well.

$ irb
irb:001:0> 100.times do |a|
irb:002:1* if a*a > 1000
irb:003:2> s = (a*a).to_s
irb:004:2> puts "#{a}, #{s}" if s[0]==s[1] and s[2]==s[3]
irb:005:2> end
irb:006:1> end
88, 7744
=> 100

There's not much more I'd do with this particular snippet.

···

--
Posted via http://www.ruby-forum.com/\.

Like this?

100.times.select{|a| a*a>1000}.map{|a| [a, (a*a).to_s]}.select{|a|
a[1][0]==a[1][1] and a[1][2]==a[1][3]}

···

2013/4/15 Hyun Oh K. <lists@ruby-forum.com>

hello, I am newbie for ruby.
these days i am forcusing learning about ruby languages.
and I wonder and hope ruby will be simply answer below quiz just one
line.
below is python code.

Python 2.3.4 (#1, Nov 10 2004, 13:08:40) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> a=0
>>> while a*a < 10000:
... if a*a > 1000:
... s = str(a*a)
... if s[0]==s[1] and s[2]==s[3]:
... print a, a*a
... a = a+1
...
88 7744
>>>

quiz is about number.
and ruby have lambda func. so I hope easly solution make.
anybody wellknown ruby user, help and solve above quiz.
Thank you~!

--
Posted via http://www.ruby-forum.com/\.

--
Daekeun Lee

Department of Computer Science
Korea Advanced Institute of Science and Technology (KAIST)
+82 10-4012-4809
zmsquf99@gmail.com

@zmsquf99

감사합니다~ 적절한 예를 좀 찾고 잇었는데 많은 도움이 되었습니다.
우리나라분인거 같아서 한글로 남김니다~ ^^

Lee Daekeun wrote in post #1105644:

···

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

Or

irb(main):007:0> 100.times {|a| s = (a * a).to_s; puts a, s if
/\A(\d)\1(\d)\2/ =~ s}
88
7744
=> 100

Cheers

robert

···

On Mon, Apr 15, 2013 at 6:29 AM, Lee Daekeun <zmsquf99@gmail.com> wrote:

Like this?

100.times.select{|a| a*a>1000}.map{|a| [a, (a*a).to_s]}.select{|a|
a[1][0]==a[1][1] and a[1][2]==a[1][3]}

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Thanks for your help.
More appropriate to solve the problem, was looking for an easy example.
Thank you.

Matthew Kerwin wrote in post #1105635:

···

Hyun Oh K. wrote in post #1105634:

below is python code.

[snip]

and ruby have lambda func. so I hope easly solution make.
anybody wellknown ruby user, help and solve above quiz.

You don't need lambdas to do this. If you want to start simply, why not
just translate the python directly to ruby?

--
Posted via http://www.ruby-forum.com/\.

OMG! this is the simplest way!
Thank you!
Very simple... beautiful!

Robert Klemme wrote in post #1105671:

···

On Mon, Apr 15, 2013 at 6:29 AM, Lee Daekeun <zmsquf99@gmail.com> wrote:

Like this?

100.times.select{|a| a*a>1000}.map{|a| [a, (a*a).to_s]}.select{|a|
a[1][0]==a[1][1] and a[1][2]==a[1][3]}

Or

irb(main):007:0> 100.times {|a| s = (a * a).to_s; puts a, s if
/\A(\d)\1(\d)\2/ =~ s}
88
7744
=> 100

Cheers

robert

--
Posted via http://www.ruby-forum.com/\.

That's Ruby. :slight_smile:

Here's another one
- just for fun

irb(main):003:0> 100.times {|a| s = (a * a).to_s; puts a, s if 2.times.all?
{|i| s[2*i] == s[2*i+1]} }
88
7744
=> 100

:wink:

Cheers

robert

···

On Mon, Apr 15, 2013 at 2:53 PM, Hyun Oh K. <lists@ruby-forum.com> wrote:

OMG! this is the simplest way!
Thank you!
Very simple... beautiful!

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/