Question: I'm a newbie in algorithms. I have recently started studying binary search and tryed to implement it on my own. The task is simple: we have an array of integers `a` and an integer `x`. If `a` contains `x` the result should be its index, otherwise the function should return `-1`.

Here is the code I have written:

```
def binary_search(a, x):
    l = 0
    r = len(a)
    while r - l > 0:
        m = (l + r) // 2
        if a[m] < x:
            l = m
        else:
            r = m
    if a[l] == x:
        return l
    return -1

```

But this code stucks in infinite cycle on `a = [1, 2]` and `x = 2`. I suppose, that I have incorrect cycle condition (probably, should be `r - l >= 0`), but this solution does not help. Where am I wrong?

Answer: 
Well, I think you [already see](https://stackoverflow.com/a/70111704/507738) what went wrong here.

It *could* have worked if the `for` loop would have been rewritten to this:

```java
Coordinate[] coords = {
    new Coordinate(0, -1),
    new Coordinate(1, 0),
    new Coordinate(-1, 0),
    new Coordinate(0, 1)
}; // Or a List<Coordinate>
for (int i = 0; i < arrayListString.size(); i++) {
    if (arrayListString.get(i).equals(directionMoved)) {
        direction = coords[i];
        return direction;
    }
}

```

But this code effectively uses two separate arrays to search for a letter and provide the appropriate Coordinate.

As OH GOD SPIDERS already said [in the comments](https://stackoverflow.com/questions/70111538/two-methods-should-do-the-same-thing-but-do-not#comment123937871_70111538), you're better off using a map. A map maps a key to a value.

```java
Map<String, Coordinate> map = Map.of(
    "U", new Coordinate(0, -1),
    "R", new Coordinate(1, 0),
    "L", new Coordinate(-1, 0),
    "D", new Coordinate(0, 1)
);

```

```java
Coordinate processHeadDirectionCoordinate(String directionMoved) {
    Coordinate c = map.get(directionArrow);
    if (coordinate != null) {
        direction = c;
    }
    return direction;
}

```

---

But your `switch` statement is also fine, although since newer Java versions, it could be replaced by a *switch expression*:

```java
Coordinate processHeadDirectionCoordinate(String directionMoved) {
    return direction = switch (directionMoved) {
        case "U" -> new Coordinate(0, -1);
        case "R" -> new Coordinate(1, 0);
        case "L" -> new Coordinate(-1, 0);
        case "D" -> new Coordinate(0, 1);
        default -> direction;
    };
}

```
In the method 2, your for statement is not really doing what you want. If you think about it, you are always going to return *new Coordinate(0,-1)*

```
        for(int i = 0; i < arrayListString.size(); i++) {
        if(arrayListString.get(i).equals(directionMoved)) {    
            direction = new Coordinate(0,-1);    
            return direction;    
        }               
        if(arrayListString.get(i).equals(directionMoved)) {    
            direction = new Coordinate(1,0);    
            return direction;    
        }               
        if(arrayListString.get(i).equals(directionMoved)) {    
            direction = new Coordinate(-1,0);    
            return direction;    
        }               
        if(arrayListString.get(i).equals(directionMoved)) {    
            direction = new Coordinate(0, 1);    
            return direction;
        }    
    }

```

Your if statements are all the same. That means, in the moment *arrayListString.get(i)* matches what you passed in *directionMoved* , the first if will be true and return that *Coordinate(0,-1)*