code for chapter 3 in ruby
This commit is contained in:
12
03_recursion/ruby/01_countdown.rb
Normal file
12
03_recursion/ruby/01_countdown.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
def countdown(i)
|
||||
puts i
|
||||
# base case
|
||||
if i <= 0
|
||||
return
|
||||
# recursive case
|
||||
else
|
||||
countdown(i - 1)
|
||||
end
|
||||
end
|
||||
|
||||
countdown(5)
|
||||
16
03_recursion/ruby/02_greet.rb
Normal file
16
03_recursion/ruby/02_greet.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
def greet2(name)
|
||||
puts "how are you, #{name}?"
|
||||
end
|
||||
|
||||
def bye
|
||||
print "ok bye!"
|
||||
end
|
||||
|
||||
def greet(name)
|
||||
puts "hello, #{name}!"
|
||||
greet2(name)
|
||||
puts "getting ready to say bye..."
|
||||
bye
|
||||
end
|
||||
|
||||
greet("adit")
|
||||
9
03_recursion/ruby/03_factorial.rb
Normal file
9
03_recursion/ruby/03_factorial.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
def fact(x)
|
||||
if x == 1
|
||||
1
|
||||
else
|
||||
x * fact(x - 1)
|
||||
end
|
||||
end
|
||||
|
||||
puts fact(5)
|
||||
Reference in New Issue
Block a user