Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
29
Task/Four-bit-adder/Nim/four-bit-adder.nim
Normal file
29
Task/Four-bit-adder/Nim/four-bit-adder.nim
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
proc ha(a, b): auto = [a xor b, a and b] # sum, carry
|
||||
|
||||
proc fa(a, b, ci): auto =
|
||||
let a = ha(ci, a)
|
||||
let b = ha(a[0], b)
|
||||
[b[0], a[1] or b[1]] # sum, carry
|
||||
|
||||
proc fa4(a,b): array[5, bool] =
|
||||
var co,s: array[4, bool]
|
||||
for i in 0..3:
|
||||
let r = fa(a[i], b[i], if i > 0: co[i-1] else: false)
|
||||
s[i] = r[0]
|
||||
co[i] = r[1]
|
||||
result[0..3] = s
|
||||
result[4] = co[3]
|
||||
|
||||
proc int2bus(n): array[4, bool] =
|
||||
var n = n
|
||||
for i in 0..result.high:
|
||||
result[i] = (n and 1) == 1
|
||||
n = n shr 1
|
||||
|
||||
proc bus2int(b): int =
|
||||
for i,x in b:
|
||||
result += (if x: 1 else: 0) shl i
|
||||
|
||||
for a in 0..7:
|
||||
for b in 0..7:
|
||||
assert a + b == bus2int fa4(int2bus(a), int2bus(b))
|
||||
31
Task/Four-bit-adder/Sidef/four-bit-adder.sidef
Normal file
31
Task/Four-bit-adder/Sidef/four-bit-adder.sidef
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
func bxor(a, b) {
|
||||
(~a & b) | (a & ~b)
|
||||
}
|
||||
|
||||
func half_adder(a, b) {
|
||||
return (bxor(a, b), a & b)
|
||||
}
|
||||
|
||||
func full_adder(a, b, c) {
|
||||
var (s1, c1) = half_adder(a, c)
|
||||
var (s2, c2) = half_adder(s1, b)
|
||||
return (s2, c1 | c2)
|
||||
}
|
||||
|
||||
func four_bit_adder(a, b) {
|
||||
var (s0, c0) = full_adder(a[0], b[0], 0)
|
||||
var (s1, c1) = full_adder(a[1], b[1], c0)
|
||||
var (s2, c2) = full_adder(a[2], b[2], c1)
|
||||
var (s3, c3) = full_adder(a[3], b[3], c2)
|
||||
return ([s3,s2,s1,s0].join, c3.to_s)
|
||||
}
|
||||
|
||||
say " A B A B C S sum"
|
||||
for a in ^16 {
|
||||
for b in ^16 {
|
||||
var(abin, bbin) = [a,b].map{|n| "%04b"%n->chars.reverse.map{.to_i} }...
|
||||
var(s, c) = four_bit_adder(abin, bbin)
|
||||
printf("%2d + %2d = %s + %s = %s %s = %2d\n",
|
||||
a, b, abin.join, bbin.join, c, s, "#{c}#{s}".bin)
|
||||
}
|
||||
}
|
||||
42
Task/Four-bit-adder/jq/four-bit-adder-1.jq
Normal file
42
Task/Four-bit-adder/jq/four-bit-adder-1.jq
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
# Start with the 'not' and 'and' building blocks.
|
||||
# These allow us to construct 'nand', 'or', and 'xor',
|
||||
# and so on.
|
||||
|
||||
def bit_not: if . == 1 then 0 else 1 end;
|
||||
|
||||
def bit_and(a; b): if a == 1 and b == 1 then 1 else 0 end;
|
||||
|
||||
def bit_nand(a; b): bit_and(a; b) | bit_not;
|
||||
|
||||
def bit_or(a; b): bit_nand(bit_nand(a;a); bit_nand(b;b));
|
||||
|
||||
def bit_xor(a; b):
|
||||
bit_nand(bit_nand(bit_nand(a;b); a);
|
||||
bit_nand(bit_nand(a;b); b));
|
||||
|
||||
def halfAdder(a; b):
|
||||
{ "carry": bit_and(a; b), "sum": bit_xor(a; b) };
|
||||
|
||||
def fullAdder(a; b; c):
|
||||
halfAdder(a; b) as $h0
|
||||
| halfAdder($h0.sum; c) as $h1
|
||||
| {"carry": bit_or($h0.carry; $h1.carry), "sum": $h1.sum };
|
||||
|
||||
# a and b should be strings of 0s and 1s, of length no greater than 4
|
||||
def fourBitAdder(a; b):
|
||||
|
||||
# pad on the left with 0s, and convert the string
|
||||
# representation ("101") to an array of integers ([1,0,1]).
|
||||
def pad: (4-length) * "0" + . | explode | map(. - 48);
|
||||
|
||||
(a|pad) as $inA | (b|pad) as $inB
|
||||
| [][3] = null # an array for storing the four results
|
||||
| halfAdder($inA[3]; $inB[3]) as $pass
|
||||
| .[3] = $pass.sum # store the lsb
|
||||
| fullAdder($inA[2]; $inB[2]; $pass.carry) as $pass
|
||||
| .[2] = $pass.sum
|
||||
| fullAdder($inA[1]; $inB[1]; $pass.carry) as $pass
|
||||
| .[1] = $pass.sum
|
||||
| fullAdder($inA[0]; $inB[0]; $pass.carry) as $pass
|
||||
| .[0] = $pass.sum
|
||||
| map(tostring) | join("") ;
|
||||
1
Task/Four-bit-adder/jq/four-bit-adder-2.jq
Normal file
1
Task/Four-bit-adder/jq/four-bit-adder-2.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
fourBitAdder("0111"; "0001")
|
||||
Loading…
Add table
Add a link
Reference in a new issue