From cf3d7d32968b5814a8f8b9d0cfdd6866013aa336 Mon Sep 17 00:00:00 2001 From: Elex Date: Thu, 15 Jan 2026 14:01:21 +0900 Subject: [PATCH] Ruby Examples --- .gitignore | 58 ++++++++++++++++++++ .idea/.gitignore | 10 ++++ .idea/modules.xml | 8 +++ .idea/ruby-examples.iml | 15 +++++ .idea/vcs.xml | 6 ++ README.md | 119 ++++++++++++++++++++++++++++++++++++++++ level1/01.rb | 10 ++++ level1/02.rb | 11 ++++ level1/03.rb | 17 ++++++ level1/04.rb | 11 ++++ level1/05.rb | 23 ++++++++ level1/06.rb | 19 +++++++ level1/07.rb | 11 ++++ level1/08.rb | 12 ++++ level1/09.rb | 11 ++++ level1/10.rb | 14 +++++ level2/11.rb | 17 ++++++ level2/12.rb | 12 ++++ level2/13.rb | 12 ++++ level2/14.rb | 14 +++++ level2/15.rb | 12 ++++ level2/16.rb | 12 ++++ level2/17.rb | 12 ++++ level2/18.rb | 16 ++++++ level2/19.rb | 12 ++++ level2/20.rb | 19 +++++++ level3/21.rb | 10 ++++ level3/22.rb | 11 ++++ level3/23.rb | 10 ++++ level3/24.rb | 16 ++++++ level3/25.rb | 12 ++++ level3/26.rb | 12 ++++ level3/27.rb | 10 ++++ level3/28.rb | 10 ++++ level3/29.rb | 10 ++++ level3/30.rb | 12 ++++ level4/31.rb | 10 ++++ level4/32.rb | 10 ++++ level4/33.rb | 18 ++++++ level4/34.rb | 14 +++++ level4/35.rb | 14 +++++ level4/36.rb | 12 ++++ level4/37.rb | 13 +++++ level4/38.rb | 12 ++++ level4/39.rb | 12 ++++ level4/40.rb | 13 +++++ level5/41.rb | 10 ++++ level5/42.rb | 10 ++++ level5/43.rb | 10 ++++ level5/44.rb | 10 ++++ level5/45.rb | 10 ++++ level5/46.rb | 12 ++++ level5/47.rb | 10 ++++ level5/48.rb | 11 ++++ level5/49.rb | 12 ++++ level5/50.rb | 12 ++++ level6/51.rb | 10 ++++ level6/52.rb | 10 ++++ level6/53.rb | 10 ++++ level6/54.rb | 10 ++++ level6/55.rb | 10 ++++ level6/56.rb | 10 ++++ level6/57.rb | 14 +++++ level6/58.rb | 10 ++++ level6/59.rb | 19 +++++++ level6/60.rb | 13 +++++ level7/61.rb | 19 +++++++ level7/62.rb | 22 ++++++++ level7/63.rb | 19 +++++++ level7/64.rb | 19 +++++++ level7/65.rb | 23 ++++++++ level7/66.rb | 25 +++++++++ level7/67.rb | 24 ++++++++ level7/68.rb | 24 ++++++++ level7/69.rb | 24 ++++++++ level7/70.rb | 35 ++++++++++++ 76 files changed, 1191 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/modules.xml create mode 100644 .idea/ruby-examples.iml create mode 100644 .idea/vcs.xml create mode 100644 README.md create mode 100755 level1/01.rb create mode 100755 level1/02.rb create mode 100755 level1/03.rb create mode 100755 level1/04.rb create mode 100755 level1/05.rb create mode 100755 level1/06.rb create mode 100755 level1/07.rb create mode 100755 level1/08.rb create mode 100755 level1/09.rb create mode 100755 level1/10.rb create mode 100755 level2/11.rb create mode 100755 level2/12.rb create mode 100755 level2/13.rb create mode 100755 level2/14.rb create mode 100755 level2/15.rb create mode 100755 level2/16.rb create mode 100755 level2/17.rb create mode 100755 level2/18.rb create mode 100755 level2/19.rb create mode 100755 level2/20.rb create mode 100755 level3/21.rb create mode 100755 level3/22.rb create mode 100755 level3/23.rb create mode 100755 level3/24.rb create mode 100755 level3/25.rb create mode 100755 level3/26.rb create mode 100755 level3/27.rb create mode 100755 level3/28.rb create mode 100755 level3/29.rb create mode 100755 level3/30.rb create mode 100755 level4/31.rb create mode 100755 level4/32.rb create mode 100755 level4/33.rb create mode 100755 level4/34.rb create mode 100755 level4/35.rb create mode 100755 level4/36.rb create mode 100755 level4/37.rb create mode 100755 level4/38.rb create mode 100755 level4/39.rb create mode 100755 level4/40.rb create mode 100755 level5/41.rb create mode 100755 level5/42.rb create mode 100755 level5/43.rb create mode 100755 level5/44.rb create mode 100755 level5/45.rb create mode 100755 level5/46.rb create mode 100755 level5/47.rb create mode 100755 level5/48.rb create mode 100755 level5/49.rb create mode 100755 level5/50.rb create mode 100755 level6/51.rb create mode 100755 level6/52.rb create mode 100755 level6/53.rb create mode 100755 level6/54.rb create mode 100755 level6/55.rb create mode 100755 level6/56.rb create mode 100755 level6/57.rb create mode 100755 level6/58.rb create mode 100755 level6/59.rb create mode 100755 level6/60.rb create mode 100755 level7/61.rb create mode 100755 level7/62.rb create mode 100755 level7/63.rb create mode 100755 level7/64.rb create mode 100755 level7/65.rb create mode 100755 level7/66.rb create mode 100755 level7/67.rb create mode 100755 level7/68.rb create mode 100755 level7/69.rb create mode 100755 level7/70.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..afab3cd --- /dev/null +++ b/.gitignore @@ -0,0 +1,58 @@ +### Ruby template +*.gem +*.rbc +/.config +/coverage/ +/InstalledFiles +/pkg/ +/spec/reports/ +/spec/examples.txt +/test/tmp/ +/test/version_tmp/ +/tmp/ + +# Used by dotenv library to load environment variables. +# .env + +# Ignore Byebug command history file. +.byebug_history + +## Specific to RubyMotion: +.dat* +.repl_history +build/ +*.bridgesupport +build-iPhoneOS/ +build-iPhoneSimulator/ + +## Specific to RubyMotion (use of CocoaPods): +# +# We recommend against adding the Pods directory to your .gitignore. However +# you should judge for yourself, the pros and cons are mentioned at: +# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control +# +# vendor/Pods/ + +## Documentation cache and generated files: +/.yardoc/ +/_yardoc/ +/doc/ +/rdoc/ + +## Environment normalization: +/.bundle/ +/vendor/bundle +/lib/bundler/man/ + +# for a library or gem, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# Gemfile.lock +# .ruby-version +# .ruby-gemset + +# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: +.rvmrc + +# Used by RuboCop. Remote config files pulled in from inherit_from directive. +# .rubocop-https?--* + diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..9879198 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# 디폴트 무시된 파일 +/shelf/ +/workspace.xml +# 쿼리 파일을 포함한 무시된 디폴트 폴더 +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# 에디터 기반 HTTP 클라이언트 요청 +/httpRequests/ diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..55e0ecd --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/ruby-examples.iml b/.idea/ruby-examples.iml new file mode 100644 index 0000000..916fb3b --- /dev/null +++ b/.idea/ruby-examples.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..62f1b8e --- /dev/null +++ b/README.md @@ -0,0 +1,119 @@ +# 루비 연습 문제 + +## 1단계: 루비 기초 감각 익히기 (초급 입문) + +**목표**: 문법, 출력, 변수, 제어문에 익숙해지기 + +1. `"Hello, Ruby!"`를 출력하는 프로그램 작성 +2. 이름을 변수에 저장하고 `"안녕하세요, 이름님"` 출력 +3. 두 정수를 변수로 받아 합, 차, 곱, 몫 출력 +4. 숫자를 입력받아 짝수/홀수 판별 +5. 점수(0~100)를 받아 A/B/C/D/F 등급 출력 +6. 1부터 100까지 출력 (for, while 각각 사용) +7. 1~100 중 3의 배수만 출력 +8. 사용자 입력이 `"exit"`이면 종료되는 루프 작성 +9. 구구단 2단 출력 +10. 구구단 전체 출력 + +--- + +## 2단계: 메서드와 조건 분기 (초급) + +**목표**: 메서드 정의, 리턴값, 재사용 + +11. 두 수를 받아 더한 값을 반환하는 메서드 작성 +12. 세 수 중 최댓값을 반환하는 메서드 +13. 숫자를 받아 절댓값을 반환하는 메서드 +14. 문자열을 받아 길이를 반환하는 메서드 +15. 원의 반지름을 받아 넓이를 계산하는 메서드 +16. 나이를 받아 성인/미성년자 판별 메서드 +17. 문자열과 반복 횟수를 받아 반복 출력하는 메서드 +18. BMI 계산 메서드 작성 +19. 비밀번호 길이가 8자 이상인지 검사하는 메서드 +20. 윤년 여부를 판단하는 메서드 + +--- + +## 3단계: 배열(Array)과 반복 (초급 → 중급) + +**목표**: 컬렉션과 반복 처리에 익숙해지기 + +21. 숫자 배열의 합을 구하기 +22. 배열에서 최댓값, 최솟값 찾기 +23. 배열 요소를 하나씩 출력 (each 사용) +24. 배열의 짝수만 출력 +25. 문자열 배열에서 글자 수가 5 이상인 단어만 출력 +26. 배열 요소를 모두 대문자로 변환 +27. 배열에서 특정 값이 있는지 검사 +28. 배열의 평균값 구하기 +29. 중복 제거된 배열 만들기 +30. 배열을 오름차순/내림차순 정렬 + +--- + +## 4단계: 해시(Hash) 다루기 (중급 초입) + +**목표**: 키-값 구조 이해 + +31. 사람의 이름과 나이를 해시로 표현 +32. 해시에서 특정 키의 값 출력 +33. 모든 키와 값을 `"이름: 나이"` 형식으로 출력 +34. 가장 나이가 많은 사람 찾기 +35. 점수 해시에서 평균 점수 계산 +36. 해시에 새로운 요소 추가 +37. 해시에서 특정 조건(나이 ≥ 30)인 항목만 출력 +38. 해시 키 목록만 배열로 추출 +39. 해시 값 목록만 배열로 추출 +40. 단어 빈도수 계산 프로그램 + +--- + +## 5단계: 블록과 Enumerable (중급) + +**목표**: 루비스러운 코드 작성 + +41. `map`을 사용해 배열의 제곱값 배열 생성 +42. `select`로 조건에 맞는 값 필터링 +43. `reject`로 특정 조건 제거 +44. `reduce`로 배열 합계 계산 +45. 문자열 배열을 하나의 문자열로 합치기 +46. 배열에서 가장 긴 문자열 찾기 +47. 해시를 배열로 변환 +48. 배열을 해시로 변환 (index → 값) +49. `each_with_index` 사용 +50. 사용자 정의 메서드에 블록 전달하기 + +--- + +## 6단계: 문자열 처리 (중급) + +**목표**: 텍스트 처리 감각 익히기 + +51. 문자열에서 모음 개수 세기 +52. 문자열 뒤집기 +53. 특정 문자 제거 +54. 공백 기준으로 단어 분리 +55. 단어 수 세기 +56. 문자열이 회문인지 검사 +57. 이메일 형식 간단 검증 +58. 파일명에서 확장자 추출 +59. snake_case → camelCase 변환 +60. 문자열 압축 (aaabb → a3b2) + +--- + +## 7단계: 객체지향 기초 (중급) + +**목표**: 클래스와 객체 개념 이해 + +61. Person 클래스 정의 (이름, 나이) +62. 인스턴스 메서드 작성 +63. 생성자(`initialize`) 사용 +64. 접근자(`attr_accessor`) 사용 +65. 나이 증가 메서드 +66. 여러 Person 객체를 배열로 관리 +67. 클래스 메서드 작성 +68. 간단한 BankAccount 클래스 +69. 입금/출금 기능 구현 +70. 잔액 부족 예외 처리 + diff --git a/level1/01.rb b/level1/01.rb new file mode 100755 index 0000000..0ad622d --- /dev/null +++ b/level1/01.rb @@ -0,0 +1,10 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +`"Hello, Ruby!"`를 출력하는 프로그램 작성 +=end + +puts "Hello, Ruby!" + +# puts는 개행 포함 출력 diff --git a/level1/02.rb b/level1/02.rb new file mode 100755 index 0000000..a3140e0 --- /dev/null +++ b/level1/02.rb @@ -0,0 +1,11 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +이름을 변수에 저장하고 `"안녕하세요, 이름님"` 출력 +=end + +name = "홍길동" +puts "안녕하세요, #{name}님." + +# 루비는 #{} 문자열 보간 사용 \ No newline at end of file diff --git a/level1/03.rb b/level1/03.rb new file mode 100755 index 0000000..1808d62 --- /dev/null +++ b/level1/03.rb @@ -0,0 +1,17 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +두 정수를 변수로 받아 합, 차, 곱, 몫 출력 +=end + +print "숫자 1: " +number1 = gets.chomp.to_i +print "숫자 2: " +number2 = gets.chomp.to_i + +puts "#{number1} + #{number2} = #{number1 + number2}" +puts "#{number1} - #{number2} = #{number1 - number2}" +puts "#{number1} * #{number2} = #{number1 * number2}" +puts "#{number1} / #{number2} = #{number1 / number2}" +puts "#{number1} % #{number2} = #{number1 % number2}" \ No newline at end of file diff --git a/level1/04.rb b/level1/04.rb new file mode 100755 index 0000000..e0d57d4 --- /dev/null +++ b/level1/04.rb @@ -0,0 +1,11 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +숫자를 입력받아 짝수/홀수 판별 +=end + +print "숫자 : " +number = gets.chomp.to_i + +puts number.even? ? "짝수" : "홀수" \ No newline at end of file diff --git a/level1/05.rb b/level1/05.rb new file mode 100755 index 0000000..871ef5b --- /dev/null +++ b/level1/05.rb @@ -0,0 +1,23 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +점수(0~100)를 받아 A/B/C/D/F 등급 출력 +=end + +print "점수(0~100) : " +number = gets.chomp.to_i + +grade = case number +when 90..100 + "A" +when 80..89 + "B" +when 70..79 + "C" +when 60..69 + "D" +else + "F" +end +puts "#{number}점은 #{grade} 등급입니다." diff --git a/level1/06.rb b/level1/06.rb new file mode 100755 index 0000000..1df55f5 --- /dev/null +++ b/level1/06.rb @@ -0,0 +1,19 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +1부터 100까지 출력 (for, while 각각 사용) +=end + +for i in 1..100 + puts i +end + +i = 1 +while i <= 100 do + puts i + i += 1 +end + +(1..100).each { |item| puts item } + diff --git a/level1/07.rb b/level1/07.rb new file mode 100755 index 0000000..a75da44 --- /dev/null +++ b/level1/07.rb @@ -0,0 +1,11 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +1~100 중 3의 배수만 출력 +=end + +(1..100).each do |item| + puts item if (item % 3 == 0) +end + diff --git a/level1/08.rb b/level1/08.rb new file mode 100755 index 0000000..c5c209d --- /dev/null +++ b/level1/08.rb @@ -0,0 +1,12 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +사용자 입력이 `"exit"`이면 종료되는 루프 작성 +=end + +loop do + print "입력 : " + break if gets.chomp == "exit" +end + diff --git a/level1/09.rb b/level1/09.rb new file mode 100755 index 0000000..c3813cf --- /dev/null +++ b/level1/09.rb @@ -0,0 +1,11 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +구구단 2단 출력 +=end + +(1..9).each do |item| + puts "2 * #{item} = #{2 * item}" +end + diff --git a/level1/10.rb b/level1/10.rb new file mode 100755 index 0000000..03a3a10 --- /dev/null +++ b/level1/10.rb @@ -0,0 +1,14 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +구구단 전체 출력 +=end + +(2..9).each do |i| + (1..9).each do |j| + puts "#{i} * #{j} = #{i * j}" + end + puts +end + diff --git a/level2/11.rb b/level2/11.rb new file mode 100755 index 0000000..399f6f3 --- /dev/null +++ b/level2/11.rb @@ -0,0 +1,17 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +두 수를 받아 더한 값을 반환하는 메서드 작성 +=end + +def add(a, b) + a + b +end + +print "숫자 1: " +number1 = gets.chomp.to_i +print "숫자 2: " +number2 = gets.chomp.to_i + +puts "#{number1} + #{number2} = #{add(number1, number2)}" \ No newline at end of file diff --git a/level2/12.rb b/level2/12.rb new file mode 100755 index 0000000..a68f6fe --- /dev/null +++ b/level2/12.rb @@ -0,0 +1,12 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +세 수 중 최댓값을 반환하는 메서드 +=end + +def max(a, b, c) + [a, b, c].max +end + +puts "Max = #{max(5, 2, 9)}" \ No newline at end of file diff --git a/level2/13.rb b/level2/13.rb new file mode 100755 index 0000000..7d5592d --- /dev/null +++ b/level2/13.rb @@ -0,0 +1,12 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +숫자를 받아 절댓값을 반환하는 메서드 +=end + +def abs(a) + a.abs +end + +puts "Abs = #{abs(-7)}" \ No newline at end of file diff --git a/level2/14.rb b/level2/14.rb new file mode 100755 index 0000000..14f133c --- /dev/null +++ b/level2/14.rb @@ -0,0 +1,14 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +문자열을 받아 길이를 반환하는 메서드 +=end + +def len(s) + s.length +end + +print "?" +s = gets.chomp +puts "#{s}의 길이는 #{len(s)}" \ No newline at end of file diff --git a/level2/15.rb b/level2/15.rb new file mode 100755 index 0000000..56f19ff --- /dev/null +++ b/level2/15.rb @@ -0,0 +1,12 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +원의 반지름을 받아 넓이를 계산하는 메서드 +=end + +def area(r) + Math::PI * r ** 2 +end + +puts "Area = #{area(2)}" \ No newline at end of file diff --git a/level2/16.rb b/level2/16.rb new file mode 100755 index 0000000..9d4e9a4 --- /dev/null +++ b/level2/16.rb @@ -0,0 +1,12 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +나이를 받아 성인/미성년자 판별 메서드 +=end + +def adult?(age) + age >= 20 +end + +puts adult?(13) ? "성인입니다." : "미성년자입니다." \ No newline at end of file diff --git a/level2/17.rb b/level2/17.rb new file mode 100755 index 0000000..7543429 --- /dev/null +++ b/level2/17.rb @@ -0,0 +1,12 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +문자열과 반복 횟수를 받아 반복 출력하는 메서드 +=end + +def repeat(s, i) + s * i +end + +puts repeat("도깨비", 6) diff --git a/level2/18.rb b/level2/18.rb new file mode 100755 index 0000000..9e00841 --- /dev/null +++ b/level2/18.rb @@ -0,0 +1,16 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +BMI 계산 메서드 작성 +=end + +def bmi(kg, cm) + weight = kg.to_f + height = cm.to_f / 100 + + weight / (height ** 2) +end + +puts "BMI = #{bmi(70, 170)}" # 24.22 +puts "BMI = #{bmi(75, 175)}" # 24.5 \ No newline at end of file diff --git a/level2/19.rb b/level2/19.rb new file mode 100755 index 0000000..b5d19e9 --- /dev/null +++ b/level2/19.rb @@ -0,0 +1,12 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +비밀번호 길이가 8자 이상인지 검사하는 메서드 +=end + +def password_valid?(pw) + pw.length >= 8 +end + +puts password_valid?("dfjirgaofmrioa") ? "OK" : "Nope" \ No newline at end of file diff --git a/level2/20.rb b/level2/20.rb new file mode 100755 index 0000000..f73f56c --- /dev/null +++ b/level2/20.rb @@ -0,0 +1,19 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +윤년 여부를 판단하는 메서드 + +## 윤년의 조건 +1. 서력 기원 연수가 004로 나누어 떨어지는 해는 윤년으로 한다. +2. 서력 기원 연수가 100으로 나누어 떨어지는 해는 평년으로 한다. +3. 서력 기원 연수가 400으로 나누어 떨어지는 해는 윤년으로 둔다. +=end + +def leap_year?(year) + year %4 == 0 && (year %100 != 0 || year %400 == 0) +end + +puts leap_year?(2000) ? "윤년" : "평년" +puts leap_year?(2020) ? "윤년" : "평년" +puts leap_year?(2025) ? "윤년" : "평년" \ No newline at end of file diff --git a/level3/21.rb b/level3/21.rb new file mode 100755 index 0000000..ba00c8a --- /dev/null +++ b/level3/21.rb @@ -0,0 +1,10 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +숫자 배열의 합을 구하기 +=end + +arr = [1,3,5,7,9] + +puts arr.sum \ No newline at end of file diff --git a/level3/22.rb b/level3/22.rb new file mode 100755 index 0000000..08296b1 --- /dev/null +++ b/level3/22.rb @@ -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}" diff --git a/level3/23.rb b/level3/23.rb new file mode 100755 index 0000000..3c9c254 --- /dev/null +++ b/level3/23.rb @@ -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 } \ No newline at end of file diff --git a/level3/24.rb b/level3/24.rb new file mode 100755 index 0000000..a826ce0 --- /dev/null +++ b/level3/24.rb @@ -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 } \ No newline at end of file diff --git a/level3/25.rb b/level3/25.rb new file mode 100755 index 0000000..fdf89bd --- /dev/null +++ b/level3/25.rb @@ -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 } \ No newline at end of file diff --git a/level3/26.rb b/level3/26.rb new file mode 100755 index 0000000..b3a507c --- /dev/null +++ b/level3/26.rb @@ -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 } \ No newline at end of file diff --git a/level3/27.rb b/level3/27.rb new file mode 100755 index 0000000..b7a113c --- /dev/null +++ b/level3/27.rb @@ -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" \ No newline at end of file diff --git a/level3/28.rb b/level3/28.rb new file mode 100755 index 0000000..da44a89 --- /dev/null +++ b/level3/28.rb @@ -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 \ No newline at end of file diff --git a/level3/29.rb b/level3/29.rb new file mode 100755 index 0000000..ac0508e --- /dev/null +++ b/level3/29.rb @@ -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 \ No newline at end of file diff --git a/level3/30.rb b/level3/30.rb new file mode 100755 index 0000000..7625f16 --- /dev/null +++ b/level3/30.rb @@ -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 \ No newline at end of file diff --git a/level4/31.rb b/level4/31.rb new file mode 100755 index 0000000..0f7bfd6 --- /dev/null +++ b/level4/31.rb @@ -0,0 +1,10 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +사람의 이름과 나이를 해시로 표현 +=end + +person = { name: "Charlie", age: 13 } + +puts person \ No newline at end of file diff --git a/level4/32.rb b/level4/32.rb new file mode 100755 index 0000000..5c9cd00 --- /dev/null +++ b/level4/32.rb @@ -0,0 +1,10 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +해시에서 특정 키의 값 출력 +=end + +person = { name: "Charlie", age: 13 } + +puts person[:name] \ No newline at end of file diff --git a/level4/33.rb b/level4/33.rb new file mode 100755 index 0000000..5501efc --- /dev/null +++ b/level4/33.rb @@ -0,0 +1,18 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +모든 키와 값을 "이름: 나이" 형식으로 출력 +=end + +person = { name: "Charlie", age: 13 } + +person.each do |key, value| + puts "#{key}: #{value}" +end + + +people = [{ name: "Charlie", age: 13 }, + { name: "Steve", age: 34 }, + { name: "Anne", age: 23 }] +people.each { |item| puts "#{item[:name]}: #{item[:age]}"} diff --git a/level4/34.rb b/level4/34.rb new file mode 100755 index 0000000..47ddd2a --- /dev/null +++ b/level4/34.rb @@ -0,0 +1,14 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +가장 나이가 많은 사람 찾기 +=end + +people = [{ name: "Charlie", age: 13 }, + { name: "Steve", age: 34 }, + { name: "Anne", age: 23 }] + +person = people.max_by { |_| _[:age] } + +puts person[:name] \ No newline at end of file diff --git a/level4/35.rb b/level4/35.rb new file mode 100755 index 0000000..86c0225 --- /dev/null +++ b/level4/35.rb @@ -0,0 +1,14 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +점수 해시에서 평균 점수 계산 +=end + +people = [{ name: "Charlie", score: 13 }, + { name: "Steve", score: 34 }, + { name: "Anne", score: 23 }] + +avg = people.sum { |_| _[:score] } / people.length.to_f + +puts avg \ No newline at end of file diff --git a/level4/36.rb b/level4/36.rb new file mode 100755 index 0000000..5db6b62 --- /dev/null +++ b/level4/36.rb @@ -0,0 +1,12 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +해시에 새로운 요소 추가 +=end + +person = { name: "Charlie", score: 13 } + +person[:address] = "Seoul" + +puts person \ No newline at end of file diff --git a/level4/37.rb b/level4/37.rb new file mode 100755 index 0000000..1748424 --- /dev/null +++ b/level4/37.rb @@ -0,0 +1,13 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +해시에서 특정 조건(나이 ≥ 30)인 항목만 출력 +=end + +people = [{ name: "Charlie", score: 13 }, + { name: "Steve", score: 34 }, + { name: "Anne", score: 23 }] + +people.select { |item| item[:score] >= 30 } + .each { |item| puts "#{item[:name]}: #{item[:score]}" } \ No newline at end of file diff --git a/level4/38.rb b/level4/38.rb new file mode 100755 index 0000000..c75b08b --- /dev/null +++ b/level4/38.rb @@ -0,0 +1,12 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +해시 키 목록만 배열로 추출 +=end + +person = { name: "Charlie", score: 13 } + +keys = person.keys + +puts keys \ No newline at end of file diff --git a/level4/39.rb b/level4/39.rb new file mode 100755 index 0000000..07bf2ec --- /dev/null +++ b/level4/39.rb @@ -0,0 +1,12 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +해시 값 목록만 배열로 추출 +=end + +person = { name: "Charlie", score: 13 } + +keys = person.values + +puts keys \ No newline at end of file diff --git a/level4/40.rb b/level4/40.rb new file mode 100755 index 0000000..f880e23 --- /dev/null +++ b/level4/40.rb @@ -0,0 +1,13 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +단어 빈도수 계산 프로그램 +=end + +words = "This is a test This is only a test".downcase.split(" ") + +result = Hash.new +words.each { |w| result[w] = result[w]?result[w]+1:1} + +puts result \ No newline at end of file diff --git a/level5/41.rb b/level5/41.rb new file mode 100755 index 0000000..e2f0131 --- /dev/null +++ b/level5/41.rb @@ -0,0 +1,10 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +map을 사용해 배열의 제곱값 배열 생성 +=end + +array = [2, 3, 5, 7, 9] + +puts array.map { |item| item ** 2 } \ No newline at end of file diff --git a/level5/42.rb b/level5/42.rb new file mode 100755 index 0000000..2fdf4ec --- /dev/null +++ b/level5/42.rb @@ -0,0 +1,10 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +select로 조건에 맞는 값 필터링 +=end + +array = [2, 3, 5, 7, 9] + +puts array.select { |item| item.odd? } \ No newline at end of file diff --git a/level5/43.rb b/level5/43.rb new file mode 100755 index 0000000..e837db0 --- /dev/null +++ b/level5/43.rb @@ -0,0 +1,10 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +reject로 특정 조건 제거 +=end + +array = [2, 3, 5, 7, 9] + +puts array.reject { |item| item.even? } \ No newline at end of file diff --git a/level5/44.rb b/level5/44.rb new file mode 100755 index 0000000..5909bc5 --- /dev/null +++ b/level5/44.rb @@ -0,0 +1,10 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +reduce로 배열 합계 계산 +=end + +array = [2, 3, 5, 7, 9] + +puts array.reduce { |sum,item| sum += item } \ No newline at end of file diff --git a/level5/45.rb b/level5/45.rb new file mode 100755 index 0000000..4ddd1b5 --- /dev/null +++ b/level5/45.rb @@ -0,0 +1,10 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +문자열 배열을 하나의 문자열로 합치기 +=end + +array = ["Hello", "World"] + +puts array.join(" ") \ No newline at end of file diff --git a/level5/46.rb b/level5/46.rb new file mode 100755 index 0000000..20e3c1f --- /dev/null +++ b/level5/46.rb @@ -0,0 +1,12 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +배열에서 가장 긴 문자열 찾기 +=end + +array = %w[This is a Hello Ruby examples] + +word = array.max_by{ |str| str.length } + +puts word \ No newline at end of file diff --git a/level5/47.rb b/level5/47.rb new file mode 100755 index 0000000..fbc5fa3 --- /dev/null +++ b/level5/47.rb @@ -0,0 +1,10 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +해시를 배열로 변환 +=end + +person = { name: "Charlie", age: 13 } + +puts person.to_a \ No newline at end of file diff --git a/level5/48.rb b/level5/48.rb new file mode 100755 index 0000000..7da7390 --- /dev/null +++ b/level5/48.rb @@ -0,0 +1,11 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +배열을 해시로 변환 (index → 값) +=end + +people = ["Charlie", "Steve", "Anne"] + +i = 0 +puts people.map { |e| { name: e, index: i = i + 1 } } \ No newline at end of file diff --git a/level5/49.rb b/level5/49.rb new file mode 100755 index 0000000..eda5c24 --- /dev/null +++ b/level5/49.rb @@ -0,0 +1,12 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +each_with_index 사용 +=end + +people = ["Charlie", "Steve", "Anne"] + +h = people.each_with_index.to_h + +puts h \ No newline at end of file diff --git a/level5/50.rb b/level5/50.rb new file mode 100755 index 0000000..b80630d --- /dev/null +++ b/level5/50.rb @@ -0,0 +1,12 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +사용자 정의 메서드에 블록 전달하기 +=end + +def my_method + puts yield if block_given? +end + +my_method {"Hello, Ruby!"} \ No newline at end of file diff --git a/level6/51.rb b/level6/51.rb new file mode 100755 index 0000000..a5e61b0 --- /dev/null +++ b/level6/51.rb @@ -0,0 +1,10 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +문자열에서 모음 개수 세기 +=end + +str = "Hello World" + +puts str.count("aeiou") \ No newline at end of file diff --git a/level6/52.rb b/level6/52.rb new file mode 100755 index 0000000..1dd771b --- /dev/null +++ b/level6/52.rb @@ -0,0 +1,10 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +문자열 뒤집기 +=end + +str = "Hello World" + +puts str.reverse \ No newline at end of file diff --git a/level6/53.rb b/level6/53.rb new file mode 100755 index 0000000..1b09d48 --- /dev/null +++ b/level6/53.rb @@ -0,0 +1,10 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +특정 문자 제거 +=end + +str = "Hello World" + +puts str.delete("aeiou") diff --git a/level6/54.rb b/level6/54.rb new file mode 100755 index 0000000..623ee38 --- /dev/null +++ b/level6/54.rb @@ -0,0 +1,10 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +공백 기준으로 단어 분리 +=end + +str = "Hello World" + +puts str.split diff --git a/level6/55.rb b/level6/55.rb new file mode 100755 index 0000000..1c595b3 --- /dev/null +++ b/level6/55.rb @@ -0,0 +1,10 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +단어 수 세기 +=end + +str = "Hello World" + +puts str.split.count diff --git a/level6/56.rb b/level6/56.rb new file mode 100755 index 0000000..3279cdc --- /dev/null +++ b/level6/56.rb @@ -0,0 +1,10 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +문자열이 회문인지 검사 +=end + +str = "Hello World" + +puts str == str.reverse diff --git a/level6/57.rb b/level6/57.rb new file mode 100755 index 0000000..01b2f4d --- /dev/null +++ b/level6/57.rb @@ -0,0 +1,14 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +이메일 형식 간단 검증 +=end + +str = "hello@examples.com" + +puts str =~ /\A\S+@\S+\.\S+\z/ ? "Valid" : "Invalid" + +# --- + +puts str.match?(/\A\S+@\S+\.\S+\z/) ? "Valid" : "Invalid" \ No newline at end of file diff --git a/level6/58.rb b/level6/58.rb new file mode 100755 index 0000000..574729d --- /dev/null +++ b/level6/58.rb @@ -0,0 +1,10 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +파일명에서 확장자 추출 +=end + +str = "hello.txt" + +puts File.extname(str) diff --git a/level6/59.rb b/level6/59.rb new file mode 100755 index 0000000..d359aef --- /dev/null +++ b/level6/59.rb @@ -0,0 +1,19 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +snake_case → camelCase 변환 +=end + +str = "hello_world" + +puts str.split("_") + .map { |s| s.capitalize} + .join + +# --- + +x = str.gsub(/_[a-z]/) do |s| + s.to_s.delete("_").upcase +end +puts x \ No newline at end of file diff --git a/level6/60.rb b/level6/60.rb new file mode 100755 index 0000000..3590d4c --- /dev/null +++ b/level6/60.rb @@ -0,0 +1,13 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +문자열 압축 (aaabb → a3b2) +=end + +str = "hello world" + + +puts str.chars + .chunk{|c| c.itself} + .map{|c,a| "#{c}#{a.size}"}.join \ No newline at end of file diff --git a/level7/61.rb b/level7/61.rb new file mode 100755 index 0000000..ba82ad5 --- /dev/null +++ b/level7/61.rb @@ -0,0 +1,19 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +Person 클래스 정의 (이름, 나이) +=end + +class Person + attr_accessor :name, :age + + def initialize(name, age) + @name = name + @age = age + end +end + +charlie = Person.new("Charlie", 13) +p charlie +puts charlie.name \ No newline at end of file diff --git a/level7/62.rb b/level7/62.rb new file mode 100755 index 0000000..2ee9ad8 --- /dev/null +++ b/level7/62.rb @@ -0,0 +1,22 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +인스턴스 메서드 작성 +=end + +class Person + attr_accessor :name, :age + + def initialize(name, age) + @name = name + @age = age + end + + def hello + puts "Hello, I'm #{name}!" + end +end + +charlie = Person.new("Charlie", 13) +puts charlie.hello \ No newline at end of file diff --git a/level7/63.rb b/level7/63.rb new file mode 100755 index 0000000..a26033c --- /dev/null +++ b/level7/63.rb @@ -0,0 +1,19 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +생성자(initialize) 사용 +=end + +class Person + attr_accessor :name, :age + + def initialize(name, age) + @name = name + @age = age + end + +end + +charlie = Person.new("Charlie", 13) +puts charlie.hello \ No newline at end of file diff --git a/level7/64.rb b/level7/64.rb new file mode 100755 index 0000000..90f8a35 --- /dev/null +++ b/level7/64.rb @@ -0,0 +1,19 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +접근자(attr_accessor) 사용 +=end + +class Person + attr_accessor :name, :age + + def initialize(name, age) + @name = name + @age = age + end + +end + +charlie = Person.new("Charlie", 13) +puts charlie.hello \ No newline at end of file diff --git a/level7/65.rb b/level7/65.rb new file mode 100755 index 0000000..da00881 --- /dev/null +++ b/level7/65.rb @@ -0,0 +1,23 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +나이 증가 메서드 +=end + +class Person + attr_accessor :name, :age + + def initialize(name, age) + @name = name + @age = age + end + + def grow_up + @age += 1 + end +end + +charlie = Person.new("Charlie", 13) +charlie.grow_up +p charlie \ No newline at end of file diff --git a/level7/66.rb b/level7/66.rb new file mode 100755 index 0000000..18ba90f --- /dev/null +++ b/level7/66.rb @@ -0,0 +1,25 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +여러 Person 객체를 배열로 관리 +=end + +class Person + attr_accessor :name, :age + + def initialize(name, age) + @name = name + @age = age + end + + def grow_up + @age += 1 + end +end + +people = [ + Person.new("Charlie", 13), + Person.new("Steve", 48), + Person.new("Anne", 34) +] \ No newline at end of file diff --git a/level7/67.rb b/level7/67.rb new file mode 100755 index 0000000..b4b509e --- /dev/null +++ b/level7/67.rb @@ -0,0 +1,24 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +클래스 메서드 작성 +=end + +class Person + attr_accessor :name, :age + + def initialize(name, age) + @name = name + @age = age + end + + def self.species + "Homo Sapiens" + end + +end + +puts Person.species +charlie = Person.new("Charlie", 13) +puts charlie.class.species \ No newline at end of file diff --git a/level7/68.rb b/level7/68.rb new file mode 100755 index 0000000..8bd7618 --- /dev/null +++ b/level7/68.rb @@ -0,0 +1,24 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +간단한 BankAccount 클래스 +=end + +class BankAccount + def initialize(balance) + @balance = balance + end + + def deposit(amount) + @balance += amount + end + + def withdraw(amount) + @balance -= amount + end + + def get_balance + @balance + end +end \ No newline at end of file diff --git a/level7/69.rb b/level7/69.rb new file mode 100755 index 0000000..8bd7618 --- /dev/null +++ b/level7/69.rb @@ -0,0 +1,24 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +간단한 BankAccount 클래스 +=end + +class BankAccount + def initialize(balance) + @balance = balance + end + + def deposit(amount) + @balance += amount + end + + def withdraw(amount) + @balance -= amount + end + + def get_balance + @balance + end +end \ No newline at end of file diff --git a/level7/70.rb b/level7/70.rb new file mode 100755 index 0000000..a286d1f --- /dev/null +++ b/level7/70.rb @@ -0,0 +1,35 @@ +#!/usr/bin/ruby +# -*- coding: utf-8 -*- + +=begin +잔액 부족 예외 처리 +=end + +class BankAccount + def initialize(balance) + @balance = balance + end + + def deposit(amount) + @balance += amount + end + + def withdraw(amount) + begin + raise StandardError if @balance < amount + @balance -= amount + rescue + puts "잔액이 부족합니다." + @balance + end + + end + + def get_balance + @balance + end +end + +account = BankAccount.new(100) +puts account.withdraw(99) +puts account.withdraw(5) \ No newline at end of file