How do I make a program in Brainfuck that will add two digits? - brainfuck

I'm trying to make a program in Brainfuck which I think is also called "Brainflake", that will add two single digit decimal integers input with ASCII numeric characters and display the sum in ASCII numeric characters in the output. How would I go about doing so?

ASCII 0-9 are values 48-57.
So take both your ascii digits, subtract 48 from them and you get a number between 0 and 9.
For B times: subtract 1 from B and add 1 to A
Add 48 back to the result, and you have the ascii value for the sum.
Note that this only works if the sum only has a single digit as well.

As another answer stated, ASCII 0-9 are values 48-57.
++++++++ Set cell 0 to 8
[>++++++<-] Loop: Add 6 to cell1 8 times Cell1 contains 48 cell0 contains 0 ending on cell0
>> Move to cell2
, Read ASCII character to cell2
> Move to cell3
, Read ASCII character to cell3
<< Move to cell1
[<+> >-< -] Loop: Add 1 to cell0 and subtract 1 from cell1 and cell2 48 times ending on cell1
< Move to cell0
[>+< >>>-<<< -] Loop: Add 1 to cell1 and subtract 1 from cell0 and cell 3 48 times ending on cell 0
AT THIS POINT THE CELLS LOOK LIKE THIS (n1 and n2 are the numbers you entered):
0|48|n1|n2
>>> Move to cell3
[<+> -] Loop: Add 1 to cell2 n2 times
<< Move to cell1
[>+< -] Loop: Add 1 to cell2 48 times
> Move to cell2
. Output ASCII character
Here it is without the comments:
++++++++[>++++++<-]>>,>,<<[<+>>-<-]<[>+<>>>-<<<-]>>>[<+>-]<<[>+<-]>.
You can run it here: Try It Online
This service lets you step through the code (add breakpoints with #): https://www.iamcal.com/misc/bf_debug/

Related

How to add a digit to a string that is represented always using two characters?

I want to align some digits and numbers (which will be Strings) one below of another:
1
2
3
4
5
6
7
8
9
10
I want every digit from 1-9 to have a space in front so then it will look like this - (10 should have the 0 digit below 9, and not 1 digit below 9):
1
2
3
4
5
6
7
8
9
10
In the second case it looks more like right aligned.
Is there a way in Swift to do this ?
Use String(format:) with a field width of 2:
for i in 1...10 {
print(String(format: "%2d", i))
}
You can learn more about the String Format Specifiers here.
1- give them all equal leading
2- make equal width constraint for all
3- right align labels from 1-9
Apples NumberFormatter is what you want to use here.
let numbers:[NSNumber] = [1,2,3,4,5,6,7,8,9,10,11,12,100,1000]
let formatter = NumberFormatter()
formatter.minimumIntegerDigits = 2
for n in numbers {
print(formatter.string(from: n))
}
// prints 01, 02, 03 etc.
edit:
Ah, I didn't read the question properly, you want a space instead of a 0. Nevermind this answer :)

Selecting all columns in a cell array that contain a certain value in the first row?

I currently have a 4x3500 cell array. First row is a single number, 2 row is a single string, 3rd and 4th rows are also single numbers.
Ex:
1 1 2 3 3 4 5 5 5 6
hi no ya he ........ % you get the idea
28 34 18 0 3 ......
55 2 4 42 24 .....
I would like to be able to select all columns that have a certain value in the first row. ie if I wanted '1' as the first row value, it would return
1 1
hi no
28 34
55 2
Then I would like to sort based on the 2nd row's string. ie if I wanted to have'hi', it would return:
1
hi
28
55
I have attempted to do:
variable = cellArray{:,find(cellArray{1,:} == 1)}
However I keep getting:
Error using find
Too many input arguments.
or
Error using ==
Too many input arguments.
Any help would be much appreciated! :)
{} indexing will return a comma separated list which will provide multiple outputs. When you pass this to find, it's the same as passing each element of your cell array as a separate input. This is what leads to the error about to many input arguments.
You will want to surround the comma-separated list with [] to create an array or numbers. Also, you don't need find because you can just use logical indexing to grab the columns you want. Additionally, you will want to index using () to grab the relevant rows, again to avoid the comma-separated list.
variable = cellArray(:, [cellArray{1,:}] == 1)

Add unknown number of elements to another cell array from a certain index on

I've data in the format of
a{1}(1,1)=1
a{1}(1,2)=3
a{1}(1,3)=0.5
a{1}(2,1)=1
a{1}(2,2)=5
a{1}(3,1)=2
a{1}(3,2)=7
...
Now I'd like to place item 1 & 2 of all rows in this cell into another cell array from an specific index on. How should I do this? for example to place column 1 & 2 of these unknown number of row in the cell array 'b' from index 5 on without repetition I used:
b{1}(1,(5:end + 1)) = unique(a{1}(:,(1:2)))
I'd like the output of cell array 'b{1}' from 5th elements becomes '1 2 3 5 7'.
However, it is false and gives the "Subscripted assignment dimension mismatch." error. I dont know where is the problem. Is my snippet right?
Any help is appreciated

comparing cell of strings with different size in matlab

i want to compare [1*232] cells of strings containing individual words from text document with [1*23] cells that contain individual sentences from the same text, can any one help me how to program it in Matlab?
for example:
"pollution" and "trees" are two words in separate cells
and following are the two sentences in separate cells:
1. trees reduce pollution.
2. trees prevent floods.
what i want to do is put 0 or 1 after comparing pollution and trees with both the sentences or in my case "n" sentences and put 1's and 0's in the form of matrices.
any help will be appreciated.
You can use a combination of cellfun and strfind. Here is a try:
Sentences = {'trees reduce pollution' ; ...
'trees prevent floods' ; ...
'pollution is bad' ; ...
'flood is worse'};
Words = {'trees', 'pollution', 'bad'};
Out = NaN(numel(Sentences), numel(Words));
for i = 1:numel(Words)
Out(:,i) = cellfun(#(x) numel(strfind(x, Words{i})), Sentences);
end
And Out contains:
Out =
1 1 0
1 0 0
0 1 1
0 0 0
Hope this helps.

OpenOffice Compare two cell strings

So I am working on a macro in Basic, and where I'm at now I need to compare two columns for duplicates.
Here's what I have going so far:
for i = 0 To 5
Cell1 = Sheet.getCellByPosition(0,i)
for j = 0 to 5
Cell2 = Sheet.getCellByPosition(1,j)
rem COMPARISON WOULD HAPPEN HERE
Next j
Next i
I would like to do something along the lines of: if Cell1.String == Cell2.String then ...
This is my first attempt at writing a macro and so I would greatly appreciate any help and/or guidance.
Thanks!
Also on a side note if anyone know of good tutorials.documentation for this other than wiki, I would be extremely grateful for the link
You should store all values of the first column into an array and then compare every value of the second column with all entries of the array using a simple recursion.
A code that may work is
Dim firstcolumn(5) As String
For i = 0 to 5
firstcolumn(i) = Sheet.getCellByPosition(0,i)
Next i
For j = 0 to 5
Cell2 = Sheet.getCellByPosition(1,j)
for i = 0 to 5
if Cell2 = firstcolumn(i) then
MsgBox("The value of the cell " + i + " in the first column is the same with the value of the cell " + j + " of the second column")
Next i
Next j
The best place to look for code samples is the openoffice forum https://forum.openoffice.org/en/forum/
I hope that the above will help you.