From c2bdd9385373298d12931d0f2e93bdbd8de1a591 Mon Sep 17 00:00:00 2001 From: nweniwincs4 Date: Sat, 15 Feb 2025 11:17:56 +1100 Subject: [PATCH 1/2] added: a new file for git testing --- test.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test.java diff --git a/test.java b/test.java new file mode 100644 index 00000000..e69de29b From 5cadb3c3877778fe29ba98017af07f636f2f8967 Mon Sep 17 00:00:00 2001 From: nweniwincs4 Date: Sun, 16 Feb 2025 19:45:59 +1100 Subject: [PATCH 2/2] added: miscellaneous testing --- nnw_python/MergeSortTest.py | 40 +++++++ nnw_python/QueueWithLinkedList.py | 65 ++++++++++++ nnw_python/QueueWithList.py | 38 +++++++ nnw_python/StackTest.py | 35 ++++++ .../java/com/nnw/AdvancedSortingTest.java | 47 ++++++++ src/main/java/com/nnw/ArraySortingTest.java | 23 ++++ src/main/java/com/nnw/CreateFileTest.java | 100 ++++++++++++++++++ src/main/java/com/nnw/DateTest.java | 18 ++++ src/main/java/com/nnw/EnumTest.java | 31 ++++++ .../java/com/nnw/ExceptionHandlingTest.java | 37 +++++++ src/main/java/com/nnw/HashMapTest.java | 19 ++++ src/main/java/com/nnw/HashSetTest.java | 31 ++++++ src/main/java/com/nnw/IteratorTest.java | 24 +++++ src/main/java/com/nnw/LambdaTest.java | 34 ++++++ src/main/java/com/nnw/LinkedListTest.java | 32 ++++++ src/main/java/com/nnw/ListSortingTest.java | 25 +++++ src/main/java/com/nnw/MultiThreadTest.java | 24 +++++ .../java/com/nnw/RegularExpressionTest.java | 42 ++++++++ src/main/java/com/nnw/SortEvenFirstTest.java | 33 ++++++ src/main/java/com/nnw/filename.txt | 5 + .../com/nnw_possibleQ/BinarySearchTest.java | 32 ++++++ .../nnw_possibleQ/ReverseLinkedListTest.java | 25 +++++ .../com/rampatra/arrays/BooleanMatrix.java | 2 +- .../com/rampatra/arrays/CelebrityProblem.java | 2 +- test.java | 0 25 files changed, 762 insertions(+), 2 deletions(-) create mode 100644 nnw_python/MergeSortTest.py create mode 100644 nnw_python/QueueWithLinkedList.py create mode 100644 nnw_python/QueueWithList.py create mode 100644 nnw_python/StackTest.py create mode 100644 src/main/java/com/nnw/AdvancedSortingTest.java create mode 100644 src/main/java/com/nnw/ArraySortingTest.java create mode 100644 src/main/java/com/nnw/CreateFileTest.java create mode 100644 src/main/java/com/nnw/DateTest.java create mode 100644 src/main/java/com/nnw/EnumTest.java create mode 100644 src/main/java/com/nnw/ExceptionHandlingTest.java create mode 100644 src/main/java/com/nnw/HashMapTest.java create mode 100644 src/main/java/com/nnw/HashSetTest.java create mode 100644 src/main/java/com/nnw/IteratorTest.java create mode 100644 src/main/java/com/nnw/LambdaTest.java create mode 100644 src/main/java/com/nnw/LinkedListTest.java create mode 100644 src/main/java/com/nnw/ListSortingTest.java create mode 100644 src/main/java/com/nnw/MultiThreadTest.java create mode 100644 src/main/java/com/nnw/RegularExpressionTest.java create mode 100644 src/main/java/com/nnw/SortEvenFirstTest.java create mode 100644 src/main/java/com/nnw/filename.txt create mode 100644 src/main/java/com/nnw_possibleQ/BinarySearchTest.java create mode 100644 src/main/java/com/nnw_possibleQ/ReverseLinkedListTest.java delete mode 100644 test.java diff --git a/nnw_python/MergeSortTest.py b/nnw_python/MergeSortTest.py new file mode 100644 index 00000000..82d83532 --- /dev/null +++ b/nnw_python/MergeSortTest.py @@ -0,0 +1,40 @@ +def merge(left, right): + result = [] + i = j = 0 + + while i < len(left) and j < len(right): + if left[i] < right[j]: + result.append(left[i]) + i += 1 + else: + result.append(right[j]) + j += 1 + + result.extend(left[i:]) + result.extend(right[j:]) + + return result + +def mergeSort(arr): + step = 1 # Starting with sub-arrays of length 1 + length = len(arr) + + while step < length: + for i in range(0, length, 2 * step): + left = arr[i:i + step] + right = arr[i + step:i + 2 * step] + + merged = merge(left, right) + + # Place the merged array back into the original array + for j, val in enumerate(merged): + arr[i + j] = val + + step *= 2 # Double the sub-array length for the next iteration + + return arr + +# unsortedArr = [3, 7, 6, -10, 15, 23.5, 55, -13] +unsortedArr = [3, 7, 6, 5] +sortedArr = mergeSort(unsortedArr) +print("Sorted array:", sortedArr) \ No newline at end of file diff --git a/nnw_python/QueueWithLinkedList.py b/nnw_python/QueueWithLinkedList.py new file mode 100644 index 00000000..8b1aa775 --- /dev/null +++ b/nnw_python/QueueWithLinkedList.py @@ -0,0 +1,65 @@ +class Node: + def __init__(self, data): + self.data = data + self.next = None + +class Queue: + def __init__(self): + self.front = None + self.rear = None + self.length = 0 + + def enqueue(self, element): + new_node = Node(element) + if self.rear is None: + self.front = self.rear = new_node + self.length += 1 + return + self.rear.next = new_node + self.rear = new_node + self.length += 1 + + def dequeue(self): + if self.isEmpty(): + return "Queue is empty" + temp = self.front + self.front = temp.next + self.length -= 1 + if self.front is None: + self.rear = None + return temp.data + + def peek(self): + if self.isEmpty(): + return "Queue is empty" + return self.front.data + + def isEmpty(self): + return self.length == 0 + + def size(self): + return self.length + + def printQueue(self): + temp = self.front + while temp: + print(temp.data, end=" ") + temp = temp.next + print() + +# Create a queue +myQueue = Queue() + +myQueue.enqueue('A') +myQueue.enqueue('B') +myQueue.enqueue('C') +print("Queue: ", end="") +myQueue.printQueue() + +print("Dequeue: ", myQueue.dequeue()) + +print("Peek: ", myQueue.peek()) + +print("isEmpty: ", myQueue.isEmpty()) + +print("Size: ", myQueue.size()) \ No newline at end of file diff --git a/nnw_python/QueueWithList.py b/nnw_python/QueueWithList.py new file mode 100644 index 00000000..cc201b37 --- /dev/null +++ b/nnw_python/QueueWithList.py @@ -0,0 +1,38 @@ +class Queue: + def __init__(self): + self.queue = [] + + def enqueue(self, element): + self.queue.append(element) + + def dequeue(self): + if self.isEmpty(): + return "Queue is empty" + return self.queue.pop(0) + + def peek(self): + if self.isEmpty(): + return "Queue is empty" + return self.queue[0] + + def isEmpty(self): + return len(self.queue) == 0 + + def size(self): + return len(self.queue) + +# Create a queue +myQueue = Queue() + +myQueue.enqueue('A') +myQueue.enqueue('B') +myQueue.enqueue('C') +print("Queue: ", myQueue.queue) + +print("Dequeue: ", myQueue.dequeue()) + +print("Peek: ", myQueue.peek()) + +print("isEmpty: ", myQueue.isEmpty()) + +print("Size: ", myQueue.size()) \ No newline at end of file diff --git a/nnw_python/StackTest.py b/nnw_python/StackTest.py new file mode 100644 index 00000000..1382c3ed --- /dev/null +++ b/nnw_python/StackTest.py @@ -0,0 +1,35 @@ +class Stack: + def __init__(self): + self.stack = [] + + def push(self,item): + self.stack.append(item) + + def pop(self): + self.stack.pop() + + def peek(self): + return self.stack[-1] + + def isEmpyt(self): + return len(self.stack)==0 + + def size(self): + return len(self.stack) + + +if __name__=="__main__": + stack = Stack() + stack.push("A") + stack.push("B") + stack.push("C") + stack.push("D") + print(f" Stack's items : {stack.stack}") + print(f" Stack Size : {stack.size()}") + print(f" Stack's last item : {stack.peek()}") + stack.pop() + print(f" Stack's items after removing last item: {stack.stack}") + print(f" Is Stack empty? : {stack.isEmpyt()}") + + + diff --git a/src/main/java/com/nnw/AdvancedSortingTest.java b/src/main/java/com/nnw/AdvancedSortingTest.java new file mode 100644 index 00000000..fbf99fc3 --- /dev/null +++ b/src/main/java/com/nnw/AdvancedSortingTest.java @@ -0,0 +1,47 @@ + +import java.util.ArrayList; +import java.util.Collections; + +public class AdvancedSortingTest { + static class Car{ + int year; + String name; + public Car(int year,String name){ + this.year=year; + this.name=name; + } + } + public static void main(String[] args) { + Car c1= new Car(2010,"Mazda"); + Car c2 = new Car(2012,"Mazda"); + Car c3 = new Car(2012,"BMW"); + Car c4 = new Car(2012,"BMW"); + ArrayList myCars = new ArrayList<>(); + myCars.add(c1); + myCars.add(c2); + myCars.add(c3); + myCars.add(c4); + // Collections.sort(myCars, (obj1, obj2) -> { + // Car a = (Car) obj1; + // Car b = (Car) obj2; + // if (a.year < b.year) return -1; + // if (a.year > b.year) return 1; + // return 0; + // }); + + Collections.sort(myCars,(obj1,obj2)->{ + Car c_obj1= (Car)obj1; + Car c_obj2= (Car)obj2; + if(c_obj1.yearc_obj2.year) + return 1; + // return 0; + else + return c_obj1.name.compareTo(c_obj2.name); + }); + myCars.forEach(car->{System.out.printf("Car Brand : %s \t Model Year : %d \n",car.name,car.year);}); + } +} + + diff --git a/src/main/java/com/nnw/ArraySortingTest.java b/src/main/java/com/nnw/ArraySortingTest.java new file mode 100644 index 00000000..ca9b250e --- /dev/null +++ b/src/main/java/com/nnw/ArraySortingTest.java @@ -0,0 +1,23 @@ +import java.util.Arrays; + +public class ArraySortingTest { + public static void main(String[] args) { + String[] cars = {"Volvo", "BMW", "Tesla", "Ford", "Fiat", "Mazda", "Audi"}; + System.out.println("Ascending order : "); + Arrays.sort(cars); + for (String i : cars) { + System.out.println(i); + } + System.out.printf("Lowest value : %s \n",cars[0]); + System.out.printf("Highest value : %s \n",cars[cars.length-1]); + System.out.println("------------------------"); + System.out.println("Descending order : "); + Arrays.sort(cars,(obj1,obj2)->{ + int comp = obj1.compareTo(obj2); + return comp*-1; + }); + for (String i : cars) { + System.out.println(i); + } + } +} diff --git a/src/main/java/com/nnw/CreateFileTest.java b/src/main/java/com/nnw/CreateFileTest.java new file mode 100644 index 00000000..202e7c12 --- /dev/null +++ b/src/main/java/com/nnw/CreateFileTest.java @@ -0,0 +1,100 @@ + +import java.io.File; // Import the File class +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Scanner; + +public class CreateFileTest { + + public static void main(String[] args) { + // try { + // File myFile = new File("filename.txt"); + // // windows + // // File myFile = new File("C:\\Users\\MyName\\filename.txt"); + // // mac + // // File myFile = new File("..//filename.txt"); // mac: step back to the higher level + // // File myFile = new File("/Users/nweniwin/Documents/Java Study/Algorithms-and-Data-Structures-in-Java/filename.txt"); + // if (myFile.createNewFile()){ + // System.out.printf("New file : %s is created.",myFile.getName()); + // // System.out.printf("The file is created at %s",myFile.getAbsolutePath()); + // }else{ + // System.out.println("File already exists"); + // // System.out.printf("The file is at %s",myFile.getAbsolutePath()); + // } + // } catch (IOException e) { + // System.out.println("An error occured"); + // e.printStackTrace(); + // } + + try { + FileWriter myWriter = new FileWriter("filename.txt"); + String st = "Files in Java might be tricky, but it is fun enough!!!!!"; + // for(String w:st.split(" ")){ + for (char w : st.toCharArray()) { + myWriter.write(w + "\n"); + } + myWriter.close(); + System.out.println("Successfully wrote to the file."); + } catch (IOException e) { + System.out.println("An error occurred."); + e.printStackTrace(); + } + File myFile = new File("filename.txt"); + if (myFile.exists()) { + System.out.printf("File Name : %s \n", myFile.getName()); + System.out.printf("File Absolute Path : %s \n", myFile.getAbsolutePath()); + System.out.printf("File Path: %s \n", myFile.getPath()); + System.out.printf("File size in bytes : %s \n", myFile.length()); + System.out.printf("Can Write : %s \n", myFile.canRead()); + System.out.printf("Can Read : %s \n", myFile.canWrite()); + } + if (myFile.delete()) { + System.out.printf("File Name : %s has been deleted", myFile.getName()); + } + // To create a directory + try { + File myfolder = new File("files"); + if (myfolder.createNewFile()) { + System.out.printf("The folder name : %s has been created.", myfolder.getAbsolutePath()); + } + if (myfolder.delete()) { + System.out.printf("The folder name : %s has been deleted.", myfolder.getAbsolutePath()); + } + } catch (IOException e) { + System.out.println("An error occured in creating a folder"); + } + + System.out.println("----------------------"); + try { + FileWriter myWriter = new FileWriter("filename.txt"); + Scanner myScanner = new Scanner(System.in); + for (int i=1;i<=5;i++){ + String ln = myScanner.nextLine(); + myWriter.write(ln + "\n"); + if (i==5){ + System.out.println("Do you have anything to say? (Type yes or no)"); + String ans = myScanner.nextLine(); + if (ans.toLowerCase().equals("yes")){ + i=0; + } + } + } + myScanner.close(); + myWriter.close(); + } catch (IOException e) { + System.out.println("An error occured"); + } + + try { + System.out.println("-----------------"); + Scanner myScanner1 = new Scanner(new File("filename.txt")); + System.out.println("Here is the file content you have saved"); + while(myScanner1.hasNext()){ + System.out.println(myScanner1.nextLine()); + } + } catch (FileNotFoundException e) { + System.out.println(e.getMessage()); + } + } +} diff --git a/src/main/java/com/nnw/DateTest.java b/src/main/java/com/nnw/DateTest.java new file mode 100644 index 00000000..61576b48 --- /dev/null +++ b/src/main/java/com/nnw/DateTest.java @@ -0,0 +1,18 @@ +import java.time.LocalDate; // import the LocalDate class +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; + +public class DateTest { + public static void main(String[] args) { + LocalDate obj1 = LocalDate.now(); // Create a date object + System.out.printf("Local Date : %s \n",obj1); // Display the current date + LocalTime obj2 = LocalTime.now(); + System.out.printf("Local Time : %s \n",obj2); + LocalDateTime obj3 = LocalDateTime.now(); + System.out.printf("Local Date Time : %s \n",obj3); + DateTimeFormatter df = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); + String formatted_obj3 = obj3.format(df); + System.out.printf("Local Date Time After format : %s \n",formatted_obj3); + } +} \ No newline at end of file diff --git a/src/main/java/com/nnw/EnumTest.java b/src/main/java/com/nnw/EnumTest.java new file mode 100644 index 00000000..0fd5ae15 --- /dev/null +++ b/src/main/java/com/nnw/EnumTest.java @@ -0,0 +1,31 @@ + +public class EnumTest { + + enum Gender{ + MALE, + FEMALE + } + public static void main(String[] args) { + for (Level myVar : Level.values()) { + System.out.println(myVar); + } + System.out.println("-----------------------"); + Gender m = Gender.MALE; + System.out.println(m); + System.out.println("-----------------------"); + System.out.println(Math.sqrt(100000)); + System.out.println(Math.pow(100, 2)); + System.out.println(Math.PI); + System.out.println("-----------------------"); + System.out.println(Math.random()*101); + java.util.Random r= new java.util.Random(); + int num = r.nextInt(1, 3); + System.out.println(num); + + } +} +enum Level { + LOW, + MEDIUM, + HIGH +} diff --git a/src/main/java/com/nnw/ExceptionHandlingTest.java b/src/main/java/com/nnw/ExceptionHandlingTest.java new file mode 100644 index 00000000..31d88f58 --- /dev/null +++ b/src/main/java/com/nnw/ExceptionHandlingTest.java @@ -0,0 +1,37 @@ +public class ExceptionHandlingTest { + + static void checkAge(int age) throws MyException{ + if (age < 18) { + // throw new ArithmeticException("Access denied - You must be at least 18 years old."); + throw new MyException("This is custom exception object"); + } + else { + System.out.println("Access granted - You are old enough!"); + } + } + + public static void main(String[] args) throws MyException{ + try{ + checkAge(15); // Set age to 15 (which is below 18...) + }catch(Exception e){ + System.out.println(e.getMessage()); + } + } + } + + class MyException extends Exception{ + private String message; + public MyException(String errMessage){ + this.setMessage(errMessage); + } + + @Override + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} + \ No newline at end of file diff --git a/src/main/java/com/nnw/HashMapTest.java b/src/main/java/com/nnw/HashMapTest.java new file mode 100644 index 00000000..73ed6030 --- /dev/null +++ b/src/main/java/com/nnw/HashMapTest.java @@ -0,0 +1,19 @@ +import java.util.HashMap; + +public class HashMapTest { + public static void main(String[] args) { + // Create a HashMap object called capitalCities + HashMap capitalCities = new HashMap<>(); + + // Add keys and values (Country, City) + capitalCities.put("England", "London"); + capitalCities.put("Germany", "Berlin"); + capitalCities.put("Norway", "Oslo"); + capitalCities.put("USA", "Washington DC"); + System.out.println(capitalCities); + for(String k: capitalCities.keySet()){ + System.out.printf("Key : %s \t Value : %s \n", k, capitalCities.get(k)); + // System.out.printf("%s : %s \n", k, capitalCities.get(k)); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/nnw/HashSetTest.java b/src/main/java/com/nnw/HashSetTest.java new file mode 100644 index 00000000..42187583 --- /dev/null +++ b/src/main/java/com/nnw/HashSetTest.java @@ -0,0 +1,31 @@ +// Import the HashSet class +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; + +public class HashSetTest { + public static void main(String[] args) { + HashSet cars = new HashSet<>(); + cars.add("Volvo"); + cars.add("BMW"); + cars.add("Ford"); + cars.add("BMW"); + cars.add("Mazda"); + System.out.println(cars); + System.out.println("******************"); + ArrayList cars_list= new ArrayList<>(cars); + Collections.sort(cars_list); + for (String i : cars_list) { + System.out.println(i); + } + System.out.println("******************"); + try{ + for (int i=0;i<=cars_list.size();i++) { + System.out.println(cars_list.get(i)); + } + }catch(IndexOutOfBoundsException e){ + System.out.println(e.getMessage()); + } + System.out.println(cars.size()); + } +} diff --git a/src/main/java/com/nnw/IteratorTest.java b/src/main/java/com/nnw/IteratorTest.java new file mode 100644 index 00000000..cd78cd0b --- /dev/null +++ b/src/main/java/com/nnw/IteratorTest.java @@ -0,0 +1,24 @@ +// Import the ArrayList class and the Iterator class +import java.util.ArrayList; +import java.util.Iterator; + +public class IteratorTest { + public static void main(String[] args) { + + // Make a collection + ArrayList cars = new ArrayList<>(); + cars.add(1); + cars.add(3); + cars.add(4); + cars.add(5); + + // Get the iterator + Iterator it = cars.iterator(); + + // Print the first item + System.out.println(it.next()); + while(it.hasNext()){ + System.out.println(Integer.parseInt(it.next().toString())*10); + } + } +} diff --git a/src/main/java/com/nnw/LambdaTest.java b/src/main/java/com/nnw/LambdaTest.java new file mode 100644 index 00000000..5b7d11a5 --- /dev/null +++ b/src/main/java/com/nnw/LambdaTest.java @@ -0,0 +1,34 @@ +import java.util.ArrayList; +import java.util.function.Consumer; + +public class LambdaTest { + public static void main(String[] args) { + ArrayList numbers = new ArrayList<>(); + numbers.add(5); + numbers.add(9); + numbers.add(8); + numbers.add(1); + + ArrayList arr = new ArrayList<>(); + arr.add("Soe"); + arr.add("Hla"); + + // numbers.forEach((n) -> { System.out.println(n); }); + Consumer method = (n) -> { System.out.println(n); }; + numbers.forEach( method ); + arr.forEach(method); + System.out.println("----------------"); + StringFunction123 exclaim = (s) -> s + "!"; + StringFunction123 ask = (s) -> s + "?"; + printFormatted("Hello", exclaim); + printFormatted("Hello", ask); + } + private static void printFormatted(String str, StringFunction123 format) { + String result = format.run(str); + System.out.println(result); + } +} + +interface StringFunction123 { + String run(String str); +} \ No newline at end of file diff --git a/src/main/java/com/nnw/LinkedListTest.java b/src/main/java/com/nnw/LinkedListTest.java new file mode 100644 index 00000000..d29ec54e --- /dev/null +++ b/src/main/java/com/nnw/LinkedListTest.java @@ -0,0 +1,32 @@ +public class LinkedListTest{ + static class Node { + int data; + Node next; + + Node(int data) { + this.data = data; + this.next = null; + } + } + + public static void main(String[] args) { + // Creating individual nodes + Node firstNode = new Node(3); + Node secondNode = new Node(5); + Node thirdNode = new Node(13); + Node fourthNode = new Node(2); + + // Linking nodes together + firstNode.next = secondNode; + secondNode.next = thirdNode; + thirdNode.next = fourthNode; + + // Printing linked list + Node currentNode = firstNode; + while (currentNode != null) { + System.out.print(currentNode.data + " -> "); + currentNode = currentNode.next; + } + System.out.println("null"); + } +} \ No newline at end of file diff --git a/src/main/java/com/nnw/ListSortingTest.java b/src/main/java/com/nnw/ListSortingTest.java new file mode 100644 index 00000000..d578ad8f --- /dev/null +++ b/src/main/java/com/nnw/ListSortingTest.java @@ -0,0 +1,25 @@ +import java.util.ArrayList; +import java.util.Collections; // Import the Collections class + +public class ListSortingTest { + public static void main(String[] args) { + ArrayList cars = new ArrayList<>(); + cars.add("Volvo"); + cars.add("BMW"); + cars.add("Ford"); + cars.add("Mazda"); + + Collections.sort(cars); // Sort cars in ascending order + System.out.println(cars.get(0)); + + for (String i : cars) { + System.out.println(i); + } + + System.out.println("----------------Reversed Order"); + Collections.sort(cars,Collections.reverseOrder()); + for (String i : cars) { + System.out.println(i); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/nnw/MultiThreadTest.java b/src/main/java/com/nnw/MultiThreadTest.java new file mode 100644 index 00000000..82c4a20e --- /dev/null +++ b/src/main/java/com/nnw/MultiThreadTest.java @@ -0,0 +1,24 @@ +public class MultiThreadTest extends Thread { + static int amount=0; + public static void main(String[] args) { + MultiThreadTest thread = new MultiThreadTest(); + thread.start(); + while(thread.isAlive()) { + System.out.println("Waiting..."); + } + // Update amount and print its value + System.out.println("Outside Thread: " + amount); + amount++; + System.out.println("Outside Thread: " + amount); + } + @Override + public void run(){ + try{ + amount++; + Thread.sleep(1000); + System.out.println("This code is running in a thread"); + }catch(InterruptedException e){ + System.out.println(e.getMessage()); + } + } + } diff --git a/src/main/java/com/nnw/RegularExpressionTest.java b/src/main/java/com/nnw/RegularExpressionTest.java new file mode 100644 index 00000000..044f793d --- /dev/null +++ b/src/main/java/com/nnw/RegularExpressionTest.java @@ -0,0 +1,42 @@ +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class RegularExpressionTest { + public static void main(String[] args) { + // Create a pattern to be searched + // Custom pattern + Pattern p = Pattern.compile("geeks"); + // Search above pattern in "geeksforgeeks.org" + Matcher m = p.matcher("geeksforgeeks.org"); + // Finding string using find() method + while (m.find()){ + // Print starting and ending indexes + // of the pattern in the text + // using this functionality of this class + System.out.println("Pattern found from " + + m.start() + " to " + + (m.end() - 1)); + } + System.out.println("----------------"); + p = Pattern.compile("[a-z]"); + m = p.matcher("g"); + System.out.println(m.find()); + System.out.println("----------------"); + p = Pattern.compile("[0-9a-zA-Z]"); + m = p.matcher("20,a30,45"); + System.out.println(m.find()); + System.out.println("----------------"); + p = Pattern.compile("[^a-z]"); + m = p.matcher("200a"); + while (m.find()){ + System.out.println("Found from " + m.start() + " to "+ (m.end()-1)); + } + System.out.println("----------------"); + p = Pattern.compile("[^0-9]"); + m = p.matcher("200a"); + while (m.find()){ + System.out.println("Found from " + m.start() + " to "+ (m.end()-1)); + } + } +} +// Outputs Match found diff --git a/src/main/java/com/nnw/SortEvenFirstTest.java b/src/main/java/com/nnw/SortEvenFirstTest.java new file mode 100644 index 00000000..7575b014 --- /dev/null +++ b/src/main/java/com/nnw/SortEvenFirstTest.java @@ -0,0 +1,33 @@ + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + +public class SortEvenFirstTest { + static class SortEvenFirst implements Comparator{ + @Override + public int compare(Object o1,Object o2){ + Integer n1 = (Integer)o1; + Integer n2 = (Integer)o2; + boolean isEven1 = n1%2 == 0; + boolean isEven2 = n2%2 == 0; + if (isEven1==isEven2) + return 0; + else if(isEven1) + return -1; + else + return 1; + } + } + public static void main(String[] args) { + ArrayList list = new ArrayList(); + list.add(10); + list.add(20); + list.add(5); + list.add(2); + list.add(3); + Comparator myComparator= new SortEvenFirst(); + Collections.sort(list,myComparator); + list.forEach(num->{System.out.println(num);}); + } +} diff --git a/src/main/java/com/nnw/filename.txt b/src/main/java/com/nnw/filename.txt new file mode 100644 index 00000000..5f6303d0 --- /dev/null +++ b/src/main/java/com/nnw/filename.txt @@ -0,0 +1,5 @@ +Hello +Nwe Ni +How are you? +Relly nice to meet you? +I wish you all the best for your interview. diff --git a/src/main/java/com/nnw_possibleQ/BinarySearchTest.java b/src/main/java/com/nnw_possibleQ/BinarySearchTest.java new file mode 100644 index 00000000..58ccc89a --- /dev/null +++ b/src/main/java/com/nnw_possibleQ/BinarySearchTest.java @@ -0,0 +1,32 @@ + +import java.util.Arrays; + +public class BinarySearchTest { + public static int binarySearch(int [] arr, int key){ + int low = 0; + int high = arr.length-1; + while(low<=high){ + int midIndex = (low+high)/2; + if (key == arr[midIndex]){ + return midIndex; + } + else if (keyarr[midIndex]){ + low = midIndex+1; + } + } + return -1; + } + + public static void main(String[] args) { + int [] arr = {2,3,6,8,9,13,20}; + int searcKey = 13; + Arrays.sort(arr); + System.out.println("Start......."); + System.out.printf("The search key : %d is found at index %d \n" , searcKey, binarySearch(arr, searcKey)); + System.out.println("Finish......."); + } + +} \ No newline at end of file diff --git a/src/main/java/com/nnw_possibleQ/ReverseLinkedListTest.java b/src/main/java/com/nnw_possibleQ/ReverseLinkedListTest.java new file mode 100644 index 00000000..5fa8885b --- /dev/null +++ b/src/main/java/com/nnw_possibleQ/ReverseLinkedListTest.java @@ -0,0 +1,25 @@ + +import java.util.Collections; +import java.util.LinkedList; + +public class ReverseLinkedListTest { + public static void main(String[] args){ + LinkedList list = new LinkedList<>(); + list.add(40); + list.add(20); + list.add(30); + // Collections.sort(list,Collections.reverseOrder()); + Collections.sort(list,(o1,o2)->{ + int res; + if (o1o2) + res = 1; + else + res = 0; + res *=-1; + return res; + }); + list.forEach(item->{System.out.println(item);}); + } +} diff --git a/src/main/java/com/rampatra/arrays/BooleanMatrix.java b/src/main/java/com/rampatra/arrays/BooleanMatrix.java index 04ee2d88..a59d9b52 100644 --- a/src/main/java/com/rampatra/arrays/BooleanMatrix.java +++ b/src/main/java/com/rampatra/arrays/BooleanMatrix.java @@ -72,7 +72,7 @@ public static void main(String[] args) { print2DMatrix(ar); modifyBooleanMatrix(ar); print2DMatrix(ar); - System.out.println("-------"); + System.out.println("----- --"); ar = new int[][]{{1, 0}, {0, 0}}; print2DMatrix(ar); modifyBooleanMatrix(ar); diff --git a/src/main/java/com/rampatra/arrays/CelebrityProblem.java b/src/main/java/com/rampatra/arrays/CelebrityProblem.java index 8b7278ea..9478a029 100644 --- a/src/main/java/com/rampatra/arrays/CelebrityProblem.java +++ b/src/main/java/com/rampatra/arrays/CelebrityProblem.java @@ -51,7 +51,7 @@ public static boolean haveAcquaintance(int[][] peoples, int a, int b) { public static int findCelebrity(int[][] peoples) { Stack possibleCelebrities = new LinkedStack<>(); - + // System.out.println("***********" + peoples.length + ""); for (int i = 0; i < peoples.length; i++) { for (int j = 0; j < peoples[0].length; j++) { if (haveAcquaintance(peoples, i, j)) { diff --git a/test.java b/test.java deleted file mode 100644 index e69de29b..00000000