Skip to content

Commit 161b003

Browse files
committed
created new shuffle program to extend Array class; added orange tree script
1 parent 097a212 commit 161b003

File tree

5 files changed

+105
-11
lines changed

5 files changed

+105
-11
lines changed

ToDo.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
1 - Chapter 9: Modern Roman Numerals
22
7 - Chapter 11 --> What is the difference between mixed up and random?
3-
8 - Chapter 12: Section 12.6
43
9 - Chapter 13: Orange Tree
54
10 - Chapter 13: Interactive Baby Dragon

chapter_10_recursion/shuffle.original

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# the point of this exercise is to create a method that takes an array and
2+
# returns the items in the array in in a completely random way
3+
#
4+
# for this program, I am going to be using the rand function, which returns an
5+
# integer greater than or equal to 0 but less than the number passed.
6+
#
7+
# I'm also going to use the Array#delete_at(index) function, which deletes the
8+
# element at the given index, but also returns it
9+
10+
def shuffle(input)
11+
randomized = Array.new
12+
13+
until input.length == 0
14+
# initialize a random integer within the array's # of elements
15+
randomized_index = rand(input.length)
16+
17+
# push the element at that integer to our output array and delete that
18+
# element from our input array
19+
randomized << input.delete_at(randomized_index)
20+
end
21+
22+
return randomized
23+
end

chapter_10_recursion/shuffle.rb

+17-10
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,25 @@
66
#
77
# I'm also going to use the Array#delete_at(index) function, which deletes the
88
# element at the given index, but also returns it
9+
#
10+
# In this method, we are extending the Array class to support our shuffle
11+
# method. Notice the use of the keyword self to refer to the calling object
912

10-
def shuffle(input)
11-
randomized = Array.new
13+
class Array
14+
def shuffle
15+
original_array = Array.new(self)
1216

13-
until input.length == 0
14-
# initialize a random integer within the array's # of elements
15-
randomized_index = rand(input.length)
17+
randomized = Array.new
1618

17-
# push the element at that integer to our output array and delete that
18-
# element from our input array
19-
randomized << input.delete_at(randomized_index)
20-
end
19+
until original_array.length == 0
20+
# initialize a random integer within the array's # of elements
21+
randomized_index = rand(original_array.length)
2122

22-
return randomized
23+
# push the element at that integer to our output array and delete that
24+
# element from our input array
25+
randomized << original_array.delete_at(randomized_index)
26+
end
27+
28+
return randomized
29+
end
2330
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# =============================================================================
2+
# Orange Tree
3+
# =============================================================================
4+
5+
class OrangeTree
6+
def initialize(name=nil)
7+
@height = 0
8+
@age = 0
9+
@name = name
10+
@oranges = 0
11+
end
12+
13+
def height
14+
one_year_passes
15+
return @height
16+
end
17+
18+
def name
19+
one_year_passes
20+
return @name
21+
end
22+
23+
def count_the_oranges
24+
one_year_passes
25+
return @oranges
26+
end
27+
28+
def pick_an_orange
29+
if @oranges >= 1
30+
puts "You have picked an orange."
31+
@oranges -= 1
32+
puts "The tree now has #{@oranges} fruit left."
33+
else
34+
puts "You don't have any oranges to pick."
35+
end
36+
37+
one_year_passes
38+
end
39+
40+
def continue
41+
one_year_passes
42+
end
43+
44+
private
45+
def one_year_passes
46+
@height += 1
47+
@age += 1
48+
49+
if @age >= 13
50+
puts "Your orange tree #{@name} has withered away and died."
51+
exit
52+
elsif @age >= 3
53+
random = 0
54+
loop do
55+
random = rand(@age)
56+
break if random > 0
57+
end
58+
59+
@oranges = random * 2
60+
end
61+
62+
print "\nYour tree has grown. It is now #{@height} feet tall.\nIt is has #{@oranges} fruit.\n"
63+
end
64+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../chapter_10_recursion/shuffle.rb

0 commit comments

Comments
 (0)