Programming without using conditional constructs

One friend told me that any program can be written without using if / else. Of course, I was immediately outraged and formulated to him (and at the same time myself) the simplest task: to write a program that would be happy if you input, for example, the word “cookie” to it, and be upset otherwise; but you cannot use any constructions that change the direction of the program — that is, it must be strictly linear. The solution is under the cut.

Pseudo-correct decisions

I implemented my version of the solution in Java. Therefore, probably, my first thought was to use polymorphism. But, frankly, I still do not understand how to apply this principle to this task. Perhaps you need to define a method in a class Stringand override it for an object "cake"? Unfortunately in Java this is not feasible.

The real first thought was, if you can’t do without if, you need to use something library that can become its replacement. The choice fell on Hashtable.

First pancake:
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String input = in.readLine();
    HashMap map = new HashMap();
    map.put(true, "Yummy!!!");
    map.put(false, "The cake is a lie :(");
    System.out.println(map.get(input.equals("cake")));

It seems that the conditions of the task are fulfilled. But let me, equals()is this not an outright scam? Indeed, in essence, it is through if / else that it is implemented! Well, you can do without it. And at the same time and without Boolean values. You just need to scroll through the values HashMaptwice:
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String input = in.readLine();
    Object yeah = new Object();
    HashMap map = new HashMap();
    map.put("cake", yeah);
    map.put(yeah, "Yummy, thanks!!!");
    map.put(null, "You are still lying to me!");
    System.out.println(map.get(map.get(input)));

And if this is implemented in another language in which hash tables are sewn into the syntax, then the task will completely turn into a single line. Is this a perfect solution? I believe that still not. After all, the internal implementation of the hash table will just need to use conditional constructs to find the key. Is it possible to do without them?

One stop solution

In order to build the right solution, suitable for any language, you need to go not up to the abstraction, but down to the numbers. What will remain with us? Well of course the math operations! In the language of mathematics, a condition can be written as (assuming, of course, that truth is 1 and false is 0). Then, to solve the problem, it is enough to write a function equal to 1 for the string “cake” (or “cookie”, or “candy”), and 0 in other cases. The message output may look like this:ifxthenaelsebx*a+(1-x)*b
    int x = isCake(input);
    System.out.print((char)(x*'Y'+(1-x)*'N'));
    System.out.print((char)(x*'e'+(1-x)*'o'));
    System.out.print((char)(x*'s'));

When writing a characteristic function for a cake, it is worth considering that the string length is a variable value, but without branching operations we cannot check an arbitrary number of characters. The solution is simple: we add the string with five zeros (guaranteeing that it has at least five characters) and check that this is the first five characters "cake\0". Total:
import java.io.*;
class CakeEater {
    static public void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String input = in.readLine().concat("\0\0\0\0\0");
        int x = input.charAt(0)^'c';
        x |= input.charAt(1)^'a';
        x |= input.charAt(2)^'k';
        x |= input.charAt(3)^'e';
        x |= input.charAt(4);
        //дальше несколько утомительный фокус, чтобы превратить ненулевое число в 1
        x = x | (x>>1) | (x>>2) | (x>>3) | (x>>4) | (x>>5) | (x>>6) | (x>>7) | (x>>8)
          | (x>>9) | (x>>10) | (x>>11) | (x>>12) | (x>>13) | (x>>14) | (x>>15);
        x = 1 - (x&1);
        System.out.print((char)(x*'Y'+(1-x)*'N'));
        System.out.print((char)(x*'e'+(1-x)*'o'));
        System.out.println((char)(x*'s'));
    }
}

Conclusion: you can write a simple program without using if / else, but it already becomes cumbersome. And try to write a linear program that calculates, for example, factorial!

Also popular now: