Skip to content

Instantly share code, notes, and snippets.

View petertseng's full-sized avatar

Peter Tseng petertseng

View GitHub Profile
@petertseng
petertseng / all_one.rb
Created December 2, 2017 11:47
Inc, Dec, GetMax, GetMin
class AllOne
def initialize()
@sorted = []
@positions = {}
@leftmost = {}
@count = {}
end
def inc(key)
if (pos = @positions[key])
# http://www.mountainvistasoft.com/chaocipher/ActualChaocipher/Chaocipher-Revealed-Algorithm.pdf
# suggested improvements (IV)
# https://pthree.org/2015/07/09/the-chaocipher-with-playing-cards/
# reasonable allegation that one alphabet is irrelevant, so strength is only 26!
# https://prgomez.com/scrabble-cipher/
def permute_left(left, idx)
rotated = left.chars.rotate(idx)
rotated[1..13] = rotated[2..13] << rotated[1]
rotated.join
@petertseng
petertseng / insert_signs.rb
Created November 14, 2017 04:39
add up to 100
def ops(nums, result, sign = 1)
case nums.size
when 0, 1
result == (nums[0] || 0) * sign ? [[nums[0]].compact] : []
else
n1, n2 = nums.first(2)
plus = ops(nums.drop(1), result - n1 * sign, 1).map { |r| [n1, :+] + r }
minus = ops(nums.drop(1), result - n1 * sign, -1).map { |r| [n1, :-] + r }
concat = ops([n1 * 10 + n2] + nums.drop(2), result, sign)
plus + minus + concat
require 'json'
require 'time'
# Create a personal access token at https://github.com/settings/tokens
# It needs NO scopes.
# In fact, you could just not use a token, but then you get rate-limited to 60 an hour instead of 5000 an hour.
TOKEN_FILE = 'secrets/token.txt'.freeze
SLEEP_TIME = 2
CACHE = 'cache'.freeze
@petertseng
petertseng / minijson.rb
Created March 17, 2017 04:56
mini JSON
class ParseError < StandardError; end
def object(s, i)
o = {}
state = :key
key = nil
j = i + 1
while (c = s[j])
case state
@petertseng
petertseng / submit.rb
Last active November 3, 2019 03:36
Exercism autosubmit
#!/usr/bin/env ruby
if ARGV.empty?
puts "usage: #{$PROGRAM_NAME} dir"
Kernel.exit(1)
end
track = File.basename(File.dirname(File.realpath(ARGV[0])))
def glob(*components, except: [])
@petertseng
petertseng / exercism-teams.rb
Last active May 10, 2017 06:48
Exercism teams
require 'json'
require 'set'
# Create a personal access token at https://github.com/settings/tokens
# It needs only one scope: read:org ("Read org and team membership").
TOKEN_FILE = 'secrets/token.txt'.freeze
SLEEP_TIME = 2
global_name = 'track-maintainers'.freeze
@petertseng
petertseng / vttoffset.rb
Created January 3, 2017 01:12
VTT offset
if ARGV.empty?
puts "usage: #{$PROGRAM_NAME} offset [file] (or stdin)"
exit 1
end
offset = ARGV.shift.to_i
ARGF.each_line { |l|
unless l.include?('-->')
puts l
@petertseng
petertseng / astar.rb
Last active December 27, 2016 08:14
A* search, maybe useful someday
require 'set'
def heuristic(start, goal)
{
start: 5, # Not given in the diagram but probably not important.
a: 4,
b: 2,
c: 4,
d: 4.5,
e: 2,
@petertseng
petertseng / regex_golf.rb
Created December 19, 2016 23:28
Regex Golf
# http://norvig.com/ipython/xkcd1313.ipynb
# http://nbviewer.jupyter.org/url/norvig.com/ipython/xkcd1313.ipynb
#
# http://norvig.com/ipython/xkcd1313-part2.ipynb
# http://nbviewer.jupyter.org/url/norvig.com/ipython/xkcd1313-part2.ipynb
#
# Only implemented the 'covers' optimisation in part 2
# no branch and bound.
# no repetition characters or pairs