Skip to content

Instantly share code, notes, and snippets.

View studentbrad's full-sized avatar
🇨🇦

Bradley Aaron Kohler studentbrad

🇨🇦
View GitHub Profile
@studentbrad
studentbrad / median.c
Created October 23, 2019 23:14
Median of Two Sorted Arrays
double find_median_sorted_arrays(int *nums1, int nums1Size, int *nums2, int nums2Size)
{
int nums3Size = nums1Size + nums2Size;
int nums3[nums3Size];
int i = 0;
int j = 0;
while ((i != nums1Size) && (j != nums2Size))
{
if (nums1[i] <= nums2[j])
{
@studentbrad
studentbrad / linked_addition.c
Created October 23, 2019 23:21
Add two linked lists representing a non-negative number
struct ListNode *linked_addition(struct ListNode *l1, struct ListNode *l2)
{
struct ListNode *l1Start = l1;
struct ListNode *l2Start = l2;
struct ListNode *lSum = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *lSumStart = lSum;
bool flag = true;
while (flag)
{
lSum->val = l1->val + l2->val;
@studentbrad
studentbrad / palidromic_substring.c
Last active October 23, 2019 23:29
Find the longest palindromic substring in a string
char *longest_palindrome(char *s)
{
int n = strlen(s);
if (n < 2) return s; // Every string of length 1 or 0 is a palindrome
bool p[n][n];
memset(p, 0, sizeof(p));
int start = 0;
int length = 1;
@studentbrad
studentbrad / zigzag_conversion.c
Created October 23, 2019 23:28
Zigzag conversion on a string
char *zigzag_convert(char *s, int numRows)
{
int numChars = strlen(s);
if (numRows == 1) return s; // Division by zero will occur in the next line otherwise.
int numColumns = numChars / (numRows + numRows - 2) * (numRows - 1) + numChars % (numRows + numRows - 2) / numRows + numChars % (numRows + numRows - 2) % numRows;
// Allocate space for the table.
char **table = malloc(numRows * sizeof(char *));
for (int i = 0; i < numRows; i++)
{
@studentbrad
studentbrad / longest_substring.c
Created October 25, 2019 19:37
The longest sub-string length in a character array
int longest_substring(char* s)
{
int string_length = strlen(s);
if (string_length == 0)
{
return 0;
}
// form the string matrix
int string_matrix[string_length][string_length];
@studentbrad
studentbrad / lu_pp.m
Created February 22, 2020 04:50
lu solver using partial pivoting
function [L, U, P] = lu_pp(A)
% [L, U, P] = lu_pp(A) computes a unit lower triangular matrix L,
% an upper triangular matrix U, and a permutation matrix P such that
% P * A = L * U.
% The LU factorization is computed using partial pivoting.
% for the n x m matrix A, n == m
size_A = size(A);
if(size_A(1) ~= size_A(2))
return
@studentbrad
studentbrad / lu_spp.m
Created February 22, 2020 04:51
lu solver using scaled partial pivoting
function [L, U, P] = lu_spp(A)
% [L, U, P] = lu_spp(A) computes a unit lower triangular matrix L,
% an upper triangular matrix U, and a permutation matrix P such that
% P * A = L * U.
% The LU factorization is computed using scaled partial pivoting.
% for the n x m matrix A, n == m
size_A = size(A);
if(size_A(1) ~= size_A(2))
return
@studentbrad
studentbrad / get_yahoo_stockdata3.m
Created March 22, 2020 22:54
yahoo stock data in matlab
function stock = get_yahoo_stockdata3(ticker,d1,d2,freq)
% Updated from v3 when in May 2017, yahoo went and changed how stock data
% was shown on web pages.
%
% INPUTS:
%
% ticker <-- Yahoo ticker symbol for desired security. This can be a char
% string for a single stock, or data can be retrieved for
% multiple stocks by using a cellstr array.
%
@studentbrad
studentbrad / adaptive_simpson.m
Created March 22, 2020 23:18
adaptive simpson matrix multiplication in matlab
function [S, count] = adaptive_simpson(f, a, b, epsilon, level, level_max)
% Evaluates the integral of f using Adaptive Simpson
% over [a, b] such that 1 / 15 * abs(S^(2) - S^(1)) < e.
count = 1;
level = level + 1;
h = b - a;
c = (a + b) / 2;
f_a = f(a);
f_c = f(c);
f_b = f(b);
@studentbrad
studentbrad / bfgs.m
Last active May 20, 2020 01:41
Various methods for solving unconstrained non-linear optimization problems in Matlab including Newton, TrustRegion and BFGS. An additional test script `experiment.m` is added along with test functions Fletcher, Powell and Rosenbrock.
function [minx, minf, iter, funev] = bfgs(ffunc, gfunc, n, x0, epsfun, epsgrad, epsx, maxiter, maxfun)
%
% *******************
% * MANDATORY INPUT *
% *******************
% ffunc : A string indicating the function evaluation file. For example,
% if the file name is 'fMyFunction.m', then ffunc should be
% 'fMyFunction'.
% gfunc : A string indicating the gradient evaluation file. For example,
% if the file name is 'gadient.m', then gfunc should be