Skip to content

Instantly share code, notes, and snippets.

View jakelosh's full-sized avatar

Jake Losh jakelosh

  • Affirm
  • Pacifica, California, USA
  • X @jakelosh
View GitHub Profile
@jakelosh
jakelosh / fizz_buzz.py
Created March 30, 2020 16:29
An implementation of FizzBuzz in Python
def fizz_buzz(start, stop, fizz_val, buzz_val):
for i in range(start, stop):
str_out = ""
if i % fizz_val == 0:
str_out += "Fizz"
if i % buzz_val == 0:
str_out += "Buzz"
if str_out == "":
str_out += str(i)
print(str_out)
@jakelosh
jakelosh / VBAFizzBuzz
Created August 23, 2019 23:37
VBA implementation of FizzBuzz
Public Sub FizzBuzz()
For i = 1 to 100
result = ""
If (i mod 3) = 0 Then result = result & "Fizz"
If (i mod 5) = 0 Then result = result & "Buzz"
If result = "" result = str(i)
Debug.Print (result)
Next i
End Sub
@jakelosh
jakelosh / chick-game-sim.py
Last active July 4, 2019 07:13
I play a board game with my daughter called "Count Your Chickens". At the start, little chick tokens are scattered throughout the board and the objective is to get them all back in the coop. Each turn you spin a wheel with a number of farm-related items, which determines the number of spaces you move. The number of spaces you move determines the…
import numpy as np
# Create spinner function
def spin():
return np.random.randint(0,5)
spin_categories = {
0: 'pig',
1: 'dog',
2: 'sheep',
@jakelosh
jakelosh / copy_file_query_db_populate_spreadsheet.py
Created May 5, 2018 06:03
This is a simple script that copies a spreadsheet from a directory, makes a call to a database, loads the results into a data frame and then populates a spreadsheet with values. I used a very similar set of scripts to automate a series of monthly admin tasks.
# import relevant libraries
import pyodbc # needed for database call
import pandas # needed for data frame functionality
import shutil # needed for file copying
import datetime # needed to find dates for file paths
import xlwings # needed to populate the spreadsheet
# COPY FILE STEP
# determine destination directory to save a copy of the file
# file folders are nested according to month and year, so we do a bit of work
@jakelosh
jakelosh / pinyin.R
Created January 23, 2018 06:57
A simple regex exercise in R whereby I build pinyin syllables from base consonant and vowel pairs and then pare down to valid syllables.
# The purpose of this script is to generate a random Mandarin syllable pairing for use in tone
# practice drills. The random syllable pair is used to populate a grid that does not have tones.
# The tone_table has the tones of the two syllables. The student then tries to say the random
# pairing with the tones they see. The partner then tries to guess which tones were meant.
#
# The script generates the syllables from scratch, starting with all possible vowel and consonant
# pairings. Then it uses a very complicated string of regex to exclude out invalid syllables.
# My goal with this script was to make the tone tables, but it was also meant to learn some
# useful regex, which is why I indulged in it rather than just typing out all possible syllables
# (which would have been much faster at my typing speed than finding all the exclusions I needed).
@jakelosh
jakelosh / think_python_ch13_ex02.py
Created July 2, 2017 05:27
This is a solution to exercise 2 of chapter 13 of Think Python (http://greenteapress.com/thinkpython/html/index.html). It opens text files of several books and then gives the top 10 most used words in each book, excluding common words.
# Chapter 13 Exercise 2
"""
Go to Project Gutenberg (http://gutenberg.org) and download
your favorite out-of-copyright book in plain text format.
Modify your program from the previous exercise to read the
book you downloaded, skip over the header information at the
beginning of the file, and process the rest of the words as
@jakelosh
jakelosh / vbaAddSingleQuotes
Created January 9, 2015 19:34
I often have a need to query my company's proprietary database using securities identifiers I get from a spreadsheet. I wrote this script to help me do that more efficiently. I store it in my personal workbook so I can use it anytime I open Excel. The script takes any selected range (contiguous or not) and puts single quotes around the values in…
Public Sub AddSingleQuotes()
Dim rng as Range
Set rng = Selection
Dim cell as Range
Dim booFirst as Boolean
booFirst = True
With rng
For Each cell in rng
If booFirst = True Then
@jakelosh
jakelosh / vbaNavigateInternetExplorer
Last active October 4, 2023 19:45
As part of a larger process I was trying to automate, I had to interact with a web browser. Interacting with Internet Explorer from Excel VBA is pretty simple and there are a lot of good explanations out there, but I thought I'd share a snippet of code that selects everything on the webpage and pastes it to an output sheet for further processing…
Public Sub NavigateIe()
'Declare some helpful variables
Dim wksIn As Worksheet, wksOut As Worksheet
Dim strTickerArr() As String, varNotionalArr() As Variant, strBloomArr() As String
Dim strLink As String
'Set some worksheet names
Set wksIn = ThisWorkbook.Worksheets("INPUT")
@jakelosh
jakelosh / vbaRollDays
Created January 13, 2014 04:07
When you're doing quality control processes where you compare values day-over-day in Excel, you'll often need to "roll" data on the worksheets so that you can build a new comparison. This macro assumes you have two sheets, one named "Current Day" and one named "Prior Day" and moves the information from the Current Day worksheet to the Prior Day …
Public Sub RollDays()
Dim wksCd As Worksheet, wksPd As Worksheet
Dim rngCd As Range, rngPd As Range
Dim lngCdLc as Long, lngPdLc as Long, lngMaxLc as Long
Dim lngCdLr As Long, lngPdLr As Long, lngMaxLr As Long
With ThisWorkbook
Set wksCd = .Worksheets("Current Day")
Set wksPd = .Worksheets("Prior Day")
@jakelosh
jakelosh / vbaWriteTextFile
Created September 23, 2013 15:08
Last week in my company's process improvements group we discussed writing out log messages from spreadsheet applications to text files. While the thought came from somebody in the group, I'm fairly certain the original source of the code they shared came from here: http://msdn.microsoft.com/en-us/library/dd439413%28v=office.12%29.aspx#Office2007…
Public Sub WriteTextFile(ByVal strMessage As String)
Dim objFile As FileSystemObject
Set objFile = New FileSystemObject
Dim txtStream As TextStream
Dim strPath As String
strPath = ThisWorkbook.Path & "\"