查看完整版本: [结]2008-03-14 测试题(00001)

martin 2008-3-14 11:14

[结]2008-03-14 测试题(00001)

版规:
1:周一至周五 每日一题,周末休息
2:请用ruby/rails解答,你可以任选一题,或者全选
3:每人在回帖时,请尽量在一贴内答完,将余下的版面留给其他的用户。
4:大家在做题之前,尽量不要去看别人的答案,[b][color=Red]为了防止你的答案影响别人的思维定势,或者被别人直接抄袭,请将你的全部内容包含在discuz代码 hide中,不会使用bbcode的用户,请点击回复按钮,进入富文本编辑器,点隐藏代码,然后在其中输入内容。[/color][/b]

5:根据你的答案我们会做出评选,并给予小小的积分奖励

说明:
1:欢迎你踊跃提供你认为有趣的题目,请发在,题目征集栏目中
2:我们需要评委,如果你认为你可以,并且时间充足,请站内短信联系我。期待你能为大家贡献。


[color=DarkRed][b][size=3]Ruby 类:[/size][/b][/color]
目前Ruby类,我们采用[url]http://projecteuler.net/index.php?section=problems&id=1[/url] 的题目,简单描述 如下:

[quote]
在自然数中,10 以内能被3或者5整除的数有 3, 5, 6 和 9,和为23,那么请计算10000以内能被3或者5整除的自然数的总和。
[/quote]


[color=DarkRed][b][size=3] Rails 类:[/size][/b][/color]
[quote]
我们在项目中经常遇到去关联对象的属性,而关联对象又经常为空,则需要做nil?的判断。譬如
[code]
class Coment<AR
  belongs_to :user
end

class User<AR
  has_many :comments
end
[/code]

我们在使用的时候一般如下:
[code]
unless @comment.user.nil?
   puts @comment.user.login
end
[/code]

想个方法简化他,去掉这冗余的nil?判断
[/quote]


知识的积累在于点滴。千万别被你的惰性给淹没了你的才智。开动你的脑筋,来吧。祝你好运。

[[i] 本帖最后由 martin 于 2008-3-17 11:08 编辑 [/i]]

martin 2008-3-14 11:15

稍等

[[i] 本帖最后由 martin 于 2008-3-17 10:55 编辑 [/i]]

blackanger 2008-3-14 11:58

我来顶一顶。周末来做这些题

blackanger 2008-3-14 12:00

试下Hide

**** Hidden Message *****

drive2me 2008-3-14 12:09

请积极参与!

Hide的用法可以了?哈哈。

谢谢Martin积极想出办法帮助我们的会员提高Ruby/Rails技巧。希望大家积极参与,参与者均有奖励。

我一会看下Rails的题。呵呵!

谢谢!

jmouse 2008-3-14 12:16

[code]
def Euler1(x)
sum = 0
(1...x).each{|i| sum += i if (i % 3 == 0 || i % 5 == 0)}
puts sum
end
#test
Euler1(10000)

用短路|比用OR能快多少?
数学方法:x=1000          233168
               x=10000        23331668
               x=100000      2333316668
               x=1000000    233333166668

[/code]

[[i] 本帖最后由 martin 于 2008-3-17 09:47 编辑 [/i]]

xiaoqiu 2008-3-14 12:27

呵呵.支持,一会看一下.

maninred 2008-3-14 12:38

Ruby的数值问题,好像那个网站那里是1000,这里是10000。
[code]
[code]def count (lim)
        sum = 0
        3.upto(lim - 1) { |n| sum += n if((n%3).zero? || (n%5).zero?) }
        sum
end

puts count 10000
#23331668 [/code]

第二个问题再想想。。。

[/code]

[[i] 本帖最后由 martin 于 2008-3-17 09:47 编辑 [/i]]

wscc111 2008-3-14 13:19

晕,他说我没有权限使用[hide]

``~怎么搞?

wscc111 2008-3-14 13:28

先做简单的

[code]
@sum3 =0
@sum5 =0
def show(num)
0.step(num-1,3){|i| @sum3 +=i  if i % 5 !=0}
0.step(num-1,5){|i| @sum5 +=i }
return @sum3 + @sum5
end
p show(10000)
[/code]

[[i] 本帖最后由 martin 于 2008-3-17 09:48 编辑 [/i]]

blackanger 2008-3-14 14:15

**** Hidden Message *****

blackanger 2008-3-14 14:52

Rails类:

第二题:

我在项目里是用了:
是用validates_associated这个方法来确保不出现comment.user为nil的情况。

或者在commentmodel里写个方法。
def user_login
      user ? user.login : [xxx
end

然后使用@comment.user_login

[[i] 本帖最后由 martin 于 2008-3-17 09:49 编辑 [/i]]

drive2me 2008-3-14 16:18

各位,

Hide的用法,所有级别的会员应该都可以用了。
如有问题,请及时报告。

谢谢!

xavier 2008-3-14 18:17

[code]
    s = 0
    3.upto(9999) {|i| s+=i if i%3 == 0 || i%5==0}
    puts s
  [/code]

[[i] 本帖最后由 martin 于 2008-3-17 09:49 编辑 [/i]]

cclong 2008-3-14 18:55

total=0
for i in 1...10000
if  i%3==0 ||  i%5==0
  total+=i
end
end
puts total

zengjinbai 2008-3-14 19:36

[attach]627[/attach]

[code]$arr=[ ] #存放的数组
$arz=0  #总和
def add_prime(n)#定义方法,将符合条件的数加入数组$arr
    3.step(n,1){|num|$arr <<num if is_prime?num}
     end
def is_prime?(number)#定义方法,判断一个数是否符合能被3或5整除的条件

    return true if (number%3 ==0 or number%5 ==0)
     return false

end
add_prime(1000)
$arr.each{|i|$arz=$arz+i if i<$arr.length}
print "符合条件的数:\n"
print $arr.join(", "),"\n"
print "总和:\n"
print $arz,"\n"[/code]

[[i] 本帖最后由 martin 于 2008-3-17 09:49 编辑 [/i]]

Ragnarok 2008-3-14 19:47

<hide>Ruby:
[code]def sum(m,n)
  n=((n-1)/m)*m
  m*(((1+n/m)*(n/m))/2)   #求和公式:(a1+an)*n/2  
end
puts sum(3,10000)+sum(5,10000)-sum(15,10000) #23331668
#感觉搞到有点复杂,不过算了复习一下求和公式也好!
[/code]
</hide>

[[i] 本帖最后由 Ragnarok 于 2008-3-15 10:26 编辑 [/i]]

blackanger 2008-3-14 21:37

楼上的,应该用[hide]标签来隐藏

cclong 2008-3-14 21:56

class User<AR
  has_many :comments
  has_many :comment_no_nil,
                :throught => :comments,
                :conditions => "#这里不知道如何写"
end

blackanger 2008-3-14 23:42

讨论一下第二题:

讨论一下:
如果用method_missing的话,可能难度比较大哈。
method_missing只是来处理不存在的方法。而我们如果在这种情况下该如何处理 :

user.comment.login
当user.comment的时候,返回一个nil对象,这个时候nil.login会抛出异常。感觉没法去method_missing来处理啊。当声明belongs_to方法之后,user是肯定有comment方法的。只是返回结果有可能是nil。这里排除用method_missing来处理。也许有别的思路。

[[i] 本帖最后由 martin 于 2008-3-17 09:50 编辑 [/i]]

blackanger 2008-3-15 00:21

[quote]原帖由 [i]cclong[/i] 于 2008-3-14 21:56 发表 [url=http://www.ruby-lang.org.cn/forums/redirect.php?goto=findpost&pid=12469&ptid=3733][img]http://www.ruby-lang.org.cn/forums/images/common/back.gif[/img][/url]
class User :comments,
                :conditions => "#这里不知道如何写"
end [/quote]


看不太懂你的意思!可否把思路说出来讨论下呢

love8909 2008-3-15 00:57

[code]
class Problem1
  def Problem1.sum(n)
    (3 + (n - 1) - (n - 1) % 3) * ((n - 1) / 3) / 2 +       #3+6+9+...
      (5 + (n - 1) - (n - 1) % 5) * ((n - 1) / 5) / 2 -      #5+10+15+...
        (15 + (n - 1) - (n - 1) % 15) * ((n - 1) / 15) / 2  #15+30+...
  end
end
[/code]

[[i] 本帖最后由 martin 于 2008-3-17 09:51 编辑 [/i]]

jmouse 2008-3-15 01:16

楼上,结束应该有/才能隐藏。

Ragnarok 2008-3-15 10:28

[quote]原帖由 [i]blackanger[/i] 于 2008-3-14 21:37 发表 [url=http://www.ruby-lang.org.cn/forums/redirect.php?goto=findpost&pid=12468&ptid=3733][img]http://www.ruby-lang.org.cn/forums/images/common/back.gif[/img][/url]
楼上的,应该用[hide]标签来隐藏 [/quote]

我编辑的时候根本就没有这个选项!~!~难道是人品问题!~!~

cclong 2008-3-15 10:41

[quote]原帖由 [i]blackanger[/i] 于 2008-3-15 00:21 发表 [url=http://www.ruby-lang.org.cn/forums/redirect.php?goto=findpost&pid=12474&ptid=3733][img]http://www.ruby-lang.org.cn/forums/images/common/back.gif[/img][/url]



看不太懂你的意思!可否把思路说出来讨论下呢 [/quote]
没,我乱来的!
因为看了好友申请的代码,所以有这个想法.
has_many :requested_friends,
:through => :friendships,
:source => :friend,
:conditions => "status = 'requested'"

xavier 2008-3-15 13:42

Rails问题.....

关于布尔值:在Ruby里除了nil和false是false以外,其他全部是true
那么只要
[code]
if @comment.user
   puts @comment.user.login
end
[/code]
难道不是这样么.......
[code]
def test(x)
  if x
    puts x
  else
    return "This parameter is nil"
  end
end

test(1)
test((1..10).to_a)
test(Time.now)
test(nil)
[/code]

[[i] 本帖最后由 martin 于 2008-3-17 09:51 编辑 [/i]]

blackanger 2008-3-15 17:45

[quote]原帖由 [i]xavier[/i] 于 2008-3-15 13:42 发表 [url=http://www.ruby-lang.org.cn/forums/redirect.php?goto=findpost&pid=12497&ptid=3733][img]http://www.ruby-lang.org.cn/forums/images/common/back.gif[/img][/url]
Rails问题.....
**** 本内容被作者隐藏 ***** [/quote]

xavier可能没有理解这个意思哈。是要去除判断,不管是if或unless。可以让用户放心的去使用user.comment.login而不用考虑commnet是否为nil 的情况。

bless4me 2008-3-15 18:54

[quote]原帖由 [i]blackanger[/i] 于 2008-3-15 17:45 发表 [url=http://www.ruby-lang.org.cn/forums/redirect.php?goto=findpost&pid=12513&ptid=3733][img]http://www.ruby-lang.org.cn/forums/images/common/back.gif[/img][/url]


xavier可能没有理解这个意思哈。是要去除判断,不管是if或unless。可以让用户放心的去使用user.comment.login而不用考虑commnet是否为nil 的情况。 [/quote]

我认为关于Rails问题的理解,Blackanger说的对。那题的本意就是要简化用法,给大家一个提示。

drive2me 2008-3-15 19:05

好热闹,呵呵。
本来说来看第二题。但由于权限较高,能看到大家的答案,这不是版主的原意了,也没有意思了。就不参加答题吧。

不过我在外围,继续为大家服务。希望大家谅解!

liumuqing 2008-3-15 19:29

看看
页: [1] 2
查看完整版本: [结]2008-03-14 测试题(00001)