Posts

Showing posts from December, 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

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