Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
2
Task/Set-right-adjacent-bits/00-META.yaml
Normal file
2
Task/Set-right-adjacent-bits/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Set_right-adjacent_bits
|
||||
51
Task/Set-right-adjacent-bits/00-TASK.txt
Normal file
51
Task/Set-right-adjacent-bits/00-TASK.txt
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
Given a left-to-right ordered collection of <code>e</code> bits, <code>b</code>, where <code>1 <= e <= 10000</code>,
|
||||
and a zero or more integer <code>n</code> :
|
||||
* Output the result of setting the <code>n</code> bits to the right of any set bit in <code>b</code>
|
||||
(if those bits are present in b and therefore also preserving the width, <code>e</code>).
|
||||
|
||||
'''Some examples:''' <br>
|
||||
|
||||
Set of examples showing how one bit in a nibble gets changed:
|
||||
|
||||
n = 2; Width e = 4:
|
||||
|
||||
Input b: 1000
|
||||
Result: 1110
|
||||
|
||||
Input b: 0100
|
||||
Result: 0111
|
||||
|
||||
Input b: 0010
|
||||
Result: 0011
|
||||
|
||||
Input b: 0000
|
||||
Result: 0000
|
||||
|
||||
Set of examples with the same input with set bits of varying distance apart:
|
||||
|
||||
n = 0; Width e = 66:
|
||||
|
||||
Input b: 010000000000100000000010000000010000000100000010000010000100010010
|
||||
Result: 010000000000100000000010000000010000000100000010000010000100010010
|
||||
|
||||
n = 1; Width e = 66:
|
||||
|
||||
Input b: 010000000000100000000010000000010000000100000010000010000100010010
|
||||
Result: 011000000000110000000011000000011000000110000011000011000110011011
|
||||
|
||||
n = 2; Width e = 66:
|
||||
|
||||
Input b: 010000000000100000000010000000010000000100000010000010000100010010
|
||||
Result: 011100000000111000000011100000011100000111000011100011100111011111
|
||||
|
||||
n = 3; Width e = 66:
|
||||
|
||||
Input b: 010000000000100000000010000000010000000100000010000010000100010010
|
||||
Result: 011110000000111100000011110000011110000111100011110011110111111111
|
||||
|
||||
<br>'''Task:'''<br>
|
||||
|
||||
* Implement a routine to perform the setting of right-adjacent bits on representations of bits that will scale over the given range of input width <code>e</code>.
|
||||
* Use it to show, here, the results for the input examples above.
|
||||
* Print the output aligned in a way that allows easy checking by eye of the binary input vs output.
|
||||
|
||||
75
Task/Set-right-adjacent-bits/Ada/set-right-adjacent-bits.ada
Normal file
75
Task/Set-right-adjacent-bits/Ada/set-right-adjacent-bits.ada
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Set_Right_Bits is
|
||||
|
||||
type Bit_Number is new Positive range 1 .. 10_000;
|
||||
type Bit is new Boolean;
|
||||
type Bit_Collection is array (Bit_Number range <>) of Bit
|
||||
with Pack;
|
||||
|
||||
function Right_Adjacent (B : Bit_Collection;
|
||||
N : Natural) return Bit_Collection
|
||||
is
|
||||
Result : Bit_Collection := B;
|
||||
Mask : Bit_Collection := B;
|
||||
begin
|
||||
for A in 1 .. N loop
|
||||
Mask := False & Mask (Mask'First .. Mask'Last - 1);
|
||||
-- Shift Mask by appending False/0 in front of slice.
|
||||
|
||||
Result := Result or Mask;
|
||||
end loop;
|
||||
return Result;
|
||||
end Right_Adjacent;
|
||||
|
||||
procedure Put (Collection : Bit_Collection) is
|
||||
use Ada.Text_IO;
|
||||
begin
|
||||
for Bit of Collection loop
|
||||
Put ((if Bit then '1' else '0'));
|
||||
end loop;
|
||||
end Put;
|
||||
|
||||
function Value (Item : String) return Bit_Collection
|
||||
is
|
||||
Length : constant Bit_Number := Item'Length;
|
||||
Result : Bit_Collection (1 .. Length);
|
||||
Index : Natural := Item'First;
|
||||
begin
|
||||
for R of Result loop
|
||||
R := (case Item (Index) is
|
||||
when '0' | 'F' | 'f' => False,
|
||||
when '1' | 'T' | 't' => True,
|
||||
when others =>
|
||||
raise Constraint_Error with "invalid input");
|
||||
Index := Index + 1;
|
||||
end loop;
|
||||
return Result;
|
||||
end Value;
|
||||
|
||||
procedure Show (Bit_String : String; N : Natural)
|
||||
is
|
||||
B : constant Bit_Collection := Value (Bit_String);
|
||||
R : constant Bit_Collection := Right_Adjacent (B, N);
|
||||
Prefix : constant String := " ";
|
||||
use Ada.Text_IO;
|
||||
begin
|
||||
Put ("n ="); Put (N'Image);
|
||||
Put ("; Width e ="); Put (Bit_String'Length'Image);
|
||||
Put (":"); New_Line;
|
||||
Put (Prefix); Put ("Input B: "); Put (B); New_Line;
|
||||
Put (Prefix); Put ("Result : "); Put (R); New_Line;
|
||||
New_Line;
|
||||
end Show;
|
||||
|
||||
begin
|
||||
Show ("1000", 2);
|
||||
Show ("0100", 2);
|
||||
Show ("0010", 2);
|
||||
Show ("0000", 2);
|
||||
|
||||
Show ("010000000000100000000010000000010000000100000010000010000100010010", 0);
|
||||
Show ("010000000000100000000010000000010000000100000010000010000100010010", 1);
|
||||
Show ("010000000000100000000010000000010000000100000010000010000100010010", 2);
|
||||
Show ("010000000000100000000010000000010000000100000010000010000100010010", 3);
|
||||
end Set_Right_Bits;
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
setBits: function [inp, n][
|
||||
e: size inp
|
||||
print "n = " ++ (to :string n) ++ "; Width e = " ++ (to :string e) ++ ":"
|
||||
|
||||
result: new ø
|
||||
if? or? zero? e n =< 0 -> result: inp
|
||||
else [
|
||||
result: split inp
|
||||
loop 0..e-1 'i [
|
||||
if inp\[i] = `1` [
|
||||
j: i + 1
|
||||
while [and? j =< i+n j < e][
|
||||
result\[j]: `1`
|
||||
j: j + 1
|
||||
]
|
||||
]
|
||||
]
|
||||
result: join result
|
||||
]
|
||||
|
||||
print ["\tinput :" inp]
|
||||
print ["\tresult :" result]
|
||||
]
|
||||
|
||||
setBits "1000" 2
|
||||
setBits "0100" 2
|
||||
setBits "0010" 2
|
||||
setBits "0000" 2
|
||||
|
||||
setBits "010000000000100000000010000000010000000100000010000010000100010010" 0
|
||||
setBits "010000000000100000000010000000010000000100000010000010000100010010" 1
|
||||
setBits "010000000000100000000010000000010000000100000010000010000100010010" 2
|
||||
setBits "010000000000100000000010000000010000000100000010000010000100010010" 3
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
setRight(num, n){
|
||||
x := StrSplit(num)
|
||||
for i, v in StrSplit(num)
|
||||
if v
|
||||
loop, % n
|
||||
x[i+A_Index] := 1
|
||||
Loop % n
|
||||
x.removeAt(StrLen(num)+1)
|
||||
for i, v in x
|
||||
res .= v
|
||||
return res
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
test1 := [
|
||||
(join,
|
||||
"1000"
|
||||
"0100"
|
||||
"0010"
|
||||
"0000"
|
||||
)]
|
||||
|
||||
test2 := [
|
||||
(join,
|
||||
"010000000000100000000010000000010000000100000010000010000100010010"
|
||||
"010000000000100000000010000000010000000100000010000010000100010010"
|
||||
"010000000000100000000010000000010000000100000010000010000100010010"
|
||||
"010000000000100000000010000000010000000100000010000010000100010010"
|
||||
)]
|
||||
|
||||
for i, num in test1
|
||||
result .= "n=2; Width e = 4:`nInput :`t" num "`nResult :`t" setRight(num, 2) "`n`n"
|
||||
|
||||
for i, num in test2
|
||||
result .= "n=" i-1 "; Width e = 66:`nInput :`t" num "`nResult :`t" setRight(num, i-1) "`n`n"
|
||||
|
||||
MsgBox % result
|
||||
return
|
||||
30
Task/Set-right-adjacent-bits/C++/set-right-adjacent-bits.cpp
Normal file
30
Task/Set-right-adjacent-bits/C++/set-right-adjacent-bits.cpp
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
void setRightAdjacent(std::string text, int32_t number) {
|
||||
std::cout << "n = " << number << ", Width = " << text.size() << ", Input: " << text << std::endl;
|
||||
|
||||
std::string result = text;
|
||||
for ( uint32_t i = 0; i < result.size(); i++ ) {
|
||||
if ( text[i] == '1' ) {
|
||||
for ( uint32_t j = i + 1; j <= i + number && j < result.size(); j++ ) {
|
||||
result[j] = '1';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << std::string(16 + std::to_string(text.size()).size(), ' ') << "Result: " + result << "\n" << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
setRightAdjacent("1000", 2);
|
||||
setRightAdjacent("0100", 2);
|
||||
setRightAdjacent("0010", 2);
|
||||
setRightAdjacent("0000", 2);
|
||||
|
||||
std::string test = "010000000000100000000010000000010000000100000010000010000100010010";
|
||||
setRightAdjacent(test, 0);
|
||||
setRightAdjacent(test, 1);
|
||||
setRightAdjacent(test, 2);
|
||||
setRightAdjacent(test, 3);
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
// Set right-adjacent bits. Nigel Galloway: December 21st., 2021
|
||||
let fN g l=let rec fG n g=[|match n,g with ('0'::t,0)->yield '0'; yield! fG t 0
|
||||
|('0'::t,n)->yield '1'; yield! fG t (n-1)
|
||||
|(_::t,_) ->yield '1'; yield! fG t l
|
||||
|_ ->()|]
|
||||
fG (g|>List.ofSeq) 0|>System.String
|
||||
|
||||
[("1000",2);("0100",2);("0010",2);("0001",2);("0000",2);("010000000000100000000010000000010000000100000010000010000100010010",0);("010000000000100000000010000000010000000100000010000010000100010010",1);("010000000000100000000010000000010000000100000010000010000100010010",2);("010000000000100000000010000000010000000100000010000010000100010010",3)]|>List.iter(fun(n,g)->printfn "%s\n%s" n (fN n g))
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
USING: formatting io kernel math math.parser math.ranges
|
||||
sequences ;
|
||||
|
||||
: set-rab ( n b -- result )
|
||||
[0,b] [ neg shift ] with [ bitor ] map-reduce ;
|
||||
|
||||
:: show ( n b e -- )
|
||||
b e "n = %d; width = %d\n" printf
|
||||
n n b set-rab [ >bin e CHAR: 0 pad-head print ] bi@ ;
|
||||
|
||||
{ 0b1000 0b0100 0b0010 0b0000 } [ 2 4 show nl ] each
|
||||
0x10020080404082112 4 <iota> [ 66 show nl ] with each
|
||||
55
Task/Set-right-adjacent-bits/Go/set-right-adjacent-bits.go
Normal file
55
Task/Set-right-adjacent-bits/Go/set-right-adjacent-bits.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type test struct {
|
||||
bs string
|
||||
n int
|
||||
}
|
||||
|
||||
func setRightBits(bits []byte, e, n int) []byte {
|
||||
if e == 0 || n <= 0 {
|
||||
return bits
|
||||
}
|
||||
bits2 := make([]byte, len(bits))
|
||||
copy(bits2, bits)
|
||||
for i := 0; i < e-1; i++ {
|
||||
c := bits[i]
|
||||
if c == 1 {
|
||||
j := i + 1
|
||||
for j <= i+n && j < e {
|
||||
bits2[j] = 1
|
||||
j++
|
||||
}
|
||||
}
|
||||
}
|
||||
return bits2
|
||||
}
|
||||
|
||||
func main() {
|
||||
b := "010000000000100000000010000000010000000100000010000010000100010010"
|
||||
tests := []test{
|
||||
test{"1000", 2}, test{"0100", 2}, test{"0010", 2}, test{"0000", 2},
|
||||
test{b, 0}, test{b, 1}, test{b, 2}, test{b, 3},
|
||||
}
|
||||
for _, test := range tests {
|
||||
bs := test.bs
|
||||
e := len(bs)
|
||||
n := test.n
|
||||
fmt.Println("n =", n, "\b; Width e =", e, "\b:")
|
||||
fmt.Println(" Input b:", bs)
|
||||
bits := []byte(bs)
|
||||
for i := 0; i < len(bits); i++ {
|
||||
bits[i] = bits[i] - '0'
|
||||
}
|
||||
bits = setRightBits(bits, e, n)
|
||||
var sb strings.Builder
|
||||
for i := 0; i < len(bits); i++ {
|
||||
sb.WriteByte(bits[i] + '0')
|
||||
}
|
||||
fmt.Println(" Result :", sb.String())
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
smearright=: {{ +./ (-i.1+x) |.!.0"0 1/ y }}
|
||||
15
Task/Set-right-adjacent-bits/J/set-right-adjacent-bits-2.j
Normal file
15
Task/Set-right-adjacent-bits/J/set-right-adjacent-bits-2.j
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
b=: '1'&= :.(' '-.~":)
|
||||
task=: {{y,:x&smearright&.:b y}}
|
||||
|
||||
0 task '010000000000100000000010000000010000000100000010000010000100010010'
|
||||
010000000000100000000010000000010000000100000010000010000100010010
|
||||
010000000000100000000010000000010000000100000010000010000100010010
|
||||
1 task '010000000000100000000010000000010000000100000010000010000100010010'
|
||||
010000000000100000000010000000010000000100000010000010000100010010
|
||||
011000000000110000000011000000011000000110000011000011000110011011
|
||||
2 task '010000000000100000000010000000010000000100000010000010000100010010'
|
||||
010000000000100000000010000000010000000100000010000010000100010010
|
||||
011100000000111000000011100000011100000111000011100011100111011111
|
||||
3 task '010000000000100000000010000000010000000100000010000010000100010010'
|
||||
010000000000100000000010000000010000000100000010000010000100010010
|
||||
011110000000111100000011110000011110000111100011110011110111111111
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
public final class SetRightAdjacentBits {
|
||||
|
||||
public static void main(String[] aArgs) {
|
||||
setRightAdjacent("1000", 2);
|
||||
setRightAdjacent("0100", 2);
|
||||
setRightAdjacent("0010", 2);
|
||||
setRightAdjacent("0000", 2);
|
||||
|
||||
String test = "010000000000100000000010000000010000000100000010000010000100010010";
|
||||
setRightAdjacent(test, 0);
|
||||
setRightAdjacent(test, 1);
|
||||
setRightAdjacent(test, 2);
|
||||
setRightAdjacent(test, 3);
|
||||
}
|
||||
|
||||
private static void setRightAdjacent(String aText, int aNumber) {
|
||||
System.out.println("n = " + aNumber + ", Width = " + aText.length() + ", Input: " + aText);
|
||||
|
||||
char[] text = aText.toCharArray();
|
||||
char[] result = aText.toCharArray();
|
||||
for ( int i = 0; i < result.length; i++ ) {
|
||||
if ( text[i] == '1' ) {
|
||||
for ( int j = i + 1; j <= i + aNumber && j < result.length; j++ ) {
|
||||
result[j] = '1';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String spaces = " ".repeat(16 + String.valueOf(aText.length()).length());
|
||||
System.out.println(spaces + "Result: " + new String(result) + System.lineSeparator());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
function setrightadj(s, n)
|
||||
if n < 1
|
||||
return s
|
||||
else
|
||||
arr = reverse(collect(s))
|
||||
for (i, c) in enumerate(reverse(s))
|
||||
if c == '1'
|
||||
arr[max(1, i - n):i] .= '1'
|
||||
end
|
||||
end
|
||||
return String(reverse(arr))
|
||||
end
|
||||
end
|
||||
|
||||
@show setrightadj("1000", 2)
|
||||
@show setrightadj("0100", 2)
|
||||
@show setrightadj("0010", 2)
|
||||
@show setrightadj("0000", 2)
|
||||
|
||||
@show setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 0)
|
||||
@show setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 1)
|
||||
@show setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 2)
|
||||
@show setrightadj("010000000000100000000010000000010000000100000010000010000100010010", 3)
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
ClearAll[ShowSetRightBits]
|
||||
ShowSetRightBits[b_String,n_Integer]:=Module[{poss,chars},
|
||||
chars=Characters[b];
|
||||
poss=Position[chars,"1"];
|
||||
poss=Union[Flatten[Outer[Plus,poss,Range[n]]]];
|
||||
{{"In :",b},{"Out:",StringJoin[ReplacePart[chars,(List/@poss)->"1"]]}}//Grid
|
||||
]
|
||||
ShowSetRightBits["1000",2]
|
||||
ShowSetRightBits["0100",2]
|
||||
ShowSetRightBits["0010",2]
|
||||
ShowSetRightBits["0000",2]
|
||||
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",0]
|
||||
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",1]
|
||||
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",2]
|
||||
ShowSetRightBits["010000000000100000000010000000010000000100000010000010000100010010",3]
|
||||
89
Task/Set-right-adjacent-bits/Nim/set-right-adjacent-bits.nim
Normal file
89
Task/Set-right-adjacent-bits/Nim/set-right-adjacent-bits.nim
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
import std/[bitops, strformat]
|
||||
|
||||
type
|
||||
# Bit string described by a length and a sequence of bytes.
|
||||
BitString = object
|
||||
len: Natural
|
||||
data: seq[byte]
|
||||
# Position composed of a byte number and
|
||||
# a bit number in the byte (starting from the left).
|
||||
Position = tuple[bytenum, bitnum: int]
|
||||
|
||||
func toPosition(n: Natural): Position =
|
||||
## Convert an index to a Position.
|
||||
(n div 8, 7 - n mod 8)
|
||||
|
||||
proc newBitString*(len: Natural): BitString =
|
||||
## Create a bit string of length "len".
|
||||
result.len = len
|
||||
result.data = newSeq[byte]((len + 7) div 8)
|
||||
|
||||
func checkIndex(bits: BitString; n: Natural) =
|
||||
## Check that the index "n" is not out of bounds.
|
||||
if n >= bits.len:
|
||||
raise newException(RangeDefect, &"index out of range: {n}.")
|
||||
|
||||
proc `[]`*(bits: BitString; n: Natural): bool =
|
||||
## Return the bit at index "n" (as a boolean).
|
||||
bits.checkIndex(n)
|
||||
let pos = n.toPosition
|
||||
result = bits.data[pos.bytenum].testBit(pos.bitnum)
|
||||
|
||||
func setBit*(bits: var BitString; n: Natural) =
|
||||
## Set the bit at index "n".
|
||||
bits.checkIndex(n)
|
||||
let pos = n.toPosition
|
||||
bits.data[pos.bytenum].setBit(pos.bitnum)
|
||||
|
||||
func clearBit*(bits: var BitString; n: Natural) =
|
||||
## Clear the bit at index "n".
|
||||
## Not used but provided for consistency.
|
||||
bits.checkIndex(n)
|
||||
let pos = n.toPosition
|
||||
bits.data[pos.bytenum].clearBit(pos.bitnum)
|
||||
|
||||
iterator items*(bits: BitString): bool =
|
||||
## Yield the successive bits of the bit string.
|
||||
for n in 0..<bits.len:
|
||||
yield bits[n]
|
||||
|
||||
func toBitString*(s: string): BitString =
|
||||
## Convert a string of '0' and '1' to a bit string.
|
||||
result = newBitString(s.len)
|
||||
for n, val in s:
|
||||
if val == '1':
|
||||
result.setBit(n)
|
||||
elif val != '0':
|
||||
raise newException(ValueError, &"invalid bit value: {val}")
|
||||
|
||||
func `$`*(bits: BitString): string =
|
||||
## Return the string representation of a bit string.
|
||||
const BinDigits = [false: '0', true: '1']
|
||||
for bit in bits.items:
|
||||
result.add BinDigits[bit]
|
||||
|
||||
func setAdjacentBitString(bits: BitString; n: Natural): BitString =
|
||||
## Set the "n" bits adjacent to a set bit.
|
||||
result = bits
|
||||
for i in 0..<bits.len:
|
||||
if bits[i]:
|
||||
for j in (i + 1)..(i + n):
|
||||
if j < bits.len:
|
||||
result.setBit(j)
|
||||
|
||||
let n = 2
|
||||
echo &"n = {n}; Width e = 4\n"
|
||||
for input in ["1000", "0100", "0010", "0000"]:
|
||||
echo &"Input: {input}"
|
||||
echo &"Result: {input.toBitString.setAdjacentBitString(n)}"
|
||||
echo()
|
||||
|
||||
echo()
|
||||
const BS66 = "010000000000100000000010000000010000000100000010000010000100010010".toBitString
|
||||
for n in 0..3:
|
||||
echo &"n = {n}; Width e = {BS66.len}\n"
|
||||
echo "Input:"
|
||||
echo BS66
|
||||
echo "Result:"
|
||||
echo BS66.setAdjacentBitString(n)
|
||||
echo()
|
||||
21
Task/Set-right-adjacent-bits/Perl/set-right-adjacent-bits.pl
Normal file
21
Task/Set-right-adjacent-bits/Perl/set-right-adjacent-bits.pl
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use feature 'bitwise';
|
||||
|
||||
while( <DATA> ) {
|
||||
my ($n, $input) = split;
|
||||
my $width = length $input;
|
||||
my $result = '';
|
||||
$result |.= substr 0 x $_ . $input, 0, $width for 0..$n;
|
||||
print "n = $n width = $width\n input $input\nresult $result\n\n";
|
||||
}
|
||||
|
||||
__DATA__
|
||||
2 1000
|
||||
2 0100
|
||||
2 0011
|
||||
2 0000
|
||||
0 010000000000100000000010000000010000000100000010000010000100010010
|
||||
1 010000000000100000000010000000010000000100000010000010000100010010
|
||||
2 010000000000100000000010000000010000000100000010000010000100010010
|
||||
3 010000000000100000000010000000010000000100000010000010000100010010
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">str_srb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">input</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">input</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_eq</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">m</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #008000;">'1'</span><span style="color: #0000FF;">)),</span>
|
||||
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">n</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">l</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">bit</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">odd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">odd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]):</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">bit</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span> <span style="color: #008080;">and</span> <span style="color: #008080;">not</span> <span style="color: #000000;">bit</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'1'</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">k</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #008000;">"1000"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"0100"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"0010"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"0000"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #008000;">"010000000000100000000010000000010000000100000010000010000100010010"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}}</span>
|
||||
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #004080;">string</span> <span style="color: #000000;">input</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"input: %s (width %d)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">input</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">)})</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">l</span> <span style="color: #008080;">to</span> <span style="color: #000000;">m</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"n = %d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">str_srb</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">mpz_srb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">input</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">mpz</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0b"</span><span style="color: #0000FF;">&</span><span style="color: #000000;">input</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">tmp</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">bit</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">mask</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">rask</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- (mask res)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- (backward, actually)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_even</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #7060A8;">mpz_and</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rask</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mask</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_cmp_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rask</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #7060A8;">mpz_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bit</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #7060A8;">mpz_mul_2exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bit</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bit</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- aka left shift</span>
|
||||
<span style="color: #7060A8;">mpz_fdiv_q_2exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- aka right shift</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">ret</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">lz</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">)-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ret</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">lz</span> <span style="color: #008080;">then</span> <span style="color: #000000;">ret</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lz</span><span style="color: #0000FF;">)&</span><span style="color: #000000;">ret</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">ret</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<span style="color: #000080;font-style:italic;">--...</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"n = %d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mpz_srb</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)})</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">mpz_srb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">input</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">input</span>
|
||||
<span style="color: #004080;">mpz</span> <span style="color: #000000;">tmp</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0b"</span><span style="color: #0000FF;">&</span><span style="color: #000000;">input</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">mask</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">rask</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- (mask res)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">input</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #7060A8;">mpz_and</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rask</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mask</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_cmp_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rask</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'1'</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #7060A8;">mpz_fdiv_q_2exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- aka right shift</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<span style="color: #000080;font-style:italic;">--...</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"n = %d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mpz_srb</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)})</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
from operator import or_
|
||||
from functools import reduce
|
||||
|
||||
def set_right_adjacent_bits(n: int, b: int) -> int:
|
||||
return reduce(or_, (b >> x for x in range(n+1)), 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("SAME n & Width.\n")
|
||||
n = 2 # bits to the right of set bits, to also set
|
||||
bits = "1000 0100 0010 0000"
|
||||
first = True
|
||||
for b_str in bits.split():
|
||||
b = int(b_str, 2)
|
||||
e = len(b_str)
|
||||
if first:
|
||||
first = False
|
||||
print(f"n = {n}; Width e = {e}:\n")
|
||||
result = set_right_adjacent_bits(n, b)
|
||||
print(f" Input b: {b:0{e}b}")
|
||||
print(f" Result: {result:0{e}b}\n")
|
||||
|
||||
print("SAME Input & Width.\n")
|
||||
#bits = "01000010001001010110"
|
||||
bits = '01' + '1'.join('0'*x for x in range(10, 0, -1))
|
||||
for n in range(4):
|
||||
first = True
|
||||
for b_str in bits.split():
|
||||
b = int(b_str, 2)
|
||||
e = len(b_str)
|
||||
if first:
|
||||
first = False
|
||||
print(f"n = {n}; Width e = {e}:\n")
|
||||
result = set_right_adjacent_bits(n, b)
|
||||
print(f" Input b: {b:0{e}b}")
|
||||
print(f" Result: {result:0{e}b}\n")
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
from typing import List
|
||||
|
||||
|
||||
def set_right_adjacent_bits_list(n: int, b: List[int]) -> List[int]:
|
||||
# [0]*x is padding b on the left.
|
||||
# zip(*(list1, list2,..)) returns the n'th elements on list1, list2,...
|
||||
# int(any(...)) or's them.
|
||||
return [int(any(shifts))
|
||||
for shifts in zip(*([0]*x + b for x in range(n+1)))]
|
||||
|
||||
def _list2bin(b: List[int]) -> str:
|
||||
"List of 0/1 ints to bool string."
|
||||
return ''.join(str(x) for x in b)
|
||||
|
||||
def _to_list(bits: str) -> List[int]:
|
||||
return [int(char) for char in bits]
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("SAME n & Width.\n")
|
||||
n = 2 # bits to the right of set bits, to also set
|
||||
bits = "1000 0100 0010 0000"
|
||||
first = True
|
||||
for b_str in bits.split():
|
||||
b = _to_list(b_str)
|
||||
e = len(b_str)
|
||||
if first:
|
||||
first = False
|
||||
print(f"n = {n}; Width e = {e}:\n")
|
||||
result = set_right_adjacent_bits_list(n, b)
|
||||
print(f" Input b: {_list2bin(b)}")
|
||||
print(f" Result: {_list2bin(result)}\n")
|
||||
|
||||
print("SAME Input & Width.\n")
|
||||
#bits = "01000010001001010110"
|
||||
bits = '01' + '1'.join('0'*x for x in range(10, 0, -1))
|
||||
for n in range(4):
|
||||
first = True
|
||||
for b_str in bits.split():
|
||||
b = _to_list(b_str)
|
||||
e = len(b_str)
|
||||
if first:
|
||||
first = False
|
||||
print(f"n = {n}; Width e = {e}:\n")
|
||||
result = set_right_adjacent_bits_list(n, b)
|
||||
print(f" Input b: {_list2bin(b)}")
|
||||
print(f" Result: {_list2bin(result)}\n")
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
sub rab (Int $n, Int $b = 1) {
|
||||
my $m = $n;
|
||||
$m +|= ($n +> $_) for ^ $b+1;
|
||||
$m
|
||||
}
|
||||
|
||||
sub lab (Int $n, Int $b = 1) {
|
||||
my $m = $n;
|
||||
$m +|= ($n +< $_) for ^ $b+1;
|
||||
$m
|
||||
}
|
||||
|
||||
say "Powers of 2 ≤ 8, 0 - Right-adjacent-bits: 2";
|
||||
.&rab(2).base(2).fmt('%04s').say for <8 4 2 1 0>;
|
||||
|
||||
# Test with a few integers.
|
||||
for 8,4, 18455760086304825618,5, 5444684034376312377319904082902529876242,15 -> $integer, $bits {
|
||||
|
||||
say "\nInteger: $integer - Right-adjacent-bits: up to $bits";
|
||||
|
||||
.say for ^$bits .map: -> $b { $integer.&rab($b).base: 2 };
|
||||
|
||||
say "\nInteger: $integer - Left-adjacent-bits: up to $bits";
|
||||
|
||||
.say for ^$bits .map: -> $b { $integer.&lab($b).fmt("%{0~$bits+$integer.msb}b") };
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
use std::ops::{BitOrAssign, Shr};
|
||||
|
||||
fn set_right_adjacent_bits<E: Clone + BitOrAssign + Shr<usize, Output = E>>(b: &mut E, n: usize) {
|
||||
for _ in 1..=n {
|
||||
*b |= b.clone() >> 1;
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! test {
|
||||
( $t:ident, $n:expr, $e:expr, $g:ty, $b:expr, $c:expr$(,)? ) => {
|
||||
#[test]
|
||||
fn $t() {
|
||||
let n: usize = $n;
|
||||
let e: usize = $e;
|
||||
let b_original: $g = $b;
|
||||
let mut b = b_original.clone();
|
||||
set_right_adjacent_bits(&mut b, n);
|
||||
println!("n = {n}; e = {e}:");
|
||||
println!(" b = {:0>e$b}", b_original);
|
||||
println!(" output = {:0>e$b}", b);
|
||||
assert_eq!(b, $c);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
test!(test_a1, 2, 4, u8, 0b1000, 0b1110);
|
||||
test!(test_a2, 2, 4, u8, 0b0100, 0b0111);
|
||||
test!(test_a3, 2, 4, u8, 0b0010, 0b0011);
|
||||
test!(test_a4, 2, 4, u8, 0b0000, 0b0000);
|
||||
test!(
|
||||
test_b1, 0, 66, u128,
|
||||
0b010000000000100000000010000000010000000100000010000010000100010010,
|
||||
0b010000000000100000000010000000010000000100000010000010000100010010,
|
||||
);
|
||||
test!(
|
||||
test_b2, 1, 66, u128,
|
||||
0b010000000000100000000010000000010000000100000010000010000100010010,
|
||||
0b011000000000110000000011000000011000000110000011000011000110011011,
|
||||
);
|
||||
test!(
|
||||
test_b3, 2, 66, u128,
|
||||
0b010000000000100000000010000000010000000100000010000010000100010010,
|
||||
0b011100000000111000000011100000011100000111000011100011100111011111,
|
||||
);
|
||||
test!(
|
||||
test_b4, 3, 66, u128,
|
||||
0b010000000000100000000010000000010000000100000010000010000100010010,
|
||||
0b011110000000111100000011110000011110000111100011110011110111111111,
|
||||
);
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
var setRightBits = Fn.new { |bits, e, n|
|
||||
if (e == 0 || n <= 0) return bits
|
||||
var bits2 = bits.toList
|
||||
for (i in 0...e - 1) {
|
||||
var c = bits[i]
|
||||
if (c == 1) {
|
||||
var j = i + 1
|
||||
while (j <= i + n && j < e) {
|
||||
bits2[j] = 1
|
||||
j = j + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
return bits2
|
||||
}
|
||||
|
||||
var b = "010000000000100000000010000000010000000100000010000010000100010010"
|
||||
var tests = [["1000", 2], ["0100", 2], ["0010", 2], ["0000", 2], [b, 0], [b, 1], [b, 2], [b, 3]]
|
||||
for (test in tests) {
|
||||
var bits = test[0]
|
||||
var e = bits.count
|
||||
var n = test[1]
|
||||
System.print("n = %(n); Width e = %(e):")
|
||||
System.print(" Input b: %(bits)")
|
||||
bits = bits.map { |c| c.bytes[0] - 48 }.toList
|
||||
bits = setRightBits.call(bits, e, n)
|
||||
System.print(" Result: %(bits.join())\n")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue