Skip to content

Instantly share code, notes, and snippets.

@basilwong
basilwong / ancestors.py
Last active July 7, 2021 05:39
Basic Adjacency List Questions
# Suppose we have some input data describing a graph of relationships between parents and children over multiple generations. The data is formatted as a list of (parent, child) pairs, where each individual is assigned a unique positive integer identifier.
# For example, in this diagram, the earliest ancestor of 6 is 14, and the earliest ancestor of 15 is 2.
# 14
# |
# 2 4
# | / | \
# 3 5 8 9
# / \ / \ \
class Node():
def __init__(self, val):
self.val = val
self.left = None
self.right = None
T = Node(0)
T.left = Node(1)
T.right = Node(-2)
T.left.right = Node(2)
@basilwong
basilwong / sevenish_algorithm_problem_solution.py
Created November 19, 2019 08:24
Let's define a sevenish number is a number that is one of a power of 7, or a number that is the sum of unique power of 7s
'''
Let's define a sevenish number is a number that is one of a power of 7, or a number that is the sum of unique power of 7s
From the beginning the first few sevenish are:
1, 7, 8, 49, 50 and so on
You are to create an algorithm that finds the i'th sevenish number
'''
@basilwong
basilwong / max_seq.py
Created September 17, 2019 08:27
Given a sequence of positive integers in a string, and a positive integer k between 2 and n where n is the number of positive integers in the given string. Return the string that is a size k sequence of the given string that represents the largest number.
"""
Given a sequence of positive integers in a string, and a positive integer k between 2 and n where n is the number of positive integers in the given string,
Return the string that is a size k sequence of the given string that represents the largest number.
Example:
Input: '85888900', 3
Output: '900'
"""
@basilwong
basilwong / ParseFlatFile.java
Created September 16, 2019 19:09
Static Function that can be used as a map lambda to translate a string into the columns for a Spark Dataframe.
import org.apache.spark.sql.*;
/**
* Parses a line of a fixed width flat file
* @param pos list of integers describing the width of each column of the flat file
* @param str line of the fixed width flat file
* @return Row containing data from fixed width flat file line
*/
public static Row lSplit(List<Integer> pos, String str) {
@basilwong
basilwong / hashmapListsEqualSize.java
Created September 5, 2019 20:20
Checks if all the lists representing the values of a hashmap are the same length.
/**
* Checks if all the lists representing the values of a hashmap are the same length.
* @param occurrenceFields a hashmap with strings as the keys and lists of strings as the values
* @return true if all the lists are the same length, false otherwise
*/
private static boolean checkLengths(HashMap<String, HashMap<String, String>> occurrenceFields) {
Set<Integer> sizes = new HashSet<Integer>();
for (HashMap<String, String> list : occurrenceFields.values()) {
sizes.add(list.size());
}
@basilwong
basilwong / nav_maze.cpp
Created February 20, 2019 20:11
Simple logic function for navigating through a 2d maze.
/* Question 4: Maze Problem (Bonus)
* Starting point is m[0][0], need to find a path go to m[9][9]. 0 means OK,
* 1 means cannot go there, boundary is 0 and 9, cannot go beyond boundary.
* Each step can be made horizontally or vertically for one more grid (diagonal
* jump is not allowed).
* Your program should print a series of grid coordinates that start from m[0][0]
* and go to m[9][9]
* Hint: No need to find the shortest path, only need to find one path that gets
* you to desitination.
*/
@basilwong
basilwong / del_linked_list_elem.cpp
Created February 20, 2019 20:10
Function for deleting a specified element in a basic linked list.
/* Question 1: Delete an item in a linked list */
#include <bits/stdc++.h>
typedef struct list_item {
int value;
list_item* next;
} list_item;
// Iterates through the items in the linked list and checks for the specified
@basilwong
basilwong / vector_2d_to_sunmatrix.cpp
Last active August 23, 2018 00:20
Function that takes a 2d STL vector and converts it to a (SUNDIALS defined) sparse matrix utilizing sundials internal functions.
#include <iostream>
#include <vector>
#include <sunmatrix/sunmatrix_dense.h> // access to dense SUNMatrix
#include <sunmatrix/sunmatrix_sparse.h> // access to sparse SUNMatrix
// Converts 2d vector into a sparse matrix. This function assumes all the rows
// are the same size.
SUNMatrix convert_to_sparse(std::vector< std::vector< realtype > > vec) {
// Defining the rows and columns.
SUNMatrix temp_dense = SUNDenseMatrix(vec.size(), vec[0].size());