Top Level Namespace
Defined Under Namespace
Modules: RubyVersion, StudyRuby
Classes: Array, Bublle_sort, String
Instance Method Summary
collapse
Instance Method Details
#binary_search(arr, target) ⇒ Object
1
2
3
|
# File 'lib/Algoritimos_fase1/binary_search.rb', line 1
def binary_search (arr,target)
arr.bsearch { |x| x >= target} end
|
#counting_sort(arr) ⇒ Object
O algoritimo counting sort é baseado em três arrays
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
|
# File 'lib/Algoritimos_fase1/counting_sort.rb', line 3
def counting_sort(arr)
max_value = 7 count = Array.new(max_value, 0)
arr.each do |element|
count[element - 1] += 1 end
count.each_with_index do |_, i|
next if i == 0 count[i] += count[i - 1] end
array_b = Array.new(0)
(arr.length - 1).downto(0) do |i| array_b[count[arr[i] - 1] - 1] = arr[i] count[arr[i]-1] -= 1 end
array_b
end
|
#deletado(enum) ⇒ Object
23
24
25
26
27
|
# File 'lib/Algoritimos_fase1/Exercise.rb', line 23
def deletado (enum)
numeros = enum.dup numeros.delete_at(2) numeros
end
|
#greedy(recebido, valor) ⇒ Object
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# File 'lib/Algoritimos_fase1/greedy_algorithm.rb', line 2
def greedy (recebido, valor)
moeda = [1, 5, 10, 25, 50]
troco = Array.new(0)
troco_total = recebido - valor
while troco_total > 0
maior_moeda = moeda.select { |moeda| moeda <= troco_total}.max
troco << maior_moeda
troco_total -= maior_moeda
end
troco
end
|
#maximum_sum(array, k) ⇒ Object
1
2
3
4
5
6
7
8
9
10
11
|
# File 'lib/Algoritimos_fase1/exemplo_maximum.rb', line 1
def maximum_sum(array, k)
array.sort!
while k.positive?
array[array.index(array.min)] = -array.min
k -= 1
end
array
end
|
#name(arr) ⇒ Object
1
2
3
|
# File 'lib/Algoritimos_fase1/ordanition_name.rb', line 1
def name (arr)
arr.sort
end
|
#negativos(num) ⇒ Object
9
10
11
12
13
14
15
|
# File 'lib/Algoritimos_fase1/Exercise.rb', line 9
def negativos (num)
new_array = []
num.each_with_index do |_, i | new_array << num[i] * -1
end
new_array
end
|
#ordenation(list) ⇒ Object
5
6
7
|
# File 'lib/Algoritimos_fase1/Exercise.rb', line 5
def ordenation (list)
list.count
end
|
#reversal(arr, start, fim) ⇒ Object
3
4
5
6
7
8
9
|
# File 'lib/Algoritimos_fase1/array_reversal.rb', line 3
def reversal(arr, start, fim)
while start < fim
arr[start],arr[fim] = arr[fim], arr[start]
start += 1
fim -= 1
end
end
|
#rothate(arr, d, n) ⇒ Object
11
12
13
14
15
|
# File 'lib/Algoritimos_fase1/array_reversal.rb', line 11
def rothate(arr ,d, n)
reversal(arr, 0, n - 1)
reversal(arr, 0, d - 1)
reversal(arr, d, n - 1)
end
|
#search(arr, target) ⇒ Object
Este código representa a pesquisa binaria essa pesquisa só funciona quando a lista é ordenada a sua notação é log2n
3
4
5
|
# File 'lib/Algoritimos_fase1/search_binary_teste.rb', line 3
def search (arr)
arr.min
end
|
#verificar_list(numb) ⇒ Object
Essa função verifica se todos os elementos da lista são pares se for imprimi true senão false
17
18
19
20
21
|
# File 'lib/Algoritimos_fase1/Exercise.rb', line 17
def verificar_list (numb) numb.none? do |element|
element.even?
end
end
|