Skip to content

Instantly share code, notes, and snippets.

@arrieta
arrieta / slurp.hpp
Created June 28, 2016 04:33
Slurps a file into a C++ std::string.
// -*- coding:utf-8; mode:c++; mode:auto-fill; fill-column:80; -*-
/// @file slurp.hpp
/// @brief Slurps a file into an std::string.
/// @author J. Arrieta <Juan.Arrieta@nablazerolabs.com>
/// @copyright Copyright (c) 2016 Nabla Zero Labs. All rights reserved.
/// @license This software is released under the MIT License.
///
/// The MIT License (MIT)
/// Copyright (c) 2016 Nabla Zero Labs
@arrieta
arrieta / list.hpp
Created August 23, 2016 02:08
Educational implementation of a list in the style of the C++ Standard Library (it is minimal and incomplete, but easier to read than GCC's source).
// -*- coding:utf-8; mode:c++; mode:auto-fill; fill-column:80; -*-
/// @file list.hpp
/// @brief A minimal list implementation copying the style of the C++
/// Standard Library --- it is meant for educational purposes.
/// @author J. Arrieta <Juan.Arrieta@nablazerolabs.com>
/// @copyright Copyright (c) 2016 Nabla Zero Labs. All rights reserved.
/// @license This software is released under the MIT License.
/// @warning Do not use this turd in production code, or for anything that is
/// not strictly educational. Just use std::list (actually you most
@arrieta
arrieta / example-linspace.cpp
Created November 1, 2017 22:59
C++ implementation of linspace.
// -*- coding:utf-8; mode:c++; mode:auto-fill; fill-column:80; -*-
/// @file example-linspace.cpp
/// @brief Sample use of Nabla Zero Labs' linspace utilities.
/// @author J. Arrieta <Juan.Arrieta@nablazerolabs.com>
/// @date November 01, 2017
/// @copyright (c) 2017 Nabla Zero Labs
/// @license MIT License
// C++ Standard Library
@arrieta
arrieta / lcs.py
Created December 6, 2017 15:33
Longest Common Subsequence
def lcs(s1, s2):
def lcs_(s1, s2, m, n):
if (m < 0) or (n < 0):
return 0
elif s1[m] == s2[n]:
return 1 + lcs_(s1, s2, m - 1, n - 1)
@arrieta
arrieta / lpsub.py
Created December 6, 2017 15:33
Longest Palindromic Substring
def longest_palindrome_constant_space(s):
N = len(s)
if N == 0:
return 0
elif N == 1:
return 1
elif N == 2:
return 1 + (s[0] == s[1])
@arrieta
arrieta / mintrans.py
Created December 6, 2017 15:38
Minimum transformations from N to 1
def T(n):
memo = {
1 : (0, "<Base>"),
2 : (1, " / 2"),
3 : (1, " / 3")
}
def build_memo(n):
@arrieta
arrieta / knapsack.py
Created December 6, 2017 21:48
Solve small knapsack problems by different methods
"""
knapsack.py
Solve small knapsack problems by different methods.
(C) 2017 J. Arrieta, Nabla Zero Labs
MIT License
"""
@arrieta
arrieta / pds.js
Last active February 21, 2018 16:24
Lexer for the Planetary Data System (PDS) Object Description Language (ODL)
/*
This program implements a lexer for the Object Description Language (ODL), a legacy metadata format
used by NASA's Planetary Data System (PDS).
As of this writing, the ODL version is 2.1, and the specification can be found in:
https://pds.jpl.nasa.gov/documents/sr/Chapter12.pdf
This lexer simply emits the tokens needed by an ODL parser, which is provided as a separate program.
@arrieta
arrieta / contiguous-slice-mt-access.cpp
Created February 24, 2018 22:30
Simple pattern for thread-safe, read-only access to an existing, fixed, contiguous memory slice.
/*
A simple pattern for thread-safe, read-only access to an existing, fixed,
contiguous memory slice.
This pattern is useful when simple chunking would be undesirable. For example:
A job may consist of three hard (H) tasks, each lasting two seconds, and three
easy (E) tasks, each lasting one second. If we simply pass the first three to
one thread, and the last three to a second thread, the second thread will
finish after three seconds, and leave the first thread to finish six seconds
worth of work. (total runtime: six seconds). The proposed approach would take
@arrieta
arrieta / wsgi_middleware.py
Created March 2, 2018 20:54
Brief explanation of WSGI middleware
"""
A brief explanation of WSGI middleware.
Nabla Zero Labs
"""
# The main WSGI application returns the string "Hello, {ip_address}!", where
# `ip_address` is the IP address of the host making the request.
def application(environ, start_response):
print("Main application")