Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Min distance between 2 nodes in graph

def min_distance(a, b, graph)
    visited = {}
    distance = {}
    queue = Queue.new
    queue.enqueue(a)
    visited[a] = true
    distance[a] = 0
    
    while !queue.is_empty?
        current_node = queue.dequeue
        
        current_node.adjacent_nodes.each do |adjacent_node|
            if !visited[adjacent_node]
                distance[adjacent_node] = distance[current_node] + 1
                queue.enqueue(adjacent_node)
                visited[adjacent_node] = true
            end
        end
    end
    distance[b]
end

Greatest Common Divisor - Euclidean algorithm

def gcd(a, b)
    if b > a
        temp = a
        a = b
        b = temp
    end
    mod = a % b
    if mod == 0 
        return b
    else
        return gcd(b, mod)
    end
end

p gcd(210, 45) #15

p gcd(1, 1) #1

Ruby Online

# Hello World Program in Ruby
puts "Hello World!";

Execute Ruby Online

=begin
def triple_sequence(start, length)
	result = [start]
    n = 0
	while n < length - 1
      result << result [n] * 3
      n += 1
    end
  	return result
end
=end

def fibonacci(length)
	n = 0
  	result = [1, 1]
  if length == 0
  	return []
  elsif length == 1
    return [1]
  end
    	while  result.length < length 
          last = result[-1]
          sec_last = result[-2]
      	result <<	last + sec_last
   		end
  result.shift
  return result
end

print triple_sequence(2, 4) # => [2, 6, 18, 54]
puts
print triple_sequence(4, 5) # => [4, 12, 36, 108, 324]
puts

Execute Ruby Online

=begin
def greatest_factor_array(arr)
  result = []
	arr.each do |element|
        if element % 2 == 0
            result << divides(element)[-2]
        else
            result << element
         
         end   
end
  return result
end


def divides(num)
  n = num
	dividers_array = []
  	while n > 0
      if num % n == 0
        dividers_array << n
      end
      n -= 1
    end
  return dividers_array.sort!
end
=end

def greatest_factor_array(array)
    result = array.map do |i|
            if i % 2 == 0 
                greatest_factor(i)
            else
                i
            end
    end
    return result
end

def greatest_factor(num)
   (1...num).reverse_each do |n|
       if num % n == 0
           return n
       end
   end
end

#puts greates_factor(16)
print greatest_factor_array([16, 7, 9, 14]) # => [8, 7, 9, 7]
puts
print greatest_factor_array([30, 3, 24, 21, 10]) # => [15, 3, 12, 21, 5]
puts

1st try

puts "ich grüße dich"
wer = "ich"
was = " kann nix mehr - OMG"
puts wer + was
puts "und jetzt"
Ahnungslosigkeit = "?"
puts Ahnungslosigkeit

Execute Ruby Online

# Hello World Program in Ruby
puts "Hello World!";

Execute Ruby Online

### MAP METHOD

array = ["A", "B", "C"]

puts array.map {|n| n + "!"}
puts array


### SELECT METHOD

numeric_array = [111, 24, 3, 76, 5, 60, 7, 8, 9, 10]
puts "--------------------------------------------"
puts numeric_array.sort {|a, b| b <=> a}
puts 
puts "START"

puts "END"
puts
puts numeric_array.select {|element| element % 2 == 1}


fruits = ["apple", "pinapple"]

puts fruits.map {|element|  element[0].upcase + element[1..-1].downcase}
puts 
result = fruits.map.with_index do |element, index|
     new_word = element[0].upcase + element[1..-1].downcase 
    # "hello!!!"
    new_word * index
end
puts 
puts
puts result

###### MAP BY NAME






word = "Ruslan"
wor = word.split("")
puts wor.reverse.join
 wor.sort! {|x, y| y <=> x}
puts wor.join()

xxxx

class Hello
    private def add
        puts :add_called
    end
    
    def self.call
        puts :called
        add
        puts :add_executed
    end
end

Hello.call

test 1

puts "Bussi"

Previous 1 ... 4 5 6 7 8 9 10 ... 50 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.