Skip to content

Instantly share code, notes, and snippets.

public class OrderCreationController {
private final OrderRepository orderRepository;
private final ProductCatalog productCatalog;
public OrderCreationController(OrderRepository orderRepository, ProductCatalog productCatalog) {
this.orderRepository = orderRepository;
this.productCatalog = productCatalog;
}
public Response post(List<Item> requests) {
@renanreismartins
renanreismartins / configure_personal_professional_git_account.txt
Last active November 22, 2023 18:12
Configure personal and professional git
~ cat .gitconfig-personal
[user]
signingkey = XXXXXXXXXX
name = Renan Reis Martins de Paula
email = renanreismartins@gmail.com
[core]
sshCommand = "ssh -i ~/.ssh/id_rsa_personal"
@renanreismartins
renanreismartins / BestSum.scala
Created March 2, 2023 11:19
needs memoization
object BestSum extends App {
def bestSum(n: Int, coins: Array[Int]): Array[Int] = {
if (n == 0) return Array[Int]()
if (n < 0) return null
var shortest: Array[Int] = null;
for (coin <- coins) {
@renanreismartins
renanreismartins / HowSum.scala
Last active March 2, 2023 10:29
null pointers because of the null return when there is no combination, but fine
object HowSum extends App {
def howSum(n: Int, coins: Array[Int]): Array[Int] = {
if (n == 0) return Array[Int]()
if (n < 0) return null
for (coin <- coins) {
val rest = n - coin
val ints = howSum(rest, coins)
@renanreismartins
renanreismartins / CanSum.scala
Created March 1, 2023 23:05
move the stop to the beginning
object CanSum extends App {
def canSum(n: Int, coins: Array[Int]): Boolean = {
if (n == 0) return true
if (n < 0) return false
for (coin <- coins) {
val rest = n - coin
return canSum(rest, coins)
@renanreismartins
renanreismartins / canSum.scala
Created March 1, 2023 22:27
coin change problem
object CanSum extends App {
def canSum(n: Int, coins: Array[Int]): Boolean = {
if (n == 0) return true
for (coin <- coins) {
val rest = n - coin
if (rest >= 0) return canSum(rest, coins)
}
@renanreismartins
renanreismartins / MissingPositive2.scala
Created February 21, 2023 15:48
A bit better, much to be done. Maybe do both operations in one traverse, fix the imperative checks and 0 checks. But enough for the day
object MissingPositive extends App {
def missingPositive(nums: Array[Int]): Int = {
val found: Map[Int, Int] = nums.foldLeft(Map[Int, Int]())((m, i) => if (i >= 0) m + (i -> 1) else m)
val min = nums
.filter(e => e > 0)
.foldLeft(Integer.MAX_VALUE)((m, i) => i.min(m))
if (min >= 0) {
var count = 1
@renanreismartins
renanreismartins / MissingPositive.scala
Created February 21, 2023 15:23
Procedural approach
object MissingPositive extends App {
def missingPositive(nums: Array[Int]): Int = {
//val m1: Map[Int, Int] = (0 until nums.length).foldLeft(Map[Int, Int]())((m, i) => if (nums(i) > 0) m + (i -> 1) else m)
val found = scala.collection.mutable.Map[Int, Int]()
var min = Integer.MAX_VALUE
object MissingNumber extends App {
def missingNumber(nums: Array[Int]): Int = {
val found = new Array[Int](nums.length)
for (n <- nums) {
if (n < nums.length) found(n) = 1
}
val missing = found.indexOf(0)
(ns griffin.spec.journal-entry
(:require [clojure.spec.alpha :as s]))
(s/def ::id uuid?)
(s/def ::account-id uuid?)
(s/def ::type #{:credit :debit})
(s/def ::amount pos-int?)
(s/def ::line-item (s/keys :req-un [::account-id ::amount ::type]))
(s/def ::line-items (s/coll-of ::line-item :min-count 2))
(s/def ::journal-entry (s/keys :req-un [::id ::line-items]))