Skip to content

Instantly share code, notes, and snippets.

View yairzaslavsky's full-sized avatar

Yair Zaslavsky yairzaslavsky

View GitHub Profile
@yairzaslavsky
yairzaslavsky / reverseListStub.kt
Last active June 17, 2021 15:13
Reverse List signature
data class Node(val data: Int, var next: Node? = null)
fun reverseList(head: Node?): Node? = ... //implement this
fun firstIndexOfNthMostFrequentChar(str: String, n: Int): Int {
val countsMap = createCountsMap(str)
if (countsMap.size < n) {
return -1
}
val sortedKeyValuePairs = sortKeyValuePairs(countsMap)
val ch = sortedKeyValuePairs[n-1].first
return str.indexOf(ch, 0)
@yairzaslavsky
yairzaslavsky / nthfrequent1.kt
Created March 16, 2021 07:26
n-th frequent letter (with stubs)
/** Given a string return the index of the first apperance of the n-th most frequent letter
* If two letters are equally frequent return the matching index of any of the the two
* Examples: "aabbbc" , 3 will return 5 , "aabbbbc", 1 will return 2
*/
fun firstIndexOfNthMostFrequentChar(str: String, n: Int): Int {
val countsMap = createCountsMap(str)
if (countsMap.size < n) {
return -1
}
enum class Beverage { MILK, SODA, WATER, JUICE }
enum class Main { NUGGETS, HOT_DOG, HAMBURGER }
enum class Side { FRIES, APPLE_SLICES }
enum class Size { SMALL, MEDIUM, LARGE }
@JvmOverloads
fun makeMealOrder(drink: Beverage = Beverage.MILK, mainPart: Main = Main.HAMBURGER, size: Size = Size.MEDIUM , side: Side = Side.FRIES) {
}
@yairzaslavsky
yairzaslavsky / Person.kt
Created August 18, 2020 18:51
Person2.kt
/**
* DDB Enhanced client mapper annotations can be applied only to methods.
* This is why we cannot use a data class here.
*/
@DynamoDbBean
class Person(personId: String, name: String, familyName: String) {
@get:DynamoDbPartitionKey
var personId = personId;
@get:DynamoDbSecondaryPartitionKey(indexNames = ["familyName-index"])
@yairzaslavsky
yairzaslavsky / Person.kt
Last active August 18, 2020 18:58
Person.kt
@DynamoDBTable(tableName = "Person")
data class Person(
@DynamoDBHashKey(attributeName = "personId")
var personId: String = "",
@DynamoDBAttribute(attributeName = "name")
var name: String = "",
@DynamoDBIndexHashKey(globalSecondaryIndexName = "familyName-index", attributeName = "familyName")
var familyName: String = "",
@yairzaslavsky
yairzaslavsky / whole test.kt
Created September 16, 2019 07:09
whole test
package blog.post.blms.proxy
import blog.post.Item
import blog.post.ItemsManager
import blog.post.PaginatedList
import blog.post.blms.Book
import blog.post.blms.Library
import io.mockk.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
@yairzaslavsky
yairzaslavsky / Test factory function.kt
Created September 16, 2019 07:08
Test factory function.kt
@TestFactory
fun `successful call to listBooks`(): List<DynamicTest> =
listOf(
ListBooksTestCase(
"Getting list of items",
testLibrary.identifier,
itemsList(book1.toItem(), book2.toItem()),
booksList(book1, book2)
),
ListBooksTestCase(
@yairzaslavsky
yairzaslavsky / CreateDynamicTest.kt
Created September 16, 2019 07:07
from test data to dynamic test
private fun ListBooksTestCase.`successful call to listBooks`(): DynamicTest {
return DynamicTest.dynamicTest(description) {
// Arrange
every {
itemsManager.getItems(testLibrary.identifier, ofNullable(maxResults), ofNullable(nextToken))
} returns (returnedItems)
//Act
val actualBooks = booksDataProvider.listBooks(testLibrary.identifier, maxResults, nextToken)
private val testLibrary = Library(randomUUID(), "Town library", "1200 Oak St")
private val book1 = Book(randomUUID(), "Foundation and earth", "Sci-Fi")
private val book2 = Book(randomUUID(), "Lord of the rings", "Fantasy")