Skip to content

Instantly share code, notes, and snippets.

View steve-taylor's full-sized avatar

Steve Taylor steve-taylor

  • Sydney, Australia
View GitHub Profile
public class Challenge {
public static void main(String[] args) throws Exception {
int r=0;
for(String t : ("+"+Input.read().replaceAll("plus\\s+","+").replaceAll("minus\\s+","-")).split(" "))
r+=(t.charAt(0)=='+'?1:-1)*(java.util.Arrays.asList("on","tw","th","fo","fi","si","se","ei","ni").indexOf(t.substring(1,2))+1);
Output.write(""+r);
}
}
@steve-taylor
steve-taylor / Challenge.java
Created September 27, 2012 15:19
Semi (actual submission)
public class Problem1 {
static enum Oper {
PLUS,
MINUS
}
public static void main(String[] args) {
Oper lastOp = null;
Integer lastNum = null;
Integer res = null;
for (String s : Input.read().split(" ")) {
@steve-taylor
steve-taylor / Challenge.java
Created September 27, 2012 15:59
Grand final
import java.util.*;
public class Challenge {
public static void main(String[] args) throws Exception {
// Build map.
Map<String,Set<String>> graph = new HashMap<String,Set<String>>();
for (String rel : Input.read().split("\\,")) {
String[] toks = rel.split("\\-");
Set<String> friends = graph.containsKey(toks[0]) ? graph.get(toks[0]) : new HashSet<String>();
graph.put(toks[0], friends);
friends.add(toks[1]);
@steve-taylor
steve-taylor / DelayedTaskRunner.java
Created October 29, 2012 08:24
DelayedTaskRunner
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
/**
* Runs the last surviving task published after a specified delay.
*
* @author STaylor
@steve-taylor
steve-taylor / setup-firewall.sh
Created February 27, 2013 13:42
General purpose iptables script for a web server, allowing incoming TCP connections only on ports 22, 80 and 443, responses to outbound DNS requests, outbound and inbound pings, and outbound TCP connections. On some earlier versions of Linux (e.g. Centos 6.0), I have noticed that it is not enough to simply allow all outbound packets to allow TCP…
#!/bin/sh
service iptables stop
iptables -F
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
# Allow all loopback
iptables -A INPUT -i lo -j ACCEPT
@steve-taylor
steve-taylor / public-ipaddress.sh
Created February 27, 2013 14:58
Assign the name of the Internet facing network interface to a variable in a shell script. Requires Internet access.
#!/bin/sh
IPADDR=`wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`
NIC=`ip addr show | grep $IPADDR | awk '{ print $(NF) }'`
# Rest of your script here
@steve-taylor
steve-taylor / install-javaee6-postgresql9.sh
Created February 27, 2013 15:41
Install and configure a Java EE 6 web stack on Centos 6 consisting of OpenJDK 1.7.0, GlassFish 3.1.2.2, Maven 3.0.4, PostgreSQL 9.1 and PostgreSQL JDBC drivers.
#!/bin/sh
# PostgreSQL user that owns the schema (i.e. executes the deployment DDL). Change as needed.
DBSCHEMA_OWNER='app_owner'
DBSCHEMA_OWNER_PW='app_owner'
# PostgreSQL user that your web app uses to connect to the database (i.e. executes queries and DML). Change as needed.
DBSCHEMA_USER='app_user'
DBSCHEMA_USER_PW='app_user'
@steve-taylor
steve-taylor / change-server-timezone.sh
Created February 27, 2013 15:48
Set server time to Adelaide, Australia. Change as needed for your required timezone.
#!/bin/sh
# Set time to Adelaide.
echo "ZONE=\"Australia/Adelaide\"" > /etc/sysconfig/clock
/bin/cp /usr/share/zoneinfo/Australia/Adelaide /etc/localtime
@steve-taylor
steve-taylor / StreamingTokenizer.js
Last active December 14, 2015 10:10
Streaming string tokenizer, made specifically for Node.js, but could be used in the browser too. Note fully tested yet!
function StreamingTokenizer(delimiter, callback) {
this.delimiter = delimiter;
this.callback = callback;
this.pendingText = '';
this.open = true;
}
StreamingTokenizer.prototype = {
add: function(text) {
if (!this.open) {
@steve-taylor
steve-taylor / doSynchronousLoop.js
Last active December 4, 2022 00:51
Synchronous for loop in JavaScript to ensure that the n+1th item isn't processed until the callback of the nth item's processor has been called.
/**
* Process an array of data synchronously.
*
* @param data An array of data.
* @param processData A function that processes an item of data.
* Signature: function(item, i, callback), where {@code item} is the i'th item,
* {@code i} is the loop index value and {@code calback} is the
* parameterless function to call on completion of processing an item.
*/
function doSynchronousLoop(data, processData, done) {