Skip to content

Instantly share code, notes, and snippets.

View yairzaslavsky's full-sized avatar

Yair Zaslavsky yairzaslavsky

View GitHub Profile
@yairzaslavsky
yairzaslavsky / GPT Santiago prompt - ChatGPT game inspired from the amazing "Where in the world is Carmen Sandiago" gae
Last active June 11, 2023 09:49
GPT Santiago prompt - ChatGPT game inspired from the amazing "Where in the world is Carmen Sandiago" game
We will play a game called “Where in the world is GPT Santiago”
Game synopsis:
A detective is chasing the notorious thief “GPT Santiago” worldwide.
The detective visits different cities that the thief visited, and by asking for hints from the locals, they get a sense of what city they should visit next.
If the detective makes too many mistakes and arrives at the wrong destinations, the thief slips away.
If the deactivate reaches the last city with a small number of mistakes or no mistakes, they manage the apprehend the thief and win the game.
Gameplay:
I will be the detective. You (ChatGPT) will create a sequence of 10 random cities that will be the route of the thief.
@yairzaslavsky
yairzaslavsky / mergeTwoSortedListsWithSafeGuards.java
Last active February 17, 2023 04:46
Merge two sorted lists using safe guards
public class MergeTwoLists {
public class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
this.next = null;
}
}
public class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
this.next = null;
}
}
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
data class Direction(val positionChange: PositionChange, directionType: DirectionType)
val directions = listOf(
Direction(PositionChange(-1,0), DirectionType.LEFT),
..... //TODO: add the other 3 entries
)
package leetcode
import leetcode.Action.*
import leetcode.PersonState.*
import leetcode.StateOfPerson.Asleep
import leetcode.StateOfPerson.Awake
enum class PersonState {
AWAKE,
ASLEEP,
@yairzaslavsky
yairzaslavsky / functionTheory.kt
Created April 19, 2022 16:37
Function composition and currying (Kotlin)
operator fun<A> ((A)->A).times(rhs: (A)->A): (A)->(A) = fun(arg: A): A = this(rhs(arg))
fun <A,B,C> curryLeft(func: (A,B) -> C) = fun(lhs: A) = fun(rhs: B) = func(lhs, rhs)
fun <A,B,C> curryRight(func: (A,B) -> C) = fun(rhs: B) = fun(lhs: A) = func(lhs, rhs)
fun div(content: String) = "<div>$content</div>"
fun anchor(link: String, text: String) = "<a href =\"$link\"> $text </a>"
class TreeNode(var `val`: Int) {
var left: TreeNode? = null
var right: TreeNode? = null
}
fun main(args: Array<String>) {
val root = TreeNode(1).apply {
left = TreeNode(2).apply { // left child of 1
left = TreeNode(4) // left child of 2
@yairzaslavsky
yairzaslavsky / MaxSubArray.kt
Created December 24, 2021 01:43
max sub array - kotlin, pure functional
fun maxSubArray(nums: IntArray): Int = nums.map { it to Integer.MIN_VALUE }
.fold(Pair(0, Integer.MIN_VALUE)) {
currSum,num -> with (num.first + currSum.first) {
Pair(this.takeIf { it >= 0 } ?: 0 , this.coerceAtLeast(currSum.second) )
}
}
.second
fun reverseList(head: Node?): Node? = reverseList(null, head)
fun reverseList(current: Node?, next: Node?): Node {
if (next == null) {
return current!!
}
val nextnext = next.next
next.next = current
return reverseList(next, nextnext)
data class Node(val data: Int, var next: Node? = null)
fun reverseList(head: Node?): Node? = ... //implement this
fun reverseList(current: Node?, next: Node?): Node? = ... //implement this