Skip to content

Instantly share code, notes, and snippets.

View carolinemusyoka's full-sized avatar
:octocat:
so?.let { it -> be (it) }

Carol carolinemusyoka

:octocat:
so?.let { it -> be (it) }
View GitHub Profile
/**
* Example:
* var li = ListNode(5)
* var v = li.`val`
* Definition for singly-linked list.
* class ListNode(var `val`: Int) {
* var next: ListNode? = null
* }
*/
class Solution {
@carolinemusyoka
carolinemusyoka / TwoSum.kt
Created October 28, 2021 12:16
1. Two Sum
//Solves the problem in O(n) time complexity
class Solution {
fun twoSum(nums: IntArray, target: Int): IntArray {
//initialize an empty HashMap
val map = hashMapOf<Int, Int>()
//and for every element in the array, if it exists in the Map,
//check if its complement exists a in the Map or not. If the complement exists then return the indices
//of the current element and the complement.
//Otherwise, put the element in the Map, and move to the next iteration.
for(i in 0..nums.size - 1){
<style name="checkable_chip" parent="Widget.MaterialComponents.Chip.Choice">
<item name="android:checkable">true</item>
<item name="android:textColor">@color/chip_colors_text</item>
<item name="chipBackgroundColor">@color/chip_colors</item>
<item name="checkedIcon">@null</item>
<item name="chipIconVisible">false</item>
<item name="chipStrokeColor">@color/purple</item>
<item name="chipStrokeWidth">1dp</item>
<item name="ensureMinTouchTargetSize">true</item>
<item name="checkedIconVisible">false</item>
<com.google.android.material.chip.ChipGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="15dp"
app:singleLine="true"
android:id="@+id/chip_group"
app:singleSelection="true">
<!-- app:checkedChip="@id/yes"-->
// Preference DataStore
implementation "androidx.datastore:datastore-preferences:1.0.0-alpha04"
class ViewModelFactory(private val apiHelper: ApiHelper) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(MainViewModel::class.java)) {
return MainViewModel(MainRepository(apiHelper)) as T
}
throw IllegalArgumentException("Unknown class name")
}
}
class MainViewModel(private val mainRepository: MainRepository) : ViewModel() {
fun getSuperHeroes() = liveData(Dispatchers.IO) {
emit(Resource.loading(data = null))
try {
emit(Resource.success(data = mainRepository.getSuperHeroes()))
} catch (exception: Exception) {
emit(Resource.error(data = null, message = exception.message ?: "Error Occurred!"))
}
}
class MainActivity : AppCompatActivity() {
private lateinit var viewModel: MainViewModel
private lateinit var adapter: MainAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
class MainAdapter(private val result: ArrayList<Hero>,
private val clickListener: (Long) -> Unit):
RecyclerView.Adapter<MainAdapter.DataViewHolder>() {
class DataViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(hero: Hero, clickListener: (Long) -> Unit ) {
itemView.apply {
superheroName.text = hero.name
Glide.with(superheroImage.context)
.load(hero.image.lg)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="@+id/card_view"