Repository: TheAlgorithms/Java
Pull request #4278 (2023-08-02 13:11:13)

Create StrobogrammaticNumber.java
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.


HimeshKohad: > Could you please add some JUnit tests? (see https://github.com/TheAlgorithms/Java/tree/master/src/test/java/com/thealgorithms)

I have created PR #4273 StrobogrammaticNumberTest. Can you help me with its build failure?
siriak: It says Error:  /home/runner/work/Java/Java/src/test/java/com/thealgorithms/maths/StrobogrammaticNumberTest.java:[11,9] cannot find symbol
  symbol:   class StrobogrammaticNumber
  location: class com.thealgorithms.maths.StrobogrammaticNumberTest
  Because tests and code must be in the same PR
siriak: https://github.com/TheAlgorithms/Java/actions/runs/5717366238/job/15490947507?pr=4273
HimeshKohad: > It says Error: /home/runner/work/Java/Java/src/test/java/com/thealgorithms/maths/StrobogrammaticNumberTest.java:[11,9] cannot find symbol symbol: class StrobogrammaticNumber location: class com.thealgorithms.maths.StrobogrammaticNumberTest Because tests and code must be in the same PR

How can I make a single PR for test and code?
siriak: Commit them to the same branch and open a PR for that branch
debasishbsws: If you already raised a PR, then you can just add the test to the same branch, commit and push it, and it will be reflected in the same PR.
HimeshKohad: > If you already raised a PR, then you can just add the test to the same branch, commit and push it, and it will be reflected in the same PR.

Thank you for the help!

Patch:
diff --git a/src/main/java/com/thealgorithms/maths/StrobogrammaticNumber.java b/src/main/java/com/thealgorithms/maths/StrobogrammaticNumber.java
new file mode 100644
index 00000000..bcd9a846
--- /dev/null
+++ b/src/main/java/com/thealgorithms/maths/StrobogrammaticNumber.java
@@ -0,0 +1,43 @@
+package com.thealgorithms.maths;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A strobogrammatic number is a number that remains the same when rotated 180 degrees.
+ * In other words, the number looks the same when rotated upside down.
+ * Examples of strobogrammatic numbers are "69", "88", "818", and "101".
+ * Numbers like "609" or "120" are not strobogrammatic because they do not look the same when rotated.
+ */
+public class StrobogrammaticNumber {
+    /**
+     * Check if a number is strobogrammatic
+     * @param number the number to be checked
+     * @return true if the number is strobogrammatic, false otherwise
+     */
+    public boolean isStrobogrammatic(String number) {
+        Map<Character, Character> strobogrammaticMap = new HashMap<>();
+        strobogrammaticMap.put('0', '0');
+        strobogrammaticMap.put('1', '1');
+        strobogrammaticMap.put('6', '9');
+        strobogrammaticMap.put('8', '8');
+        strobogrammaticMap.put('9', '6');
+
+        int left = 0;
+        int right = number.length() - 1;
+
+        while (left <= right) {
+            char leftChar = number.charAt(left);
+            char rightChar = number.charAt(right);
+
+            if (!strobogrammaticMap.containsKey(leftChar) || strobogrammaticMap.get(leftChar) != rightChar) {
+                return false;
+            }
+
+            left++;
+            right--;
+        }
+
+        return true;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/StrobogrammaticNumberTest.java b/src/test/java/com/thealgorithms/maths/StrobogrammaticNumberTest.java
new file mode 100644
index 00000000..2269291e
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/StrobogrammaticNumberTest.java
@@ -0,0 +1,19 @@
+package com.thealgorithms.maths;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.jupiter.api.Test;
+
+class StrobogrammaticNumberTest {
+
+    @Test
+    void testIsStrobogrammatic() {
+        StrobogrammaticNumber strobogrammaticNumber = new StrobogrammaticNumber();
+        assertThat(strobogrammaticNumber.isStrobogrammatic("69")).isTrue();
+        assertThat(strobogrammaticNumber.isStrobogrammatic("88")).isTrue();
+        assertThat(strobogrammaticNumber.isStrobogrammatic("818")).isTrue();
+        assertThat(strobogrammaticNumber.isStrobogrammatic("101")).isTrue();
+        assertThat(strobogrammaticNumber.isStrobogrammatic("609")).isTrue();
+        assertThat(strobogrammaticNumber.isStrobogrammatic("120")).isFalse();
+    }
+}