Adding shuffling in test.
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();
var _ = require('lodash'); describe("Minus One", function() { it("Static Examples", function() { Test.assertEquals('123', minusOne('124')) Test.assertEquals('0', minusOne('1')) Test.assertEquals('9', minusOne('10')) }); it("Auto test Small numbers", function() { _.shuffle(_.range(1,1500)).forEach(x => Test.assertEquals((x-1).toString(), minusOne(x.toString())) ); }); it("Auto test Huge numbers", function() { _.shuffle(_.range(1,1500)).forEach(x =>{ let a = (BigInt('1062462935174393900') + BigInt(x)- BigInt(1)).toString(); let b = (BigInt('1062462935174393900') + BigInt(x)).toString(); Test.assertEquals(a, minusOne(b)) }); }); });
- var _ = require('lodash');
- describe("Minus One", function() {
- it("Static Examples", function() {
- Test.assertEquals('123', minusOne('124'))
- Test.assertEquals('0', minusOne('1'))
- Test.assertEquals('9', minusOne('10'))
- });
- it("Auto test Small numbers", function() {
_.range(1,1500).forEach(x =>- _.shuffle(_.range(1,1500)).forEach(x =>
- Test.assertEquals((x-1).toString(), minusOne(x.toString()))
- );
- });
- it("Auto test Huge numbers", function() {
_.range(1,1500).forEach(x =>{- _.shuffle(_.range(1,1500)).forEach(x =>{
- let a = (BigInt('1062462935174393900') + BigInt(x)- BigInt(1)).toString();
- let b = (BigInt('1062462935174393900') + BigInt(x)).toString();
- Test.assertEquals(a, minusOne(b))
- });
- });
- });
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'
};
var _ = require('lodash');
describe("Minus One", function() {
it("Static Examples", function() {
Test.assertEquals('123', minusOne('124'))
Test.assertEquals('0', minusOne('1'))
Test.assertEquals('9', minusOne('10'))
});
it("Auto test Small numbers", function() {
_.range(1,1500).forEach(x =>
Test.assertEquals((x-1).toString(), minusOne(x.toString()))
);
});
it("Auto test Huge numbers", function() {
_.range(1,1500).forEach(x =>{
let a = (BigInt('1062462935174393900') + BigInt(x)- BigInt(1)).toString();
let b = (BigInt('1062462935174393900') + BigInt(x)).toString();
Test.assertEquals(a, minusOne(b))
});
});
});
// 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");
var _ = require('lodash');
describe("Solution", function() {
it("should test for something", function() {
Test.assertNotEquals('123', distraction('124',1));
console.log(minus_one('124'))
assert.strictEqual('123', minus_one('124'))
});
it("Auto test", function() {
_.range(1,1500).forEach(x =>
Test.assertEquals((x-1).toString(), minus_one(x.toString()))
// hm this red assert.strictEqual((x-1).toString(), minus_one(x.toString()))
);
});
});
Show optimization of code. Instead 4 passs throught array (max,min,count double time), use only one pass durring sorting.
# 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
x = rand(1..100) y = rand(1..100) describe "Solution" do it "find stray" do Test.assert_equals(find_stray([x,y,y]), x, "[x,y,y] works") Test.assert_equals(find_stray([x,y,y,y]), x, "[x,y,y,y] DOSE NOT works") Test.assert_equals(find_stray([x,y,y,y,y]), x, "[x,y,y,y,y] works") end it "find stray optimized" do Test.assert_equals(find_stray_o([x,y,y]), x, "[x,y,y] works") Test.assert_equals(find_stray_o([x,y,y,y]), x, "[x,y,y,y] DOSE NOT works") Test.assert_equals(find_stray_o([x,y,y,y,y]), x, "[x,y,y,y,y] works") end end
- x = rand(1..100)
- y = rand(1..100)
- describe "Solution" do
it "should test for something" do- it "find stray" do
- Test.assert_equals(find_stray([x,y,y]), x, "[x,y,y] works")
- Test.assert_equals(find_stray([x,y,y,y]), x, "[x,y,y,y] DOSE NOT works")
- Test.assert_equals(find_stray([x,y,y,y,y]), x, "[x,y,y,y,y] works")
- end
- it "find stray optimized" do
- Test.assert_equals(find_stray_o([x,y,y]), x, "[x,y,y] works")
- Test.assert_equals(find_stray_o([x,y,y,y]), x, "[x,y,y,y] DOSE NOT works")
- Test.assert_equals(find_stray_o([x,y,y,y,y]), x, "[x,y,y,y,y] works")
- end
- 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
x = rand(1..100)
y = rand(1..100)
describe "Solution" do
it "should test for something" do
Test.assert_equals(find_stray([x,y,y]), x, "[x,y,y] works")
Test.assert_equals(find_stray([x,y,y,y]), x, "[x,y,y,y] DOSE NOT works")
Test.assert_equals(find_stray([x,y,y,y,y]), x, "[x,y,y,y,y] works")
end
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
- 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
describe "Solution" do it "should test for something" do Test.assert_equals(Kolya.new.give_me_beer?, true) Test.assert_equals(Kolya.new.are_you_dev?, true) end end
- describe "Solution" do
- it "should test for something" do
- Test.assert_equals(Kolya.new.give_me_beer?, true)
- Test.assert_equals(Kolya.new.are_you_dev?, true)
- end
- end
class Ivan
def give_me_beer?
true
end
end
class Andy < Ivan
end
class Vasa < Andy
end
class Kolya < Vasa
end
describe "Solution" do
it "should test for something" do
Test.assert_equals(Kolya.new.give_me_beer?, true)
end
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
- # 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.
# 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 Argumenntdef a bp "==> This is 'a' method"p b.classp bend- # 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 onep twop 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 variablec **hash # Using some supper modern and cool splat sytaxc 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 hashp "Using one splas operator hash => array"p *hashp "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
describe "Solution" do it "should test for something" do Test.assert_equals(1, 1) end end
- describe "Solution" do
- it "should test for something" do
- Test.assert_equals(1, 1)
- end
- 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 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 enumerEnumerator.new do |x|a = 1loop do # How do this loop know where to stop?x << aa *= 2endend- # Method with Hash Argumennt
- def a b
- p "==> This is 'a' method"
- p b.class
- p b
- end
def simple nx = []a = 1i = 0loop dox << aa *= 2i += 1break unless i < n # in this case condition for stop usedendxend- # 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
describe "Solution" do
it "should test for something" do
Test.assert_equals(enumer.take(4), [1, 2, 4, 8])
Test.assert_equals(simple(4), [1, 2, 4, 8])
end
end
Need to find amount of natural number that can be generated from an array of digit characters.
- Answers is one number: total amount of natural numbers can be generated by moving elements of the array.
- Each number have N digits, where N is an Array size.
- 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
describe "Solution" do
it "should test for something" do
Test.assert_equals(g(['9','0']), 1)
Test.assert_equals(g(['0','9','0']), 1)
Test.assert_equals(g(['5','7','2']), 6) # ans = !3
Test.assert_equals(g(['5','7','2','4']), 24) # ans = !4
Test.assert_equals(g(['1','3','4','0']), 18)
end
end
Make hahahah progamm
Improve it for different x inputs.
def ha x
"Ha" + "-ha" * (x - 1)
end
describe "Solution" do
it "should test for something" do
Test.assert_equals(ha(1), "Ha")
Test.assert_equals(ha(2), "Ha-ha")
end
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("!!!??")