Skip to content

Instantly share code, notes, and snippets.

@johnludwigm
johnludwigm / HappyNumbers.java
Created February 13, 2018 01:34
Solution to the Happy Numbers problem in Java.
import java.util.HashSet;
import java.util.Scanner;
import java.io.*;
public class HappyNumber {
//Note that we did not account for the fact that all permutations of a happy number's digits
//must also form happy numbers since addition commutes. This is mentioned in the README.
private final static long ten = 10;
static long squareSum(long number) {
long total = 0;
@johnludwigm
johnludwigm / HappyNumbers.md
Last active February 13, 2018 01:35
Happy numbers problem description.

Problem:

We'll say that for a positive whole number n, the squareSum of n is the sum of the squares of the digits of n. So if n is 1406, then squareSum(n) is 1^(2) + 4^(2) + 0^(2) + 6^(2) = 1 + 16 + 0 + 36 = 53.

We further say that the k^(th)-squareSum of n is squareSum(squareSum(...(squareSum(n)))), where squareSum is composed k-many times. For example, the 3rd-squareSum of 1406 is squareSum(squareSum(squareSum(1406))) = squareSum(squareSum(53)) (as we know from above) = squareSum(5^(2) + 3^(2)) = squareSum(25 + 9) = squareSum(34) = 3^(2) + 4^(2) = 9 + 16 = 25.

Definition: A number n is happy if for some positive integer m, the m^(th)-squareSum of n is equal to 1.

@johnludwigm
johnludwigm / tuple_riddle.py
Last active February 6, 2018 12:32
Python riddle.
#February 6, 2018
#1. Give the return value of the following expression:
>>> 1, 2, 3, 4 == (1, 2, 3, 4)
##############
##############
##############
##############
##############
@johnludwigm
johnludwigm / inline_example1.java
Last active February 4, 2018 17:11
Example of in-line assignment
import java.io.*;
class Main {
public static void main(String[] args) {
String fileName = "blog.txt";
FileReader fileReader = new FileReader(fileName);
BufferedReader br = new BufferedReader(fileReader);
String line;
@johnludwigm
johnludwigm / securestore.py
Last active January 26, 2018 00:20
Securely storing JSON credentials for AWS.
>>> aws_access_key_id = "BLPEVU91RZCTCC4CE01J"
>>> aws_secret_access_key = "AOIjf2CEW2faefAErwrt43G/9OaJfijvansEJfib"
>>> region_name = "us-east-1"
>>> the_password_I_type = "MySecretPassword"
>>> ciphertext = encrypt(the_password_I_type.encode(), aws_secret_access_key.encode())
>>> #Let's take a look!
>>> ciphertext
'OJ+T8lHBRqxGyZjTsJjP634iBhjJImYyaep/nv03aCnQkv9+8+I/m+2m1dsAv9JGu6JjnUcVyQSd5WPUEAqSYg=='
@johnludwigm
johnludwigm / enc.py
Created January 25, 2018 22:28
A possible way to encrypt a secret key only using base
aws_secret_access_key = "BmUejjI9Fp23FAOJoijeRAJse/fAEIfo4FAJFIwe"
#It is absolutely infeasible to find a string which hashes to something
#that even remotely resembles your aws_secret_access_key.
the_password_I_will_enter = "MySecretPassword"
#Note that the_password_I_will_enter is a string rather than a bytes object:
"""
>>> isinstance("MySecretPassword", str)
@johnludwigm
johnludwigm / create_statements.py
Last active December 12, 2018 17:47
SQL CREATE statements for IMDb database.
create_Title = ("CREATE TABLE IF NOT EXISTS Title ("
"tconst TEXT PRIMARY KEY, "
"title_type TEXT, "
"primary_title TEXT, "
"original_title TEXT, "
"is_adult INTEGER, "
"start_year INTEGER, "
"end_year INTEGER, "
"runtime_minutes INTEGER, "
"genres TEXT);")
@johnludwigm
johnludwigm / builddb.py
Created January 16, 2018 15:54
Creates IMDb Database Using sqlite3 in Python.
import sqlite3
import StreamTSV
import sqliteops
import create_statements as cts
import os
table_urls = [("Title", "https://datasets.imdbws.com/title.basics.tsv.gz"),
("Name", "https://datasets.imdbws.com/name.basics.tsv.gz"),
@johnludwigm
johnludwigm / noneandnull.py
Last active August 29, 2023 10:02
NULL vs None in Sqlite3, Python
dbpath = "/.../test.db"
>>> import sqlite3
>>> #Create the database.
>>> connection = sqlite3.connect(dbpath)
>>> cursor = connection.cursor()
>>> cursor.execute("CREATE TABLE Test (testcolumn TEXT);")
<sqlite3.Cursor object at 0x111952ce0>
>>> cursor.execute("INSERT INTO Test VALUES(NULL);")
<sqlite3.Cursor object at 0x111952ce0>
@johnludwigm
johnludwigm / IMDbtsv.py
Last active January 3, 2018 20:14
Explains and solves IMDb tsv file problem.
import csv
import sys
def getmaxsize():
"""Returns max field size."""
return csv.field_size_limit(sys.maxsize)
def setmaxsize(size = 500 * 1024 * 1024):
"""Sets max field size as high as possible."""