Created
May 24, 2012 13:56
-
-
Save igaiga/2781680 to your computer and use it in GitHub Desktop.
ある配列が与えられたとき、3以下の要素がいくつあるか数えるコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
#ある配列が与えられたとき、Array [6,2,3] の中で3以下の数字の数を表示するコードを書いてください。 | |
array = [6,2,3] | |
count = 0 | |
array.each do |i| | |
count += 1 if i <= 3 | |
end | |
p count | |
# 別解 | |
# array = [6,2,1] | |
# p array.count { |i| i < 3 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment