27 lines
858 B
Text
27 lines
858 B
Text
with javascript_semantics
|
|
function str_srb(string input, integer n)
|
|
string res = input
|
|
integer l = length(input),
|
|
m = min(n,l),
|
|
count = sum(sq_eq(input[-m..-1],'1')),
|
|
k = l-n
|
|
for i=l to 1 by -1 do
|
|
integer bit = odd(input[i])
|
|
count += iff(k>0?odd(input[k]):0)-bit
|
|
if count and not bit then res[i] = '1' end if
|
|
k -= 1
|
|
end for
|
|
assert(count=0)
|
|
return res
|
|
end function
|
|
|
|
constant tests = {{"1000",2,2},{"0100",2,2},{"0010",2,2},{"0000",2,2},
|
|
{"010000000000100000000010000000010000000100000010000010000100010010",0,3}}
|
|
|
|
for i=1 to length(tests) do
|
|
{string input, integer l, integer m} = tests[i]
|
|
printf(1,"input: %s (width %d)\n",{input,length(input)})
|
|
for n=l to m do
|
|
printf(1,"n = %d: %s\n",{n,str_srb(input,n)})
|
|
end for
|
|
end for
|