Posts

Showing posts from 2018

Kotlin Language Features Related to Null Handling

Any software engineer with a Java background would find the null handling features in the Kotlin language interesting. Let's summarize this topic with some examples. Nullable types: In Kotlin, types are non-nullable by default. If you want a variable to be able to hold a null value, you need to explicitly declare its type as nullable using the Type? syntax. For example, String? denotes a nullable string, while String represents a non-nullable string. Safe calls (?.): Kotlin introduces the safe call operator (?.) for handling nullable types. It allows you to safely invoke a method or access a property on a nullable object. If the object is null, the expression returns null instead of throwing a NullPointerException. Example: data class Person(val name: String, val age: Int, val address: String?) fun main() {     // Create a person with a nullable address     val person1 = Person("John Doe", 25, "123 Main Street")     val person2 = Person("Jane Doe", 30,

A* Search Algorithm and Priority Queues

Image
In this blog post, I'll examine the fifteen-puzzle problem . We have an n x n grid filled with numbers from 1 to n-1. Our aim is to reach the position called the "goal board." For example, for a 3x3 grid, our goal board is: 1  2  3 4  5  6 7  8 To reach there, we will be sliding blocks horizontally and vertically to the blank square. The solution approach to this problem utilizes the A* search algorithm . These kinds of algorithms use heuristics to arrive better positions. Let's explain the steps of the algorithm: Create a priority queue and enqueue the initial position. Dequeue the best item using the heuristics. Insert all neighbours of the dequeued item into the queue. Repeat this procedure until the goal board is dequeued.  For each position we need to calculate the heuristic function. For this problem, we can use one of the two priority fuctions: 1) Hamming priority function: The number of blocks in the wrong position. 2) Manhattan prior

A Pattern Recognition Problem: Finding Line Segments Effectively In Java

Lets say we are given a set of  n  points and we need to find every line segment that connects 4 or more of the points. We have an immutable data type for Point: public class Point implements Comparable {     private final int x;        private final int y;        //...     public int compareTo(Point that) {         if (that.y == y && that.x == x) {             return 0;         }         if (y < that.y) {             return -1;         }         if (y > that.y) {             return 1;         }         if (x < that.x) {             return -1;         }         // x > that.x         return 1;     } } So the comparison first looks at the y coordinate and then there is a tie-break on the x coordinate.      public double slopeTo(Point that) {         if (that.x == x && that.y == y) {             return Double.NEGATIVE_INFINITY;         }         return ((double) (that.y - y)) / (that.x - x);

Dynamic Connectivity Problem and the Union-Find Algorithm

Lets say we need to connect some objects and query if some objects are connected or not. Below is an example test: @Test     public void testConnectivity() {         MyConnectionDb connectDb = new MyConnectionDb();         connectDb.connect(4, 3);         connectDb.connect(3, 8);         connectDb.connect(6, 5);         connectDb.connect(9, 4);         connectDb.connect(2, 1);         assertTrue(connectDb.isConnected(8, 9));         assertFalse(connectDb.isConnected(5, 4));         connectDb.connect(5, 0);         connectDb.connect(7, 2);         connectDb.connect(6, 1);         connectDb.connect(7, 3);         assertTrue(connectDb.isConnected(5, 3));     } So when some numbers are connected, they form a set and we enlarge that set by applying union operations. And of course we query if given two numbers are connected or not.  I started with a solution using Java collections like HashSet. It is good to get a feel of the problem and c

Popular posts from this blog

Trie Data Structure and Finding Patterns in a Collection of Words

swapLexOrder: Finding lexicographically largest string

A Graph Application in Java: Using WordNet to Find Outcast Words