How can I duplicate a cell value to another cell in Brainfuck? - brainfuck

What I want to do is to first ask for an input for the value of cell 0(the first cell), then duplicate that input into the next cell(cell 1) while retaining the input value on cell 0. For example, if I typed in an input of 1, I would expect both cell 0 and cell 1 to have the value of 49(the ascii decimal value of 1 is 49. Check http://www.asciitable.com/ for other ascii values). My idea is to first ask for an input using the , command at cell 0, then duplicating that value to cell 1 and 2 using this code
[>+>+<<-](but cell 0 becomes 0 after this loop), then moving the value of cell 2 back to cell 0 using this code >>[<<+>>-](I need the two > at the beginning to move the pointer back to cell 2). I would like to know if there's a faster/more efficient way of doing this. Thanks!
The code im using so far to do this:
,[>+>+<<-]>>[<<+>>-]

Nope, there isn't. That's the most efficient way to do it. Brainfuck just isn't a very expressive language.

Related

Are BrainF*ck tape spots overflowing? [duplicate]

What happens in Brainfuck if I try to "-" in a cell that contains a 0?
>-
Also, what happens if I try to start a loop while pointing to a 0 cell?
>[]
Edit:
I forgot to mention it, but I'm trying to make an interpreter.
Here's a piece of code I was given to use as an example:
;>;<[->++<][->++<]
In the second loop, the cell pointed at is "0", so my program starts an infinite loop.
Brainfuck is a very implementation dependent language.
Usually, Brainfuck cells hold values ranging from 0-255, so 8 bit unsigned integer values.
If you try to decrement a cell with a value of zero, as usually happens with computers you will perform an "underflow", meaning the value will go from 0 to 255. Similarly, if you try to increment a cell with a value of 255 - you will get 0.
Looping in brainfuck can be though of with this piece of pseudo code:
[SOME_CODE]
is like
while(*ptr!=0){SOME_CODE}
So long as you are not pointing at a cell with a value of 0 in the beginning of the loop, you run the code within the brackets, and repeat.
I suggest you take a look at https://fatiherikli.github.io/brainfuck-visualizer/
Most implementations of brainfuck will roll over from 0 to 255, and 255 to 0. Many programs will not work at all if they can't roll over.
In your program example, the ';' is not a command. It should probably be commas.

How to update values from rows on a matrix

I have a matrix: 6 rows x 18 columns
and I want to ask the user which row he wants to change to 0 and make it happen, but if he inputs 0 it just exits the program
row = input('Input 0 to exit\n\n Row to eliminate:', 's');
if row ~= 0
M(row,:) = 0;
disp(M)
end
I want to to change the whole input row to 0, but it somehow creates 44 more rows below the original matrix (all 0) and doesn't change the first 6 rows(original matrix)
row is a char vector, not a number. For example if you type “4”, the variable contains the character 4, which has a numeric value of 52 (ASCII code). Using that to index will create a row #52 and fill it with zeros. To make that row it needed to make the intermediate rows as well, whose values default to 0.
To solve the problem, remove the 's' from the call to input. This will cause the return value to be interpreted as a MATLAB expression, rather than directly returning the entered text as a char vector.
However, if there is a chance that your program is used by a malicious user or a stupid user, this solution is dangerous (as indicated by #Luis Mendo in a comment) because it evaluates whatever the user types, which means the user can e.g. type a command that wipes your hard drive.
A safer solution is to use str2double to convert the char array output of input to a number that you can use to index.

LibreOffice Calc -- countif based on string length

I'd like to setup a countif function that counts the cells in a range based on the cell string length.
I've tried inserting the formula as an argument to the countif function, but I can't get it to work.
I wanted to compare the length for each cell in the range with a specific lenght defined somewhere else, but I don't know how to reference back the cell in the range.
One of my failed attempts was =COUNTIF($'Delovni list1'.$D$3:$D$102;">2")
given that the cells in the range are formatted as text. But the above doesn't even work if there are numbers in the cells.
My use case is this: I need to count the cells with 1 specific letter and cells which have many characters.
Thank you.
seba
I found this to work:
=COUNTIF($'Delovni list1'.$D$3:$D$102;"[:alnum:]..*")
regards,
seba

MATLAB UITABLE AND FORMAT OPTIONS

I have followed many posts about format of numbers in Matlab. Now I have set up Matlab uitable in such a way that one can fill the table cell by cell. I want the first column to maintain numbers up to say 12 digits, so I set the format to longG. Matlab uitable is refusing to accept my format and the frustrating part is, in the command window all digits are shown even up to 15 digits with format longG option. This is the sample;
colNam={'W','X','Y','Z'};
t=uitable('ColumnName',colNam,'Data',zeros(4),'ColumnEditable',...
[true true true true],'ColumnWidth',{150,'auto'},...
'ColumnFormat',{'numeric'});
set(t,'ColumnFormat',{'longG'});
I know of sprintf and fprintf as has been explained elsewhere. But please what we are refusing to realize is that, these change the class of the number from double to char. What if you want to set up the table so that it increases by 1 dynamically (credits to thewaywewalk)? We can't add double to char for this purpose. Please this is frustrating. Any suggestions? Thank you.
There is no such ColumnFormat property called "longG", where dis you see that ?
You have the possibility of manipulating the Java part of the Uitable in order to really display numeric values... but I think the best (and easiest) way is to use char.
If you want to increase the values, just convert it first to numeric, increase, then convert it back.
Based on John's suggestion, I put up this code and I could set the table to accept any number of digits.
f=figure;
dat=[101100220001;101100220002;101100220003;101100220004;101100220005];
colNam={'W'};
t=uitable('ColumnName',colNam,'Data',dat,'ColumnEditable',...
[true],'ColumnWidth',{150},'pos',[5 250 200 120]);
newdat=get(t,'Data');
newdat=strtrim(cellstr(num2str(newdat))); %Convert data to cell array of strings
tt=uitable('ColumnName',colNam,'Data',newdat,'pos',[300 250 200 120]);

calculating the number of columns in a row of a cell array in matlab

i've got a cell array full of numbers, with 44 rows and different column length in each row
how could i calculate the number of columns in each row?(the columns which their contents are not empty)
i've used 2 different ways which both of them where wrong
the 1st one:
%a is the cell array
s=length(a)
it gives 44 which is the number of rows
the 2nd one
[row, columms]=size(a)
but it doesn't work either cause the number of columns is different in each row.
at least i mean the number of columns which are not empty
for example i need the number of columns in row one which it is 43(a{1 1:43}) but it gives the number of columns for each elements like a{1,1} which is 384 or a{1,2},a{1,3} and so on
You need to access each member of the cell array separately, you are looking for the size of the data contained in the cell - the cell is the container. Two methods
for loop:
cell_content_lengths=zeros(1,length(a));
for v=1:length(a)
cell_content_lengths(v)=length(a{v});
end
cellfun:
cell_content_lengths=cellfun(#length,a);
Any empty cells will just have length 0. To extend the for-loop to matrices is trivial, and you can extend the cellfun part to cells containing matrix by using something like this, if you are interested:
cell_content_sizes=cell2mat(cellfun(#length,a,'uniformoutput',false));
(Note for the above, each element of a needs to have the same dimension, otherwise it will give errors about concatenating different size matrices)
EDIT
Based on your comment I think I understand what you are looking for:
non_empty_cols = sum(~cellfun(#isempty,a),2);
With thanks to #MZimmerman6 who understood it before me.
So what you're really asking, is "How many non-empty elements are in each row of my cell array?"
filledCells = ~cellfun(#isempty,a);
columns = sum(filledCells,2);