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 aa < 10000:
… if a
a > 1000:
… s = str(aa)
… 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~!

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 aa < 10000
irb:003:1> if a
a > 1000
irb:004:2> s = (aa).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 aa > 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.

Thank you!
Ruby and really good language.
I was looking for a suitable example to common problems that you can
solve a little more succinctly.
It has helped a lot.

감사합니다!
루비가 정말 좋은 언어라고 해서
일반적인 문제에 대해서 좀 더 간결하게 해결할 수 있다는 적당한 예제를 찾고 있었거든요.
많은 도움이 되었습니다.
우리나라분인거 같아서 한글로 남김니다~ ^^

Lee D. wrote in post #1105644:

On Mon, Apr 15, 2013 at 6:29 AM, Lee D. [email protected] wrote:

Like this?

100.times.select{|a| aa>1000}.map{|a| [a, (aa).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

Like this?

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

2013/4/15 Hyun Oh K. [email protected]

while a*a < 10000:
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
[email protected]

@zmsquf99

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

Matthew K. 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?

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

Robert K. wrote in post #1105671:

On Mon, Apr 15, 2013 at 6:29 AM, Lee D. [email protected] wrote:

Like this?

100.times.select{|a| aa>1000}.map{|a| [a, (aa).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

On Mon, Apr 15, 2013 at 2:53 PM, Hyun Oh K. [email protected]
wrote:

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

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[2i] == s[2i+1]} }
88
7744
=> 100

:wink:

Cheers

robert