Skip to content

Instantly share code, notes, and snippets.

View Seniru's full-sized avatar
:octocat:
Let's go!

Seniru Pasan Indira Seniru

:octocat:
Let's go!
View GitHub Profile
@Seniru
Seniru / MainActivity.kt
Created June 12, 2025 15:44
Room database example. Please refer to my other gist related to todo items for stuff related with the RecyclerView (this is an updated version of that)
package com.seniru.recylerviewexample
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.CoroutineScope
@Seniru
Seniru / CountingService.kt
Created June 12, 2025 14:34
Services example
package com.seniru.servicesexample
import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.util.Log
class CountingService : Service() {
private var isRunning = false
@Seniru
Seniru / AirplaneModeBroadcastReceiever.kt
Created June 12, 2025 13:47
Broadcast receivers example
package com.seniru.broadcastreceiverexample
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.Toast
class AirplaneModeBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
@Seniru
Seniru / MainActivity.kt
Created June 12, 2025 12:25
Android ViewModel demo. This gist only shows the code necessary to handle ViewModels You may you have to implement the layouts yourself if you wish to run this.
/**
* Viewmodel example
*
* The basis of viewmodel is to save the state between configuration changes (such as
* changing screen orientation)
*/
package com.seniru.viewmodeldemo
import android.os.Bundle
@Seniru
Seniru / MainActivity.kt
Last active June 12, 2025 11:47
Android RecyclerView example. This gist only showcases the recycler view handling. You may you have to implement the layouts yourself if you wish to run this.
package com.seniru.recylerviewexample
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
@Seniru
Seniru / Fragment1.kt
Created June 12, 2025 10:46
Kotlin fragment example. This gist only showcases the logic required for fragment switching. You may you have to implement the layouts yourself if you wish to run this.
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.seniru.fragmentexample.R
// this method doesn't initialize the instance with the fragment layout.

Data structures and Algorithms

This gist contains some useful data structure and algorithm implementations. Note that some of these are not optimized enough for real use cases. The purpose of these snippets are to demonstrate the algorithm. There are better guides and tutorials out there: w3schools, tutorialspoint, geeksforgeeks. Please refer to them for better knowledge. This is just an all in one place cheatsheet.

Data structures

# Increase the divisor to reduce the number of spurious hits
DIVISOR = 13
# this is a ultra simplified version of rabin karp method to demonstrate the logic behind it.
# this method only works for integer strings, has to be modified a little to support strings
def rabin_karp_string_matcher(string, pattern):
modulo = sum(pattern) % DIVISOR
string_len = len(string)
pattern_len = len(pattern)
def naive_string_matcher(string, pattern):
string_len = len(string)
pattern_len = len(pattern)
for i in range(len(string)):
if string[i:i+pattern_len] == pattern:
return i
"""heapify algorithm
Creates a max heap from an unsorted array. The logic behind this is the parent node should be larger than
its children. Therefore, if one of those children have greater values, swap the parent with its child.
By doing this repeatedly, we can guarantee each nodes' value is higher than its children'
"""
def heapify(arr, n, i):
largest = i
left = 2 * i + 1
right = 2 * i + 2