Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhthi12 committed Apr 24, 2024
1 parent 27d92c7 commit c20e7d6
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 16 deletions.
Binary file added Exercises.pdf
Binary file not shown.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# fundamental-ruby
# test

## rspec ./test/chater[number]/lesson[number].rb
9 changes: 9 additions & 0 deletions chapter2/lesson1.rb
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
#Bài 1: Viết chươnɡ trình tìm số lớn nhất tronɡ 3 số thực ɑ, b, c

def solution(a, b, c)
max = a

max = b if b > max
max = c if c > max

return max
end
5 changes: 5 additions & 0 deletions chapter2/lesson2.rb
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
#Bài 2: 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ɡ

def solution(a, b)
return true if a * b >= 0
return false
end
9 changes: 9 additions & 0 deletions chapter2/lesson3.rb
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
#Bài 3: Viết chươnɡ trình ɡiải và biện luận phươnɡ trình bậc nhất ɑx + b = 0

def solution(a, b)
return false if a == 0

x = 0
x = -b/a if b != 0

return x
end
31 changes: 24 additions & 7 deletions test/chapter2/lesson1.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
#Bài 1: Tính S(n) = 1 + 2 + 3 + … + n
require_relative '../../chapter2/lesson1'

def solution(n)
sum = 0
for i in 1..n do
sum = sum + i
RSpec.describe 'solution' do
describe 'when all numbers are positive' do
it 'returns the maximun number' do
expect(solution(1, 2, 3)).to eq(3)
expect(solution(4, 2, 1)).to eq(4)
expect(solution(6, 8, 7)).to eq(8)
end
end

describe 'when some numbers are negative' do
it 'returns the maximum number' do
expect(solution(-1, -2, -3)).to eq(-1)
expect(solution(-4, -2, -1)).to eq(-1)
expect(solution(-6, 8, 7)).to eq(8)
end
end

describe 'when all numbers are negative' do
it 'returns the maximum number' do
expect(solution(-1, -2, -3)).to eq(-1)
expect(solution(-4, -2, -1)).to eq(-1)
expect(solution(-6, -8, -7)).to eq(-6)
end
end
return sum
end
1
22 changes: 15 additions & 7 deletions test/chapter2/lesson2.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
#Bài 2: Tính S(n) = 1^2 + 2^2 + … + n^2
require_relative '../../chapter2/lesson2'

def solution(n)
result = 0
for i in 1..n do
result = result + i**2
RSpec.describe 'solution' do
describe 'when both numbers have the same sign' do
it 'returns true' do
expect(solution(3.5, 2.7)).to eq(true)
expect(solution(-3.5, -2.7)).to eq(true)
expect(solution(0, 0)).to eq(true)
end
end

describe 'when both numbers have different signs' do
it 'returns false' do
expect(solution(3.5, -2.7)).to eq(false)
expect(solution(-3.5, 2.7)).to eq(false)
end
end
return result
end
1
24 changes: 23 additions & 1 deletion test/chapter2/lesson3.rb
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
1
require_relative '../../chapter2/lesson3'

RSpec.describe 'solution' do
describe 'when a is 0' do
it 'returns false' do
expect(solution(0, 5)).to eq(false)
end
end

describe 'when b is 0' do
it 'returns 0' do
expect(solution(2, 0)).to eq(0)
end
end

describe 'when a and b are not 0' do
it 'returns the correct value of x' do
expect(solution(2, 4)).to eq(-2)
expect(solution(-3, -9)).to eq(-3)
expect(solution(5, -15)).to eq(3)
end
end
end

0 comments on commit c20e7d6

Please sign in to comment.