Skip to content

Instantly share code, notes, and snippets.

View mrnkr's full-sized avatar
🏠
Working from home

Alvaro Nicoli mrnkr

🏠
Working from home
View GitHub Profile
task Buffer is
entry Insert (In_Val: in T);
entry Extract (Out_Val: out T);
end
task body Buffer is
Data: Array 0..N-1 of Int;
Top: Int;
Bottom: Int;
Length: Int;
x: T;
task Readers_Writers is
procedure Reader (Read: out Int);
entry Writer (Written: in Int);
entry Begin_Reading;
entry Finish_Reading;
end;
procedure Reader (Read: out Int) is
begin
Begin_Reading;
task Readers_Writers is
procedure Reader (Read: out Int);
entry Writer (Written: in Int);
entry Begin_Reading;
entry Finish_Reading;
end
procedure Reader (Read: out Int) is
begin
Begin_Reading;
Read := Shared;
task type Semaphore is
entry Init (Initial_Capacity: in Int);
entry Wait;
entry Signal;
end
task body Semaphore is
Remaining_Capacity: Int;
begin
accept Init (Initial_Capacity: in Int) do
Remaining_Capacity := Initial_Capacity;
@mrnkr
mrnkr / MinimizeSum.swift
Created December 5, 2018 14:18
Backtracking / Dynamic programming exercise - Go from start to finish in a matrix accumulating the least possible sum of inner elements
import Foundation
func min(a: Int, b: Int) -> Int {
return a < b ? a : b
}
func adjacentSpots(board: [[Int]], start: [Int]) -> [[Int]] {
let i = start[0]
let j = start[1]
var ret: [[Int]] = []
@mrnkr
mrnkr / Necklace.swift
Created December 5, 2018 14:52
Dynamic programming exercise - Build a necklace with the greatest value without surpassing the maximum allowed weight (modification of the 0/1 knapsack problem)
import Foundation
func max(a: Int, b: Int) -> Int {
return a > b ? a : b
}
func necklace(pearls: [[Int]]) -> [[Int]] {
var res = Array.init(repeating: Array.init(repeating: 0, count: 151), count: pearls.count)
var ret: [[Int]] = []
@mrnkr
mrnkr / AyudameNey.swift
Created December 5, 2018 18:10
Dynamic Programming exercise - Help someone on the run from the cops to get to the border stopping in as few gas stations as possible. When you stop at one you drop all your gas cause your car is a bit messed up :P
import Foundation
func min(array: [Int]) -> Int {
var ret = Int.max;
for i in 0...array.count - 1 {
if array[i] < ret {
ret = array[i]
}
}
@mrnkr
mrnkr / MalaMiaDisculpame.swift
Last active December 5, 2018 18:54
Backtracking exercise - You just got home and it's late, get to your bedroom making as little noise as humanly possible (alla home invasion in GTA SA)
import Foundation
func calcNoise(standingOn: Character) -> Int {
switch standingOn {
case "A":
return 1
case "S":
return 3
case "H":
return 5
@mrnkr
mrnkr / Pedrito.swift
Created December 5, 2018 18:18
Backtracking/Dynamic Programming exercise - Find out the minimum number of throws needed to know which floor to throw a marker from in order to know whether it breaks
import Foundation
func max(a: Int, b: Int) -> Int {
return a > b ? a : b
}
// Assume this one works just fine, since I copied it
func pedritoBt(markers: Int, floors: Int) -> Int {
if floors <= 1 {
return floors
@mrnkr
mrnkr / ArrayContainsSubArray.swift
Created December 5, 2018 23:04
Dynamic programing exercise - Recieve two arrays and check if the second one contains the first (like the longest common subsequence)
import Foundation
// Returns true iif a in contained by b :)
// This is a modification of the longest common subsequence problem
func contains(a: [String], b: [String]) -> Bool {
var res: [[Int]] = Array.init(repeating: Array.init(repeating: 0, count: b.count), count: a.count)
for i in 0...a.count - 1 {
for j in 0...b.count - 1 {
if a[i] == b[j] {