code-golf

Code Golf: Four is magic

佐手、 提交于 2019-11-29 19:13:51
The puzzle A little puzzle I heard while I was in high school went something like this... The questioner would ask me to give him a number; On hearing the number, the questioner would do some sort of transformation on it repeatedly (for example, he might say ten is three ) until eventually arriving at the number 4 (at which point he would finish with four is magic ). Any number seems to be transformable into four eventually, no matter what. The goal was to try to figure out the transformation function and then be able to reliably proctor this puzzle yourself. The solution The transformation

Code Golf: Regex parser

梦想与她 提交于 2019-11-29 19:11:10
The goal Today's Code Golf challenge is to create a regex parser in as few characters as possible. The syntax No, I'm not asking you to match Perl-style regular expressions. There's already a very reliable interpreter for those, after all! :-) Here's all you need to know about regex syntax for this challenge: A term is defined as a single literal character, or a regular expression within grouping parentheses () . The * (asterisk) character represents a Kleene star operation on the previous TERM. This means zero or more of the previous term, concatenated together. The + (plus) character

The Skyline Problem‍​​

可紊 提交于 2019-11-29 18:46:01
I just came across this little problem on UVA's Online Judge and thought, that it may be a good candidate for a little code-golf. The problem: You are to design a program to assist an architect in drawing the skyline of a city given the locations of the buildings in the city. To make the problem tractable, all buildings are rectangular in shape and they share a common bottom (the city they are built in is very flat). The city is also viewed as two-dimensional. A building is specified by an ordered triple (Li, Hi, Ri) where Li and Ri are left and right coordinates, respectively, of building i

Decimal alignment formatting in Python

我怕爱的太早我们不能终老 提交于 2019-11-29 10:05:26
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: 79.9914 20.8 394.0 4661.0 5.0 1725.751 391.5 19061.0 11624.7 353.92 I want to space pad to the left and

Code Golf: Build Me an Arc

时光毁灭记忆、已成空白 提交于 2019-11-29 05:33:11
Challenge The shortest program by character count that accepts standard input of the form X-Y R , with the following guarantees: R is a non-negative decimal number less than or equal to 8 X and Y are non-negative angles given in decimal as multiples of 45° ( 0 , 45 , 90 , 135 , etc.) X is less than Y Y is not 360 if X is 0 And produces on standard output an ASCII "arc" from the starting angle X to the ending angle Y of radius R , where: The vertex of the arc is represented by o Angles of 0 and 180 are represented by - Angles of 45 and 225 are represented by / Angles of 90 and 270 are

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

醉酒当歌 提交于 2019-11-29 02:56:45
问题 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 : solutions which concatenate the input lists then use a language-provided sort function are not in-keeping with the spirit of golf, and will not be accepted: sorted

Code-Golf: Friendly Number Abbreviator

冷暖自知 提交于 2019-11-28 19:58:41
问题 Based on this question: Is there a way to round numbers into a friendly format? THE CHALLENGE - UPDATED! (removed hundreds abbreviation from spec) The shortest code by character count that will abbreviate an integer (no decimals). Code should include the full program. Relevant range is from 0 - 9,223,372,036,854,775,807 (the upper limit for signed 64 bit integer). The number of decimal places for abbreviation will be positive. You will not need to calculate the following: 920535 abbreviated

Code Golf: Gray Code

元气小坏坏 提交于 2019-11-28 18:45:21
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 gray code in a reasonable time ( n=100000 is overkill); I do expect it to start printing though. Example Input : 4 Expected Output : 0000 0001 0011 0010 0110 0111 0101 0100 1100 1101 1111 1110 1010 1011 1001 1000 gnibbler Python - 53 chars n=1<<input() for x in range(n):print

Code Golf: All +-*/ Combinations for 3 integers

安稳与你 提交于 2019-11-28 17:51:09
Write a program that takes 3 integers separated by spaces and perform every single combination of addition, subtraction, multiplication and division operations possible and display the result with the operation combination used. Example: $./solution 1 2 3 Results in the following output 1+2+3 = 6 1-2-3 = -4 1*2*3 = 6 1/2/3 = 0 (integer answers only, round up at .5) 1*2-3 = -1 3*1+2 = 5 etc... Order of operation rules apply, assume there will be no parenthesis used i.e. (3-1)*2 = 4 is not a combination, although you could implement this for "extra credit" For results where a divide by 0 occurs

Fibonacci Code Golf

一笑奈何 提交于 2019-11-28 17:33:23
问题 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. Generate the Fibonacci sequence in the fewest amount of characters possible. Any language is OK, except for one that you define with one operator, f , which prints the Fibonacci numbers. Starting point: 25 14 characters in Haskell : f=0:1:zipWith(+)f(tail f) f=0:scanl(+)1f 回答1: RePeNt, 9 , 8 chars 1↓[2?+1] Or 10 chars