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~!
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.
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~!