-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextra_recursion_probs.rb
More file actions
222 lines (197 loc) · 4.87 KB
/
extra_recursion_probs.rb
File metadata and controls
222 lines (197 loc) · 4.87 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
require 'byebug'
def range(start, finish)
return [] if finish < start
return [start] if finish == start
# range(start+1, finish) << start
range(start, finish - 1).push(finish)
end
def sum(array)
return 0 if array.empty?
array.first + sum(array[1..-1])
end
def iterative_sum(array)
array.reduce(:+)
end
def exponent(base, power)
return 1 if power.zero?
return base if power == 1
if power.even?
step = exponent(base, power / 2)
step * step
else
step = exponent(base, (power - 1) / 2)
base * step * step
end
end
def exponent2(base, power)
return 1 if power.zero?
base * exponent(base, power -1)
end
def deep_dup(array)
result = []
array.each do |entry|
if entry.is_a?(Array)
result << deep_dup(entry)
else
result << entry
end
end
result
end
def dup2(array)
result = []
array.each do |entry|
result << entry
end
result
end
def deep_dup_ternary(array)
result = []
array.each do |entry|
entry.is_a?(Array) ? result << deep_dup(entry) : result << entry
end
result
end
def iterative_fibonacci(number)
first_num = 1
second_num = 1
third_num = nil
number.times do
third_num = first_num + second_num
first_num, second_num = second_num, third_num
end
third_num
end
HASH = {}
def fibonacci(number)
return 1 if (0..1).include?(number)
fibonacci(number-1) + fibonacci(number-2)
end
def hash_fibonacci(number)
return 1 if (0..1).include?(number)
return HASH[number] if HASH[number]
HASH[number] = hash_fibonacci(number-1) + hash_fibonacci(number-2)
end
def array_subsets(array)
result = []
# return result if array.length.zero?
array.each_with_index do |e,i|
reduced = array[0...i] + array[i+1..-1]
next_subset = array_subsets(reduced)
result << next_subset
result << next_subset.zip([e])
end
result.uniq
end
def bsearch(array, value)
mid = (array.length - 1) / 2
return nil if array.size <= 1 && value != array[0]
if value < array[mid]
bsearch(array[0...mid], value)
elsif value > array[mid]
bsearch(array[mid+1..-1], value)
else
return mid
end
end
def merge_sort(array)
return array if array.length <= 1
mid = (array.length - 1) / 2
first = merge_sort(array[0..mid])
second = merge_sort(array[mid+1..-1])
merge(first,second)
end
def merge(first_array, second_array)
aux = []
until first_array.empty? && second_array.empty?
if first_array.empty?
aux += second_array
break
end
if second_array.empty?
aux += first_array
break
end
if first_array.first <= second_array.first
aux << first_array.shift
else
aux << second_array.shift
end
end
aux
end
def greedy_make_change(amount, array)
coins = array.sort.reverse
greedy_recursive(amount, coins)
end
def greedy_recursive(amount, array, remainder = amount, answer = [])
next_coin = array.find { |e| e <= remainder }
return answer if next_coin.nil?
remainder -= next_coin
answer << next_coin
greedy_recursive(amount, array, remainder, answer)
end
def make_better_change_NOT(amount, array, remainder = amount, answer = [], max = greedy_make_change(amount,array))
return answer if array.all? { |coin| amount < coin }
array.each do |coin|
if coin < remainder
answer << coin
remainder -= coin
make_better_change(amount,array.select{ |e| e >= coin }, remainder, answer)
else
max = answer if answer.count < max.count
end
end
max
end
def find_least_change(amount,array)
make_better_change(amount, array).min_by {|subarr| subarr.length}
end
def make_better_change(amount, array, answer = [], possible_results = [])
unless answer.empty?
return nil if answer.reduce(:+) > amount
return answer if answer.reduce(:+) == amount
end
array.each do |coin|
attempt = make_better_change(amount, array, answer + [coin], possible_results)
next unless attempt
possible_results << attempt if attempt.reduce(:+) == amount
end
possible_results
end
def permutations(array,addon=[],extension = [])
if array.empty?
return extension + [addon]
end
array.each do |val|
extension = permutations(array.reject{|x| x == val},addon+[val],extension)
end
extension
end
def eight_queens_possibilities(current_row, taken_columns, positions)
return [positions] if current_row == 8
all_positions = []
avoid = []
columns_left = (0..7).to_a
taken_columns.reverse.each_with_index do |col,inx|
avoid.push(col,col+inx+1,col-inx-1)
end
columns_left -= avoid
return nil if columns_left.empty?
columns_left.each do |col|
duped_poses = positions.dup
duped_poses << [current_row,col]
x = eight_queens_possibilities(current_row+1,taken_columns+[col],duped_poses)
all_positions += x if x
end
all_positions
end
def subsets(arr)
return [[]] if arr.empty?
result = []
addon = arr.pop
result += subsets(arr)
result + result.map do |subarr|
subarr + [addon]
end
end