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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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?) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """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 |
NewerOlder