How can I print the ascii value of an input in Brainfuck? - numbers

What I want to do is for a Brainfuck code to print out the ascii value of the input. For example, typing in an input of "a" will give an output of 97. The python equivalent of this is print(ord(input())). What I'm thinking is that once I get the input with the , command, I can split the input value's digits into separate cells, and then print each cell individually. What I mean by this is let's say you type in an input of a. The , command will store the ascii value of a in the first cell(cell 0), which is 97 in this case. Then I run some algorithm that will split the 97 into its individual digits. So, in this case, cell 1 will have a value of 0(because 97 has a hundred digit of 0), cell 2 will have a value of 9, and cell 3 will have a value of 7. Then we can add 48 to each of those cells(0 has an ascii value of 48) and print each cell individually, starting from cell 1(the hundreds place). The problem I'm facing is writing the digit separation algorithm. I can't seem to make it work. My idea is to subtract 100 from the original number until that number is less than 100 while keeping track of how many times 100 has been subtracted, then repeatedly subtract 10, and finally we are left with the ones place. But the problem with this idea is that I have no idea how to track if the number falls under 100 or 10. Any suggestions or ideas? Thanks for the help in advance.

What you are trying to implement is called "divmod". divmod is a function that divides two numbers (in your case positive integers) and stores the result and the remainder. Implementations for this in brainfuck exist: Divmod algorithm in brainfuck
Good luck!

Related

matlab textscan gives me wrong number of lines

I have a file names inputR_revised.tsv at https://www.dropbox.com/s/vtby4027rvprhga/inputR_revised.tsv?dl=0
In matlab, I typed
fid=fopen('BMC3C/example/inputR_revised.tsv','r')
covTable = textscan(fid,['%s',repmat('%.8n',[1,20])],'HeaderLines',1);
I get covTable{1,1} of size 41699 times 1. However when I type the following at terminal
wc -l inputR_revised.tsv
I get 41677.
Why does it differ? I have used sed and cut to modify the original file to get inputR_revised.tsv. Is this the reason?
Is there a way to fix this?
%.8 is not enough if you have decimals printed with more than 8 digits. For these cases digits after the 8th decimal could be treated as a separate entry. That will make more numbers than expected. You should use a higher value for number of decimals in the scan format. For example,
fid=fopen('BMC3C/example/inputR_revised.tsv','r')
covTable = textscan(fid,['%s',repmat('%.18n',[1,20])],'HeaderLines',1);
This should give you the correct number of rows.

Matlab - Splitting a column into two (efficiently)

I had previously wrote some code to split 3 columns into 4, however the code was very inefficient and time consuming. As I am working with millions of rows it wasn't suitable. (Below is my previous code)
tline = fgetl(fid);
ID=tline(1:4);
IDN = str2double(ID);
Day=tline(6:8);
DayN = str2double(Day);
HalfHour=tline(9:10);
HalfHourN = str2double(HalfHour);
Usage=tline(12:end);
UsageN = str2double(Usage);
There must be a more efficient and quicker way of doing this?
Going back to basics, I have produced a x by 3 matrix. but require an x by 4 matrix
To show what I am trying to do, examining one row -
I am trying to change
1001 36501 1005
to
1001 365 01 1005
Any help would be much appreciated!
Edit:
The second column I am trying to divide into two, is always composed of 5 characters. I am trying to get the first 3 characters into their own column, likewise for the remaining characters.
What might take time in your case is actually the use of the str2double function. It is known that this built-in function becomes very slow when the data set is large. You might try to get rid of it if possible.
you can use modulo
ans = (36501 - mod(36501,100))/100
This would give you 365
if you want the 1, it is mod(36501,100)
so this would effectively split your second column into 2 different numbers, you can then re name them etc.
hmmm on second thoughts, if all your numbers on your second column are 5 digits, this can be extremely efficient, since mod is computed in matlab by b = a - m.*floor(a./m);
check http://uk.mathworks.com/help/matlab/ref/mod.html it should work for vectors (i.e. your second column)

Random Number in Octave

I need to generate a random number that is between .0000001 and 1, I have been using rand(1) but this only gives me 4 decimal points, is there any other way to do this generation?
Thanks!
From the Octave docs:
By default, Octave displays 5 significant digits in a human readable form (option ‘short’ paired with ‘loose’ format for matrices).
So it's probably an issue with the way you're printing the value rather than the value itself.
That same page shows the other output formats in addition to short, the one you may want to look in to is long, giving 15 significant digits.
And there is also the output_precision which can be set as per here:
old_val = output_precision (7)
disp (whatever)
old_val = output_precision (old_val)
Set the output_precision to 7 and it should be ok :)
Setting the output precision won't help though because the number can still be less than .0000001 in theory but you will only be displaying the first 7 digits. The simplest way is:
req=0;
while (req<.0000001)
req=rand(1);
end
It is possible that this could get you stuck in a loop but it will produce the right number. To display all the decimals you can also use the following command:
format long
This will show you 15 decimal places. To switch back go:
formay short

Splitting a string into various combinations

I am in need of some direction on splitting a string into various combinations. Actually my requirement is to split an integer, but I guess those can't be split, that's why I've converted the integer into string.
For eg.
I've a string "123456"
I want to split it like
12 34 56
123 45 6
12 345 6
12 3 456
and like wise. One more problem is, the size of the string can be variable. As I told, these are actually integers, so it can have from 4 places to 7-8 places, and so will be the size of resultant string to be split into combinations.
I currently don't have any code to achieve it. I've just performed the simple splitting operation in the command box, but couldn't think of the way of achieving the required result. Please give me some direction on what I can do.
Thanks.
First you can use the num2str() function to convert the integer value to a string. Once you have converted the number to a string, you can then use the length() function to determine how many digits there are in the number. You can then use the length of the string split up the number in various ways. The example below only splits in groups of two, but you can adjust as desired.
val=123456;
str=num2str(val);
i=1;
k=1;
len=2;%split values into groups of 2
while(i<length(str)-1)
val(k)=str2num(str(i:i+len-1));
i=i+len;
k=k+1;
end
if(i<=length(str))
val(k)=str2num(str(i:end));%catches the remainder
end

lisp program for hexadecimal to decimal

how to write a lisp program to convert given hexadecimal number into decimal. can somebody give me a clue.
thank you
I'm assuming its a homework problem so i'll give you a hint in the right direction.
Here is how to convert decimal to binary ->
Lets say you start with the number 9 in binary its 1001.
Start of by dividing 9 by 2. You get 4 with remainder 1. Save the remainder.
Now divide that 4 by 2 again, you get 2 with remainder 0. Save the remainder.
Divide that 2 again by 2, you get 1 with remainder 0. Save the remainder.
Divide that 1 by 2 and finally you get 0 with reaminder 1. Save the remainder.
If you read the saved remainders backwards you get 1001! The binary number you've been looking for. Best to push the remainders on the stack and pop them back out, that way they'll come out backwards.
It's already provided by Common Lisp.
The input is the string for the hex integer.
Then you parse the integer with radix 16
the result is the number
if you write the number with base 10 to an output stream, then you can get the number as a string in base 10