Skip to content

Instantly share code, notes, and snippets.

@igaiga
Created May 24, 2012 13:56
Show Gist options
  • Save igaiga/2781680 to your computer and use it in GitHub Desktop.
Save igaiga/2781680 to your computer and use it in GitHub Desktop.
ある配列が与えられたとき、3以下の要素がいくつあるか数えるコード
# -*- 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