Brainfuck. How to check a palindrome?

The problem is to check if the sequence is a palindrome using Brainfuck .

input is a sequence of numbers

output 0 if not palindorme, else 1.

I have one idea: Let's say we have a sequence 1 2 3 2 1. We can remember the first cell from our array in a variable (this is done using the "!" Operation),

  • then change the value 1 to 0 (do it with the "0" operation), the array will be 0 2 3 2 1,
  • then we go to the end of the array until we meet 0 (do this using '> [>]'),
  • then we take the number from the variable and get the sequence 0 2 3 2 1 1.
  • The next step should be to compare the last two numbers if they are equal. continue algo from begining else do something ...

I don't know how to implement the last step.

+3


source to share


1 answer


Please excuse me if I don't write the whole program in brainfuck,

This is the basic idea:



  • Reading input (after this, the pointer must be the last)
  • remember symbol
  • Set value 0
  • Go to first [<]
  • Compare with the remembered symbol (see Brainfuck compare 2 numbers greater or less )
  • If not equal, print 1
  • If the next (>) cell array is 0, print 0
  • Move pointer to end [>]
  • Go back to step 2
+1


source







All Articles