0

This would not be a problem for me if this was a "regular" program, however I am working with program synthesis and I have to have a code as compact as possible.

Consider the pseudocode below:

if A:
    return 'n'
elif B:
    return 'y'
else:
    if C:
        return 'n'
    else:
        return 'y'

A, B and C are boolean conditions (functions that returns a boolean in my real problem - their implementations are not important). I need this whole if-elif-else-if-else structure to be condensed into a single if-else structure.

The closest I got was:

if A or C:
    return 'n'
else:
    return 'y'

However, it fails for a single test case where A = False, B = True, C = True: it returns 'n' instead of 'y'.

The correct truth table is shown below for reference.

|-------|-------|-------|----------|
|   A   |   B   |   C   |  Result  |
|-------|-------|-------|----------|
|   T   |   T   |   T   |    n     |
|-------|-------|-------|----------|
|   T   |   T   |   F   |    n     |
|-------|-------|-------|----------|
|   T   |   F   |   T   |    n     |
|-------|-------|-------|----------|
|   T   |   F   |   F   |    n     |
|-------|-------|-------|----------|
|   F   |   T   |   T   |    y     |
|-------|-------|-------|----------|
|   F   |   T   |   F   |    y     |
|-------|-------|-------|----------|
|   F   |   F   |   T   |    n     |
|-------|-------|-------|----------|
|   F   |   F   |   F   |    y     |
|-------|-------|-------|----------|
ihavenoidea
  • 629
  • 1
  • 7
  • 26
  • 1
    Another way: `return B or not(A or C) ? 'y' : 'n'`. – dxiv Nov 25 '20 at 05:06
  • 1
    FYI: if you have the table [this is how it minimises](http://tma.main.jp/logic/logic.php?lang=en&type=3&v0=a&v1=b&v2=c&00=1&01=0&02=1&03=1&04=0&05=0&06=0&07=0). I converted `T=1`, `F=0`, `y=1`, `n=0`. [This is the main page](http://tma.main.jp/logic/index_en.html) - pick a table with the number of boolean conditions you have, express what you want the outputs to be (`1`, `0`, or `x` for doesn't matter) and you can get a minimised form. – VLAZ Nov 25 '20 at 07:15

1 Answers1

1

if A or ( C and not B): return 'n' else: return 'y'

Start from the logic table, and use boolean properties

alaniv
  • 26
  • 4