Created
November 21, 2017 15:16
-
-
Save akouryy/fa6a58129bd3c7d3e0ba9fae36ba40a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
COMPLEMENT = %r{ | |
^ (?: | |
(?<A> [01] ) | |
(?= (?<B> [01] \g<B> [01] | , .*? (?! \k<A>) [01] ) $ ) | |
)++ | |
, [01]++ $ | |
}x | |
BINARY_X = %r{ | |
^ (?<B>) | |
(?<A> | |
(?: 0 (?= (?<D>)) | 1 (?= [^x]*+ (?<D> x))) | |
(?= [01]*+ , (?<B> \k<B-1>{2} \k<D+0>)) | |
\g<A> | |
| , \k<B-1> $ | |
) | |
}x | |
SUCC = %r{ | |
^ 0,1 $ | |
| ^ (?<A> 1 \g<A> 0 | , 1 ) $ | |
| ^ (?= 1 (?<B> [01] \g<B> [01] | , 1 ) $ ) | |
(?<C> | |
(?<D> [01]) (?= (?<E> [01] \g<E> [01] | , [01]* \k<D> ) $ ) | |
| 0 (?<F> 1 \g<F> 0 | , [01]* 1 ) $ | |
)++ $ | |
}x | |
ADD_WRONG_ANSWER = %r{ | |
^ (?= [01]++ \+ [01]++ \= [01]++ $ ) | |
(?: | |
# I. 0 only | |
0++ \+ 0++ = 0++ | |
| # J. 1 only | |
(?= [01]++ \+ [01]++ \= 1++ $ ) | |
(?= | |
[01] | |
(?<J_Ones> | |
[01] \g<J_Ones> 1 | |
| \+ [01]++ \= 1 | |
) | |
) | |
(?<J_CheckCompl> | |
(?<J_CC_Left> [01]) | |
(?= | |
(?<J_CC_ToRight> | |
[01] \g<J_CC_ToRight> [01] | |
| \+ [01]* (?! \k<J_CC_Left>) [01] | |
) | |
\= | |
) | |
\g<J_CheckCompl> | |
| \+ | |
) | |
.++ | |
| # both 0 and 1 | |
TODO | |
)$ }x | |
R1 = (0..127).to_a | |
R2 = (0..1023).to_a | |
Tests = { | |
COMPLEMENT: [ | |
(1..10).flat_map{|i| | |
c = 2**i - 1 | |
(0..c).map{|j| "%0*b,%0*b" % [i, j, i, c ^ j] } | |
}, | |
(1..8).flat_map{|i| | |
c = 2**i - 1 | |
(0..c).to_a.combination(2).map{|j,k| "%0*b,%0*b" % [i, j, i, c ^ k] } | |
}, | |
], | |
BINARY_X: [ | |
R2.map{|i| "%b,%s" % [i, ?x * i] }, | |
R1.combination(2).map{|i,j| "%b,%s" % [i, ?x * j] }, | |
], | |
SUCC: [ | |
R2.map{|i| "%b,%b" % [i, i + 1] }, | |
R1.combination(2).map{|i,j| "%b,%b" % [i, (j + 1) % 128] }, | |
], | |
#ADD: [ | |
# ["0+1=1", "01110+10001=11111"], | |
# ["0+0=1", "01110+10000=11111"], | |
#], | |
} | |
Tests.each do |name, tests| | |
reg = eval name.to_s | |
tests[0].each do |t| | |
if (m = reg =~ t) != 0 | |
raise "#{name} =~ #{t.inspect} was #{m.inspect}" | |
end | |
end | |
tests[1].each do |t| | |
if m = reg =~ t | |
raise "#{name} =~ #{t.inspect} was #{m.inspect}" | |
end | |
end | |
puts "succeed: #{name}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment