This file contains 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
#include <list> | |
#include <cassert> | |
#include <iostream> | |
#include <chrono> | |
std::list<int>* sieveEratosphen(const int n) | |
{ | |
assert(n > 2); | |
bool *numbers{new bool[n]}; | |
for (int i = 0; i < n; i++) { numbers[i] = true; } |
This file contains 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 time | |
def measured_time(func): | |
def wrapped(*args): | |
start = time.perf_counter_ns() | |
result = func(*args) | |
elapsed = int((time.perf_counter_ns() - start) / 1000000) | |
print(f'Elapsed time is {elapsed} ms') | |
return result |
This file contains 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 kotlin.system.measureTimeMillis | |
fun sieveEratosphen(n: Int): List<Int> { | |
require(n > 2) | |
val numbers = Array(n) { true } | |
var next = 2 | |
while (next * next <= n) { | |
if (numbers[next]) { | |
for (index in 0 until n - next) { | |
val step = next * next + index * next |
This file contains 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
// 1. In Common-level gradle file set source dir for assets to one folder both for desktop and android modules | |
sourceSets { | |
named("commonMain") { | |
resources.srcDirs("resources") | |
// ... | |
} | |
} | |
android { //... | |
sourceSets { //... |
This file contains 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
val shakeInterpolator = TimeInterpolator { delta -> | |
val freq = 6f | |
val decay = 2f | |
(sin(freq * delta * Math.PI) * exp((-delta * decay).toDouble())).toFloat() | |
} | |
fun RecyclerView.addItemShaker(position: Int) { | |
val shaker = object : RecyclerView.OnScrollListener() { | |
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { |
This file contains 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
// BaseAdapter -> inner class DataViewHolder -> method bind | |
fun bind(item: Any, clickListener: AdapterClickListenerById) { | |
holder.bind(binding, item, clickListener) | |
binding.root.animation = AnimationUtils.loadAnimation(binding.root.context, R.anim.recycler_item_arrive) | |
} | |
// Animation file in resources | |
<?xml version="1.0" encoding="utf-8"?> | |
<set xmlns:android="http://schemas.android.com/apk/res/android"> | |
<translate |
This file contains 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
// On ViewModel we need to return position to scroll. Good practice is delegate it to responsible class: | |
fun getScrollPosition(items: List<Any>) = currentScreen.getPositionToScroll(items) | |
// On Fragment we need to observe event of scroll triggering: | |
viewModel.getScrollTrigger().collectOnFragment(this) { state -> | |
when (state) { | |
false -> {} // Other actions if scrolling is not available, i.e. show a SnackBar | |
true -> { | |
viewModel.getScrollPosition(referenceList)?.let { (position, snackText) -> | |
binding.recyclerView.addItemShaker(position) |
This file contains 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
// Game Events | |
sealed class GameEvent | |
class GameOver(val score: Int) : GameEvent() | |
class CopCatchEnemy(val cop: Entity, val enemy: Entity) : GameEvent() | |
class PlayerRestoresCop(val cop: Entity) : GameEvent() | |
object EnemyMissed : GameEvent() | |
object EnemyCaught : GameEvent() | |
object CopMissed : GameEvent() |
This file contains 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
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { | |
// ... | |
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, backPressHandler) | |
// ... | |
} |
This file contains 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
private var backPressedTime: Long = 0 | |
private val backPressHandler = object : OnBackPressedCallback(true) { | |
override fun handleOnBackPressed() { | |
if (mainViewModel.currentScreen is MainScreen) { | |
if (backPressedTime + 2000 > System.currentTimeMillis()) { | |
activity?.finishAndRemoveTask() | |
} else { | |
Toast.makeText(requireContext(), "Press again to exit", Toast.LENGTH_SHORT).show() | |
backPressedTime = System.currentTimeMillis() | |
} |