Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
75
Task/Set-right-adjacent-bits/Ada/set-right-adjacent-bits.adb
Normal file
75
Task/Set-right-adjacent-bits/Ada/set-right-adjacent-bits.adb
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,28 @@
|
|||
local function set_right_bits(bits, e, n)
|
||||
if e == 0 or n <= 0 then return bits end
|
||||
local bits2 = bits:clone()
|
||||
for i = 1, e - 1 do
|
||||
local c = bits[i]
|
||||
if c == 1 then
|
||||
local j = i + 1
|
||||
while j <= i + n and j <= e do
|
||||
bits2[j] = 1
|
||||
j += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
return bits2
|
||||
end
|
||||
|
||||
local b = "010000000000100000000010000000010000000100000010000010000100010010"
|
||||
local tests = {{"1000", 2}, {"0100", 2}, {"0010", 2}, {"0000", 2}, {b, 0}, {b, 1}, {b, 2}, {b, 3}}
|
||||
for tests as test do
|
||||
local bits = test[1]
|
||||
local e = #bits
|
||||
local n = test[2]
|
||||
print($"n = {n}; Width e = {e}:")
|
||||
print($" Input b: {bits}")
|
||||
bits = bits:split(""):map(|c| -> c:byte() - 48)
|
||||
bits = set_right_bits(bits, e, n)
|
||||
print($" Result: {bits:concat("")}\n")
|
||||
end
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
Bf ← ↥/↥≡⌟⬚0↻⊃(¯+1⊚⌕1|⊢⬚0↯⊂∞⧻⊙(˜↯1)|∘)
|
||||
Pb ← &p/$"__"
|
||||
&p"n = 2"
|
||||
≡⌟(&p"-"∩Pb⤚Bf) [1_0_0_0 0_1_0_0 0_0_1_0 0_0_0_0] 2
|
||||
&p"n = (0), 1, 2, 3"
|
||||
≡⌟(Pb˜Bf)[1 2 3] ⊸Pb≡⋕⊂@0/◇⊂⇌⍥(⊙□⊸˜⊂@0)10 "10"
|
||||
Loading…
Add table
Add a link
Reference in a new issue