calculate fibonacci sequence position from user inserted value - ruby
Below is my full program's code:
GradeAvg = [[59, 'F'], [69, 'D'], [79, 'C'], [89, 'B'], [100, 'A']]
def letter_grade
num = number_grade
_, letter = GradeAvg.find { |n, _| num <= n }
[num, letter]
end
def number_grade
loop do
puts 'Please insert a number between 1 and 100.'
num = gets.to_i
break(num) if (1..100).cover?(num)
end
end
def fib(n)
return n if n < 2
fib(n-1) + fib(n-2)
end
10.times { print letter_grade; (num).each { |n| puts fib(n) }; puts }
It is failing with the below error:
undefined local variable or method `num' for main:Object (NameError)
Why can't I apply my fibonacci sequence calculation on my acceptable user inserted value in variable num?
You're trying to access num which is assigned in letter_grade. You must use the return value of the function call (ie num, letter = letter_grade) to read the result.
It should also be noted that Fixnum#each (ie (num).each) is not a method. 10.times { ... } already makes a loop though, so I think this was just an oversight on your part.
See the bolded section for changes. I also changed your fibonacci function to calculate in linear time (instead of exponential time)
GradeAvg = [[59, 'F'], [69, 'D'], [79, 'C'], [89, 'B'], [100, 'A']]
def number_grade
loop do
print "Please insert a number between 1 and 100."
num = gets.to_i
break(num) if (1..100).cover?(num)
end
end
def letter_grade
num = number_grade
_, letter = GradeAvg.find { |n, _| num <= n }
[num, letter]
end
def fib (n, a = 0, b = 1)
if n == 0 then
a
else
fib n - 1, b, a + b
end
end
10.times do
num, letter = letter_grade
puts letter
puts (fib num)
end
Example program execution
# Please insert a number between 1 and 100. 95
# A
# 31940434634990099905
# Please insert a number between 1 and 100. 87
# B
# 679891637638612258
# Please insert a number between 1 and 100. 77
# C
# 5527939700884757
# Please insert a number between 1 and 100. 66
# D
# 27777890035288
# Please insert a number between 1 and 100. 55
# F
# 139583862445
# Please insert a number between 1 and 100. 10
# F
# 55
# Please insert a number between 1 and 100. ...
# ...
Try like this:
GradeAvg = [[59, 'F'], [69, 'D'], [79, 'C'], [89, 'B'], [100, 'A']]
def letter_grade
#num = number_grade
_, letter = GradeAvg.find { |n, _| #num <= n }
[#num, letter]
end
def number_grade
loop do
puts 'Please insert a number between 1 and 100.'
#num = gets.to_i
break(#num) if (1..100).cover?(#num)
end
end
def fib(n)
return n if n < 2
fib(n-1) + fib(n-2)
end
10.times { print letter_grade; puts fib(#num)}
Related
How do I fix a problem to call a function in Ruby?
I'm trying to use some ruby code that I've found in Github. I've downloaded the code and did the necessary imports the "requires" and tried to run it as it is described in the readme file on github repository. The code is the following: In the file pcset_test.rb the code is the following: require './pcset.rb' require 'test/unit' # # When possible, test cases are adapted from # Introduction to Post-Tonal Theory by Joseph N. Straus, # unless obvious or otherwise noted. # class PCSetTest < Test::Unit::TestCase def test_init #assert_raise(ArgumentError) {PCSet.new []} assert_raise(ArgumentError) {PCSet.new [1, 2, 3, 'string']} assert_raise(ArgumentError) {PCSet.new "string"} assert_raise(ArgumentError) {PCSet.new [1, 2, 3.6, 4]} assert_equal([0, 1, 2, 9], PCSet.new([0, 1, 2, 33, 13]).pitches) assert_equal([3, 2, 1, 11, 10, 0], PCSet.new_from_string('321bac').pitches) assert_equal([0,2,4,5,7,11,9], PCSet.new([12,2,4,5,7,11,9]).pitches) assert_nothing_raised() {PCSet.new []} end def test_inversion end def test_transposition end def test_multiplication end # # set normal prime forte # # 0,2,4,7,8,11 7,8,11,0,2,4 0,1,4,5,7,9 6-31 # 0,1,2,4,5,7,11 11,0,1,2,4,5,7 0,1,2,3,5,6,8 7-Z36 # 0,1,3,5,6,7,9,10,11 5,6,7,9,10,11,0,1,3 0,1,2,3,4,6,7,8,10 9-8 # def test_normal_form testPC = PCSet.new [0,4,8,9,11] assert_kind_of(PCSet, testPC.normal_form) assert_equal([8,9,11,0,4], testPC.normal_form.pitches) assert_equal([10,1,4,6], PCSet.new([1,6,4,10]).normal_form.pitches) assert_equal([2,4,8,10], PCSet.new([10,8,4,2]).normal_form.pitches) assert_equal([7,8,11,0,2,4], PCSet.new([0,2,4,7,8,11]).normal_form.pitches) assert_equal([11,0,1,2,4,5,7], PCSet.new([0,1,2,4,5,7,11]).normal_form.pitches) assert_equal([5,6,7,9,10,11,0,1,3], PCSet.new([0,1,3,5,6,7,9,10,11]).normal_form.pitches) end def test_prime_form assert_equal([0,1,2,6], PCSet.new([5,6,1,7]).prime.pitches) assert_equal([0,1,4], PCSet.new([2,5,6]).prime.pitches) assert_equal([0,1,4,5,7,9], PCSet.new([0,2,4,7,8,11]).prime.pitches) assert_equal([0,1,2,3,5,6,8], PCSet.new([0,1,2,4,5,7,11]).prime.pitches) assert_equal([0,1,2,3,4,6,7,8,10], PCSet.new([0,1,3,5,6,7,9,10,11]).prime.pitches) end def test_set_class testPcs = PCSet.new([2,5,6]) testPrime = testPcs.prime assert_equal([ [2,5,6], [3,6,7], [4,7,8], [5,8,9], [6,9,10], [7,10,11], [8,11,0],[9,0,1], [10,1,2],[11,2,3],[0,3,4], [1,4,5], [6,7,10],[7,8,11],[8,9,0], [9,10,1],[10,11,2],[11,0,3], [0,1,4], [1,2,5], [2,3,6], [3,4,7], [4,5,8], [5,6,9] ].sort, PCSet.new([2,5,6]).set_class.map{|x| x.pitches}) assert_equal(testPcs.set_class.map{|x| x.pitches}, testPrime.set_class.map{|x| x.pitches}) end def test_interval_vector assert_equal([2,1,2,1,0,0], PCSet.new([0,1,3,4]).interval_vector) assert_equal([2,5,4,3,6,1], PCSet.new([0,1,3,5,6,8,10]).interval_vector) assert_equal([0,6,0,6,0,3], PCSet.new([0,2,4,6,8,10]).interval_vector) end def test_complement assert_equal([6,7,8,9,10,11], PCSet.new([0,1,2,3,4,5]).complement.pitches) assert_equal([3,4,5], PCSet.new([0,1,2], 6).complement.pitches) end # # Test values from (Morris 1991), pages 105-111 # Citation: # Morris. Class Notes for Atonal Music Theory # Lebanon, NH. Frog Peak Music, 1991. # def test_invariance_vector assert_equal([1,0,0,0,5,6,5,5],PCSet.new([0,2,5]).invariance_vector) assert_equal([2,2,2,2,6,6,6,6],PCSet.new([0,1,6,7]).invariance_vector) assert_equal([6,6,6,6,6,6,6,6],PCSet.new([0,2,4,6,8,10]).invariance_vector) assert_equal([1,0,0,0,0,0,0,0],PCSet.new([0,1,2,3,4,5,8]).invariance_vector) assert_equal([1,0,0,1,0,0,0,0],PCSet.new([0,1,2,3,5,6,8]).invariance_vector) assert_equal([12,12,12,12,0,0,0,0],PCSet.new([0,1,2,3,4,5,6,7,8,9,10,11]).invariance_vector) end # # Test values from (Huron 1994). Huron rounds, thus the 0.01 margin of error. # Citation: # Huron. Interval-Class Content in Equally Tempered Pitch-Class Sets: # Common Scales Exhibit Optimum Tonal Consonance. # Music Perception (1994) vol. 11 (3) pp. 289-305 # def test_huron h1 = PCSet.new([0,1,2,3,4,5,6,7,8,9,10,11]).huron assert_in_delta(-0.2, h1[0], 0.01) assert_in_delta(0.21, h1[1], 0.01) h2 = PCSet.new([0,2,4,5,7,9,11]).huron assert_in_delta(4.76, h2[0], 0.01) assert_in_delta(0.62, h2[1], 0.01) end def test_coherence end end And in the file pcset.rb the folloing code: # # => PCSet Class for Ruby # => Beau Sievers # => Hanover, Fall 2008. # # # TODO: Make this a module to avoid namespace collisions. # Lilypond and MusicXML output # include Math def choose(n, k) return [[]] if n.nil? || n.empty? && k == 0 return [] if n.nil? || n.empty? && k > 0 return [[]] if n.size > 0 && k == 0 c2 = n.clone c2.pop new_element = n.clone.pop choose(c2, k) + append_all(choose(c2, k-1), new_element) end def append_all(lists, element) lists.map { |l| l << element } end def array_to_binary(array) array.inject(0) {|sum, n| sum + 2**n} end # the following method is horrifically inelegant # but avoids monkey-patching. # TODO: do this right, incl. error checking def pearsons(x, y) if !x.is_a?(Array) || !y.is_a?(Array) then raise StandardError, "x and y must be arrays", caller end if x.size != y.size then raise StandardError, "x and y must be same size", caller end sum_x = x.inject(0) {|sum, n| sum + n} sum_y = y.inject(0) {|sum, n| sum + n} sum_square_x = x.inject(0) {|sum, n| sum + n * n} sum_square_y = y.inject(0) {|sum, n| sum + n * n} xy = [] x.zip(y) {|a, b| xy.push(a * b)} sum_xy = xy.inject(0) {|sum, n| sum + n} num = sum_xy - ((sum_x * sum_y)/x.size) den = Math.sqrt((sum_square_x - ((sum_x*sum_x)/x.size)) * (sum_square_y - ((sum_y*sum_y)/x.size))) (num/den) end class PCSet include Comparable attr_reader :pitches, :base, :input def initialize(pcarray, base = 12) if pcarray.instance_of?(Array) && pcarray.all?{|pc| pc.instance_of?(Fixnum)} #base, #input = base, pcarray #pitches = pcarray.map{ |x| x % #base }.uniq else raise ArgumentError, "Improperly formatted PC array", caller end end def PCSet.new_from_string(pcstring, base = 12) if base > 36 then raise StandardError, "Use PCSet.new to create pcsets with a base larger than 36", caller end pcarray = [] pcstring.downcase.split(//).each do |c| if c <= 'z' and c >= '0' then pcarray.push(c.to_i(36)) end end PCSet.new pcarray, base end def <=>(pcs) #pitches <=> pcs.pitches end def [](index) #pitches[index] end # Intersection def &(other) PCSet.new #pitches & other.pitches end # Union def |(other) PCSet.new #pitches | other.pitches end def inspect #pitches.inspect end def length #pitches.length end def invert(axis = 0) PCSet.new #pitches.map {|x| (axis-x) % #base} end def invert!(axis = 0) #pitches.map! {|x| (axis-x) % #base} end def transpose(interval) PCSet.new #pitches.map {|x| (x + interval) % #base} end def transpose!(interval) #pitches.map! {|x| (x + interval) % #base} end def multiply(m = 5) PCSet.new #pitches.map {|x| (x * m) % #base} end def multiply!(m = 5) #pitches.map! {|x| (x * m) % #base} end def zero transpose(-1 * #pitches[0]) end def zero! transpose!(-1 * #pitches[0]) end def transpositions (0..(#base-1)).to_a.map{|x| #pitches.map {|y| (y + x) % #base}}.sort.map {|x| PCSet.new x} end def transpositions_and_inversions(axis = 0) transpositions + invert(axis).transpositions end # # Normal form after Straus. Morris and AthenaCL do this differently. # def normal_form tempar = #pitches.sort arar = [] # [[1,4,7,8,10],[4,7,8,10,1], etc.] get each cyclic variation tempar.each {arar.push PCSet.new(tempar.unshift(tempar.pop))} most_left_compact(arar) end def normal_form! #pitches = normal_form.pitches end def is_normal_form? self.pitches == self.normal_form.pitches end def set_class transpositions_and_inversions.map{|pcs| pcs.normal_form}.sort end def prime most_left_compact([normal_form.zero, invert.normal_form.zero]) end def prime! self.pitches = self.prime.pitches end def is_prime? self.pitches == self.prime.pitches end def complement new_pitches = [] #base.times do |p| if !#pitches.include? p then new_pitches.push p end end PCSet.new new_pitches end def full_interval_vector pairs = choose(#pitches, 2) # choose every pc pair intervals = pairs.map {|x| (x[1] - x[0]) % #base} # calculate every interval i_vector = Array.new(#base-1).fill(0) intervals.each {|x| i_vector[x-1] += 1} # count the intervals i_vector end def interval_vector i_vector = full_interval_vector (0..((#base-1)/2)-1).each {|x| i_vector[x] += i_vector.pop} i_vector end # # Morris's invariance vector # def invariance_vector(m = 5) t = transpositions.map!{|pcs| self & pcs} ti = invert.transpositions.map!{|pcs| self & pcs} tm = multiply(m).transpositions.map!{|pcs| self & pcs} tmi = invert.multiply(m).transpositions.map!{|pcs| self & pcs} tc = complement.transpositions.map!{|pcs| self & pcs} tic = complement.invert.transpositions.map!{|pcs| self & pcs} tmc = complement.multiply(m).transpositions.map!{|pcs| self & pcs} tmic = complement.invert.multiply(m).transpositions.map!{|pcs| self & pcs} [t, ti, tm, tmi, tc, tic, tmc, tmic].map{|x| x.reject{|pcs| pcs.pitches != #pitches}.length} end # Huron's aggregate dyadic consonance measure. # Huron. Interval-Class Content in Equally Tempered Pitch-Class Sets: # Common Scales Exhibit Optimum Tonal Consonance. # Music Perception (1994) vol. 11 (3) pp. 289-305 def huron if #base != 12 then raise StandardError, "PCSet.huron only makes sense for mod 12 pcsets", caller end # m2/M7 M2/m7 m3/M6 M3/m6 P4/P5 A4/d5 huron_table = [-1.428, -0.582, 0.594, 0.386, 1.240, -0.453] interval_consonance = [] interval_vector.zip(huron_table) {|x, y| interval_consonance.push(x * y) } aggregate_dyadic_consonance = interval_consonance.inject {|sum, n| sum + n} [aggregate_dyadic_consonance, pearsons(interval_vector, huron_table)] end # # Balzano's vector of relations. Citation for all Balzano methods: # # Balzano. "The Pitch Set as a Level of Description for Studying Musical # Pitch Perception" in Music, Mind, and Brain ed. Clynes. Plenum Press. 1982. # def vector_of_relations (0..length-1).to_a.map do |i| (0..length-1).to_a.map do |j| (#pitches[(i + j) % length] - #pitches[i]) % #base end end end # # Checks if the set satisfies Balzano's uniqueness. # def is_unique? vector_of_relations.uniq.size == vector_of_relations.size end # # Checks if the set satisfies Balzano's scalestep-semitone coherence. # For all s[i] and s[i1]: # j < k => v[i][j] < v[i1][k] # Where j and k are scalestep-counting indices. # And unless v[i][j] == 6 (a tritone), in which case the strict inequality is relaxed. # def is_coherent? v = vector_of_relations truth_array = [] all_pair_indices = choose((0..length-1).to_a, 2) all_pair_indices.each do |i, i1| all_pair_indices.each do |j, k| if v[i][j] == 6 truth_array.push(v[i][j] <= v[i1][k]) else truth_array.push(v[i][j] < v[i1][k]) end if v[i1][j] == 6 truth_array.push(v[i1][j] <= v[i][k]) else truth_array.push(v[i1][j] < v[i][k]) end end end !truth_array.include?(false) end # # Strict Balzano coherence, no inequality relaxation for tritones. # def is_strictly_coherent? v = vector_of_relations truth_array = [] all_pair_indices = choose((0..length-1).to_a, 2) all_pair_indices.each do |i, i1| all_pair_indices.each do |j, k| truth_array.push(v[i][j] < v[i1][k]) truth_array.push(v[i1][j] < v[i][k]) end end !truth_array.include?(false) end def notes(middle_c = 0) noteArray = ['C','C#','D','D#','E','F','F#','G','G#','A','A#','B'] if #base != 12 then raise StandardError, "PCSet.notes only makes sense for mod 12 pcsets", caller end out_string = String.new transpose(-middle_c).pitches.each do |p| out_string += noteArray[p] + ", " end out_string.chop.chop end def info print "modulo: #{#base}\n" print "raw input: #{#input.inspect}\n" print "pitch set: #{#pitches.inspect}\n" print "notes: #{notes}\n" print "normal: #{normal_form.inspect}\n" print "prime: #{prime.inspect}\n" print "interval vector: #{interval_vector.inspect}\n" print "invariance vector: #{invariance_vector.inspect}\n" print "huron ADC: #{huron[0]} pearsons: #{huron[1]}\n" print "balzano coherence: " if is_strictly_coherent? print "strictly coherent\n" elsif is_coherent? print "coherent\n" else print "false\n" end end # def lilypond # # end # # def musicXML # # end ############################################################################### private # # Convert every pitch array to a binary representation, e.g.: # [0,2,4,8,10] -> 010100010101 # 2^n: BA9876543210 # The smallest binary number is the most left-compact. # def most_left_compact(pcset_array) if !pcset_array.all? {|pcs| pcs.length == pcset_array[0].length} raise ArgumentError, "PCSet.most_left_compact: All PCSets must be of same cardinality", caller end zeroed_pitch_arrays = pcset_array.map {|pcs| pcs.zero.pitches} binaries = zeroed_pitch_arrays.map {|array| array_to_binary(array)} winners = [] binaries.each_with_index do |num, i| if num == binaries.min then winners.push(pcset_array[i]) end end winners.sort[0] end end I'm calling them as follows: > my_pcset = PCSet.new([0,2,4,6,8,10]) > my_pcset2 = PCSet.new([1,5,9]) It shoud return: > my_pcset = PCSet.new([0,2,4,6,8,10]) => [0, 2, 4, 6, 8, 10] > my_pcset2 = PCSet.new([1,5,9]) => [1, 5, 9] But is returning nothing. The code is available on github Thanks
Try this in terminal: irb -r ./path_to_directory/pcset.rb and then initialize the objects.
I think the documentation for the repo is bad as it does not explain how you should be running this. The result of my_pcset = PCSet.new([0,2,4,6,8,10]) should set my_pcset to an instance of a PCSet not an array, so these lines from the README file are confusing at best. 3. How to use it Make new PCSets: my_pcset = PCSet.new([0,2,4,6,8,10]) => [0, 2, 4, 6, 8, 10] my_pcset2 = PCSet.new([1,5,9]) => [1, 5, 9] Looking at the code, I see inspect has been delegated to #pitches def inspect #pitches.inspect end I think if you inspect my_pcset you will get the expected result. my_pcset = PCSet.new([0,2,4,6,8,10]) p my_pcset # will print [0, 2, 4, 6, 8, 10] or `my_pcset.inspect` will return what you are expecting.
How to optimize code - it works, but I know I'm missing much learning
The exercise I'm working on asks "Write a method, coprime?(num_1, num_2), that accepts two numbers as args. The method should return true if the only common divisor between the two numbers is 1." I've written a method to complete the task, first by finding all the factors then sorting them and looking for duplicates. But I'm looking for suggestions on areas I should consider to optimize it. The code works, but it is just not clean. def factors(num) return (1..num).select { |n| num % n == 0} end def coprime?(num_1, num_2) num_1_factors = factors(num_1) num_2_factors = factors(num_2) all_factors = num_1_factors + num_2_factors new = all_factors.sort dups = 0 new.each_index do |i| dups += 1 if new[i] == new[i+1] end if dups > 1 false else true end end p coprime?(25, 12) # => true p coprime?(7, 11) # => true p coprime?(30, 9) # => false p coprime?(6, 24) # => false
You could use Euclid's algorithm to find the GCD, then check whether it's 1. def gcd a, b while a % b != 0 a, b = b, a % b end return b end def coprime? a, b gcd(a, b) == 1 end p coprime?(25, 12) # => true p coprime?(7, 11) # => true p coprime?(30, 9) # => false p coprime?(6, 24) # => false```
You can just use Integer#gcd: def coprime?(num_1, num_2) num_1.gcd(num_2) == 1 end
You don't need to compare all the factors, just the prime ones. Ruby does come with a Prime class require 'prime' def prime_numbers(num_1, num_2) Prime.each([num_1, num_2].max / 2).map(&:itself) end def factors(num, prime_numbers) prime_numbers.select {|n| num % n == 0} end def coprime?(num_1, num_2) prime_numbers = prime_numbers(num_1, num_2) # & returns the intersection of 2 arrays (https://stackoverflow.com/a/5678143) (factors(num_1, prime_numbers) & factors(num_2, prime_numbers)).length == 0 end
Code wars: Flap Display with while loops
I'm trying to work through a level 5 kata by using while loops. Essentially the problem is to turn each letter rotors[n] number of times and then move on to the next rotors number until you get an output word. flap_display(["CAT"],[1,13,27]) should output ["DOG"] Here's what I have so far def flap_display(lines, rotors) stuff = "ABCDEFGHIJKLMNOPQRSTUVWXYZ?!##&()|<>.:=-+*/0123456789" i = 0 j = 0 new_word = lines while i < rotors.length while j < new_word[0].length new_word[0][j] = stuff[stuff.index(new_word[0][j]) + rotors[i]] j += 1 end i += 1 j = 0 end new_word end This technically traverses the stuff string and assigns the right letters. However it fails two important things: it does not skip each letter when it rotates to the correct position (C should stop rotating when it hits D, A when it hits O etc) and it does not account for reaching the end of the stuff list and eventually returns a nil value for stuff[stuff.index(new_word[0][j]) + rotors[i]]. How can I fix these two problems using basic loops and enumerables or maybe a hash?
A fuller statement of the problem is given here. This is one Ruby-like way it could be done. FLAPS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ?!##&()|<>.:=-+*/0123456789" NBR_FLAPS = FLAPS.size def flap_display(str, rot) rot_cum = rot.each_with_object([]) { |n,a| a << a.last.to_i + n } str.gsub(/./) { |c| FLAPS[(c.ord + rot_cum.shift - 65) % NBR_FLAPS] } end flap_display("CAT", [1,13,27]) #=> "DOG" flap_display("DOG", [-1,-13,-27]) #=> "CAT" flap_display("CAT", [5,37,24]) #=> "H*&" 'A'.ord #=> 65 and rot_cum contains the cumulative values of rot: arr = [1, 13, 27] rot_cum = arr.each_with_object([]) { |n,a| a << a.last.to_i + n } #=> [1, 14, 41] I've written a.last.to_i rather than a.last to deal with the case where a is empty, so a.last #=> nil, meaning a.last.to_i => nil.to_i => 0. See NilClass#to_i. Those opposed to such trickery could write: rot_cum = arr.drop(1).each_with_object([arr.first]) { |n,a| a << a.last + n }
How can I pass in a block to my "bubble sort" method?
The below code is my newbie take on a bubble sort method. #For each element in the list, look at that element and the element #directly to it's right. Swap these two elements so they are in #ascending order. def bubble_sort (array) a = 0 b = 1 until (array.each_cons(2).all? { |a, b| (a <=> b) <= 0}) == true do sort = lambda {array[a] <=> array[b]} sort_call = sort.call loop do case sort_call when -1 #don't swap a += 1 b += 1 break when 0 #don't swap a += 1 b += 1 break when 1 #swap array.insert(a,array.delete_at(b)) a += 1 b += 1 break else #end of array, return to start a = 0 b = 1 break end end end puts array.inspect end array = [4, 2, 5, 6, 3, 23, 5546, 234, 234, 6] bubble_sort(array) I want to be able to alter this method so that it takes a block of code as an argument and uses this to determine how it sorts. For example: array = ["hello", "my", "name", "is", "daniel"] bubble_sort(array) {array[#a].length <=> array[#b].length} (When I've tried this I've turned a and b into instance variables throughout the code.) I have tried using yield but I get undefined method 'length' for nil:NilClass once the end of the array is reached. I've tried adding in things such as if array[#b+1] == nil #a = 0 #b = 1 end This helps but I still end up with weird problems like infinite loops or not being able to sort more than certain amount of elements. Long story short, I have been at this for hours. Is there a simple way to do what I want to do? Thanks.
The way you're calling your lambda is a bit odd. It's actually completely unnecessary. I refactored your code and cleaned up a bit of the redundancy. The following works for me: def sorted?(arr) arr.each_cons(2).all? { |a, b| (a <=> b) <= 0 } end def bubble_sort (arr) a = 0 b = 1 until sorted?(arr) do # The yield call here passes `arr[a]` and `arr[b]` to the block. comparison = if block_given? yield(arr[a], arr[b]) else arr[a] <=> arr[b] end if [-1, 0, 1].include? comparison arr.insert(a, arr.delete_at(b)) if comparison == 1 a += 1 b += 1 else a = 0 b = 1 end end arr end sample_array = [4, 2, 5, 6, 3, 23, 5546, 234, 234, 6] # Sanity check: 100.times do # `a` is the value of `arr[a]` in our function above. Likewise for `b` and `arr[b]`. print bubble_sort(sample_array.shuffle) { |a, b| a <=> b }, "\n" end EDIT A cleaner version: # In place swap will be more efficient as it doesn't need to modify the size of the arra def swap(arr, idx) raise IndexError.new("Index #{idx} is out of bounds") if idx >= arr.length || idx < 0 temp = arr[idx] arr[idx] = arr[idx + 1] arr[idx + 1] = temp end def bubble_sort(arr) loop do sorted_elements = 0 arr.each_cons(2).each_with_index do |pair, idx| comparison = if block_given? yield pair.first, pair.last else pair.first <=> pair.last end if comparison > 0 swap(arr, idx) else sorted_elements += 1 end end return arr if sorted_elements >= arr.length - 1 end end # A simple test sample_array = [4, 2, 2, 2, 2, 2, 5, 5, 6, 3, 23, 5546, 234, 234, 6] sample_str_array = ["a", "ccc", "ccccc"] 100.times do print bubble_sort(sample_array.shuffle) { |a, b| a <=> b }, "\n" print bubble_sort(sample_str_array.shuffle) { |a, b| a.length <=> b.length }, "\n" end
You're not too far off. Just a few things: Make your function take a block argument def bubble_sort (array, &block) Check to see if the user has provided a block if block_given? # Call user's comparator block else # Use the default behavior end Call the user's comparator block block.call(a, b) In the user-provided block, accept block params for the elements to compare bubble_sort(array) {|a,b| a.length <=> b.length} That should put you in the right ballpark.
very simple ruby programing, getting error and don't understand it
I'm asked to write the ruby program that generate the output based the given command, The full description I'm really new in ruby (maybe few hours that I have started ruby) I'm getting this error, please check my code for other possible errors: Thank you. n `block in each2': undefined method `[]' for #<MyVector:0x00000002c4ad90 #array=[2, 3, 4]> (NoMethodError) What I have done so far: # MyVector Class class MyVector def initialize (a) if !(a.instance_of? Array) raise "ARGUMENT OF INITIALIZER MUST BE AN ARRAY" else #array = a end end def array #array end def to_s #array.to_s end def length #array.length end def each2(a) raise Error, "INTEGER IS NOT LIKE VECTOR" if a.kind_of?(Integer) Vector.Raise Error if length != a.length return to_enum(:each2, a) unless block_given? length.times do |i| yield #array[i], a[i] end self end def * (a) Vector.Raise Error if length != a.length p = 0 each2(a) {|a1, a2|p += a1 * a2} p end end # MyMatrix Class class MyMatrix def initialize a #array=Array.new(a.length) i=0 while(i<a.length) #array[i]=MyVector.new(a[i]) end end def to_s #array.to_s end def transpose size=vectors[0].length arr= Array.new(size) i=0 while i<size a=Array.new(vector.length) j=0 while j<a.length a[j]=vectors[j].arr[i] j+=1 end arr[i]=a i+=1 end arr[i]=a i+=1 end def *m if !(m instance_of? MyMatrix) raise Error a=Array.new(#array.length) i=0 while (i<#array.length) a[i]=#array[i]*m i=i+1 end end end end Input: Test code v = MyVector.new([1,2,3]) puts "v = " + v.to_s v1 = MyVector.new([2,3,4]) puts "v1 = " + v1.to_s puts "v * v1 = " + (v * v1).to_s m = MyMatrix.new([[1,2], [1, 2], [1, 2]]) puts "m = " + m.to_s + "\n" puts "v * m = " + (v * m).to_s m1 = MyMatrix.new([[1, 2, 3], [2, 3, 4]]) puts "m1 = " + m1.to_s + "\n" puts "m * m1 = " + (m * m1).to_s puts "m1 * m = " + (m1 * m).to_s Desired Output: v = 1 2 3 v1 = 2 3 4 v * v1 = 20 m = 1 2 1 2 1 2 v * m = 6 12 m1 = 1 2 3 2 3 4 m * m1 = 5 8 11 5 8 11 5 8 11 m1 * m = 6 12 9 18
length.times do |i| yield #array[i], a[i] end In the above block, a is an instance of MyVector. You need to define the [] operator on it, probably something like: def [](i) #array[i] end