code-golf

Quickest Method to Reverse in String in C#.net

Thank you for your entries. Here is my response. I'm currently writing a quick solution for Euler Problem #4 where one must find the largest palindromic number from the product of two 3-digit numbers. To identify if a number is palindromic, you would obviously compare a reverse of the number with the original. Since C# doesn't have a ...

Bash text parsing golf

Hi, I am writing a shell script to, among other things, determine the last time a branch was made in a subversion repository. The following code works: DEV='http://some/svn/server/' BRANCH='some/repos/branches/' LAST_UPDATE=`svn list http://$DEV$BRANCH | sort -r` LAST_UPDATE=${LAST_UPDATE:0:10} But ...

Finding closest match in collection of numbers

So I got asked today what was the best way to find the closes match within a collection. For example, you've got an array like this: 1, 3, 8, 10, 13, ... What number is closest to 4? Collection is numerical, unordered and can be anything. Same with the number to match. Lets see what we can come up with, from the various languages o...

Code golf: combining multiple sorted lists into a single sorted list

Implement an algorithm to merge an arbitrary number of sorted lists into one sorted list. The aim is to create the smallest working programme, in whatever language you like. For example: input: ((1, 4, 7), (2, 5, 8), (3, 6, 9)) output: (1, 2, 3, 4, 5, 6, 7, 8, 9) input: ((1, 10), (), (2, 5, 6, 7)) output: (1, 2, 5, 6, 7, 10) Note:...

The while language

For my theory of computing languages class, we got a homework assignment to implement a piece of code in a language that only has while statements for flow control (no if statements). This is mainly to prove that you can write a Turing-complete language with only a while loop. For those of you who can understand language grammars, here...

Python Golf: what's the most concise way of turning this list of lists into a dictionary:

I have a list of lists that looks like this: [['Tom', 'Dick'], ['Harry', 'John', 'Mike'], ['Bob']] and I want to turn it into a dictionary where each key is a name and each value is a number corresponding to the position of its sublist in the list: {'Tom': 0, 'Dick': 0, 'Harry': 1, 'John': 1, 'Mike': 1, 'Bob': 2} I tried various li...

Parsing and formatting search results

Search: Scripting+Language Web+Pages Applications Results: ...scripting language originally...producing dynamic web pages. It has...graphical applications....purpose scripting language that is...d creating web pages as output... Suppose I want a value that represents the amount of characters to allow as padding on eit...

What's the most efficient way to find the length of the longest item in a list?

Given a list of assorted length words, what is the best way to find the maximum length of any words? For example, the following should return 6 findMaxLen("a,set,of,random,words") Of course, it's fairly trivial to do this... <cffunction name="findMaxLen" returntype="Numeric"> <cfset var CurMax = 0 /> <cfset var CurItem = 0...

Code Golf: Encode / Decode ascii binary

Decode the following string and encode it back. Any programming language is welcome, the point is to create a solution with the minimum amount of characters. Formatting and new lines does not count toward the char count. Here's the message: 1010100 1101000 1100101 1110010 1100101 1100001 1110010 1100101 110001 110000 1110100...

CodeGolf: Work out the prime factors of a given number

Inspired by http://codegolf.com/prime-factors, try to solve this problem using the least number of keystrokes. Sadly, you can only compete with Perl/PHP/Python/Ruby and I would love to see this problem solved in other not so traditional languages. ...

Golf - Expand Templates In Text File

Golf - implement a simple templating scheme. Expansions are: %KEY% -> VALUE %% -> % Command line arguments: ARG1: dictionary file, formatted in the key=value style as in example ARG2: template file Here my not quite golf attempt(python): 261 chars. import sys dd = dict([ll.split("=",2) for ll in open( sys.argv[1],'r') if l...

Code golf - hex to (raw) binary conversion

In response to this question asking about hex to (raw) binary conversion, a comment suggested that it could be solved in "5-10 lines of C, or any other language." I'm sure that for (some) scripting languages that could be achieved, and would like to see how. Can we prove that comment true, for C, too? NB: this doesn't mean hex to ASCI...

Simple PHP form: Attachment to email (code golf)

Imagine a user that would like to put a form on their website that would allow a website visitor to upload a file and a simple message, which will immediately be emailed (ie, the file is not stored on the server, or if it is then only temporarily) as a file attachment with the note in the message body. What is the simplest way to accomp...

Code Golf: Automata

I made the ultimate laugh generator using these rules. Can you implement it in your favorite language in a clever manner? Rules: On every iteration, the following transformations occur. H -> AH A -> HA AA -> HA HH -> AH AAH -> HA HAA -> AH n = 0 | H n = 1 | AH n = 2 | HAAH n = 3 | AHAH n = 4 | HAAHHAAH n = 5 | AHAHHA n...

What's the shortest Perl program you can write for the number guessing game?

I'm looking for the shortest or most optimized solution for a simple game that generates a random number from 1 to 10 and then asks a user to enter their guess. If the user guesses the number then it's a win, if they don't guess then the program should say that it was either more or less than that. Here's my solution to this: #c:\Perl...

Shortest-code for prime-calculation

The newspaper for the Computer Science-line at my school (called readme, is's norwegian, page 19) had a fun competition: Write the shortest possible Java-code for this problem: Take in an integer (as a string in the first entry of a string array, since the Java main method only takes a string array) as an argument, and write out fir...

Injecting a javascript file with the fewest possible characters?

I'm working on a bookmarklet, and thought I'd throw down a challenge: how to inject an external javascript file from a link in as few characters as possible. Here's the shortest I was able to come up with: javascript:(function(d){d.body.appendChild(d.createElement('script')).src='URL'})(document) That's 88 characters without the URL....

Code golf in Ruby: What's your favourite trick?

Anything that saves a few characters and produces horrible, unreadable code is fair game. My favourite is cheating spacing with the ternary operator. If you're testing a question-mark-method (like .nil?), the only place you need a space is after the second question mark: x.odd?? "odd":"even" ...

python dict.add_by_value(dict_2) ?

The problem: >>> a = dict(a=1,b=2 ) >>> b = dict( b=3,c=2) >>> c = ??? c = {'a': 1, 'b': 5, 'c': 2} So, the idea is two add to dictionaries by int/float values in the shortest form. Here's one solution that I've devised, but I don't like it, cause it's long: c = dict([(i,a.get(i,0) + b.get(i,0)) for i in set(a.keys()+b.keys()...

Twitter image encoding challenge

If a picture's worth 1000 words, how much of a picture can you fit in 140 characters? Note: That's it folks! Bounty deadline is here, and after some tough deliberation, I have decided that Boojum's entry just barely edged out Sam Hocevar's. I will post more detailed notes once I've had a chance to write them up. Of course, everyone shou...