One Liner challenge!

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 9644 Views - Last Post: 02 September 2012 - 05:52 PM Rate Topic: -----

#1 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

One Liner challenge!

Post icon  Posted 06 May 2012 - 07:58 AM

This is a Python Challenge to make the ugliest one liner possible! The rules are simple, use Python (2 or 3, I don't care) to create a massive one liner piece of code, the uglier the better! The official deadline will be May 13th at Midnight (so May 13th 11:59pm is the latest [but we're pretty flexible here at DIC]). Also, these 1 liners must equate to 1 statement, so separating statements on the same line with ';' is not good.

Lastly, with any Python challenge, I ALWAYS get people asking "can I use a different language", this time, I want to challenge you to use Python. You can use an Online Interpreter to test your code, so you won't even need to download anything new. On top of that, there are so many python resources out there, it should be easy for you to create something, ANYTHING!

Your submission can be multiple lines if you need extra code to set things up, so for example, here's my one liner. It's based on Dogstopper's Character Flipper snippet.

def inverter(original = None):
    if original == None:
        original = input(">>> ")
    return ''.join([char.upper() if char.islower() else char.lower() for char in str(original)])




Notice how my submission is 4 lines, but only 1 line (number 4) will be judged.

Good luck everyone, and I hope to see a lot of participation!

Is This A Good Question/Topic? 3
  • +

Replies To: One Liner challenge!

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,204
  • Joined: 21-June 11

Re: One Liner challenge!

Posted 06 May 2012 - 05:01 PM

*
POPULAR

Okay, here you go, my 1-line (plus import statement) Brainfuck interpreter:

import sys

def bf(c,cp=0,d={},dp=0,j=None,i=None):return(None if cp==len(c)else{".":lambda:bf(c,cp+1,d,dp,j,print(chr(d.get(dp,0)),end='')),",":lambda:bf(c,cp+1,d,dp,j,d.update({dp:ord(sys.stdin.read(1))})),"<":lambda:bf(c,cp+1,d,dp-1,j,i),">":lambda:bf(c,cp+1,d,dp+1,j,i),"+":lambda:bf(c,cp+1,d,dp,j,d.update({dp:d.get(dp,0)+1})),"-":lambda:bf(c,cp+1,d,dp,j,d.update({dp:d.get(dp,0)-1})),"[":lambda:bf(c,cp+1 if d.get(dp,0)else j[cp],d,dp,j,i),"]":lambda:bf(c,j[cp]if d.get(dp,0)else cp+1,d,dp,j,i)}.get(c[cp],lambda:bf(c,cp+1,d,dp,j,i))())if j!=None else i if cp==len(c)else({"[":lambda:d.append(cp),"]":lambda:i.update({cp:d[-1]})or i.update({d.pop():cp})}.get(c[cp],lambda:None)()or bf(c,cp+1,d,dp,j,i))if i!=None else bf(c,0,{},0,bf(c,0,[],0,None,{}))



I hope this is ugly enough for you.

Example usage:

bf("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>+comment+.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.-/>-------.>+.>.")


Note that since this heavily relies on recursion and consumes stack size linear in the number of steps the program executes (not just the length of the program), it will produce stack overflow errors for most programs that are more complicated than "Hello World".

This post has been edited by sepp2k: 06 May 2012 - 05:01 PM

Was This Post Helpful? 5
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: One Liner challenge!

Posted 06 May 2012 - 05:45 PM

Holy crap, I hadn't considered gratuitous usage of lambda lol. Well done sir. :surrender:

This post has been edited by atraub: 06 May 2012 - 05:45 PM

Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,204
  • Joined: 21-June 11

Re: One Liner challenge!

Posted 06 May 2012 - 06:01 PM

The lambda-maps are actually just there to cut down on the amount of if-else-expressions in the code. They're not required for writing this in one line.

Basically every occurrence of my {x: lambda: foo(), y: lambda: bar()}.get( z, lambda: baz())() pattern could be replaced with foo() if z == x else: bar() if z == y else: baz() and it would still be in one line.

I think the more interesting tricks I used are the ones that allowed me to execute side-effects without using a ;. For example I used mydict.update({key: value}) or foo instead of mydict[key] = value; foo. In some cases I also passed the results of "void" function calls as an ignore parameter to the recursive function call.

The things we do to our code in the name of obfuscation ;-)
Was This Post Helpful? 1
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: One Liner challenge!

Posted 06 May 2012 - 06:19 PM

I hope more people respond... I'm lookin at you Simown! The last forum discussion was kind of a bust, but I can post more obfuscated one liners, definitely won't have anything that cool though.
Was This Post Helpful? 0
  • +
  • -

#6 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: One Liner challenge!

Posted 06 May 2012 - 06:27 PM

Uh oh, I've been spotted. I am in the middle of a huge, huge project right now, but I'll see what I can come up with tomorrow - it's a promise. I'll do my best not to be shadowed too much by sepp2k's post.
Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: One Liner challenge!

Posted 06 May 2012 - 08:06 PM

Haha Simown, I understand. Tonight I have to write 8 essays and a Ruby on Rails website, all due tomorrow. I'm really in it deep right now, good luck getting things done!
Was This Post Helpful? 0
  • +
  • -

#8 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,204
  • Joined: 21-June 11

Re: One Liner challenge!

Posted 06 May 2012 - 10:04 PM

Here's an iterative version of my brainfuck interpreter. Sadly Python doesn't allow you to have while-statements on the same line as the method definition, so I had to put the method definition and the method body on separate lines (but since the whole body is still one line, I'm guessing that still counts). I used a map to store the variables I needed to reassign to get around the fact that I couldn't use assignment statements. I didn't use lambda-maps this time, so that the code looks sufficiently different from my recursive version:

import sys

def bf(c,d={},j={},s=[],v={"cp":0,"dp":0,"p":True}):
    while v["cp"]<len(c)or v["p"]:v.update(p=False,cp=0)if v["p"]and v["cp"]==len(c)else s.append(v["cp"])or v.update(cp=v["cp"]+1)if v["p"]and c[v["cp"]]=="["else j.update({v["cp"]:s[-1]})or j.update({s.pop():v["cp"]})or v.update(cp=v["cp"]+1)if v["p"]and c[v["cp"]]=="]"else v.update(cp=v["cp"]+1)if v["p"]else print(chr(d.get(v["dp"],0)),end='')or v.update(cp=v["cp"]+1)if c[v["cp"]]=="."else d.update({v["dp"]:ord(sys.stdin.read(1))})or v.update(cp=v["cp"]+1)if c[v["cp"]]==","else d.update({v["dp"]:D/>.get(v["dp"],0)+1})or v.update(cp=v["cp"]+1)if c[v["cp"]]=="+"else d.update({v["dp"]:D/>.get(v["dp"],0)-1})or v.update(cp=v["cp"]+1)if c[v["cp"]]=="-"else v.update(dp=v["dp"]+1)or v.update(cp=v["cp"]+1)if c[v["cp"]]==">"else v.update(dp=v["dp"]-1)or v.update(cp=v["cp"]+1)if c[v["cp"]]=="<"else(v.update(cp=v["cp"]+1)if d.get(v["dp"],0)else v.update(cp=j[v["cp"]]))if c[v["cp"]]=="["else(v.update(cp=j[v["cp"]])if d.get(v["dp"],0)else v.update(cp=v["cp"]+1))if c[v["cp"]]=="]"else v.update(cp=v["cp"]+1)


Was This Post Helpful? 0
  • +
  • -

#9 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: One Liner challenge!

Posted 06 May 2012 - 10:08 PM

I should have named this challenge "Watch sepp2k make us all look silly."

This post has been edited by atraub: 06 May 2012 - 10:08 PM

Was This Post Helpful? 1
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: One Liner challenge!

Posted 07 May 2012 - 07:14 AM

Here's a one liner:
def nextBoard(board): return [ [ (not board[y][x] and (sum( sum( {True:1, False:0}[y+yo<len(board) and x+xo<len(board[y+yo]) and board[y+yo][x+xo]] for xo in range(-1,2) if not (xo==0 and yo==0)) for yo in range(-1,2) ))==3) or (board[y][x] and (sum( sum( {True:1, False:0}[y+yo<len(board) and x+xo<len(board[y+yo]) and board[y+yo][x+xo]] for xo in range(-1,2) if not (xo==0 and yo==0)) for yo in range(-1,2) )) in [2,3]) for x in range(len(board[y])) ] for y in range(len(board)) ]



It's intentionally annoying, but I think that might be part of the requirement. What does it do? This beautiful mess does one iteration of Conway's Game of Life.

Here's some test code:
>>> def nextBoard(board): return [ [ (not board[y][x] and (sum( sum( {True:1, False:0}[y+yo<len(board) and x+xo<len(board[y+yo]) and board[y+yo][x+xo]] for xo in range(-1,2) if not (xo==0 and yo==0)) for yo in range(-1,2) ))==3) or (board[y][x] and (sum( sum( {True:1, False:0}[y+yo<len(board) and x+xo<len(board[y+yo]) and board[y+yo][x+xo]] for xo in range(-1,2) if not (xo==0 and yo==0)) for yo in range(-1,2) )) in [2,3]) for x in range(len(board[y])) ] for y in range(len(board)) ]
... 
>>> 
>>> def printBoard(B)/>: 
...     print('\n'.join(''.join({True:'*', False:'.'}[x] for x in row) for row in B)/>)
...     return b
... 
>>> 
>>> b = printBoard([ [ (y==2 and x in [1,2,3]) for x in range(5) ] for y in range(5) ])
.....
.....
.***.
.....
.....
>>> b = printBoard(newBoard(B)/>)
.....
..*..
..*..
..*..
.....
>>> b = printBoard(newBoard(B)/>)
.....
.....
.***.
.....
.....
>>> b = printBoard(newBoard(B)/>)
.....
..*..
..*..
..*..
.....
>>> 


Was This Post Helpful? 3
  • +
  • -

#11 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: One Liner challenge!

Posted 07 May 2012 - 08:59 AM

I think this might be too far, but this is my official response
Spoiler

Was This Post Helpful? 0
  • +
  • -

#12 yonyon  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 1
  • Joined: 28-May 12

Re: One Liner challenge!

Posted 28 May 2012 - 08:15 AM

Hi! Someone alerted me to this, so I had to sign up.

Here's my mostly-iterative one line brainfuck interpreter I wrote a few months ago. It gets around the import line by using inline imports (__import__) and around the while issue using itertools.repeat(). Since it uses no recursion, stack space is not a problem =).

(lambda t:(lambda a:(lambda b:(lambda l,e,s:((lambda(Y,o,N,A,t),a,n:e('v',(Y,o,N,A,t))or[e('v',(lambda(Y,o,N,A,t):(/>{'>':(lambda(Y,o,N,A,t),a,n:(Y,o,N+1,A+1,t)),'<':(lambda(Y,o,N,A,t),a,n:(Y,o,N-1,A+1,t)),'+':(lambda(Y,o,N,A,t),a,n:((Y[:N]+[Y[N]+1]+Y[N+1:],o,N,A+1,t)if N>=0 else(Y,o[:-N-1]+[o[-N-1]+1]+o[-N:],N,A+1,t))),'-':(lambda(Y,o,N,A,t),a,n:((Y[:N]+[Y[N]-1]+Y[N+1:],o,N,A+1,t)if N>=0 else(Y,o[:-N-1]+[o[-N-1]-1]+o[-N:],N,A+1,t))),'.':(lambda(Y,o,N,A,t),a,n:__import__('sys').stdout.write(chr(Y[N] if N>=0 else o[-N-1]))or(Y,o,N,A+1,t)),',':(lambda(Y,o,N,A,t),a,n:(Y[:N]+[ord(t[0])if len(t)else -1]+Y[N+1:]if A>=0 else Y,o[:-N-1]+[ord(t[0])if len(t)else -1]+o[-N:]if A<0 else o,N,A+1,t[1:])),'[':(lambda(Y,o,N,A,t),a,n:(Y,o,N,n[A]+1 if(Y[N]==0 if N>=0 else o[-N-1]==0)else A+1,t)),']':(lambda(Y,o,N,A,t),a,n:(Y,o,N,n[A]+1 if(Y[N]>=1 if N>=0 else o[-N-1]>=1)else A+1,t))}[a[A]]((Y+[0]*(9+len(Y)) if A>=len(Y)-5 else Y,o+[0]*(9+len(o)) if -A>=len(o)-5 else o,N,A,t),a,n)if A<len(a)else False))(l('v')))for i in s.takewhile(lambda x:l('v')!=False,s.count())])(([],[],0,0,t),a,dict(e('g',[])or e('l',[])or[e('l',l('l')+[i])if a[i]=='['else(e('g',l('g')+[(l('l')[-1],i),(i,l('l')[-1])])or e('l',l('l')[:-1]))for i in range(len(a))if a[i] in'[]'][:0]or l('g'))))[:0])(b.__getattribute__,b.__setattr__,__import__('itertools')))(lambda:a)or None)(filter("<.+[,->]".count,open(__import__('sys').argv[1]).read())))(raw_input()) 


yn
Was This Post Helpful? 3
  • +
  • -

#13 RCR  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 33
  • Joined: 04-July 12

Re: One Liner challenge!

Posted 06 July 2012 - 02:39 PM

import unicodedata def uglyCode(self, stack) { lambda t:stack[1,2,3] stack.append(4) stack.pop() stack.pop() stack.pop()  lambda 4:stack.append(9) stack.pop() stack.pop() stack.pop() stack.items() stack[1,2,3] stack.append(4)  lambda w: unicodedata.bidirectional(u'\u0660') unicodedata.bidirectional(u'\u0888') lambda e:unicodedata.bidirectional(u'\u890')lambda l:unicodedata.bidirectional(u'\u382') lambda u:stringprep.in_table_a1(code) stringprep.in_table_a1(\u899) stringprep.in_table_a1(\u893) stringprep.in_table_a1(\u723) stringprep.in_table_a1(\u673) stringprep.in_table_a1(\u787) stringprep.in_table_a1(\u783) stringprep.in_table_a1(\u983) 



Was This Post Helpful? 0
  • +
  • -

#14 BloodyInari  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 106
  • Joined: 16-November 09

Re: One Liner challenge!

Posted 06 July 2012 - 10:39 PM

View Postsepp2k, on 06 May 2012 - 07:01 PM, said:

Okay, here you go, my 1-line (plus import statement) Brainfuck interpreter:

import sys

def bf(c,cp=0,d={},dp=0,j=None,i=None):return(None if cp==len(c)else{".":lambda:bf(c,cp+1,d,dp,j,print(chr(d.get(dp,0)),end='')),",":lambda:bf(c,cp+1,d,dp,j,d.update({dp:ord(sys.stdin.read(1))})),"<":lambda:bf(c,cp+1,d,dp-1,j,i),">":lambda:bf(c,cp+1,d,dp+1,j,i),"+":lambda:bf(c,cp+1,d,dp,j,d.update({dp:d.get(dp,0)+1})),"-":lambda:bf(c,cp+1,d,dp,j,d.update({dp:d.get(dp,0)-1})),"[":lambda:bf(c,cp+1 if d.get(dp,0)else j[cp],d,dp,j,i),"]":lambda:bf(c,j[cp]if d.get(dp,0)else cp+1,d,dp,j,i)}.get(c[cp],lambda:bf(c,cp+1,d,dp,j,i))())if j!=None else i if cp==len(c)else({"[":lambda:d.append(cp),"]":lambda:i.update({cp:d[-1]})or i.update({d.pop():cp})}.get(c[cp],lambda:None)()or bf(c,cp+1,d,dp,j,i))if i!=None else bf(c,0,{},0,bf(c,0,[],0,None,{}))


holy fuck-sticks...
and I thought I wrote mangled, nasty code...
Was This Post Helpful? 0
  • +
  • -

#15 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,204
  • Joined: 21-June 11

Re: One Liner challenge!

Posted 07 July 2012 - 05:16 AM

View PostBloodyInari, on 07 July 2012 - 07:39 AM, said:

and I thought I wrote mangled, nasty code...


To be fair though, the code I normally write looks at most 80% as unreadable as that one ;-)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2