Skip to content

Instantly share code, notes, and snippets.

@alwarren
alwarren / Dependencies.kt
Last active January 20, 2023 15:24
Kotlin DSL (IntelliJ IDEA 2022)
// Path project_root/buildSrc/src/main/kotlin
object Dependencies {
object Kotlin {
val stdlib: String = "org.jetbrains.kotlin:kotlin-stdlib:${Versions.kotlin}"
val coroutines: String = "org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.coroutines}"
}
object Retrofit {
val base: String = "com.squareup.retrofit2:retrofit:${Versions.retrofit}"
val gson: String = "com.squareup.retrofit2:converter-gson:${Versions.retrofit}"
@alwarren
alwarren / Api.kts
Last active April 5, 2023 18:29
Converting Metar data in CSV format to JSON using NOAA's Text Data Server
object Api {
private const val endpoint = "https://www.aviationweather.gov/adds/dataserver_current/"
private val scalarConverter by lazy { ScalarsConverterFactory.create() }
private val jsonConverter by lazy { GsonConverterFactory.create() }
private val client by lazy {
OkHttpClient.Builder()
.addInterceptor { chain ->
val original: Request = chain.request()
@alwarren
alwarren / CsvMapper.kts
Created October 19, 2020 18:04
Parsing Metar data in CSV format from NOAA's Text Data Server
object CsvMapper {
fun decode(rawText: String): Metar {
val values = rawText.split(",")
return buildMetar(values, skyConditions(values), qualityControlFlags(values))
}
private fun buildMetar(values: List<String>, skyCondition: List<SkyCondition>, qualityControlFlags: QualityControlFlags): Metar {
// 0:raw_text 1:station_id 2:observation_time 3:latitude 4:longitude 5:temp_c 6:dewpoint_c 7:wind_dir_degrees
// 8:wind_speed_kt 9:wind_gust_kt 10:visibility_statute_mi 11:altim_in_hg 12:sea_level_pressure_mb
@alwarren
alwarren / AbstractUnitTest.kt
Last active October 9, 2020 18:55
Common Unit Test Structure
abstract class AbstractUnitTest {
private var counter: Int = 0
private var passed: Int = 0
@BeforeAll
abstract fun beforeAll()
abstract fun actualMethods(): List<String>
abstract fun expectedMethods(): List<String>
abstract fun actualProperties(): List<String>
abstract fun expectedProperties(): List<String>
@alwarren
alwarren / Sudoku.kt
Created January 14, 2020 07:22
Kotin implementation of a java Sudoku generator.
import kotlin.math.sqrt
// See https://www.geeksforgeeks.org/program-sudoku-generator/
class Sudoku(private val N: Int, private val K: Int) {
// Convenience value for public operations on the matrix
val width get() = N
val matrix = Array(N) { IntArray(N) }
// square root of N
private val sqrt = sqrt(N.toDouble()).toInt()
@alwarren
alwarren / TestMyObjectApi.java
Created June 28, 2019 22:07
A Generic API Unit Test
/**
* Name: MyObject
* Date:
* Description: Tests used to create an empty API for an immutable type MyObject
* using test driven design (TDD).
*
* API Specification:
*
* public class MyObject {
* public MyObject(Object[] objects) // a general constructor description
@alwarren
alwarren / MissionsRepositoryTests.kt
Last active June 28, 2019 22:21
Trying MockK for the first time
class MissionsRepositoryTests : UnitTest() {
private val networkHandler = mockk<NetworkHandler>()
private val call = mockk<Call<List<Mission>>>(relaxed = true)
private val response = mockk<Response<List<Mission>>>()
private val missionService = mockk<MissionService>()
@BeforeAll
override fun beforeAll() {
StdOut.println(Values.suiteTitle(Values.SUITE_TITLE_MISSIONS_REPOSITORY))
}
@alwarren
alwarren / BaseballElimination.java
Last active June 17, 2019 05:08
Coursera Algorithms II BaseballElimination Junit5 Test Suite
/**
* Name: Al Warren
* Date: 6/16/2019
* Description: Test suite for an Immutable object type BaseballElimination.
*
* API Requirements:
*
* public BaseballElimination(String filename) // create a baseball division from given filename in format specified below
* public int numberOfTeams() // number of teams
* public Iterable<String> teams() // all teams
@alwarren
alwarren / README.md
Last active July 21, 2019 17:46
Coursera Algorithms II WordNet Junit5 Test Suite

Coursera Algorithms II WordNet Junit5 Test Suite

Note

  1. This test suite is not exaustive.
  2. It requires removing the org.* restriction from auto import rules if you are using the IntelliJ project included in the course zip file. This is because the Junit5 development team used the based package org.junit.* instead of junit.* in many of the new libraries.
  3. It requires adding Junit5 dependencies.
@alwarren
alwarren / CartesianChallenge.md
Last active May 19, 2019 22:01
A simple Java challenge in Cartesian space.

A simple Java challenge. This really stumped me the first time I ran across it. I made it work but looking at the code made absolutley no sense. Give it a try and let me know how you do.

Consider the following values:

 8  1  3 
 4  0  2 
 7  6  5 
  1. Store the values in a two-dimensional array of int.
  2. Move the 0 up by modifying a single index and swapping two values.