Ad

Adding shuffling in test.

Code
Diff
  • let minusOne = str => (BigInt(str) - 1n).toString();
    • let minusOne = str => {
    • // Make it green, then make it clean :-)
    • if('10' == str) return '9'
    • if('124' == str) return '123'
    • if('062462935174393901' == str) return '062462935174393900'
    • return '0'
    • };
    • let minusOne = str => (BigInt(str) - 1n).toString();
let minusOne = str => {
  // Make it green, then make it clean :-)
  if('10' == str) return '9'
  if('124' == str) return '123'
  if('062462935174393901' == str) return '062462935174393900'
  return '0'
};
// test @slavik4
let distraction = (a,b) => {
var str = a
var vichitaemoe = b;
var i = str.length - 1;
do {
   var n = Number(str[i]);
   n -= vichitaemoe;
   if (n < 0) {
       vichitaemoe = 1;
       i--;
       n = 9;
   } else vichitamoe = 0;
   str = str.substring(0, i) + n + str.substring(i + 1);
} while (vichitaemoe > 0 && i >= 0);
return str;
}

let minus_one = str => [...str].map(x => +x).reverse().reduce((a,b) =>{
 if(0<= b - a.l){
   a.a.push(b-a.l)
   a.l = 0;
 }else{
  a.a.push(10+b-a.l)
  a.l = 1;
 }
  return a
}, new Object({a: [], l: 1})).a.reverse().join('').replace(/^0([1-9]\d*)/,"$1");

Show optimization of code. Instead 4 passs throught array (max,min,count double time), use only one pass durring sorting.

Code
Diff
  • # Find stray number
    def find_stray(n)
      n.count(n.min) > n.count(n.max) ? n.max : n.min
    end
    
    # Find stray optimization
    def find_stray_o(n)
      n.sort!
      n[0] == n[1] ? n[-1] : n[0]
    end
    
    require "benchmark"
    
    array = (Array.new(1000_000,7) + [1]).shuffle
    
    Benchmark.bm(10) do |x|
      x.report("Find stray") { 10.times{ find_stray(array.clone.shuffle) }}
      x.report("Optimized") { 10.times{find_stray_o(array.clone.shuffle) }}
    end
    • # Find stray number
    • def find_stray(n)
    • n.count(n.min) > n.count(n.max) ? n.max : n.min
    • end
    • # Find stray optimization
    • def find_stray_o(n)
    • n.sort!
    • n[0] == n[1] ? n[-1] : n[0]
    • end
    • require "benchmark"
    • array = (Array.new(1000_000,7) + [1]).shuffle
    • Benchmark.bm(10) do |x|
    • x.report("Find stray") { 10.times{ find_stray(array.clone.shuffle) }}
    • x.report("Optimized") { 10.times{find_stray_o(array.clone.shuffle) }}
    • end

[1, 1, 2] ==> 2
[17, 17, 3, 17, 17, 17, 17] ==> 3

Subtle arr.reduce(:^) solutions dose not works on [x,y,y,y] fromat. Take a look at the Test Cases.

The key to problem was found, size of the array must be odd: Y^Y n times give 0, and 0^X gives X

# Find stray number
def find_stray(n)
  n.reduce(:^)
end
Code
Diff
  • module Developer
      def are_you_dev?
        true
      end
    end
    
    class Ivan
      def give_me_beer?
        true
      end
    end
    class Andy < Ivan
    end
    
    class Vasa < Andy
      include Developer
    end
    
    class Kolya < Vasa
    end
    
    
    • module Developer
    • def are_you_dev?
    • true
    • end
    • end
    • class Ivan
    • def give_me_beer?
    • true
    • end
    • end
    • class Andy < Ivan
    • end
    • class Vasa < Andy
    • include Developer
    • end
    • class Kolya < Vasa
    • end
class Ivan
  def give_me_beer?
    true
  end
end
class Andy < Ivan
end

class Vasa < Andy
end

class Kolya < Vasa
end
Code
Diff
  • # Method with three named arguments
    # https://robots.thoughtbot.com/ruby-2-keyword-arguments
    def c one: "one", two: "tow", three: "three"
    p "one: %s two: %s three: %s" % [one,two,three]
    
    end
    
    c  # calling without arguments
    c  two: "TWO2" # calling with one argument
    c  two: "2", one: 1111111 # Calling with 2 arguments with no order
    c(one: 1, two: 2, three: 3) # Passing 3 Named arguments
    c(two: 22, three: 333, one: 1 ) # Passing 3 Named arguments (mess the order)
    
    begin
      c  fore: "4" # calling with wrong argument
    rescue Exception => e 
      p e.message
      p e.class
    end
    begin
      #c (fore:"4") # calling with wrong argument
      # this lead to syntaxix error
    rescue Exception => e 
      p e.message
      p e.class
    end
    
    hash = {one: 'One', two: 'Two', three: 'Three'}
    c hash # calling with hash
    hash = {two: 'Two', three: 'Three', one: 'One', }
    c hash # calling with hash mess in order
    hash = { one: 'One', }
    c hash # calling with hash where not all argumetns listed
    
    # Super syntax
    hash = { two: '222', }
    c one: 1, **hash 
    # c one: 1, hash  # <== leads to error
    
    
    
    
    begin
      hash = { one: 'One', fore: "4" }
      c hash # calling with hash that contain pair unlisetd in named arguments
    rescue Exception => e 
      p e.message
      p e.class
    end
    
    • # Method with three named arguments
    • # https://robots.thoughtbot.com/ruby-2-keyword-arguments
    • def c one: "one", two: "tow", three: "three"
    • p "one: %s two: %s three: %s" % [one,two,three]
    • end
    • c # calling without arguments
    • c two: "TWO2" # calling with one argument
    • c two: "2", one: 1111111 # Calling with 2 arguments with no order
    • c(one: 1, two: 2, three: 3) # Passing 3 Named arguments
    • c(two: 22, three: 333, one: 1 ) # Passing 3 Named arguments (mess the order)
    • begin
    • c fore: "4" # calling with wrong argument
    • rescue Exception => e
    • p e.message
    • p e.class
    • end
    • begin
    • #c (fore:"4") # calling with wrong argument
    • # this lead to syntaxix error
    • rescue Exception => e
    • p e.message
    • p e.class
    • end
    • hash = {one: 'One', two: 'Two', three: 'Three'}
    • c hash # calling with hash
    • hash = {two: 'Two', three: 'Three', one: 'One', }
    • c hash # calling with hash mess in order
    • hash = { one: 'One', }
    • c hash # calling with hash where not all argumetns listed
    • # Super syntax
    • hash = { two: '222', }
    • c one: 1, **hash
    • # c one: 1, hash # <== leads to error
    • begin
    • hash = { one: 'One', fore: "4" }
    • c hash # calling with hash that contain pair unlisetd in named arguments
    • rescue Exception => e
    • p e.message
    • p e.class
    • end

Here list of way call method with named agrumets. Also example of raise a ArgumentError exceptions when call method passing hash with "unknown" key for argumentl list.

Code
Diff
  • # Method with three named arguments
    # https://robots.thoughtbot.com/ruby-2-keyword-arguments
    def c one: "one", two: "tow", three: "three"
    p "one: %s two: %s three: %s" % [one,two,three]
    
    end
    
    c  # calling without arguments
    c  two: "TWO2" # calling with one argument
    c  two: "2", one: 1111111 # Calling with 2 arguments with no order
    c(one: 1, two: 2, three: 3) # Passing 3 Named arguments
    c(two: 22, three: 333, one: 1 ) # Passing 3 Named arguments (mess the order)
    
    begin
      c  fore: "4" # calling with wrong argument
    rescue Exception => e 
      p e.message
      p e.class
    end
    begin
      #c (fore:"4") # calling with wrong argument
      # this lead to syntaxix error
    rescue Exception => e 
      p e.message
      p e.class
    end
    
    hash = {one: 'One', two: 'Two', three: 'Three'}
    c hash # calling with hash
    hash = {two: 'Two', three: 'Three', one: 'One', }
    c hash # calling with hash mess in order
    hash = { one: 'One', }
    c hash # calling with hash where not all argumetns listed
    
    begin
      hash = { one: 'One', fore: "4" }
      c hash # calling with hash that contain pair unlisetd in named arguments
    rescue Exception => e 
      p e.message
      p e.class
    end
    
    • # Method with Hash Argumennt
    • def a b
    • p "==> This is 'a' method"
    • p b.class
    • p b
    • end
    • # Method with three named arguments
    • # https://robots.thoughtbot.com/ruby-2-keyword-arguments
    • def c one: "one", two: "tow", three: "three"
    • p "--> this is 'c' method"
    • p one
    • p two
    • p three
    • p "one: %s two: %s three: %s" % [one,two,three]
    • end
    • a(one: 1, two: 2, three: 3) # Passing Hash
    • c # calling without arguments
    • c two: "TWO2" # calling with one argument
    • c two: "2", one: 1111111 # Calling with 2 arguments with no order
    • c(one: 1, two: 2, three: 3) # Passing 3 Named arguments
    • c(two: 22, three: 333, one: 1 ) # Passing 3 Named arguments (mess the order)
    • hash = {one: 'One', two: 'Two', three: 'Three'}
    • begin
    • c fore: "4" # calling with wrong argument
    • rescue Exception => e
    • p e.message
    • p e.class
    • end
    • begin
    • #c (fore:"4") # calling with wrong argument
    • # this lead to syntaxix error
    • rescue Exception => e
    • p e.message
    • p e.class
    • end
    • a hash # Passing hash from variable
    • c **hash # Using some supper modern and cool splat sytax
    • c hash
    • hash = {one: 'One', two: 'Two', three: 'Three'}
    • c hash # calling with hash
    • hash = {two: 'Two', three: 'Three', one: 'One', }
    • c hash # calling with hash mess in order
    • hash = { one: 'One', }
    • c hash # calling with hash where not all argumetns listed
    • p hash
    • p "Using one splas operator hash => array"
    • p *hash
    • p "Using two slpat operator"
    • p **hash
    • begin
    • hash = { one: 'One', fore: "4" }
    • c hash # calling with hash that contain pair unlisetd in named arguments
    • rescue Exception => e
    • p e.message
    • p e.class
    • end
Code
Diff
  • # Method with Hash Argumennt
    def a b
     p "==> This is 'a' method"
     p b.class
     p b
    end
    
    # Method with three named arguments
    # https://robots.thoughtbot.com/ruby-2-keyword-arguments
    def c one: "one", two: "tow", three: "three"
    p "--> this is 'c' method" 
    p one
    p two
    p three
    end
    
    a(one: 1, two: 2, three: 3) # Passing Hash
    c(one: 1, two: 2, three: 3) # Passing 3 Named arguments
    
    hash = {one: 'One', two: 'Two', three: 'Three'}
    
    a hash # Passing hash from variable
    c **hash # Using some supper modern and cool splat sytax
    c hash
    
    p hash
    p "Using one splas operator hash  => array"
    p *hash
    p "Using two slpat operator"
    p **hash
    • def enumer
    • Enumerator.new do |x|
    • a = 1
    • loop do # How do this loop know where to stop?
    • x << a
    • a *= 2
    • end
    • end
    • # Method with Hash Argumennt
    • def a b
    • p "==> This is 'a' method"
    • p b.class
    • p b
    • end
    • def simple n
    • x = []
    • a = 1
    • i = 0
    • loop do
    • x << a
    • a *= 2
    • i += 1
    • break unless i < n # in this case condition for stop used
    • end
    • x
    • end
    • # Method with three named arguments
    • # https://robots.thoughtbot.com/ruby-2-keyword-arguments
    • def c one: "one", two: "tow", three: "three"
    • p "--> this is 'c' method"
    • p one
    • p two
    • p three
    • end
    • a(one: 1, two: 2, three: 3) # Passing Hash
    • c(one: 1, two: 2, three: 3) # Passing 3 Named arguments
    • hash = {one: 'One', two: 'Two', three: 'Three'}
    • a hash # Passing hash from variable
    • c **hash # Using some supper modern and cool splat sytax
    • c hash
    • p hash
    • p "Using one splas operator hash => array"
    • p *hash
    • p "Using two slpat operator"
    • p **hash

How do loop in Enumerator.new knows when to stop? Take a look at simple method: in this method conditions used to prevent endless loop. But, in case Enumertor.new there is no break condtion.

def enumer
  Enumerator.new do |x|
      a = 1
      loop do # How do this loop know where to stop?
       x << a
       a *= 2
      end
  end
end

def simple n
  x  = []
  a = 1
  i = 0
  loop do
    x << a
    a *= 2
    i += 1
    break unless i < n # in this case condition for stop used
  end
  x
end

Need to find amount of natural number that can be generated from an array of digit characters.

  1. Answers is one number: total amount of natural numbers can be generated by moving elements of the array.
  2. Each number have N digits, where N is an Array size.
  3. Each number use each charachter from an Array oney once.

This code needs optimization.

P.S. Question from: https://stackoverflow.com/questions/47616564/find-amount-of-natural-numbers-generated-from-array-of-digit-characters

def g(a)
  answer = a.permutation(a.size)
  .select{|x| x.join.to_i.to_s.split("").size == a.size }
  .to_a.uniq.size
  answer
end

Make hahahah progamm

Improve it for different x inputs.

def ha x
  "Ha" + "-ha" * (x - 1)
end

Try to make match in ruby to work like js mathc.

def slot s
  p "ONE scan"
  p "input: %s" % s
  p "output " + s.scan(/!+|\?+/).inspect
  
  p "TWO scan"
  p "input: %s" % s
  p "output " + s.scan(/([?!])\1*/).inspect
  
  p "ONE match"
  p "input: %s" % s
  p "match"
  s.match(/!+|\?+/).to_a.each{|x| p x}
  p "TWO  match"
  p "input: %s" % s
  p "match"
  s.match(/([?!])\1*/).to_a.each{|x| p x}
end

slot("!!!??")

Compare JS and Ruby REEGEX

RUBY s.scan(/!+|\?+/).inspect == JS s.match(/!+|\?+/g)

RUBY s.scan(/([?!])\1*/) != JS s.match(/([?!])\1*/g)

https://gist.github.com/lbvf50mobile/4b3cd312ad411e47582af40c7cbd4e05/edit

let slot = s => {
  console.log("ONE")
  console.log("input ", s)
  console.log("output" ,s.match(/!+|\?+/g))
  
  console.log("ONE")
  console.log("input ", s)
  console.log("output", s.match(/([?!])\1*/g))
}

slot("!!!??")
Loading more items...