aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-095/ziameraj16/README.md45
-rw-r--r--challenge-095/ziameraj16/java/DemoStack.java88
-rw-r--r--challenge-095/ziameraj16/java/PalindromeNumber.java22
3 files changed, 141 insertions, 14 deletions
diff --git a/challenge-095/ziameraj16/README.md b/challenge-095/ziameraj16/README.md
index 2635085bf6..f3015ecf17 100644
--- a/challenge-095/ziameraj16/README.md
+++ b/challenge-095/ziameraj16/README.md
@@ -1,19 +1,36 @@
# Java solution by Mohammad Meraj Zia
-# Binary Tree to Linked List
-
-1. Prepare the tree using the `TreeNode` inner class in the `BinaryTreeToLinkedList`
-2. Call the `preOrderBinaryTreeTraversal()` method to convert the tree elements into a list.
-3. Call `printTreeElementAsList()` to print the list.
+# Palindrome Number
+To compile the code run
+```java
+javac PalindromeNumber.java
+```
+To run
+```java
+java PalindromeNumber
+```
## Example
+```bash
+/perlweeklychallenge-club/challenge-095/ziameraj16/java$ javac PalindromeNumber.java
+/perlweeklychallenge-club/challenge-095/ziameraj16/java$ java PalindromeNumber
+Enter the number to check if it is a palindrome
+1234554321
+1
+```
+# Demo Stack
+To compile the code run
```java
-TreeNode six = new TreeNode("6", null, null);
-TreeNode seven = new TreeNode("7", null, null);
-TreeNode five = new TreeNode("5", six, seven);
-TreeNode four = new TreeNode("4", null, null);
-TreeNode two = new TreeNode("2", four, five);
-TreeNode three = new TreeNode("3", null, null);
-TreeNode root = new TreeNode("1", two, three);
-binaryTreeToLinkedList.preOrderBinaryTreeTraversal(root);
+javac DemoStack.java
+```
+To run
+```java
+java DemoStack
+```
+## Example
+```bash
+To push into stack - `push <number>`
+To pop from the stack - `pop`
+To find the top element - `top`
+To find the minimum element - `min`
+To exit - `exit`
```
-
diff --git a/challenge-095/ziameraj16/java/DemoStack.java b/challenge-095/ziameraj16/java/DemoStack.java
new file mode 100644
index 0000000000..1fce9e3d83
--- /dev/null
+++ b/challenge-095/ziameraj16/java/DemoStack.java
@@ -0,0 +1,88 @@
+import java.util.*;
+
+public class DemoStack {
+
+ private static Scanner scanner = new Scanner(System.in);
+
+ private static final int MAX_SIZE = 1000;
+ int arr[] = new int[MAX_SIZE];
+ int top = -1;
+
+ public static void main(String[] args) {
+ DemoStack demoStack = new DemoStack();
+ System.out.println("Stack Example");
+ while (true) {
+ iterate(demoStack);
+ }
+ }
+
+ private static void iterate(DemoStack demoStack) {
+ System.out.println(String.join("\n",
+ "\n####################################",
+ "To push into stack - `push <number>`",
+ "To pop from the stack - `pop`",
+ "To find the top element - `top`",
+ "To find the minimum element - `min`",
+ "To exit - `exit`",
+ "####################################\n"));
+
+ final String[] options = scanner.nextLine().toLowerCase().split(" ");
+ if (options[0].equals("push")) {
+ demoStack.push(Integer.parseInt(options[1]));
+ } else if (options[0].equals("pop")) {
+ demoStack.pop();
+ } else if (options[0].equals("top")) {
+ demoStack.top();
+ } else if (options[0].equals("min")) {
+ demoStack.min();
+ } else if (options[0].equals("exit")) {
+ System.out.println("Bye!!!");
+ System.exit(1);
+ } else {
+ System.out.println("Invalid option: " + options[0]);
+ }
+ }
+
+ public boolean push(int number) {
+ if (top == MAX_SIZE - 1) {
+ System.err.println("Stack is full");
+ return false;
+ }
+ arr[++top] = number;
+ System.out.println(String.format("Number %s added to the stack", number));
+ return true;
+ }
+
+ public int pop() {
+ if (top < 0) {
+ System.err.println("No element to pop");
+ return Integer.MIN_VALUE;
+ }
+ int num = arr[top--];
+ System.out.println("Element popped: " + num);
+ return num;
+ }
+
+ public int top() {
+ if (top < 0) {
+ System.err.println("Stack is empty");
+ return Integer.MIN_VALUE;
+ }
+ int num = arr[top];
+ System.out.println("Top element: " + num);
+ return num;
+ }
+
+ public int min() {
+ if (top < 0) {
+ System.err.println("Stack is empty");
+ return Integer.MIN_VALUE;
+ }
+ final int[] copyArr = Arrays.copyOf(arr, top + 1);
+ Arrays.sort(copyArr);
+ int min = copyArr[0];
+ System.out.println("Minimum number in stack: " + min);
+ return min;
+ }
+}
+
diff --git a/challenge-095/ziameraj16/java/PalindromeNumber.java b/challenge-095/ziameraj16/java/PalindromeNumber.java
new file mode 100644
index 0000000000..7f09389099
--- /dev/null
+++ b/challenge-095/ziameraj16/java/PalindromeNumber.java
@@ -0,0 +1,22 @@
+import java.util.Scanner;
+
+public class PalindromeNumber {
+
+ public static void main(String[] args) {
+ Scanner scanner = new Scanner(System.in);
+ System.out.println("Enter the number to check if it is a palindrome");
+ final String input = scanner.next();
+ System.out.println(isNumberPalindrome(input));
+ }
+
+ public static int isNumberPalindrome(String input) {
+ for (int i = 0; i <= input.length() / 2; i++) {
+ if (input.charAt(i) != input.charAt(input.length() - 1 - i)) {
+ return 0;
+ }
+ }
+ return 1;
+ }
+
+
+}