Skip to content

Commit

Permalink
Just Kotlin!!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
amangautam1 committed Dec 16, 2017
1 parent 96f1006 commit 8418c82
Show file tree
Hide file tree
Showing 21 changed files with 492 additions and 36 deletions.
5 changes: 5 additions & 0 deletions kotlin.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="TornadoFX" name="TornadoFX">
<configuration />
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
Expand Down
6 changes: 3 additions & 3 deletions src/Basic/ArrayLIstEx.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ fun main(arg: Array<String>) {
println(array)
for (i in array) {
print("$i, ")
if (i.contains("arti")) {
println("found arti")
}
/* if (i.contains("arti")) {
println("found arti")
}*/
}

}
15 changes: 15 additions & 0 deletions src/Basic/ControlStatements.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package Basic

fun main(args: Array<String>) {

var a = 5;
var b = 10
var max = if (a > b) {
println("a is max")
a
} else {
println("b is max")
b
}
print("value= $max")
}
2 changes: 1 addition & 1 deletion src/Basic/FunAdd2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package Basic


fun sum1(a: Int, b: Int): Unit { //or// fun Basic.sum1(a :Int,b:Int) {
print("Basic.sum of $a and $b is ${a + b}")
print("sum of $a and $b is ${a + b}")
}
fun main(args : Array<String>){
sum1(10, 109)
Expand Down
21 changes: 21 additions & 0 deletions src/Basic/Function.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Basic

/**
* Created by Aman gautam on 12-Jul-17.
*/

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

fun add(vararg num: Int) = num.sum()

fun sub(a: Int, b: Int) = a - b

fun main(args: Array<String>) {
println("Basic.sum of digits is : ${sum(3, 5)}")
println(sub(a = 3, b = 5))
println(sub(b = 3, a = 5))
println(add(1, 2, 3, 4))
println(add(21, 32, 12))
}
33 changes: 19 additions & 14 deletions src/Basic/Functions.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package Basic

/**
* Created by Aman gautam on 12-Jul-17.
*/

fun sum(a :Int, b:Int): Int{
return a+b
fun add(a: Int, b: Int): Int {
return a + b;
}
fun add(vararg num:Int) = num.sum()

fun sub(a:Int, b:Int) =a-b
fun substract(a: Int, b: Int) = a - b

fun defAdd(a: Int = 10, b: Int): Int {
return a + b;
}

fun main(args : Array<String>){
println("Basic.sum of digits is : ${sum(3, 5)}")
println(sub(a = 3, b = 5))
println(sub(b = 3, a = 5))
println(add(1, 2, 3, 4))
println(add(21, 32, 12))
fun name(name: String) {
print(name)
}

fun main(arg: Array<String>) {
var result = add(10, 20)
println(result)
result = sub(20, 10)
println(result)
result = defAdd(b = 10)
println(result)
name("Aman")
}
6 changes: 5 additions & 1 deletion src/Basic/Loop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ package Basic
fun main(args :Array<String>) {

var items = listOf<String>("Aman", "Ankit", "kuchiya")

for (item in items) {
print(item+" ")
}

println();
for(index in items.indices) {
print(items[index]+" ")
}

println()
var i=0
while(i<items.size) {
println("item at $i is ${items[i]}")
/// print{items[i]}-
i++
}
}
13 changes: 11 additions & 2 deletions src/Basic/ranges.kt → src/Basic/Ranges.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package Basic


/**
* Created by Aman gautam on 13-Jul-17.
*/
fun main(args : Array<String>){
for(i in 1..5)
for (i in 1..5) {
print(i)
}
println()
for (i in 5 downTo 1) {
print(i)
}
println()
for (x in 1..10 step 2) {
print(x)
Expand All @@ -14,5 +19,9 @@ fun main(args : Array<String>){
for (x in 9 downTo 0 step 3) {
print(x)
}
println()
for (i in 5 until 10) {
print(i)
}

}
22 changes: 22 additions & 0 deletions src/Basic/SimpleCalculator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Created by Aman gautam on 25-Nov-17.
*/
fun main(args: Array<String>) {
println("Basic Calculator using Kotlin!")
println("Enter two values ")
print("a = ")
var a = readLine()!!.toDouble()
print("b = ")
var b = readLine()!!.toDouble()
println("1. Addition \n2. Substraction \n3. Multiplication \n4. Divide")
print("Enter Choice : ")
var ch = readLine()!!.toInt()
print("Result : ")
when (ch) {
1 -> print("a+b = ${a + b}")
2 -> print("a-b = ${a - b}")
3 -> print("a*b = ${a * b}")
4 -> print("a/b = ${a / b}")
else -> print("Wrong Choice")
}
}
3 changes: 1 addition & 2 deletions src/Basic/Variables.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ fun main(args : Array<String>){
val b =10.5
val c :Float
c=100.14f
val n=1..100
val n = 1..10
var s="aman"
var t=" gautam "
val d=a+b+c
var nullable:String?=null

println("a=$a \nb=$b \nc=$c \nd=$d")
println(s+t)
Expand Down
32 changes: 19 additions & 13 deletions src/Basic/When.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,28 @@ fun describe(obj: Any): String =
"Hello" -> "Greeting"
is Long -> "Long"
!is String -> "Not a string"
parseInt(obj) -> "Integer"
3, 4 -> "is 3 or 4"
else -> "Unknown"
}

fun main(abs :Array<String>){
var x= readLine()!!.toInt();
var y = when (x) { /////// a.k.a. switch in c java
1 -> {print("x == 1")
print("one more thing....")}
2 -> print("x == 2")
3,4-> print(" 3 or 4")
in 5..20 -> println("between 5 and 20")
else -> {
print("x is neither 1 nor 2")
}
//var x= readLine()!!.toInt();
/* var y = when (x) { /////// a.k.a. switch in c, java
1 -> {print("x == 1")
print("one more thing....")}
2 -> print("x == 2")
3,4-> print(" 3 or 4")
in 5..20 -> println("between 5 and 20")
else -> {
print("x is neither 1 nor 2")
}
}
print(y)*/


}
print(y)
print(describe(176567767))
println(describe(1765677678567745453)) // range
println(describe("Hello"))
println(describe("#%$@3"))
}
9 changes: 9 additions & 0 deletions src/Basic/conditional.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package Basic

fun main(arr: Array<String>) {

for (i in 1..10) {
print(i)
}

}
24 changes: 24 additions & 0 deletions src/Data_Structure/BinarySearchTree.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,29 @@ class TreeHelper { // class to perform basic operations of a tree
}
}

fun BFS(x: Tree? = null) {
var d = 1;
while (x != null) {
printBFS(x, d);
d++;
}
}

fun printBFS(x: Tree? = null, d: Int) {
if (x == null) {
return;
}
if (d == 1) {
print(" ${x.value} ")

} else if (d > 1) {
printBFS(x.left, d - 1);
printBFS(x.right, d - 1);
}

}


}

fun main(arg: Array<String>) {
Expand All @@ -119,6 +142,7 @@ fun main(arg: Array<String>) {
3 -> th.inOrder(th.root)
4 -> th.preOrder(th.root)
5 -> th.postOrder(th.root)
6 -> th.BFS(th.root)

}
}
Expand Down
71 changes: 71 additions & 0 deletions src/Data_Structure/HeapSort.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package Data_Structure

var heapSize = 0

fun left(i: Int): Int {
return 2 * i
}

fun right(i: Int): Int {
return 2 * i + 1
}

fun swap(A: Array<Int>, i: Int, j: Int) {
var temp = A[i]
A[i] = A[j]
A[j] = temp
}

fun max_heapify(A: Array<Int>, i: Int) {
var l = left(i);
var r = right(i);
var largest: Int;

if ((l <= heapSize - 1) && (A[l] > A[i])) {
largest = l;
} else
largest = i

if ((r <= heapSize - 1) && (A[r] > A[l])) {
largest = r
}

if (largest != i) {
swap(A, i, largest);
max_heapify(A, largest);
}
}

fun buildMaxheap(A: Array<Int>) {
heapSize = A.size
for (i in heapSize / 2 downTo 0) {
max_heapify(A, i)
}
}

fun heap_sort(A: Array<Int>) {
buildMaxheap(A)
for (i in A.size - 1 downTo 1) {
swap(A, i, 0)
heapSize = heapSize - 1
max_heapify(A, 0)

}
}

fun main(arg: Array<String>) {
print("Enter no. of elements :")
var n = readLine()!!.toInt()

println("Enter elements : ")
var A = Array(n, { 0 })
for (i in 0 until n)
A[i] = readLine()!!.toInt()

heap_sort(A)

println("Sorted array is : ")
for (i in 0 until n)
print("${A[i]} ")
}

Loading

0 comments on commit 8418c82

Please sign in to comment.