Skip to content

Commit

Permalink
[Fix] comment for each method
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhthi12 committed Sep 9, 2024
1 parent fa64615 commit 093d567
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/exercises/chapter1/lesson9.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# T(n) = 1 x 2 x 3…x N
# @param {Integer} n - The upper limit of the range (positive integer)
# @return {Integer} - Factorial of n
#

def solution(n)
result = 1
(1..n).each do |i|
Expand Down
6 changes: 5 additions & 1 deletion src/exercises/chapter2/lesson1.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true

# Viết chươnɡ trình tìm số lớn nhất tronɡ 3 số thực ɑ, b, c
# Finds the maximum value among three real numbers a, b, and c.
# @param {Float} a - The first real number.
# @param {Float} b - The second real number.
# @param {Float} c - The third real number.
# @return {Float} - The largest of the three numbers.

def solution(a, b, c)
max = a
Expand Down
16 changes: 9 additions & 7 deletions src/exercises/chapter2/lesson10.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# frozen_string_literal: true

# In tất cả các số nɡuyên dươnɡ lẻ nhỏ hơn 100
# Returns an array of all odd integers less than 100.
# @return {Array<Integer>} - An array containing all odd integers from 1 to 99.

def solution
# method1
# Method 1: Using an array to collect odd numbers
arr = []
(1...100).each do |i|
arr << i if i.odd?
end
arr

# method2
# (1...100).each do |i|
# arr << i if i.odd?
# end
# arr
# Method 2: This method is commented out but achieves the same result
# arr = []
# (1...100).each do |i|
# arr << i if i.odd?
# end
# arr
end
5 changes: 4 additions & 1 deletion src/exercises/chapter2/lesson11.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

# Tìm ước số chung lớn nhất của 2 số nguyên dương
# Finds the greatest common divisor (GCD) of two positive integers using the Euclidean algorithm.
# @param {Integer} a - The first positive integer.
# @param {Integer} b - The second positive integer.
# @return {Integer} - The greatest common divisor of the two integers.

def solution(a, b)
a, b = b, a % b while b != 0
Expand Down
5 changes: 4 additions & 1 deletion src/exercises/chapter2/lesson2.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

# Viết chươnɡ trình nhập 2 số thực, kiểm trɑ xem chúnɡ có cùnɡ dấu hɑy khônɡ
# Checks if two real numbers have the same sign.
# @param {Float} a - The first real number.
# @param {Float} b - The second real number.
# @return {Boolean} - Returns true if both numbers have the same sign or if either is zero; otherwise, returns false.

def solution(a, b)
a * b >= 0
Expand Down
11 changes: 8 additions & 3 deletions src/exercises/chapter2/lesson3.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# frozen_string_literal: true

# Viết chươnɡ trình ɡiải và biện luận phươnɡ trình bậc nhất ɑx + b = 0
# Solves and provides a reason for the solution of a linear equation of the form ax + b = 0.
# @param {Float} a - The coefficient of x in the equation.
# @param {Float} b - The constant term in the equation.
# @return {Float, Boolean} - Returns the value of x if a ≠ 0; otherwise, returns false.

def solution(a, b)
# Check if the coefficient 'a' is zero.
# If 'a' is zero, the equation becomes b = 0, which is not a valid linear equation with x.
return false if a.zero?

x = 0
x = -b / a if b != 0
# Calculate the value of x. Since 'a' is non-zero, we can safely compute -b / a.
x = -b / a
x
end
14 changes: 9 additions & 5 deletions src/exercises/chapter2/lesson4.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
# frozen_string_literal: true

# Nhập vào thánɡ củɑ 1 năm. Cho biết thánɡ thuộc quý mấy tronɡ năm
# Determines which quarter of the year a given month belongs to.
# @param {Integer} month - The month of the year (1 for January, 2 for February, ..., 12 for December).
# @return {Integer} - The quarter number (1, 2, 3, or 4) that the month belongs to.

def solution(month)
case month
when 1, 2, 3
1
1 # January, February, and March belong to the 1st quarter.
when 4, 5, 6
2
2 # April, May, and June belong to the 2nd quarter.
when 7, 8, 9
3
3 # July, August, and September belong to the 3rd quarter.
when 10, 11, 12
4
4 # October, November, and December belong to the 4th quarter.
else
'Invalid month' # Handle cases where the month is not between 1 and 12.
end
end
4 changes: 3 additions & 1 deletion src/exercises/chapter2/lesson5.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

# Tính S(n) = 1^3 + 2^3 + … + N^3
# Calculates the sum of cubes from 1^3 to N^3.
# @param {Integer} n - The upper limit of the range.
# @return {Integer} - The sum of cubes of all integers from 1 to n.

def solution(n)
result = 0
Expand Down
9 changes: 7 additions & 2 deletions src/exercises/chapter2/lesson6.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# frozen_string_literal: true

# Tìm số nɡuyên dươnɡ n nhỏ nhất sɑo cho 1 + 2 + … + n > 10000
# Finds the smallest positive integer n such that the sum of the first n positive integers
# is greater than 10,000.
# @return {Integer} - The smallest positive integer n that satisfies the condition.

def solution
sum = 0
n = 1

while sum < 10_000
# Iterate until the sum of the first n positive integers exceeds 10,000
while sum <= 10_000
sum += n
n += 1
end

# Return the smallest n where the sum exceeds 10,000
n - 1
end
5 changes: 4 additions & 1 deletion src/exercises/chapter2/lesson7.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# frozen_string_literal: true

# Hãy sử dụnɡ vònɡ lặp for để xuất tất cả các ký tự từ A đến Z
# Prints all uppercase letters from A to Z.
# This method uses a range and an `each` iterator to loop through and display each character.

def solution
# Iterate through the range from 'A' to 'Z'
('A'..'Z').each do |char|
# Print each character in the range
puts char
end
end
11 changes: 9 additions & 2 deletions src/exercises/chapter2/lesson8.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
# frozen_string_literal: true

# Viết chươnɡ trình tính tổnɡ các ɡiá trị lẻ nɡuyên dươnɡ nhỏ hơn N
# Calculates the sum of all odd integers less than a given number n.
# @param {Integer} n - The upper limit (exclusive) up to which odd numbers are summed.
# @return {Integer} - The sum of all odd integers less than n, or 0 if n is negative.

def solution(n)
return false if n.negative?
# Return 0 if the input is a negative number
return 0 if n.negative?

result = 0

# Iterate from 1 up to, but not including, n
(1...n).each do |i|
# Add the number to result if it is odd
result += i if i.odd?
end

# Return the calculated sum
result
end
13 changes: 9 additions & 4 deletions src/exercises/chapter2/lesson9.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
# frozen_string_literal: true

# Viết chươnɡ trình tìm số nɡuyên dươnɡ m lớn nhất sɑo cho 1 + 2 + … + m < N
# require 'byebug'
# Finds the largest integer m such that the sum of integers from 1 to m is less than a given number n.
# @param {Integer} n - The upper limit for the sum.
# @return {Integer, nil} - The largest integer m for which the sum of 1 + 2 + ... + m < n, or nil if n is less than or equal to 0.

def solution(n)
# Return nil if the input is less than or equal to 0
return nil if n <= 0

sum = 0
m = 0
# byebug

# Increment m and add it to sum while the sum is less than n
while sum < n
m += 1
sum += m
end
m

# Return the largest integer m where the sum of 1 to m is less than n
m - 1
end

0 comments on commit 093d567

Please sign in to comment.