-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathmy_solution.rb
147 lines (114 loc) · 4.9 KB
/
my_solution.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# U2.W5: A Nested Array to Model a Boggle Board
# I worked on this challenge by myself.
boggle_board = [["b", "r", "a", "e"],
["i", "o", "d", "t"],
["e", "c", "l", "r"],
["t", "a", "k", "e"]]
# Part 1: Access multiple elements of a nested array
# Pseudocode
# 1. DEFINE a method called create_word which takes two or more parameters.
# One is a nested array called board.
# The rest are coordinates of the array.
# 2. Inside create_word, collect each value of the coordinates and put them together to a string.
# 3. RETURN the string.
# Initial Solution
def create_word(board, *coords)
coords.map { |coord| board[coord.first][coord.last] }.join
end
# Refactored Solution
# I didn't refactor this code since it has only one line and could not think of a way to
# change it.
# DRIVER TESTS GO BELOW THIS LINE
p create_word(boggle_board, [2,1], [1,1], [1,2], [0,3]) === 'code'
p create_word(boggle_board, [0,1], [0,2], [1,2]) === 'rad'
# Reflection
# What parts of your strategy worked? What problems did you face?
# => This code is from the direction given and I just tested the code and see it works, so
# there were no problems.
# What questions did you have while coding? What resources did you find to help you answer them?
# => No question.
# What concepts are you having trouble with, or did you just figure something out? If so, what?
# => This was a veru easy challenge. (If I'm doing this right.)
# Did you learn any new skills or tricks?
# => Not really.
# How confident are you with each of the learning objectives?
# => Pretty confident.
# Which parts of the challenge did you enjoy?
# => Testing the code.
# Which parts of the challenge did you find tedious?
# => Psudocode.
#-------------------------------------------------------------------------------
# Part 2: Write a method that takes a row number and returns all the elements in the row.
# Pseudocode
# 1. DEFINE a method called get_row which takes one patameter 'row'.
# 2. Inside get_row, return boggle_board[row]
# Initial Solution
def get_row(board, row)
board[row]
end
# Refactored Solution
# I didn't refactor this code since it has only one line and could not think of a way to
# change it.
# DRIVER TESTS GO BELOW THIS LINE
p get_row(boggle_board, 0) === ["b", "r", "a", "e"]
p get_row(boggle_board, 1) === ["i", "o", "d", "t"]
p get_row(boggle_board, 2) === ["e", "c", "l", "r"]
p get_row(boggle_board, 3) === ["t", "a", "k", "e"]
# Reflection
# What parts of your strategy worked? What problems did you face?
# => Just simply returning what the mothod is supposed to return.
# What questions did you have while coding? What resources did you find to help you answer them?
# => No question.
# What concepts are you having trouble with, or did you just figure something out? If so, what?
# => This was a veru easy challenge. (If I'm doing this right.)
# Did you learn any new skills or tricks?
# => Not really.
# How confident are you with each of the learning objectives?
# => Pretty confident.
# Which parts of the challenge did you enjoy?
# => Writing and testing the code.
# Which parts of the challenge did you find tedious?
# => Psudocode.
#-------------------------------------------------------------------------------
# Part 3: Now write a method that takes a column number and returns all the elements in the column.
# Pseudocode
# 1. DEFINE a method called get_column which accepts two perameters
# 2. CREATE a new array witch includes all of the values of that paticular column
# 3. JOIN the values togetger and make it into a string
# 4. RETURN the string
# Initial Solution
def get_column(board, column)
[ board[0][column], board[1][column], board[2][column], board[3][column] ].join
end
# Refactored Solution
def get_column(board, column)
i = 0
word = []
until i >= board.length
word << board[i][column]
i += 1
end
word.join
end
# DRIVER TESTS GO BELOW THIS LINE
p get_column(boggle_board, 0) === "biet"
p get_column(boggle_board, 1) === "roca"
p get_column(boggle_board, 2) === "adlk"
p get_column(boggle_board, 3) === "etre"
# Reflection
# What parts of your strategy worked? What problems did you face?
# => Not so much of problem but I wasn't really sure if I was supposed to define a mthod that
# worls specifically with boggle_board array or just any nested arrays so I refactored my first
# code and defined a general method .
# What questions did you have while coding? What resources did you find to help you answer them?
# => No question.
# What concepts are you having trouble with, or did you just figure something out? If so, what?
# => This was a veru easy challenge. (If I'm doing this right.)
# Did you learn any new skills or tricks?
# => Not really.
# How confident are you with each of the learning objectives?
# => Pretty confident.
# Which parts of the challenge did you enjoy?
# => Writing and testing the code.
# Which parts of the challenge did you find tedious?
# => Psudocode.