code-golf

Async Google Analytics [Javascript Golf]

我的梦境 提交于 2019-12-02 19:13:05
Unfortunately, this may not be a valid Code-Golf question as it is likely Javascript only; however, since this is likely to be the only useful-in-the-real-world code-golf contest I'm going to go ahead and post it. The Google Analytics Asyncronous Tracking snippet is used by many websites. The script goes a little something like this: <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXX-X']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document

Code Golf: Ulam Spiral

醉酒当歌 提交于 2019-12-02 19:09:44
The Challenge The shortest code by character count to output Ulam's spiral with a spiral size given by user input. Ulam's spiral is one method to map prime numbers. The spiral starts from the number 1 being in the center (1 is not a prime) and generating a spiral around it, marking all prime numbers as the character ' * '. A non prime will be printed as a space ' '. alt text http://liranuna.com/junk/ulam.gif Test cases Input: 2 Output: * * * * Input: 3 Output: * * * * * ** * * Input: 5 Output: * * * * * * * * * * * ** * * * * * * * * * Code count includes input/output (i.e full program).

Code Golf: Triforce

北城以北 提交于 2019-12-02 17:51:22
This is inspired by/taken from this thread: http://www.allegro.cc/forums/thread/603383 The Problem Assume the user gives you a numeric input ranging from 1 to 7. Input should be taken from the console, arguments are less desirable. When the input is 1, print the following: *********** ********* ******* ***** *** * Values greater than one should generate multiples of the pattern, ending with the one above, but stacked symmetrically. For example, 3 should print the following: *********** *********** *********** ********* ********* ********* ******* ******* ******* ***** ***** ***** *** *** *** *

Code Golf: Beehive

随声附和 提交于 2019-12-02 16:45:49
The challenge The shortest code by character count that will generate a beehive from user input. A beehive is defined a a grid of hexagons in a size inputted by the user as two positive numbers greater than zero (no need to validate input). The first number ( W ) represents the width of the beehive - or - how many hexagons are on each row. The second number ( H ) represents the height of the beehive - or - how many hexagons are on each column. A Single hexagon is made from three ASCII characters: _ , / and \ , and three lines: __ / \ \__/ Hexagons complete each other: the first column of the

Code Golf: Connecting the dots

老子叫甜甜 提交于 2019-12-02 16:07:29
You may remember these drawings from when you were a child, but now it's time to let the computer draw them (in full ascii splendour). Have fun! Description: The input are multiple lines (terminated by a newline) which describe a 'field'. There are 'numbers' scattered across this field (seperated by whitespace). All lines can be considered to be the same length (you can pad spaces to the end). the numbers always start at 1 they follow the ordering of the natural numbers: every 'next number' is incremented with 1 every number is surrounded by (at least) one whitespace on its left and right Task

Code Golf: Finite-state machine!

允我心安 提交于 2019-12-02 14:30:04
Finite state machine A deterministic finite state machine is a simple computation model, widely used as an introduction to automata theory in basic CS courses. It is a simple model, equivalent to regular expression, which determines of a certain input string is Accepted or Rejected . Leaving some formalities aside , A run of a finite state machine is composed of: alphabet , a set of characters. states , usually visualized as circles. One of the states must be the start state . Some of the states might be accepting , usually visualized as double circles. transitions , usually visualized as

Code Golf: Playing Cubes

限于喜欢 提交于 2019-12-02 14:21:30
The challenge The shortest code by character count, that will output playing bricks tower series according to user input. The input will be a series of numbers (positive, negative and zero) that represents the height of the current cube tower following their index. A height of 0 means no tower and is spaced. A cube tower is composed of stacked cubes. If the input number on the current index is positive, the cubes go up, if the input number is negative, the cubes go down. A single cube is drawn using the 4 following lines: __ /__ /| | | | |___|/ Cubes are 3D - this means they hide each other

Code golf: the Mandelbrot set

孤街醉人 提交于 2019-12-02 14:04:39
Usual rules for the code golf. Here is an implementation in python as an example from PIL import Image im = Image.new("RGB", (300,300)) for i in xrange(300): print "i = ",i for j in xrange(300): x0 = float( 4.0*float(i-150)/300.0 -1.0) y0 = float( 4.0*float(j-150)/300.0 +0.0) x=0.0 y=0.0 iteration = 0 max_iteration = 1000 while (x*x + y*y <= 4.0 and iteration < max_iteration): xtemp = x*x - y*y + x0 y = 2.0*x*y+y0 x = xtemp iteration += 1 if iteration == max_iteration: value = 255 else: value = iteration*10 % 255 print value im.putpixel( (i,j), (value, value, value)) im.save("image.png", "PNG"

Code Golf: Shortest code to find a weighted median?

五迷三道 提交于 2019-11-30 15:50:33
问题 My try at code golfing. The problem of finding the minimum value of ∑W_i*|X-X_i| reduces to finding the weighted median of a list of x[i] with weights w[i] (see below for definition). How will you do that with a shortest, simplest and most beautiful program? Here's how my code looked originally (explanation is in the answer to the question and short version is posted as one of the answers below). #define zero(x) ( abs(x) < 1e-10 ) /* because == doesn't work for floats */ float sum = 0; int i;

Code Golf: Duplicate Character Removal in String

寵の児 提交于 2019-11-30 12:30:34
问题 Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. The challenge: The shortest code, by character count , that detects and removes duplicate characters in a String. Removal includes ALL instances of the duplicated character (so if you find 3 n's, all three have to go), and original character order needs to be preserved. Example Input 1: nbHHkRvrXbvkn Example Output 1: