code-golf

Code Golf: Tic Tac Toe

混江龙づ霸主 提交于 2019-11-27 11:06:57
Post your shortest code, by character count, to check if a player has won, and if so, which. Assume you have an integer array in a variable b (board), which holds the Tic Tac Toe board, and the moves of the players where: 0 = nothing set 1 = player 1 (X) 2 = player 2 (O) So, given the array b = [ 1, 2, 1, 0, 1, 2, 1, 0, 2 ] would represent the board X|O|X -+-+- |X|O -+-+- X| |O For that situation, your code should output 1 to indicate player 1 has won. If no-one has won you can output 0 or false . My own (Ruby) solution will be up soon. Edit : Sorry, forgot to mark it as community wiki. You

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

落爺英雄遲暮 提交于 2019-11-27 11:03:54
问题 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. 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

Code Golf: Conway's Game of Life

泄露秘密 提交于 2019-11-27 10:08:28
The Challenge: Write the shortest program that implements John H. Conway's Game of Life cellular automaton. [ link ] EDIT: After about a week of competition, I have selected a victor: pdehaan , for managing to beat the Matlab solution by one character with perl. For those who haven't heard of Game of Life, you take a grid (ideally infinite) of square cells. Cells can be alive (filled) or dead (empty). We determine which cells are alive in the next step of time by applying the following rules: Any live cell with fewer than two live neighbours dies, as if caused by under-population. Any live

Code Golf: Musical Notes

前提是你 提交于 2019-11-27 09:50:52
问题 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 will output musical notation based on user input. Input will be composed of a series of letters and numbers - letters will represent the name of the note and the number will represent the length of the note. A note is made of 4 vertical columns. The note's head

Code Golf: Collatz Conjecture

 ̄綄美尐妖づ 提交于 2019-11-27 09:42:58
问题 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. Inspired by http://xkcd.com/710/ here is a code golf for it. The Challenge Given a positive integer greater than 0, print out the hailstone sequence for that number. The Hailstone Sequence See Wikipedia for more detail.. If the number is even, divide it by two. If the number is odd, triple it and add one. Repeat this

Code Golf: The wave

陌路散爱 提交于 2019-11-27 09:36:42
问题 The challenge The shortest code by character count to generate a wave from the input string. A wave is generated by elevating (line-1) a higher character, and degrading (line+1) a lower character. Equal characters are kept on the same line (no elevating or degrading done). Input is made of lower case characters and numbers only, letters are considered higher than numbers. Test cases: Input: 1234567890qwertyuiopasdfghjklzxcvbnm Output: z l x v n k c b m j h g y p s f t u o a d w r i 9 q e 8 0

python dict.add_by_value(dict_2)?

人走茶凉 提交于 2019-11-27 09:29:05
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())]) I think there must be a shorter/concise solution (maybe something to do with reduce and operator module? itertools?)... Any ideas? Update: I'm really hoping to find something more elegant like "reduce(operator.add, key = itemgetter(0), a+b)". (Obviously that isn't real

What's the shortest code to cause a stack overflow? [closed]

十年热恋 提交于 2019-11-27 05:48:54
To commemorate the public launch of Stack Overflow, what's the shortest code to cause a stack overflow? Any language welcome. ETA: Just to be clear on this question, seeing as I'm an occasional Scheme user: tail-call "recursion" is really iteration, and any solution which can be converted to an iterative solution relatively trivially by a decent compiler won't be counted. :-P ETA2: I've now selected a “best answer”; see this post for rationale. Thanks to everyone who contributed! :-) Patrick All these answers and no Befunge? I'd wager a fair amount it's shortest solution of them all: 1 Not

Build an ASCII chart of the most commonly used words in a given text [closed]

我只是一个虾纸丫 提交于 2019-11-27 05:46:23
The challenge: Build an ASCII chart of the most commonly used words in a given text. The rules: Only accept a-z and A-Z (alphabetic characters) as part of a word. Ignore casing ( She == she for our purpose). Ignore the following words (quite arbitary, I know): the, and, of, to, a, i, it, in, or, is Clarification: considering don't : this would be taken as 2 different 'words' in the ranges a-z and A-Z : ( don and t ). Optionally (it's too late to be formally changing the specifications now) you may choose to drop all single-letter 'words' (this could potentially make for a shortening of the

Fixing a broken loop by changing exactly one character

允我心安 提交于 2019-11-27 05:22:15
问题 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