Skip to content

Instantly share code, notes, and snippets.

@alexgmcm
alexgmcm / parallel-letter-frequency.rs
Created July 19, 2020 19:24
Why does input need a static lifetime even though I'm passing an Arc to the thread?
use std::collections::HashMap;
//use std::sync::Mutex;
use std::sync::Arc;
use std::thread;
pub fn frequency(input: &[&str], worker_count: usize) -> HashMap<char, usize> {
let input_len = input.len();
let lines_per_thread = ((input_len as f64) / (worker_count as f64)).ceil() as usize;
let mut thread_handles = vec![];
@alexgmcm
alexgmcm / LeftRotate.js
Created September 18, 2018 13:48
LeftRotate.js
function LeftRotate(i, r, bitSize) {
if (i.isNegative()){
//take twos complement representation
i=bigInt(1).shiftLeft(32).subtract(i.abs()).and(bigInt("00000000ffffffff",16));
var mask=bigInt(1).shiftLeft(bitSize).subtract(1);
var x=i.shiftLeft(r).or(i.shiftRight(bitSize-r)).and(mask);
var x_arr=x.toArray(2)
var neg=false
if (x_arr['value'][0]==1){
//convert back
@alexgmcm
alexgmcm / predictOneVsAll.m
Created August 3, 2012 12:45
Stanford Machine Learning Exercise 3 code
function p = predictOneVsAll(all_theta, X)
%PREDICT Predict the label for a trained one-vs-all classifier. The labels
%are in the range 1..K, where K = size(all_theta, 1).
% p = PREDICTONEVSALL(all_theta, X) will return a vector of predictions
% for each example in the matrix X. Note that X contains the examples in
% rows. all_theta is a matrix where the i-th row is a trained logistic
% regression theta vector for the i-th class. You should set p to a vector
% of values from 1..K (e.g., p = [1; 3; 1; 2] predicts classes 1, 3, 1, 2
% for 4 examples)
@alexgmcm
alexgmcm / oneVsAll.m
Created August 2, 2012 13:17
Stanford Machine Learning Course: ex3 oneVsAll.m
function [all_theta] = oneVsAll(X, y, num_labels, lambda)
%ONEVSALL trains multiple logistic regression classifiers and returns all
%the classifiers in a matrix all_theta, where the i-th row of all_theta
%corresponds to the classifier for label i
% [all_theta] = ONEVSALL(X, y, num_labels, lambda) trains num_labels
% logisitc regression classifiers and returns each of these classifiers
% in a matrix all_theta, where the i-th row of all_theta corresponds
% to the classifier for label i
% Some useful variables
@alexgmcm
alexgmcm / costFunctionReg.m
Created August 1, 2012 15:34
Stanford Machine Learning Exercise 2 code
function [J, grad] = costFunctionReg(theta, X, y, lambda)
%COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization
% J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using
% theta as the parameter for regularized logistic regression and the
% gradient of the cost w.r.t. to the parameters.
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly