### Find the stray number (Reduce(^:) dose not solve [x,y,y,y] )

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
•  1 1 # Find stray number 2 2 def find_stray(n) 3 3 n.count(n.min) > n.count(n.max) ? n.max : n.min 4 4 end 5 + 6 + # Find stray optimization 7 + def find_stray_o(n) 8 + n.sort! 9 + n[0] == n[1] ? n[-1] : n[0] 10 + end 11 + 12 + require "benchmark" 13 + 14 + array = (Array.new(1000_000,7) + [1]).shuffle 15 + 16 + Benchmark.bm(10) do |x| 17 + x.report("Find stray") { 10.times{ find_stray(array.clone.shuffle) }} 18 + x.report("Optimized") { 10.times{find_stray_o(array.clone.shuffle) }} 19 + end
Failed Tests

### Find the stray number (Reduce(^:) dose not solve [x,y,y,y] )

[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

### Explain Ruby Inheritance (and Mudoles)

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

•  1 + module Developer 2 + def are_you_dev? 3 + true 4 + end 5 + end 6 + 1 1 class Ivan 2 2 def give_me_beer? 3 3 true 4 4 end 5 5 end 6 6 class Andy < Ivan 7 7 end 8 8 9 9 class Vasa < Andy 16 + include Developer 10 10 end 11 11 12 12 class Kolya < Vasa 13 13 end 14 14

### Explain Ruby Inheritance

class Ivan
def give_me_beer?
true
end
end
class Andy < Ivan
end

class Vasa < Andy
end

class Kolya < Vasa
end

### Hash quest

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

• 2929 c hash # calling with hash
3030 hash = {two: 'Two', three: 'Three', one: 'One', }
3131 c hash # calling with hash mess in order
3232 hash = { one: 'One', }
3333 c hash # calling with hash where not all argumetns listed
3434
35+# Super syntax
36+hash = { two: '222', }
37+c one: 1, **hash
38+# c one: 1, hash # <== leads to error
39+
40+
41+
42+
3535 begin
3636 hash = { one: 'One', fore: "4" }
3737 c hash # calling with hash that contain pair unlisetd in named arguments
3838 rescue Exception => e
3939 p e.message
4040 p e.class
4141 end

### Hash quest

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

•  1 - # Method with Hash Argumennt 2 - def a b 3 - p "==> This is 'a' method" 4 - p b.class 5 - p b 6 - end 7 - 8 8 # Method with three named arguments 9 9 # https://robots.thoughtbot.com/ruby-2-keyword-arguments 10 10 def c one: "one", two: "tow", three: "three" 11 - p "--> this is 'c' method" 12 - p one 13 - p two 14 - p three 4 + p "one: %s two: %s three: %s" % [one,two,three] 5 + 15 15 end 16 16 17 - a(one: 1, two: 2, three: 3) # Passing Hash 8 + c # calling without arguments 9 + c two: "TWO2" # calling with one argument 10 + c two: "2", one: 1111111 # Calling with 2 arguments with no order 18 18 c(one: 1, two: 2, three: 3) # Passing 3 Named arguments 12 + c(two: 22, three: 333, one: 1 ) # Passing 3 Named arguments (mess the order) 19 19 20 - hash = {one: 'One', two: 'Two', three: 'Three'} 14 + begin 15 + c fore: "4" # calling with wrong argument 16 + rescue Exception => e 17 + p e.message 18 + p e.class 19 + end 20 + begin 21 + #c (fore:"4") # calling with wrong argument 22 + # this lead to syntaxix error 23 + rescue Exception => e 24 + p e.message 25 + p e.class 26 + end 21 21 22 - a hash # Passing hash from variable 23 - c **hash # Using some supper modern and cool splat sytax 24 - c hash 28 + hash = {one: 'One', two: 'Two', three: 'Three'} 29 + c hash # calling with hash 30 + hash = {two: 'Two', three: 'Three', one: 'One', } 31 + c hash # calling with hash mess in order 32 + hash = { one: 'One', } 33 + c hash # calling with hash where not all argumetns listed 25 25 26 - p hash 27 - p "Using one splas operator hash => array" 28 - p *hash 29 - p "Using two slpat operator" 30 - p **hash 35 + begin 36 + hash = { one: 'One', fore: "4" } 37 + c hash # calling with hash that contain pair unlisetd in named arguments 38 + rescue Exception => e 39 + p e.message 40 + p e.class 41 + end
Failed Tests

### Hash quest

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
•  1 - def enumer 2 - Enumerator.new do |x| 3 - a = 1 4 - loop do # How do this loop know where to stop? 5 - x << a 6 - a *= 2 7 - end 8 - end 1 + # Method with Hash Argumennt 2 + def a b 3 + p "==> This is 'a' method" 4 + p b.class 5 + p b 9 9 end 10 10 11 - def simple n 12 - x = [] 13 - a = 1 14 - i = 0 15 - loop do 16 - x << a 17 - a *= 2 18 - i += 1 19 - break unless i < n # in this case condition for stop used 20 - end 21 - x 8 + # Method with three named arguments 9 + # https://robots.thoughtbot.com/ruby-2-keyword-arguments 10 + def c one: "one", two: "tow", three: "three" 11 + p "--> this is 'c' method" 12 + p one 13 + p two 14 + p three 22 22 end 16 + 17 + a(one: 1, two: 2, three: 3) # Passing Hash 18 + c(one: 1, two: 2, three: 3) # Passing 3 Named arguments 19 + 20 + hash = {one: 'One', two: 'Two', three: 'Three'} 21 + 22 + a hash # Passing hash from variable 23 + c **hash # Using some supper modern and cool splat sytax 24 + c hash 25 + 26 + p hash 27 + p "Using one splas operator hash => array" 28 + p *hash 29 + p "Using two slpat operator" 30 + p **hash

### The Enumerator loop Magic!

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

### Find amount of all natural numbers that can be generated from digit's array.

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.

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

### Hey, I'm show KUMITE concept to may friend

Make hahahah progamm

Improve it for different x inputs.

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

### How do match works?

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("!!!??")
Failed Tests

### Compare JS and Ruby REEGEX

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("!!!??")
Failed Tests

### Uncovered test by Simple Array rotation.

This my solusion pass the KATA, but it fails on
test generated by rolling 3 elements ASC and DESC arrays.
https://www.codewars.com/kata/simple-array-rotation/ruby

Generating tests:

"ROTATING [1, 10, 100]is ASC"
"rt: 0 ar: [1, 10, 100]is ASC a "rt: 1 ar: [10, 100, 1] ac and a>c"

"ROTATING [100, 10, 1]is DESC"
"rt: 0 ar: [100, 10, 1]is DESC a>b and b>c and a>c"
"rt: 1 ar: [10, 1, 100] a>b and b<c and a<c"

PS:
code for generate tests https://gist.github.com/lbvf50mobile/85f5abb840f3926feb80f7dfe85d2e01

def solve(arr)
return "A" if asc_? arr
return "D" if desc_? arr
if( arr.size == 3)
a = arr
return "RA" if  a[0] > a[1] and a[1] < a[2];
end
ch_rd = arr.chunk_while{ |x,y| x > y }.to_a
ch_ra = arr.chunk_while{ |x,y| x < y }.to_a
return "RD" if ch_rd.any?{|x| desc_? x} and ch_rd.size == 2
return "RA" if ch_ra.any?{|x| asc_? x} and ch_ra.size == 2

end

def asc_? arr
arr == arr.sort
end
def desc_? arr
arr == arr.sort.reverse
end

### lbvf50mobile's Kumite #5

a = "Baguette baguette baguette Encore!"
b = "Baguette baguette baguette Encore!"
p a == b
p a === b