when you meet the code, kill the code

It’s 2:45 am…I’ve been working on this for the last 6 hours:

import re
import string

l = string.lowercase
u = string.uppercase

def fun(delta, s):
    r = re.compile(':(.{%s})(.*?):' % delta)
    p = r.sub(r'\2\1', ':%s::%s:' % (l, u))
    m = string.maketrans(l + u, p)
    print s.translate(m)
    
s = raw_input("What is the string? 
delta = raw_input("And by how many places do we change it? 

fun(delta, s)


(looks like WP’s sourcecode tags are broken, but imagine a closing quote and parentheses for the raw_input functions there)

I learned a lot about regexps and list rotations in the course of this, 80% of which I didn’t know before…however the canonical solution for the same problem here can be done in half as much code. And probably less than that (actually, not probably — definitely — like in one line depending on the language). The idea is to use slices. Slices! If I had known…but now I do.

Anyway I’m still learning the syntax basically, but it’s a lot of fun.

Oh yeah…the idea is to aim toward making a Python roguelike. (!!?!!)

Advertisements

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: