Ruby Examples
This commit is contained in:
10
level3/21.rb
Executable file
10
level3/21.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
숫자 배열의 합을 구하기
|
||||
=end
|
||||
|
||||
arr = [1,3,5,7,9]
|
||||
|
||||
puts arr.sum
|
||||
11
level3/22.rb
Executable file
11
level3/22.rb
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
배열에서 최댓값, 최솟값 찾기
|
||||
=end
|
||||
|
||||
arr = [5, 7, 4, 3, 2, 9, 1, 6]
|
||||
|
||||
puts "Min = #{arr.min}"
|
||||
puts "Max = #{arr.max}"
|
||||
10
level3/23.rb
Executable file
10
level3/23.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
배열 요소를 하나씩 출력 (each 사용)
|
||||
=end
|
||||
|
||||
arr = [5, 7, 4, 3, 2, 9, 1, 6]
|
||||
|
||||
arr.each { |i| puts i }
|
||||
16
level3/24.rb
Executable file
16
level3/24.rb
Executable file
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
배열의 짝수만 출력
|
||||
=end
|
||||
|
||||
arr = [5, 7, 4, 3, 2, 9, 1, 6]
|
||||
|
||||
arr.each do |num|
|
||||
puts num if num.even?
|
||||
end
|
||||
|
||||
# select 필터 사용
|
||||
arr.select { |v| v.even? }
|
||||
.each { |v| puts v }
|
||||
12
level3/25.rb
Executable file
12
level3/25.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
문자열 배열에서 글자 수가 5 이상인 단어만 출력
|
||||
=end
|
||||
|
||||
arr = %w[this is a test array]
|
||||
|
||||
# select 필터 사용
|
||||
arr.select { |v| v.length >= 5 }
|
||||
.each { |v| puts v }
|
||||
12
level3/26.rb
Executable file
12
level3/26.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
배열 요소를 모두 대문자로 변환
|
||||
=end
|
||||
|
||||
arr = %w[this is a test array]
|
||||
|
||||
# select 필터 사용
|
||||
arr.map { |v| v.upcase }
|
||||
.each { |v| puts v }
|
||||
10
level3/27.rb
Executable file
10
level3/27.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
배열에서 특정 값이 있는지 검사
|
||||
=end
|
||||
|
||||
arr = %w[this is a test array]
|
||||
|
||||
puts arr.include?("hash") ? "Yes" : "No"
|
||||
10
level3/28.rb
Executable file
10
level3/28.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
배열의 평균값 구하기
|
||||
=end
|
||||
|
||||
arr = [5, 7, 4, 3, 2, 9, 1, 6]
|
||||
|
||||
puts arr.sum / arr.size.to_f
|
||||
10
level3/29.rb
Executable file
10
level3/29.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
중복 제거된 배열 만들기
|
||||
=end
|
||||
|
||||
arr = [5, 7, 4, 3, 2, 9, 1, 6, 2, 9, 1, 6, 7, 4, 3, 2, 9]
|
||||
|
||||
puts arr.uniq
|
||||
12
level3/30.rb
Executable file
12
level3/30.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
배열을 오름차순/내림차순 정렬
|
||||
=end
|
||||
|
||||
arr = [5, 7, 4, 3, 2, 9, 1, 6]
|
||||
|
||||
puts arr.sort
|
||||
|
||||
puts arr.sort.reverse
|
||||
Reference in New Issue
Block a user