Skip to content

Instantly share code, notes, and snippets.

View LiamHz's full-sized avatar

Liam Hinzman LiamHz

View GitHub Profile
@LiamHz
LiamHz / Voronoi.py
Last active August 29, 2018 17:13
Solution to S1 of the 2018 CCC
#Number of villages
N = int(input())
#List of village positions
villages = list()
for i in range(N):
villages.append(float(input()))
#List of distances between villages
distances = list()
// Read authentication credentials from file
var fs = require('fs');
fs.readFile('credentials.txt', 'utf8', function(err, contents) {
var text = contents.split("\n")
text[1] = text[1].substring(0, text[1].length - 1);
text[2] = text[2].substring(0, text[2].length - 1);
username = text[1];
password = text[2];
});
# Trim ending characters of a filename of every file in a folder
Get-ChildItem 'FOLDER_NAME' | rename-item -newname { $_.name.substring(0, $_.name.length-N) }
# Trim beginning N characters of a filename of every file in a folder
get-childitem *.txt | rename-item -newname { [string]($_.name).substring(N) }
# Append a file extension to every file in a folder
Get-ChildItem 'FOLDER_NAME' | rename-item -NewName {$_.FullName + ".pdf"}
# Append text to every filename BEFORE the file extension
from slackclient import SlackClient
slack_client = SlackClient(SLACK_OAUTH)
# Print a list of users and their respective IDs
request = slack_client.api_call("users.list")
if request['ok']:
for item in request['members']:
print("{}: {}".format(item['name'], item['id']))
@LiamHz
LiamHz / powershell_grep.txt
Last active October 13, 2018 01:07
Search through current directory for any matches of specified text
# Search through current directory and all sub directories for text
dir -Recurse | Select-String -pattern "STRING"
# Recursive search and table formatting
dir -Recurse | Select-String -pattern "latest_net_G" | Select Path, LineNumber, Line | Format-Table
# Search through all files with the .EXT extension in the current directory
# and find any lines that match with STRING
@LiamHz
LiamHz / json_demo.py
Created October 8, 2018 14:50
A demo of how to use JSON to read and write to disk
import json
# Read variables from disk
with open('data.json', 'r') as json_file:
data = json.load(json_file)
a = data['a']
b = data['b']
c = data['c']
d = data['d']
@LiamHz
LiamHz / pickle_demo.py
Created October 8, 2018 14:52
A demo of how to use Pickle to read and write to disk
import pickle
data = {}
# Read from disk
pickle_jar = open('data.pkl', 'rb')
data = pickle.load(pickle_jar)
# Write to disk
pickle_jar = open('data.pkl', 'wb')
# Calculate the average squared error between two high-level layer activations
content_loss = torch.mean((target_features['conv4_2'] - content_features['conv4_2'])**2)
def gram_matrix(tensor):
# Get the batch_size, depth, height, and width of the Tensor
# Reshape it, so we're multiplying the features for each channel
_, d, h, w = tensor.size()
tensor = tensor.view(d, h*w)
# Calculate the gram matrix by multiplying the tensor with its transpose
gram = torch.mm(tensor, tensor.t())
return gram
show_every = 400 # Show target image every x steps
optimizer = optim.Adam([target], lr=0.003) # Optimizer hyperparameters
steps = 2000 # How many iterations to update content image
# Training Loop
for ii in range(steps):
# Calculate the content loss
target_features = get_features(target, vgg)
content_loss = torch.mean((target_features['conv4_2'] - content_features['conv4_2'])**2)