13
1
You are sick of all of the codegolf challenges. Hence you decide to write a program that will automatically golf some Python code for you. There are 3 test cases:
print quickSort([0,7,3,-1,8,10,57,2])
def quickSort(arr):
less = []
pivotList = []
more = []
if len(arr) <= 1:
return arr
else:
pivot = arr[0]
for i in arr:
if i < pivot:
less.append(i)
elif i > pivot:
more.append(i)
else:
pivotList.append(i)
less = quickSort(less)
more = quickSort(more)
return less + pivotList + more
for i in xrange(1, 101):
if i % 15 == 0:
print "FizzBuzz"
elif i % 3 == 0:
print "Fizz"
elif i % 5 == 0:
print "Buzz"
else:
print i
from sys import argv
def randomGenerator(seed=1):
max_int32 = (1 << 31) - 1
seed = seed & max_int32
while True:
seed = (seed * 214013 + 2531011) & max_int32
yield seed >> 16
def deal(seed):
nc = 52
cards = range(nc - 1, -1, -1)
rnd = randomGenerator(seed)
for i, r in zip(range(nc), rnd):
j = (nc - 1) - r % (nc - i)
cards[i], cards[j] = cards[j], cards[i]
return cards
def show(cards):
l = ["A23456789TJQK"[c / 4] + "CDHS"[c % 4] for c in cards]
for i in range(0, len(cards), 8):
print " ", " ".join(l[i : i+8])
if __name__ == '__main__':
seed = int(argv[1]) if len(argv) == 2 else 11982
print "Hand", seed
deck = deal(seed)
show(deck)
Rules:
Your program must not target the code I posted specifically, and should work with any Python 2 code. I reserve the right to change the source code being codegolfed. You may assume that there are no multi-line strings (so you don't have build a full-blown parser), and that locals() is not called.
The output of your program should run in an identical manner as the original source code. (Namely, it must produce the same output. Variable names and language constructs can be changed, as long as the output remains the same)
You may use STDIO or a File to do your input/output of the source code.
Your score will be the sum of the bytes of your program's output.
(The code listed above has been taken from http://rosettacode.org/ under the GNU Free Documentation License 1.2)
Do we need to handle invalid code? IE if we make invalid code valid, is the entry valid? – Justin – 2015-01-07T16:32:25.560
3
This seems very similar to http://codegolf.stackexchange.com/questions/3652/write-a-code-golfer/26075
– KSab – 2015-01-07T16:34:33.060@Quincunx No, it should not deal with invalid code. However, it should be able to handle any python code (including list comprehension and comments, even though it isn't in the programs above) – Nathan Merrill – 2015-01-07T16:44:51.117
@KSab Yes, it is similar, but fundamentally different, as I have a scoring system. – Nathan Merrill – 2015-01-07T16:45:28.583
@KSab I think that the two questions are different in both scoring and in task to be accomplished. This question: Shortest output wins. Old question: Most popular code that does five tasks plus "etc" wins. Who the heck knows what "etc" means. – Rainbolt – 2015-01-07T16:46:10.850
3
Here's a bonus test case for people to try, to be devious.
– Sp3000 – 2015-01-07T16:51:59.5374What's your model for determining whether the output "[runs] in an identical manner as the original source code"? E.g. for the second example, I believe that removing
if __name__ == '__main__':
would affect the behaviour in some contexts but not others. For another example, if the ungolfed input assumes that it reads an int from stdin and throws one type of exception if given something else, could the golfed input throw a different type of exception if given a non-integer? – Peter Taylor – 2015-01-07T17:00:36.930Removing
if __name__ == '__main__':
will change the behaviour. Changing exception type will change the behaviour. In the same context, the program should produce identical output (including STDERR). – Nathan Merrill – 2015-01-07T17:16:49.717Python 2, correct? – TheNumberOne – 2015-01-07T19:42:03.130
1I'm a python noob, but for me it complains that the
quickSort
call is before the definition. Works fine if I move thequickSort
call to the end – Digital Trauma – 2015-01-07T19:43:54.320@TheBestOne *"[...] and should work with any Python 2 code."* – Rainbolt – 2015-01-07T19:43:55.617
This challenge does not seem good. I'll bet any answers that are posted (that attempt any sort of reduction) will fail on some code other than the single test case. – feersum – 2015-01-07T20:17:33.177
@feersum I updated the problem description. There are no multi-line comments, meaning that most reductions will now work. – Nathan Merrill – 2015-01-07T20:40:28.097
Even single-line strings are fairly tricky. – feersum – 2015-01-07T20:44:39.763
2What about a program like this:
random_long_variable=0;print locals()
? – Justin – 2015-01-07T20:49:33.367I didn't know about locals(). I'll exclude that as well, thanks. – Nathan Merrill – 2015-01-08T01:09:14.083
2Multiline strings aren't the only way to make empty lines functional – Sp3000 – 2015-01-08T01:15:01.517