Learn kotlin – Array, Collection and Range


In this post we will discuss about the Array, Collection and Range in Kotlin.


In Kotlin, we can create array using two different ways. First one is by using arrayOf() function and next one is by using Array() constructor.

Using arrayOf() function

val accountInfo = arrayOf("Sudeep", 34000)

The accountInfo array is of type Array<Any>. It holds array with two different types of element String and Int, next there are functions like intArrayOf(), charArrayOf(), booleanArrayOf(), longArrayOf(), shortArrayOf(), byteArrayOf().

For example using intArrayOf(3,4,5,6) make an array of type integer with the given numbers. If you try of push some other types of data into the int array then it will throw compile error. 

Alternatively you can use arrayOf<Int> instead of intArrayOf.

val num1 = arrayOf<Int>(3,4,5,6)
val num2 = intArrayOf(3,4,5,6)

Using Array() constructor

Another way of making array is by using Array() constructor. This function takes two parameter, first one is the size of the array and next one is the lambda function to create the values. 

val numbersArray = Array(5, { i -> i * 3 })

Collections : List, Set, Map

In kotlin, you can either create mutable or immutable collections. Collections include list, set and map in kotlin.


To create immutable list we use the following syntax:

val iMutNumbers: List<Int> = listOf(3,4,5,6)

You cannot edit this list afterwards, if you want to make the list which you can edit later then you should create the mutable list. We use the following syntax to create mutable list:

val mutNumbers: MutableList<int> = mutableListOf(1,3,4,5)

// You can add item to this list by using add function

Similarly, we can create list of String, Boolean, long and so on. 

We can use emptyList() function to create immutable list:

val emptyList: List<String> = emptyList<String>()

Next we have listOfNotNull() function to create list with non-null element. 

arrayListOf() creates a mutable list.


It is an unordered collections of unique element. One of the way to create set is by using setOf() function.

val mixedTypeSet = setOf(4,5,"Pokhara")
val intOnlySet: Set<Int> = setOf(3,4,5,6)
// This creates immutable set

Another way to create set which is mutable is using hashSetOf() function.

val intsHashSet: java.util.HashSet<Int> = hashSetOf(1, 2, 6, 3) intsHashSet.add(5) 

sortedSetOf() creates mutable set which is sorted by using operator. 

linkedSetOf() maintains the order in which the items are inserted in the set. It is also mutable.

mutableSetOf() creates the mutable set which is equivalent to Java LinkedHashSet.


Map is the data type which store keys and value pair. All the keys are unique for each of the value.  mapOf creates immutable map.

val countryCallingCode: Map<Int, String> = mapOf(977 to "Nepal", 1 to "United States", 61 to "Australia")
for ((key, value) in countryCallingCode) {
	println("The calling code of $value is $key")
// It can also be accessed like array

we can use mutableMapOf() function in order to create the mutable map.

hashMapOf() creates mutable hash map. Hash map class uses a hash table to implement the Java map.

linkedHashMap() stores the data in the order in which they were inserted.

sortedMapOf() stores the data in sorted order. 


Range is special data type in kotlin. For example:

var numOneToFive = 1..5 
for(i in numOneToFive) {

Similarly works for character.

var alphabets = 'a'..'g' 
for(i in alphabets) {

In the range the starting and the ending values are inclusive. We can use rangeTo() function instead of .. 

val oneToFive: IntRange = 1.rangeTo(5)

Next we can use downTo() function to make list of numbers starting from higher to lower range.

val tenToOne = 10.downTo(1)

We can also make a range using some gap. For example to create the range of numbers 1,3,5,7,9 we can use step keyword.

val oneToTenStep = 1..10 step

Lastly, if we want to check if a element in inside the range we can use in keyword.

if(4 in tenToOne) {
 print("4 exists in the range.")

So, this is all for this post. We learned about Arrays, collection and range in kotlin. We will dive more into Kotlin in the further post.


Learn Kotlin – Hello world and Basic Data Types


Since, Kotlin is already a official programming language for Android Apps development, I have been learning Kotlin lately. In this post, i will take you through basic hello world skeleton program to data types, variables in Kotlin.

Hello World Program for Kotlin

fun main(args: Array) {
    println("Hello World!")

main is the entry point in the kotlin program. Continue reading

Recursive directory traverse and append .png file to list



Python Developer’s Meetup Nepal #13 Question Solution

Q: Write a python script that recursively walks all sub-directories and searches all files with extension *.png or *.PNG and append them to the list.


import os

# list variable to store the .png and .PNG file
png_file_list = []

# Recursive function to traverse all the sub-dirctory and check for png files
def traverse_directory(dir_path):
    for child in os.listdir(dir_path):
        path = os.path.join(dir_path, child)
        if os.path.isdir(path):
        	# split the path and store file name only
        	file_name = path.split('\\')[-1]
        	# split the file name and store extension
        	file_name_extension = file_name.split('.')[-1]
        	# check if the file name has extension .png or .PNG
        	if(file_name_extension == 'png' or file_name_extension == 'PNG'):

# Traverse current directory for files and folders

# Print the final list

Credit card Verification Haskell Code



{- Convert positive integer to list of digit -}
{- Input : 12345 o/p : [1, 2, 3, 4, 5 -}
toList :: Integer -> [Integer]
toList 0 = []
toList x = toList (x `div` 10) ++ [x `mod` 10]

{- Reverse the integer after converting it to list -}
— toDigitsRev :: Integer -> [Integer]
toListReverse :: Integer -> [Integer]
toListReverse xs = (reverse . toList) xs

{- Double every second number -}
doubleSecondEach :: [Integer] -> [Integer]
doubleSecondEach [] = []
doubleSecondEach (x:[]) = [x]
doubleSecondEach (x:y:xs) = x : (2*y) : doubleSecondEach xs

{- Sum the digit in the number we are sure there is one digit number or two digit number -}
sum’ :: Integer -> Integer
sum’ x = (x `mod` 10) + (x `div` 10)

{- sum all the element in list -}
{- when a element is [13,5,6] we have to add like this: 1 + 3 + 5 + 6 -}
sumDigits :: [Integer] -> Integer
sumDigits [] = 0
sumDigits [x] = sum’ x
sumDigits (x:xs) = sum’ x + sumDigits xs

{- Validate the credit card number -}
validate :: Integer -> Bool
validate x = if (sumDigits $ doubleSecondEach $ toListReverse x ) `mod` 10 == 0 then True else False

Fixing Google Play Games plugin and Unity integration error – Android

Recently i was having multiple issues with google play games plugin integration in Unity. It was not working at all. After spending the couple of hours in it, i was able to fix the issue. This is a note to myself as well for others who are having trouble with the integration.

I was using Admob plugin along with Google Play Games plugin. In the process of fixing the error i got different kinds of error. Some error was due to the import while some was due to the Authentication issues from the play game service. I will discuss about these both.

Continue reading