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

exo18

# Tableau vide qu'on remplira avec une fonction
emails = []

# Fonction qui remplit le tableau avec des emails numérotés de 1 à 50
i = 1
50.times do
    if i < 10
        emails << "jean.dupont.0#{i}@email.fr"
    else
        emails << "jean.dupont.#{i}@email.fr"
    end
    i += 1
end

puts emails

app_store.rb

module AppStore
    
   App = Struct.new(:name, :developer, :version)
    
    APPS = [
        App.new(:Chat, :facebook, 2.0),
        App.new(:Twitter, :twitter, 5.8),
        App.new(:Weather, :yahoo, 10.15)
        ]
        
    def sel.fin_app(name)
        APPS.find { |app| app.name == name }
    end
        
end

mergetest

hash1  = {name: "one"}
hash2  = {name: "programmer"}
result = hash1.merge!(hash2)
puts result.inspect # => {:job=>"programmer", :name=>"schneems"}
puts hash1.inspect
puts hash2.inspect

string builtin method

nums = Array.new(10) { |e| e = e *3  }
puts "#{nums}"

FabiTest

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

trees

m = $<.read.lines.map do |l|
  " #{l.chomp} "
end
e = ' '*52
m = [e, *m, e]
n = Array.new(52,e)

(1..3).each do |r|
  (1..3).each do |c|
    puts r,c
    h = m[r][c]
    k = m[r+1][c-1..c+1]+m[r-1][c-1..c+1]+m[r][c-1]+m[r][c+1]
    if h='.' and k.scan('|').count>3
  end
end

aaaa

def objective_function(vector)
  return vector.inject(0.0) {|sum, x| sum + (x**2.0)}
end

def decode(bitstring, search_space, bits_per_param)
  vector = []
  search_space.each_with_index do |bounds, i|
    off, sum = i*bits_per_param, 0.0
    param = bitstring[off...(off+bits_per_param)].reverse
    param.size.times do |j|
      sum += ((param[j].chr=='1') ? 1.0 : 0.0) * (2.0 ** j.to_f)
    end
    min, max = bounds
    vector << min + ((max-min)/((2.0**bits_per_param.to_f)-1.0)) * sum
  end
  return vector
end

def evaluate(pop, search_space, bits_per_param)
  pop.each do |p|
    p[:vector] = decode(p[:bitstring], search_space, bits_per_param)
    p[:cost] = objective_function(p[:vector])
  end
end

def random_bitstring(num_bits)
  return (0...num_bits).inject(""){|s,i| s<<((rand<0.5) ? "1" : "0")}
end

def point_mutation(bitstring, rate)
  child = ""
   bitstring.size.times do |i|
     bit = bitstring[i].chr
     child << ((rand()<rate) ? ((bit=='1') ? "0" : "1") : bit)
  end
  return child
end

def calculate_mutation_rate(antibody, mutate_factor=-2.5)
  return Math.exp(mutate_factor * antibody[:affinity])
end

def num_clones(pop_size, clone_factor)
  return (pop_size * clone_factor).floor
end

def calculate_affinity(pop)
  pop.sort!{|x,y| x[:cost]<=>y[:cost]}
  range = pop.last[:cost] - pop.first[:cost]
  if range == 0.0
    pop.each {|p| p[:affinity] = 1.0}
  else
    pop.each {|p| p[:affinity] = 1.0-(p[:cost]/range)}
  end
end

def clone_and_hypermutate(pop, clone_factor)
  clones = []
  num_clones = num_clones(pop.size, clone_factor)
  calculate_affinity(pop)
  pop.each do |antibody|
    m_rate = calculate_mutation_rate(antibody)
    num_clones.times do
      clone = {}
      clone[:bitstring] = point_mutation(antibody[:bitstring], m_rate)
      clones << clone
    end
  end
  return clones
end

def random_insertion(search_space, pop, num_rand, bits_per_param)
  return pop if num_rand == 0
  rands = Array.new(num_rand) do |i|
    {:bitstring=>random_bitstring(search_space.size*bits_per_param)}
  end
  evaluate(rands, search_space, bits_per_param)
  return (pop+rands).sort{|x,y| x[:cost]<=>y[:cost]}.first(pop.size)
end

def search(search_space, max_gens, pop_size, clone_factor, num_rand, bits_per_param=16)
  pop = Array.new(pop_size) do |i|
    {:bitstring=>random_bitstring(search_space.size*bits_per_param)}
  end
  evaluate(pop, search_space, bits_per_param)
  best = pop.min{|x,y| x[:cost]<=>y[:cost]}
  max_gens.times do |gen|
    clones = clone_and_hypermutate(pop, clone_factor)
    evaluate(clones, search_space, bits_per_param)
    pop = (pop+clones).sort{|x,y| x[:cost]<=>y[:cost]}.first(pop_size)
    pop = random_insertion(search_space, pop, num_rand, bits_per_param)
    best = (pop + [best]).min{|x,y| x[:cost]<=>y[:cost]}
    puts " > gen #{gen+1}, f=#{best[:cost]}, s=#{best[:vector].inspect}"
  end
  return best
end

if __FILE__ == $0
  # problem configuration
  problem_size = 2
  search_space = Array.new(problem_size) {|i| [-5, +5]}
  # algorithm configuration
  max_gens = 10
  pop_size = 10
  clone_factor = 0.1
  num_rand = 2
  # execute the algorithm
  best = search(search_space, max_gens, pop_size, clone_factor, num_rand)
  puts "done! Solution: f=#{best[:cost]}, s=#{best[:vector].inspect}"
end

Ruby or Rybu, both smells

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



m = {}


m[:pokus] = "sss"

puts "jjj" if m[:omyl]
puts "sss" if m[:pokus]
puts m[:omyl].inspect

Dados XD

require File.expand_path(File.dirname(__FILE__) + '/neo')

# Greed is a dice game where you roll up to five dice to accumulate
# points.  The following "score" function will be used to calculate the
# score of a single roll of the dice.
#
# A greed roll is scored as follows:
#
# * A set of three ones is 1000 points
#
# * A set of three numbers (other than ones) is worth 100 times the
#   number. (e.g. three fives is 500 points).
#
# * A one (that is not part of a set of three) is worth 100 points.
#
# * A five (that is not part of a set of three) is worth 50 points.
#
# * Everything else is worth 0 points.
#
#
# Examples:
#
# score([1,1,1,5,1]) => 1150 points
# score([2,3,4,6,2]) => 0 points
# score([3,4,5,3,3]) => 350 points
# score([1,5,1,2,4]) => 250 points
#
# More scoring examples are given in the tests below:
#
# Your goal is to write the score method.

def score(dice)
  # You need to write this method
  

  sorted_dice = dice.sort            # Ordenar los dados en una nueva variable
  triple = []
  scoring = 0
  
  

  triple = sorted_dice.group_by(&:itself).map { |k,v| [k, v.count] }.to_h
  #puts "Triple #{triple}"
  #Contador de triples

  triple.each do |key, value|
    if key == 1 && value >= 3
      scoring += (key * 1000)
    elsif value >= 3 
      scoring += (key * 100)
    end
  end


  #Unos y cincos

  triple.each do |key, value|
    if key == 1 or key == 5
      if value >= 3 
        value -= 3
      end
    
      if key == 1
       scoring += (value * 100)
      elsif key == 5
        scoring += (value * 50)
      end
    end
  end

return scoring

end

class AboutScoringProject < Neo::Koan
  def test_score_of_an_empty_list_is_zero
    assert_equal 0, score([])
  end

  def test_score_of_a_single_roll_of_5_is_50
    assert_equal 50, score([5])
  end

  def test_score_of_a_single_roll_of_1_is_100
    assert_equal 100, score([1])
  end

  def test_score_of_multiple_1s_and_5s_is_the_sum_of_individual_scores
    assert_equal 300, score([1,5,5,1])
  end

  def test_score_of_single_2s_3s_4s_and_6s_are_zero
    assert_equal 0, score([2,3,4,6])
  end

  def test_score_of_a_triple_1_is_1000
    assert_equal 1000, score([1,1,1])
  end

  def test_score_of_other_triples_is_100x
    assert_equal 200, score([2,2,2])
    assert_equal 300, score([3,3,3])
    assert_equal 400, score([4,4,4])
    assert_equal 500, score([5,5,5])
    assert_equal 600, score([6,6,6])
  end

  def test_score_of_mixed_is_sum
    assert_equal 250, score([2,5,2,2,3])
    assert_equal 550, score([5,5,5,5])
    assert_equal 1100, score([1,1,1,1])
    assert_equal 1200, score([1,1,1,1,1])
    assert_equal 1150, score([1,1,1,5,1])
  end

end

Execute Ruby Online

class Binar
    def binar
        print "Input number to convert: "
        number = gets.to_i
        numerical_system = nil

        until(1..3).include?(numerical_system)      
            print "Select numeric system to convert: 1.Binar 2.Octal 3.Hexadecimal"
            numerical_system = gets.chomp
        end
        
        if numerical_system == 1
            y = number.to_s(2)
            s = "Binar"
        elsif numerical_system == 2
            y = number.to_s(8)
            s = "Octal"
        else
            y = number.to_s(16)
            s = "Hexadecimal"
        end
        
        print "#{number} in #{s} numerical system is #{y}"
    end
end

convert = Binar.new
convert.binar

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

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