Skip to content

Instantly share code, notes, and snippets.

def descending_sub_n_digit_palindromes(n):
while n >= 0:
k = (n + 1) // 2
highest = 10 ** k - 1
lowest = highest // 10
for x in range(highest, lowest, -1):
s_x = str(x) # "abcde"
rev = s_x[::-1] # "edcba"
# We could iterate over factors and check for palindromes in their products,
# but this is slower than iterating over palindromes and trying to split them
# up[1]: the approach used here examines possible solutions in descending
# order, so we always find the biggest one first. This allows us to exit early.
#
# Either way, here's a definition for `is_palindrome` that ended up being
# unnecessary:
#
# def is_palindrome(x):
# """Is the given positive integer x a palindromic number?"""
@nooodl
nooodl / futoshiki.lhs
Last active August 29, 2015 14:05
Literate Futoshiki solver
Introduction
============
Futoshiki (不等式, meaning “inequality”) is a Japanese logic puzzle similar to
Sudoku and the like. Numbers from 1 to n must be placed on an n-by-n grid
(which usually already contains some values) such that each row and each column
is a permutation of `[1..n]`. Additionally, less-than or greater-than signs are
placed between cells, constraining their mutual ordering.
An example puzzle looks like this:
@nooodl
nooodl / dewinfont.py
Last active August 29, 2015 14:04
packagable clone of Simon Tatham's dewinfont
#!/usr/bin/python
# dewinfont is copyright 2001 Simon Tatham. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
@nooodl
nooodl / imgur.rb
Created July 13, 2012 16:05
imgur album downloader
require 'nokogiri'
require 'open-uri'
url = ARGV[0] || gets
out = ARGV[1] || url.split('/')[-1]
puts "Retrieving album page..."
url = "http://imgur.com/a/#{url}" unless url =~ /^http/
doc = Nokogiri::HTML(open(url).read)
@nooodl
nooodl / rfkbot.py
Created September 10, 2011 23:55
rfkbot for acehack.sovrappensero.info
import pexpect, ANSI
import sys
from random import choice
NAME = 'rfkbot'
PASS = 'kitten'
HARD_MODE = True
login = 'l{0}\n{1}\n{2} '.format(NAME, PASS, 'R' if HARD_MODE else 'r')
@nooodl
nooodl / kroisos.rb
Last active September 26, 2015 21:38
irc bot for nethack monster spoilers in 45 lines of code
=begin
<nooodl> @mind flayer
<Kroisos> mind flayer (h) | AC: 5 | Speed: 12 | MR: 90 | Attacks: 1d4 weapon
physical, 2d1 tentacle eatbrain, 2d1 tentacle eatbrain, 2d1 tentacle
eatbrain | Corpse: none | Resist: none
<nooodl> @s ac<0 speed<=6 mr
<Kroisos> ghost (ac: -5, speed: 3, mr: 50), hezrou (ac: -2, speed: 6, mr:55),
ice devil (ac: -4, speed: 6, mr: 55), pit fiend (ac: -3, speed: 6, mr: 65),
balrog (ac: -2, speed: 5, mr: 75), Juiblex (ac: -7, speed: 3, mr: 65),
Geryon (ac: -3, speed: 3, mr: 75)