Learn Kotlin – Function

Merry Christmas Guys! The year 2018 is about to end. This have been a great year. Happy New Year 2019, May your new year be filled with Joy and Happiness. This will probably be the last post for this year 2018. Hope to see you all in the next year!

In today’s post i am gonna write about Function. If you have been programming in any of the language then you’ve been making Functions. Functions are the basic building blocks in programming. Let us discuss about the way to making Functions in the Kotlin.

First of all, let us see the basic syntax of the function in Kotlin.

fun getSum(a : Int, b: Int):Int {
	return (a+b)
}

The function above named getSum takes two parameter a of type Int and b of type Int which returns type Int.

Continue reading “Learn Kotlin – Function”

Learn Kotlin – Control Flow

In this post we are going to learn about the control flow in Kotlin. Let us learn about how to make a decision, how to perform a task multiple times by iterating and how to get out of the loop, how to continue to next iteration. So, today we will learn all about changing the control flow of Kotlin program. 

If..Else

When we want to make a decision in program we use if and else. This is not different from other languages like Java. Even the syntax is similar to Java. 
However, if is the expression and not the keyword so it returns the value only whenever essential. Let us see the syntax for if..else.

fun main(args: Array<String>) {
	val a : Int = 100
	val b : Int = 200
	
	if(a > b) {
		println("a is greater than b")
	} else {
		println("b is greater than a")
	}


}

Or we can also write without bracket in a single line like this:

if(a > b) print("a is greater than b") else print("b is greater than a")

In the code above it checks for one condition, here first it checks whether a is greater than b, if not certainly b is greater. But what if both are of same value. We will need one more condition to be checked. So we have else if for checking more condition.

Continue reading “Learn Kotlin – Control Flow”

Learn Kotlin – Array, Collection and Range

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

Array

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().

th 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. 

Continue reading “Learn Kotlin – Array, Collection and Range”

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 “Learn Kotlin – Hello world and Basic Data Types”

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.

Solution:

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):
            traverse_directory(path)
        else:
        	# 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'):
        		png_file_list.append(file_name)

# Traverse current directory for files and folders
traverse_directory(".")

# Print the final list
print(png_file_list)