code-golf

Code golf - hex to (raw) binary conversion

拟墨画扇 提交于 2019-11-28 06:33:42
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 ASCII binary - specifically the output should be a raw octet stream corresponding to the input ASCII hex. Also, the input parser should skip/ignore white space. edit (by Brian Campbell) May I propose the following rules, for consistency? Feel free to edit or delete

Fixing a broken loop by changing exactly one character

送分小仙女□ 提交于 2019-11-28 04:32:19
I found a site with some complicated C puzzles. Right now I'm dealing with this: The following is a piece of C code, whose intention was to print a minus sign 20 times. But you can notice that, it doesn't work. #include <stdio.h> int main() { int i; int n = 20; for( i = 0; i < n; i-- ) printf("-"); return 0; } Well fixing the above code is straight-forward. To make the problem interesting, you have to fix the above code, by changing exactly one character. There are three known solutions. See if you can get all those three. I cannot figure out how to solve. I know that it can be fixed by

Code-golf: generate pascal's triangle

杀马特。学长 韩版系。学妹 提交于 2019-11-28 04:30:10
Generate a list of lists (or print, I don't mind) a Pascal's Triangle of size N with the least lines of code possible! Here goes my attempt (118 characters in python 2.6 using a trick ): c,z,k=locals,[0],'_[1]' p=lambda n:[len(c()[k])and map(sum,zip(z+c()[k][-1],c()[k][-1]+z))or[1]for _ in range(n)] Explanation: the first element of the list comprehension (when the length is 0) is [1] the next elements are obtained the following way: take the previous list and make two lists, one padded with a 0 at the beginning and the other at the end. e.g. for the 2nd step, we take [1] and make [0,1] and [1

Decimal alignment formatting in Python

末鹿安然 提交于 2019-11-28 03:32:24
问题 This should be easy. Here's my array (rather, a method of generating representative test arrays): >>> ri = numpy.random.randint >>> ri2 = lambda x: ''.join(ri(0,9,x).astype('S')) >>> a = array([float(ri2(x)+ '.' + ri2(y)) for x,y in ri(1,10,(10,2))]) >>> a array([ 7.99914000e+01, 2.08000000e+01, 3.94000000e+02, 4.66100000e+03, 5.00000000e+00, 1.72575100e+03, 3.91500000e+02, 1.90610000e+04, 1.16247000e+04, 3.53920000e+02]) I want a list of strings where '\n'.join(list_o_strings) would print:

Code Golf: Countdown Number Game

倾然丶 夕夏残阳落幕 提交于 2019-11-28 03:15:56
Challenge Here is the task, inspired by the well-known British TV game show Countdown . The challenge should be pretty clear even without any knowledge of the game, but feel free to ask for clarifications. And if you fancy seeing a clip of this game in action, check out this YouTube clip . It features the wonderful late Richard Whitely in 1997. You are given 6 numbers, chosen at random from the set {1, 2, 3, 4, 5, 6, 8, 9, 10, 25, 50, 75, 100}, and a random target number between 100 and 999. The aim is to use the six given numbers and the four common arithmetic operations (addition,

Code Golf: Lasers

放肆的年华 提交于 2019-11-28 02:33:34
The challenge The shortest code by character count to input a 2D representation of a board, and output 'true' or 'false' according to the input . The board is made out of 4 types of tiles: # - A solid wall x - The target the laser has to hit / or \ - Mirrors pointing to a direction (depends on laser direction) v, ^, > or < - The laser pointing to a direction (down, up, right and left respectively) There is only one laser and only one target . Walls must form a solid rectangle of any size, where the laser and target are placed inside. Walls inside the 'room' are possible. Laser ray shots and

Code Golf: Sierpinski's Triangle

南楼画角 提交于 2019-11-27 20:04:05
问题 The challenge The shortest code, by character count to output an ASCII representation of Sierpinski's Triangle of N iterations made from the following ASCII triangle: /\ /__\ Input is a single positive number. Test cases Input: 2 Output: /\ /__\ /\ /\ /__\/__\ Input: 3 Output: /\ /__\ /\ /\ /__\/__\ /\ /\ /__\ /__\ /\ /\ /\ /\ /__\/__\/__\/__\ Input: 5 Output: /\ /__\ /\ /\ /__\/__\ /\ /\ /__\ /__\ /\ /\ /\ /\ /__\/__\/__\/__\ /\ /\ /__\ /__\ /\ /\ /\ /\ /__\/__\ /__\/__\ /\ /\ /\ /\ /__\ /__

Code Golf: Numeric equivalent of an Excel column name

孤人 提交于 2019-11-27 17:14:16
The challenge The shortest code by character count that will output the numeric equivalent of an Excel column string. For example, the A column is 1, B is 2, so on and so forth. Once you hit Z , the next column becomes AA , then AB and so on. Test cases: A: 1 B: 2 AD: 30 ABC: 731 WTF: 16074 ROFL: 326676 Code count includes input/output (i.e full program). David Perl, 36 34 33 31 30 17 15 11 characters $_=()=A..$_ Usage: $ echo -n WTF | perl -ple '$_=()=A..$_' 16074 Reduced to 17 by using echo -n to avoid a chop call. Reduced to 15 by using say instead of print. Reduced to 11 by using -p

Finding closest match in collection of numbers [closed]

谁说我不能喝 提交于 2019-11-27 11:42:07
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 of choice. Jimmy 11 bytes in J: C=:0{]/:|@- Examples: >> a =: 1 3 8 10 13 >> 4 C a 3 >> 11 C a 10 >> 12 C a 13 my breakdown for the layman: 0{ First element of ] the right argument /: sorted by | absolute value @ of - subtraction Shorter Python: 41 chars f=lambda a,l

Code Golf: Gray Code

余生颓废 提交于 2019-11-27 11:39:03
问题 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 program by character count that outputs the n-bit Gray Code. n will be an arbitrary number smaller than 1000 100000 (due to user suggestions) that is taken from standard input. The gray code will be printed in standard output, like in the example. Note : I don't expect the program to print the