Forum: Ruby simple lambda question

E921f63e31e234e85e36ba703c5c74e6?d=identicon&s=25 Hyun Oh K. (hyunoh_k)
on 2013-04-15 04:31
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~!
3df767279ce7d81db0a5bb30f5136863?d=identicon&s=25 Matthew Kerwin (mattyk)
on 2013-04-15 04:47
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.
6251987fd56cfb03e7e265a90388a24c?d=identicon&s=25 Lee Daekeun (Guest)
on 2013-04-15 06:29
(Received via mailing list)
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>

> >>> 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
zmsquf99@gmail.com

http://www.facebook.com/daekeun.lee
@zmsquf99
E921f63e31e234e85e36ba703c5c74e6?d=identicon&s=25 Hyun Oh K. (hyunoh_k)
on 2013-04-15 07:28
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 Daekeun wrote in post #1105644:
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2013-04-15 10:11
(Received via mailing list)
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
E921f63e31e234e85e36ba703c5c74e6?d=identicon&s=25 Hyun Oh K. (hyunoh_k)
on 2013-04-15 14:53
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
E921f63e31e234e85e36ba703c5c74e6?d=identicon&s=25 Hyun Oh K. (hyunoh_k)
on 2013-04-15 14:55
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?
>
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2013-04-15 15:40
(Received via mailing list)
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!
>

That's Ruby. :-)

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

;-)

Cheers

robert
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.