tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1,12 @@
}:r:|~ Read numbers in a loop.
}:b: Treat the queue as a stack and
<:2:= accumulate the binary digits
/=>&~ of the given number.
^:b:
<:0:-> Enqueue negative 1 as a sentinel.
{ Dequeue the first binary digit.
}:p:
~%={+ Rotate each binary digit into place and print it.
^:p:
<:a:~$ Output a newline.
^:r:

View file

@ -0,0 +1,4 @@
echo -e "5\n32\n2329" | 0815 bin.0
101
110010
10001100101001

View file

@ -0,0 +1,7 @@
The task is to output the sequence of binary digits for a given [[wp:Natural number|non-negative integer]].
The decimal value <tt>5</tt>, should produce an output of <tt>101</tt>
The decimal value <tt>50</tt> should produce an output of <tt>110010</tt>
The decimal value <tt>9000</tt> should produce an output of <tt>10001100101000</tt>
The results can be achieved using builtin radix functions within the language, if these are available, or alternatively a user defined function can be used. The output produced should consist just of the binary digits of each number followed by a newline. There should be no other whitespace, radix or sign markers in the produced output, and [[wp:Leading zero|leading zeros]] should not appear in the results.

View file

@ -0,0 +1,4 @@
---
category:
- Radices
note: Basic language learning

View file

@ -0,0 +1,15 @@
(include-book "arithmetic-3/top" :dir :system)
(defun bin-string-r (x)
(if (zp x)
""
(string-append
(bin-string-r (floor x 2))
(if (= 1 (mod x 2))
"1"
"0"))))
(defun bin-string (x)
(if (zp x)
"0"
(bin-string-r x)))

View file

@ -0,0 +1,14 @@
#!/usr/local/bin/a68g --script #
printf((
$g" => "2r3d l$, 5, BIN 5,
$g" => "2r6d l$, 50, BIN 50,
$g" => "2r14d l$, 9000, BIN 9000
));
# or coerce to an array of BOOL #
print((
5, " => ", []BOOL(BIN 5)[bits width-3+1:], new line,
50, " => ", []BOOL(BIN 50)[bits width-6+1:], new line,
9000, " => ", []BOOL(BIN 9000)[bits width-14+1:], new line
))

View file

@ -0,0 +1,23 @@
BEGIN {
print tobinary(5)
print tobinary(50)
print tobinary(9000)
}
function tobinary(num) {
outstr = ""
l = num
while ( l ) {
if ( l%2 == 0 ) {
outstr = "0" outstr
} else {
outstr = "1" outstr
}
l = int(l/2)
}
# Make sure we output a zero for a value of zero
if ( outstr == "" ) {
outstr = "0"
}
return outstr
}

View file

@ -0,0 +1,28 @@
with Ada.Text_IO;
procedure Binary_Output is
package IIO is new Ada.Text_IO.Integer_IO(Integer);
function To_Binary(N: Natural) return String is
S: String(1 .. 1000); -- more than plenty!
Left: Positive := S'First;
Right: Positive := S'Last;
begin
IIO.Put(To => S, Item => N, Base => 2); -- This is the conversion!
-- Now S is a String with many spaces and some "2#...#" somewhere.
-- We only need the "..." part without spaces or base markers.
while S(Left) /= '#' loop
Left := Left + 1;
end loop;
while S(Right) /= '#' loop
Right := Right - 1;
end loop;
return S(Left+1 .. Right-1);
end To_Binary;
begin
Ada.Text_IO.Put_Line(To_Binary(5)); -- 101
Ada.Text_IO.Put_Line(To_Binary(50)); -- 110010
Ada.Text_IO.Put_Line(To_Binary(9000)); -- 10001100101000
end Binary_Output;

View file

@ -0,0 +1,10 @@
MsgBox % NumberToBinary(5) ;101
MsgBox % NumberToBinary(50) ;110010
MsgBox % NumberToBinary(9000) ;10001100101000
NumberToBinary(InputNumber)
{
While, InputNumber
Result := (InputNumber & 1) . Result, InputNumber >>= 1
Return, Result
}

View file

@ -0,0 +1,15 @@
ConsoleWrite(IntToBin(50) & @CRLF)
Func IntToBin($iInt)
$Stack = ObjCreate("System.Collections.Stack")
Local $b = -1, $r = ""
While $iInt <> 0
$b = Mod($iInt, 2)
$iInt = INT($iInt/2)
$Stack.Push ($b)
WEnd
For $i = 1 TO $Stack.Count
$r &= $Stack.Pop
Next
Return $r
EndFunc ;==>IntToBin

View file

@ -0,0 +1,16 @@
FOR num% = 0 TO 16
PRINT FN_tobase(num%, 2, 0)
NEXT
END
REM Convert N% to string in base B% with minimum M% digits:
DEF FN_tobase(N%,B%,M%)
LOCAL D%,A$
REPEAT
D% = N%MODB%
N% DIV= B%
IF D%<0 D% += B%:N% -= 1
A$ = CHR$(48 + D% - 7*(D%>9)) + A$
M% -= 1
UNTIL (N%=FALSE OR N%=TRUE) AND M%<=0
=A$

View file

@ -0,0 +1,12 @@
PRINT FNbinary(5)
PRINT FNbinary(50)
PRINT FNbinary(9000)
END
DEF FNbinary(N%)
LOCAL A$
REPEAT
A$ = STR$(N% AND 1) + A$
N% = N% >>> 1 : REM BBC Basic prior to V5 can use N% = N% DIV 2
UNTIL N% = 0
=A$

View file

@ -0,0 +1,14 @@
@echo off
:num2bin IntVal [RtnVar]
setlocal enableDelayedExpansion
set /a n=%~1
set rtn=
for /l %%b in (0,1,31) do (
set /a "d=n&1, n>>=1"
set rtn=!d!!rtn!
)
for /f "tokens=* delims=0" %%a in ("!rtn!") do set rtn=%%a
(endlocal & rem -- return values
if "%~2" neq "" (set %~2=%rtn%) else echo %rtn%
)
exit /b

View file

@ -0,0 +1,17 @@
+[ Start with n=1 to kick off the loop
[>>++<< Set up {n 0 2} for divmod magic
[->+>- Then
[>+>>]> do
[+[-<+>]>+>>] the
<<<<<<] magic
>>>+ Increment n % 2 so that 0s don't break things
>] Move into n / 2 and divmod that unless it's 0
-< Set up sentinel 1 then move into the first binary digit
[++++++++ ++++++++ ++++++++ Add 47 to get it to ASCII
++++++++ ++++++++ +++++++. and print it
[<]<] Get to a 0; the cell to the left is the next binary digit
>>[<+>-] Tape is {0 n}; make it {n 0}
>[>+] Get to the 1
<[[-]<] Zero the tape for the next iteration
++++++++++. Print a newline
[-]<+] Zero it then increment n and go again

View file

@ -0,0 +1,4 @@
blsq ) {5 50 9000}{2B!}m[uN
101
110010
10001100101000

View file

@ -0,0 +1,25 @@
#include <iostream>
#include <bitset>
#include <string>
#include <climits>
void print_bin(int n)
{
// convert to binary, then to string
std::string str = std::bitset<CHAR_BIT * sizeof n>(n).to_string();
// trim leading zeroes
if(n == 0)
str = "0";
else
str = str.substr(str.find('1'));
// output
std::cout << str << '\n';
}
int main()
{
print_bin(0);
print_bin(5);
print_bin(50);
print_bin(9000);
}

View file

@ -0,0 +1,20 @@
#include <stdio.h>
void bin(int x, char *s)
{
char*_(int x){
*(s = x ? _(x >> 1) : s) = (x & 1) + '0';
return ++s;
}
*_(x) = 0;
}
int main()
{
char a[100];
int i;
for (i = 0; i <= 1984; i += 31)
bin(i, a), printf("%4d: %s\n", i, a);
return 0;
}

View file

@ -0,0 +1,30 @@
#include <stdio.h>
void IntToBitString(unsigned int number)
{
int num_bits = sizeof(unsigned int) * 8;
bool startPrinting = false;
for (int bit_pos=num_bits-1; bit_pos >= 0; bit_pos--)
{
bool isBitSet = (number & (1<<bit_pos)) != 0;
if (!startPrinting && isBitSet)
startPrinting = true;
if (startPrinting || bit_pos==0)
printf("%s", isBitSet ? "1":"0");
}
printf("\r\n");
}
int main()
{
IntToBitString(0);
IntToBitString(5);
IntToBitString(50);
IntToBitString(9000);
return 0;
}

View file

@ -0,0 +1,26 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. SAMPLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 binary_number pic X(21).
01 str pic X(21).
01 binary_digit pic X.
01 digit pic 9.
01 n pic 9(7).
01 nstr pic X(7).
PROCEDURE DIVISION.
accept nstr
move nstr to n
perform until n equal 0
divide n by 2 giving n remainder digit
move digit to binary_digit
string binary_digit DELIMITED BY SIZE
binary_number DELIMITED BY SPACE
into str
move str to binary_number
end-perform.
display binary_number
stop run.

View file

@ -0,0 +1,3 @@
(Integer/toBinaryString 5)
(Integer/toBinaryString 50)
(Integer/toBinaryString 9000)

View file

@ -0,0 +1,4 @@
binary = (n) ->
new Number(n).toString(2)
console.log binary n for n in [5, 50, 9000]

View file

@ -0,0 +1 @@
(format t "~b" 5)

View file

@ -0,0 +1,6 @@
import std.stdio;
void main() {
foreach (i; 0 .. 16)
writefln("%b", i);
}

View file

@ -0,0 +1,25 @@
String binary(int n) {
if(n<0)
throw new IllegalArgumentException("negative numbers require 2s complement");
if(n==0) return "0";
String res="";
while(n>0) {
res=(n%2).toString()+res;
n=(n/2).toInt();
}
return res;
}
main() {
print(binary(0));
print(binary(1));
print(binary(5));
print(binary(10));
print(binary(50));
print(binary(9000));
print(binary(65535));
print(binary(0xaa5511ff));
print(binary(0x123456789abcde));
// fails due to precision limit
print(binary(0x123456789abcdef));
}

View file

@ -0,0 +1,18 @@
procedure Binary_Digits;
function IntToBinStr(AInt : integer) : string;
begin
Result := '';
while AInt > 0 do
begin
Result := Chr(Ord('0')+(AInt mod 2))+Result;
AInt := AInt div 2;
end;
end;
begin
writeln(' 5: '+IntToBinStr(5));
writeln(' 50: '+IntToBinStr(50));
writeln('9000: '+IntToBinStr(9000));
end;

View file

@ -0,0 +1,11 @@
#define std'dictionary'*.
#define ext'convertors'*.
// --- Program ---
#symbol Program =
[
'program'output write &numeric:5 &radix:2 &:eintformatter << "%n".
'program'output write &numeric:50 &radix:2 &:eintformatter << "%n".
'program'output write &numeric:9000 &radix:2 &:eintformatter << "%n".
].

View file

@ -0,0 +1 @@
lists:map( fun(N) -> io:fwrite("~.2B~n", [N]) end, [5, 50, 9000]).

View file

@ -0,0 +1,13 @@
function toBinary(integer i)
sequence s
s = {}
while i do
s = prepend(s, '0'+and_bits(i,1))
i = floor(i/2)
end while
return s
end function
puts(1, toBinary(5) & '\n')
puts(1, toBinary(50) & '\n')
puts(1, toBinary(9000) & '\n')

View file

@ -0,0 +1,5 @@
USING: io kernel math math.parser ;
5 >bin print
50 >bin print
9000 >bin print

View file

@ -0,0 +1,4 @@
9000 50 5
2 base !
. . .
decimal

View file

@ -0,0 +1,11 @@
package main
import (
"fmt"
)
func main() {
for i := 0; i < 16; i++ {
fmt.Printf("%b\n", i)
}
}

View file

@ -0,0 +1,7 @@
print '''
n binary
----- ---------------
'''
[5, 50, 9000].each {
printf('%5d %15s\n', it, Integer.toBinaryString(it))
}

Some files were not shown because too many files have changed in this diff Show more