Learning Kotlin – Part 5

This time we deal with loops

Loops

Kotlin has two types of loops: for and while.

For-loop

for(x in 1..10){
    println("Loop: $x")
}

While-loop

var i = 10
while(i > 0) {
    println("$i")
    i--
}

Break & Continue

for(x in 1..20){
    if(x % 2 == 0){
        continue
    }
    println("Odd: $x")
    if(x == 15) break
}

Iterate over collections

val myArray: Array<Int> = arrayOf(3,6,9)
for(i in myArray.indices){
    println("Mult3 : ${myArray[i]}")
}

Enumerate-like iteration over collections

When you are familiar with Python’s enumerate fucntion You will appreciate the withIndex-function:

for((index, value) in myArray.withIndex()){
println("Index : $index Value : $value")
}

Internet access in Android

If You want to access some internet resouces from your Android app you add

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

to your AndroidManifest.xml

When You have resources which do not use TLS (https but just http) you have to add

<application
        ...
        android:usesCleartextTraffic="true">
</application>

The 10-3-2-1-0 Rule

10h before sleep – no caffeine
3h before sleep – no more eating
2h before sleep – no more work
1h before sleep – no more tv / tablet
0 times snoozing the alarm clock in the morning

So when I want to get to bed at around 22:00 (10pm) my schedule would look like the following

  • 12:00 – stop drinking caffee
  • 19:00 – stop eating
  • 20:00 – stop working
  • 21:00 – stop watching Netflix/Prime/Youtube
  • Stop snoozing the alarm clock when it rings at 6:00

After one week my experience is this:

  • 12:00 – stop drinking caffee
    • good and easy to do, I drink one cup in the morning and at max 2 cups at work
    • positive effect on my skin picking issue
  • 19:00 – stop eating
    • not as easy, sometimes I’m late from work
  • 20:00 – stop working
    • no-brainer, I rarely stay at work after 19:00
  • 21:00 – stop watching Netflix/Prime/Youtube
    • hard, hard, hard, at the moment I practice guitar / piano from around 19:30 till 21:00
  • Stop snoozing the alarm clock when it rings at 6:00
    • not working I still snooze between 2 to 5 times, so I get up at 6:30 minimum

Some tweeking

I shifted my sleep time from 22:00 – 6:00 to 22:30 – 6:30

  • 12:30 – stop drinking coffee
  • 19:30 – stop eating
  • 20:30 – stop working
  • 21:30 – stop watching Netflix/Prime/YouTube
  • Stop snoozing the alarm clock when it rings at 6:30

 

 

SQL-Basics: Relations

As the Junior Data Scientist of Knight Industries we created a table to keep track of all our operatives: SQL-Basics: Create, Read, Update, Delete

Devon asks us to keep track of our operations aka missions as well. For the first implementation let us assume that a mission has one operative and an operative can participate in multiple missions. that’s what we call an 1 to many relationship.

  +------------+                 +----------+
  |            |1   active in   n|          |
  | operative  +-----------------+ missions |
  |            |                 |          |
  +------------+                 +----------+

Missions have an id, a code name like “Phantom Liberty”, an operative id and a total cost. Continue reading “SQL-Basics: Relations”

SQL-Functions

In SQL-Basics

we learned the standard SQL statements to create a table, insert data into it, retrieving data from tables and altering data. Now we want to do even

Devon is a data driven man and to evaluate our mission, he likes to know from us:

    • how many missions we finished
    • the total cost of all missions
    • the mission with the lowest cost
    • the mission with the highest cost
    • the average mission cost
    • the five most expensive missions

 

SELECT COUNT(*) FROM missions;
SELECT SUM(total_cost) FROM missions;
SELECT MIN(total_cost) FROM missions;
SELECT MAX(total_cost) FROM missions;
SELECT AVG(total_cost) FROM missions;
SELECT * FROM missions ORDER BY total_cost DESC LIMIT 5;