571
\$\begingroup\$

This "sandbox" is a place where Code Golf users can get feedback on prospective challenges they wish to post to main. This is useful because writing a clear and fully specified challenge on your first try can be difficult, and there is a much better chance of your challenge being well received if you post it in the sandbox first.

Sandbox FAQ

Posting

To post to the sandbox, scroll to the bottom of this page and click "Answer This Question". Click "OK" when it asks if you really want to add another answer.

Write your challenge just as you would when actually posting it, though you can optionally add a title at the top. You may also add some notes about specific things you would like to clarify before posting it. Other users will help you improve your challenge by rating and discussing it.

When you think your challenge is ready for the public, go ahead and post it, and replace the post here with a link to the challenge and delete the sandbox post.

Discussion

The purpose of the sandbox is to give and receive feedback on posts. If you want to, feel free to give feedback to any posts you see here. Important things to comment about can include:

  • Parts of the challenge you found unclear
  • Comments addressing specific points mentioned in the proposal
  • Problems that could make the challenge uninteresting or unfit for the site

You don't need any qualifications to review sandbox posts. The target audience of most of these challenges is code golfers like you, so anything you find unclear will probably be unclear to others.

If you think one of your posts requires more feedback, but it's been ignored, you can ask for feedback in The Nineteenth Byte. It's not only allowed, but highly recommended! Be patient and try not to nag people though, you might have to ask multiple times.

It is recommended to leave your posts in the sandbox for at least several days, and until it receives upvotes and any feedback has been addressed.

Other

Search the sandbox / Browse your pending proposals

The sandbox works best if you sort posts by active.

To add an inline tag to a proposal, use shortcut link syntax with a prefix: [tag:king-of-the-hill]. To search for posts with a certain tag, include the name in quotes: "king-of-the-hill".

\$\endgroup\$
0

4636 Answers 4636

1
4 5
6
7 8
155
5
\$\begingroup\$

Draw me a Brick Wall!

I'm drawing up a plan for my house extension - and I need a simple graphic for walls...

The Challenge

Your task is to create a program, which takes an input of the wall's dimensions and draws a brick wall, in the style of the one below.

[__][__][__][__]
_][__][__][__][_
[__][__][__][__]
_][__][__][__][_
[__][__][__][__]
_][__][__][__][_

Please notice that the rows alternate between beginning on a full brick ([__]) and a half brick (_]), to create a more realistic, stable wall.

The input will be two integers, separated by a single comma, such as 4,3 or 2,6. You can assume both integers are positive and larger than 0.

The first integer specifies the width (in bricks) and the second specifies the height (in rows of bricks).

Rules / Notes

  • This is , so the shortest code (in bytes) wins. However, don't feel like you have to beat everyone else to post your solution - I'd love to see your code!
  • Standard loopholes apply, no reading from external files.
  • You may optionally take the input with brackets/braces, for example (4,3) or [4,3] as long as you specify this in your answer.
  • You should take the input from STDIN and output on STDOUT - if your language does not have these, please use the nearest equivalent.

Test Cases

Input: 1,1

[__]

Input: 2,4

[__][__]
_][__][_
[__][__]
_][__][_

Input: 5,10

[__][__][__][__][__]
_][__][__][__][__][_
[__][__][__][__][__]
_][__][__][__][__][_
[__][__][__][__][__]
_][__][__][__][__][_
[__][__][__][__][__]
_][__][__][__][__][_
[__][__][__][__][__]
_][__][__][__][__][_

Example: Python 3, 65 bytes

This is somewhat golfed but still readable.

w,h=eval(input())
for i in range(h):print(('[__]','_][_')[i%2]*w)
\$\endgroup\$
7
  • \$\begingroup\$ very closely related or dupe \$\endgroup\$
    – Emigna
    Oct 28, 2016 at 13:11
  • 3
    \$\begingroup\$ this one is related too I think \$\endgroup\$
    – Dada
    Oct 28, 2016 at 13:12
  • \$\begingroup\$ Hi, welcome to PPCG! This seems like a great first challenge. There are a few related challenges regarding brick walls, like this one (inputs as different sized bricks) or this one (is it stable). But, I think this challenge is different enough. Instead of having different sized bricks as input, this simply asks for two inputs for an NxM wall, which is imho different enough from the first challenge. \$\endgroup\$ Oct 28, 2016 at 13:12
  • \$\begingroup\$ @Emigna I'd like to think they're different, as this one is a 'more static' string to print and doesn't require an algorithm to work out a stable wall first - however I understand that they are closely related. \$\endgroup\$
    – FlipTack
    Oct 28, 2016 at 13:13
  • \$\begingroup\$ @Dada Hmm, that one might be more related than the two related ones linked by me, and more towards a dupe (despite the different ASCII used for the bricks themselves). But we'll perhaps wait on some more feedback from other users. \$\endgroup\$ Oct 28, 2016 at 13:14
  • \$\begingroup\$ The one Dada linked is similar, but not quite a dupe. The brick structure is quite a bit more flexible in Luis' challenge, given that the input is total character width and height plus a horizontal offset, whereas here it's in number of bricks. \$\endgroup\$ Oct 28, 2016 at 13:21
  • \$\begingroup\$ I'd still call the previous brick printing one a dupe. The offsetting pattern is the same, and in most of the answers it would be easy to replace constants with inputs. \$\endgroup\$
    – xnor
    Oct 29, 2016 at 6:11
5
\$\begingroup\$

The smallest circles


Challenge

This is a variant of the smallest-circle problem, but instead of one circle, you get three. Given a list of coordinates, output three circles such that the following conditions are met:

  1. Each input coordinate must be located inside or on the perimeter of a circle.
  2. The sum of the radii of all three circles must be minimal.
  3. The coordinates and radii of all three circles must be non-negative integers.

You must place all three circles. You may place overlapping circles. A circle with a radius of zero that is directly on top of an input coordinate is considered to be covering that input coordinate.

Input

A list containing between 1 and 1000 pairs of integers, inclusive. Each pair of integers represents an xy-coordinate. Use whatever input format you want to use.

For example, the input...

1,1;1,2;2,2;3,3

... can be drawn like this:

enter image description here

Output

A list of three integer triples. Each triple contains an x coordinate, followed by a y coordinate, followed by a radius. The triples, and the integers within each triple, must be distinguishable from one another. Otherwise, the output format is not important.

Example:

1,1,1;2,2,1;3,3,2

Given this example output, circles would be drawn at (1,1), (2,2), and (3,3). The first two circles would have a radius of 1, and the third would have a radius of 2. The sum of the radii would be 4.

Test case explained

Given the input...

1,1;1,2;2,2;3,3

... you could output...

1,2,1;3,3,0;0,0,0

... or you could output...

1/2/1
3/3/0
0/0/0

The radii sums to 1, and since it is not possible to draw three circles whose radii sum to less than 1 that encompass or touch all four points, this is the correct answer.


(maybe too)

\$\endgroup\$
9
  • \$\begingroup\$ The text says that the list is colon-delimited, but the example is semicolon-delimited. It would help readability to mention that you can place three circles at some point before "The sum of the radii of all three circles is minimal". It's not clear whether the coordinates and radii of the circles must be integers, nor whether the radii must be non-zero. Finally, some test cases would be nice. \$\endgroup\$ Nov 7, 2014 at 18:50
  • \$\begingroup\$ If I didn't fully address any of the comments above please let me know. \$\endgroup\$
    – Rainbolt
    Nov 7, 2014 at 19:41
  • 1
    \$\begingroup\$ If the input can contain up to 1000 points it might help to have a large test case too. \$\endgroup\$ Jan 20, 2016 at 22:30
  • \$\begingroup\$ For large numbers of input points, how will you assess whether the output is of minimal total radius? Is a proof required or is it sufficient that no other answer/person can find a counterexample? You could also have a judge program / reference implementation to define the correct total radius. \$\endgroup\$ Jan 20, 2016 at 22:31
  • 1
    \$\begingroup\$ @trichoplax Hopefully I'll have a reference implementation before I post. If I decide that I am too lazy for that, then I will assume answers are correct until someone finds a counterexample. I will come up with a larger test case. \$\endgroup\$
    – Rainbolt
    Jan 20, 2016 at 22:37
  • \$\begingroup\$ Does the rigid I/O format really add anything interesting to the challenge? meta.codegolf.stackexchange.com/a/8077/8478 \$\endgroup\$ Jan 21, 2016 at 10:55
  • \$\begingroup\$ @MartinBüttner I completely relaxed the input format, and I somewhat relaxed the output format. Thanks for the feedback. \$\endgroup\$
    – Rainbolt
    Jan 21, 2016 at 14:12
  • \$\begingroup\$ @Rainbolt If I read the output section right, you're allowing any distinguishable delimiters for the output now? Then it might be clearer if one of the two example formats didn't use commas as the inner delimiter (maybe use spaces for the second example or so). \$\endgroup\$ Jan 21, 2016 at 14:15
  • 1
    \$\begingroup\$ The rewrite states two conditions which must be met, but there's a third one hidden down in the output section: that the coordinates and radii must be integers. That should be up in the first section. \$\endgroup\$ Jan 19, 2017 at 21:13
5
\$\begingroup\$

Make A Rotating Emoji Globe

Your task is to make a globe out of the following characters, with the line being cleared ever 1/3 of a second:

🌍🌎🌏

You must clear the line your emoji globe on it and print the next one every approximate 1/3 of a second.
Output may be to the terminal or elsewhere.
The program must also continue until interrupted on purpose.
Also, your code may not contain the emoji globes themselves, but it may contain Unicode escapes.


This is , so standard loopholes & rules apply.
May the best coder win...

\$\endgroup\$
14
  • 1
    \$\begingroup\$ Simple, fairly identical to this in that you simply display a cycle of things, Most answers from that will fairly easily work copied over. \$\endgroup\$
    – ATaco
    Feb 7, 2017 at 2:47
  • \$\begingroup\$ @ATaco How could I make it more challenging? \$\endgroup\$
    – ckjbgames
    Feb 7, 2017 at 2:50
  • \$\begingroup\$ @ATaco I think it's sufficiently distinct--it requires clearing the line (an option in the other challenge), display unicode characters, and only displaying one character at a time. \$\endgroup\$ Feb 7, 2017 at 3:24
  • \$\begingroup\$ @ConorO'Brien I still feel I could distinguish my challenge a bit more, though... \$\endgroup\$
    – ckjbgames
    Feb 7, 2017 at 17:48
  • \$\begingroup\$ Define clear the line? Does it have to clear the line, or does the whole terminal suffice? \$\endgroup\$
    – Riker
    Feb 7, 2017 at 18:13
  • \$\begingroup\$ @EasterlyIrk Clearing the screen can be done in any way, as long as the globe appears to rotate onscreen. \$\endgroup\$
    – ckjbgames
    Feb 7, 2017 at 21:40
  • \$\begingroup\$ It's even more similar to codegolf.stackexchange.com/questions/101289/loading-forever. Printing a single character that cycles \$\endgroup\$
    – 12Me21
    Feb 7, 2017 at 22:26
  • \$\begingroup\$ @12Me21 How can I make it different, then? \$\endgroup\$
    – ckjbgames
    Feb 7, 2017 at 22:35
  • \$\begingroup\$ Maybe you could require them to draw an actual rotating globe, given a map image. (It's a lot more complicated though) \$\endgroup\$
    – 12Me21
    Feb 7, 2017 at 22:37
  • \$\begingroup\$ @12Me21 I just had the idea to add a moon rotating around the earth. Help me think of how to implement it. \$\endgroup\$
    – ckjbgames
    Feb 7, 2017 at 22:59
  • \$\begingroup\$ You could make it use the different moon phase emoji to display the proper phases or something \$\endgroup\$
    – 12Me21
    Feb 7, 2017 at 23:05
  • \$\begingroup\$ @12Me21, an actual rotating globe is potentially a dupe of codegolf.stackexchange.com/q/24326/194 - certainly some of the answers could be copied across. \$\endgroup\$ Feb 9, 2017 at 9:02
  • \$\begingroup\$ Take as input the "spin speed" (ms delay), althought that's fairly trivial to handle. \$\endgroup\$ Feb 10, 2017 at 22:13
  • \$\begingroup\$ @Carcigenicate I will do that. \$\endgroup\$
    – ckjbgames
    Feb 12, 2017 at 21:54
5
\$\begingroup\$

Tron Game

Write a Tron bot!

The aim of the game is to make as many moves on a grid as possible without moving onto a space that has already been occupied in the current game. If your bot is unable to make such a move, it loses the round. The board does not wrap so bots can not go off the side of the arena.

Game IO:

Your bot will be written in python and will create a class that inherits from BotSkeleton. An example test bot is shown below.

from bot_skeleton import BotSkeleton
from typing_hints import PositionDict, Position
from board import Board
from typing import List

import random


class Test(BotSkeleton):
    def make_move(self, board: Board, positions: PositionDict) -> Position:
        self.board = board
        self.position = positions[self.bot_id]
        valid_moves = self.get_valid_moves()
        try:
            return random.choice(valid_moves)
        except IndexError:
            return None

    def get_valid_moves(self) -> List[Position]:
        moves = filter(self.board.position_valid, ((self.position[0]+1, self.position[1]),
                                                   (self.position[0]-1, self.position[1]),
                                                   (self.position[0], self.position[1]+1),
                                                   (self.position[0], self.position[1]-1)))
        return list(moves)

(Type hints are not required but illustrated here to help understanding)

  • position is a 2-long tuple containing 2 integers.
  • positions is a Dict[bot_id, position]
  • board can be indexed with a position.
    • get_random_empty_pos() -> position - returns a empty position at random in the board
    • position_valid(position) -> bool - returns if this move is valid (but not next to the position given)
    • copy() -> List[List[int] - returns a 2d list that can be modifiable of the current board state
    • EMPTY - the id for an empty space
  • The value you return must be a position, and must also have a distance of 1 from this, not including diagonals.

Built in attributes for BotSkeleton:

  • log - contains a file object that you may write to
  • no_bots - the number of bots the game began with
  • bot_id - you're bot's id number.

Methods in Board:

  • get_random_empty_pos() -> Position - Returns a position at random that is empty
  • position_valid(pos: Position) -> bool - returns if a position is inside the board and is currently empty
  • copy() -> Board - return a copy of the board that is writable

Tournament structure

  • Every bot will get pitted against every other bot in a giant arena
    • That is to say every single bot will be in every battle
    • The size of the arena will be (30, 30). This may be increased depending on number of bots entered.

General rules that I can't find better places for

  • Your bot may NOT use any file storage except for write-only access to the log file provided
  • Your bot must be written in Python 3. Sorry java people
  • You may enter as many bots as you want
  • Your bot must not attempt to subvert the game state

I reserve the right to disqualify any bot from the competition

(but shall only do so after telling you I will do so and you not making any changes required)

You may download the controller here

Results:

  • Results here

Sandbox notes:

  • Should there be a minimum starting distance between players?
\$\endgroup\$
3
  • 1
    \$\begingroup\$ What happens if two bots go on the same position at the same time? \$\endgroup\$
    – Katenkyo
    Mar 23, 2016 at 11:10
  • 2
    \$\begingroup\$ Is the goal really to survive as many moves as possible, and not to survive longer than the other guy? \$\endgroup\$
    – feersum
    Dec 7, 2016 at 14:03
  • \$\begingroup\$ probably change "sorry" to "sorry not sorry" \$\endgroup\$ Feb 11, 2017 at 12:20
5
\$\begingroup\$

Part I: Triangular Manhattan Distance

Part II: Triangular Chebyshev Distance

The Chebyshev distance on a regular grid is the number of orthogonal or diagonal steps one needs to take to reach one cell from another. That is, we can move either through the edge of a cell, or through a corner, to a neighbouring cell.

We can define a similar distance on other grids, for example the triangular grid. We can address the individual cells in the grid with the following indexing scheme, where each cell contains an x,y pair:

    ____________________________________...
   /\      /\      /\      /\      /\
  /  \ 1,0/  \ 3,0/  \ 5,0/  \ 7,0/  \
 / 0,0\  / 2,0\  / 4,0\  / 6,0\  / 8,0\
/______\/______\/______\/______\/______\...
\      /\      /\      /\      /\      /
 \ 0,1/  \ 2,1/  \ 4,1/  \ 6,1/  \ 8,1/
  \  / 1,1\  / 3,1\  / 5,1\  / 7,1\  /
   \/______\/______\/______\/______\/___...
   /\      /\      /\      /\      /\
  /  \ 1,2/  \ 3,2/  \ 5,2/  \ 7,2/  \
 / 0,2\  / 2,2\  / 4,2\  / 6,2\  / 8,2\  
/______\/______\/______\/______\/______\...
\      /\      /\      /\      /\      /
 \ 0,3/  \ 2,3/  \ 4,3/  \ 6,3/  \ 8,3/
  \  / 1,3\  / 3,3\  / 5,3\  / 7,3\  /
   \/______\/______\/______\/______\/___...
   /\      /\      /\      /\      /\
  .  .    .  .    .  .    .  .    .  .
 .    .  .    .  .    .  .    .  .    .

Now the Chebyshev distance on this grid is again the minimal number of steps across edges or corners to get from one cell to another. So you can move from 3,1 to any of its 12 neighbours:

2,1 4,1 3,2 (through edges)

3,0 1,2 5,2 (the opposite triangle through corners)

2,0 4,0 1,1
5,1 2,2 4,2 (the other triangles through corners)

For instance, the distance from 2,1 to 7,2 is 3. The shortest path is generally not unique, but one way to make the distance in 3 steps is:

2,1 --> 4,1 --> 5,1 --> 7,2

The Challenge

Given two coordinate pairs x1,y1 and x2,y2 from the above addressing scheme, return the Chebyshev distance between them.

You may assume that all four inputs are non-negative integers, each less than 128. You may take them in any order and arbitrarily grouped (four separate arguments, a list of four integers, two pairs of integers, a 2x2 matrix, ...).

You may write a program or a function and use any of the standard methods of receiving input and providing output.

You may use any programming language, but note that these loopholes are forbidden by default.

This is , so the shortest valid answer – measured in bytes – wins.

Test Cases

Each test case is given as x1,y1 x2,y2 => result.

1,2 1,2 => 0
0,1 1,1 => 1
1,0 1,1 => 1
2,1 7,2 => 3

Will add more test cases when I have a reference implementation.

\$\endgroup\$
10
  • \$\begingroup\$ @trichoplax Yes, thank you. With the slow response to the Manhattan variant, I do wonder whether this one will be a bit too tricky, but we'll see. :) \$\endgroup\$ Apr 4, 2017 at 21:28
  • \$\begingroup\$ "I do wonder whether this one will be a bit too tricky, but we'll see. :)" I've found a solution for this one, but not for the other one yet. ;) \$\endgroup\$ Apr 5, 2017 at 12:01
  • \$\begingroup\$ @KevinCruijssen Oh, would you mind sharing it? :) \$\endgroup\$ Apr 5, 2017 at 12:06
  • \$\begingroup\$ I always make my answers in Java 7 btw, so I'll still be beaten by golfing answers. But this is what I came up with (I've also added an explanation of how I came up with the solution in the TIO footer): Try it here. \$\endgroup\$ Apr 5, 2017 at 12:23
  • \$\begingroup\$ Hmm, I just took another look at your part 1 challenge and realized my solution above for part 2 is incorrect.. :( Based on your current test cases I falsely assumed x1,y1 is always smaller than x2,y2. So my code fails for a test case like 4,1 1,3. Back to the drawing board.. Also, as to why I think this challenge is easier than part 1: In part 1 you had to determine whether the triangle was facing upwards (available: left; right; below) or facing downwards (available: left; right; above). With this challenge all 12 surrounding triangles - regardless of orientation - are accessible. \$\endgroup\$ Apr 7, 2017 at 8:22
  • \$\begingroup\$ @KevinCruijssen but the exact coordinates/orientations of those 12 neighbours also change, right? \$\endgroup\$ Apr 7, 2017 at 8:24
  • \$\begingroup\$ Yeah, was about to edit my comment again when I realized that.. If you have more test cases I'll take another look at this part 2. I'll start with part 1 for now. (PS: Have a nice weekend - apparently I can use it.. >.>) \$\endgroup\$ Apr 7, 2017 at 8:25
  • \$\begingroup\$ Me again, more than one year later. I've currently set a bounty for your Part 1 challenge to give it more attention, which you may or may not have noticed based on the new answers given. If you have time, could you add more test cases for this one? \$\endgroup\$ Jun 29, 2018 at 8:40
  • \$\begingroup\$ @KevinCruijssen I'm not sure how soon I'll get around to that, because I'd want to write a reference implementation for that. If you want it post it soon, feel free to add them yourself (and optionally post the challenge yourself if you like). \$\endgroup\$ Jun 29, 2018 at 16:26
  • \$\begingroup\$ Oh no, I'll patiently wait. I'm going on vacation soon anyway, so I won't have time to answer during that time. I just made that comment above as a reminder so it can hopefully be posted in the not to distant future. :) \$\endgroup\$ Jun 29, 2018 at 16:42
5
\$\begingroup\$

Table parser, code golf

Input

|===========|=============|==============|
|Left align |  Right align| Center align |
|===========|=============|==============|
|This       |         This|     This     |
|-----------|-------------|--------------|
|                column                  |
|-----------|-------------|--------------|
|will       |                            |
|-----------|          will be           |
|be         |                            |
|-----------|-------------|--------------|
|left       |        right|    center    |
|-----------|-------------|--------------|
|aligned    |      aligned|   aligned    |
|-----------|-------------|--------------|
|and can be |    vertical-|              |
|multilined | align middle|  as default but a bit long line |
|with <br>  |             |              |
|-----------|-------------|--------------|

Output should be valid HTML. And I am thinking of following requirement specs.

  • alignments (left, right, middle)
  • border thickness (normal -, |, bold, =,||)
  • with table headers or without
  • rowspans, colspans
  • multiline & vertical-align is always middle

What do you think?

\$\endgroup\$
18
  • \$\begingroup\$ I guess figuring out cells and row/colspans is enough work, so handling bold and italic could be omitted, maybe. \$\endgroup\$
    – Joey
    May 9, 2011 at 12:15
  • \$\begingroup\$ Also, should this be a code golf or a challenge? \$\endgroup\$
    – Joey
    May 9, 2011 at 12:18
  • \$\begingroup\$ code-golf @joey. and I am gonna leave bold and italic out. I was not sure it was hard enough :D \$\endgroup\$
    – YOU
    May 9, 2011 at 13:22
  • \$\begingroup\$ Can cells contain more than a line of text? I.e. do long contents wrap or do they extend the cell? \$\endgroup\$
    – Joey
    May 9, 2011 at 13:35
  • \$\begingroup\$ @Joey, yeah, a bit more complicated now, multiline involve vertical-align, so I set that as middle as default, what do you think? \$\endgroup\$
    – YOU
    May 9, 2011 at 13:49
  • \$\begingroup\$ Yikes :D. Well, I was just pondering a few ways of figuring out the layout and determining rowspans would be much easier if all rows are the same height ;). Should be ok, but bear in mind that it makes judging correct vertical align difficult. Given the current last row the valigns could be either one of mmm, ttm, tmm, btm, bmm. No show-stopper but something to keep in mind for test cases and scripts. \$\endgroup\$
    – Joey
    May 9, 2011 at 13:53
  • \$\begingroup\$ @Joey, yeah, to judge top, middle, and button, we need a blank row between every table row, just like a space in right, right, and center, so I guess I need to make vertical-align as middle as default. \$\endgroup\$
    – YOU
    May 9, 2011 at 14:06
  • \$\begingroup\$ So vertical align will not vary and always be middle, if I understand you correctly? \$\endgroup\$
    – Joey
    May 9, 2011 at 14:07
  • \$\begingroup\$ @Joey, yeah .. and for multiline items you just need to put <br> between and can be, multilined, and too, if those are supposed to be same line, input will be |and can be multilined too | ....... | ..... | . \$\endgroup\$
    – YOU
    May 9, 2011 at 14:10
  • \$\begingroup\$ I think your multi-column cells are ambiguous: the last row shows that text can extend beyond the ASCII art column width, so we cannot use the horizontal alignment of |s to judge colspans. Therefore, the 4th and 5th rows could span 1 and 2 columns (as visually, but unreliably indicated) or 2 and 1 columns. Example \$\endgroup\$
    – Adám
    Jun 5, 2017 at 7:20
  • \$\begingroup\$ @Adám, thanks for taking a look. Yeah, might be that's the reason there is not much interest on this. \$\endgroup\$
    – YOU
    Jun 5, 2017 at 10:33
  • \$\begingroup\$ @YOU Also, this is a very complex challenge, pretty much implementing a large subset of the Wiki markup for tables. Maybe you should narrow the scope by removing all formatting; alignment, borders, headers (these are also ambiguous), and just focusing on converting the -| style table into HTML. Maybe even remove the row and column spans? \$\endgroup\$
    – Adám
    Jun 5, 2017 at 10:42
  • \$\begingroup\$ I don't remember why I put all those things in, may be similar challenge was there already, for simple tables, but I don't know. I need to look up more. \$\endgroup\$
    – YOU
    Jun 5, 2017 at 14:11
  • \$\begingroup\$ @Adám, removed rowspan, colspan, multiline and vertical align. Not sure it could be duplicate entry now. \$\endgroup\$
    – YOU
    Jun 5, 2017 at 14:17
  • 1
    \$\begingroup\$ @programmer5000, became duped, because of you guys suggestion. I rollbacked. \$\endgroup\$
    – YOU
    Jun 9, 2017 at 15:37
5
\$\begingroup\$

Line up for golf!

\$\endgroup\$
7
  • 4
    \$\begingroup\$ The comments in the test cases reduce their usefulness as test cases. Moreover, the first one contradicts the "full specification", and the second one adds to it. The spec should contain everything needed to justify the correctness of the test cases. You also need to specify desired behaviour when there is no solution, and to include a test case for that scenario. \$\endgroup\$ Mar 7, 2014 at 14:04
  • 2
    \$\begingroup\$ How are conditionals to be interpreted when mixed in a single full condition? Alice is 1st or 1st to last and in front Bob or 1 space behind Bob \$\endgroup\$ Mar 7, 2014 at 14:12
  • \$\begingroup\$ This seems like a good challenge, but Peter Taylor and Jan Dvorak's concerns should be addressed. \$\endgroup\$
    – user10766
    Mar 8, 2014 at 18:42
  • \$\begingroup\$ @PeterTaylor Edited, but how does the first one contradict? \$\endgroup\$
    – Doorknob
    Mar 10, 2014 at 2:32
  • \$\begingroup\$ @JanDvorak Edited to clarify. \$\endgroup\$
    – Doorknob
    Mar 10, 2014 at 2:32
  • \$\begingroup\$ Another concern: What if there is no solution? What if there is more than one? \$\endgroup\$ Mar 10, 2014 at 6:04
  • \$\begingroup\$ Please delete this now that it is posted. \$\endgroup\$
    – user58826
    Aug 1, 2017 at 16:07
5
\$\begingroup\$

This is a proposal adopted by programmer5000. Any feedback before I post it?

Seven-segment usage

I have an old digital clock and I am concerned the individual segments of the digits might run out of magical binary energy or whatever powers them. To know which of the segments on the clock will fail first I want to know what is the percentage of time each segment is lit.

The challenge is to compute the percentage of time any segment on a digital clock is lit.

Input

The input will be in the form X.L, where:

  • X is a number from 1 to 4. 1 is the left-most number of the clock, 4 the right-most
  • L is a letter from a to g or a number from 1 to 7
  • the separator can be changed to fit your needs (no separator is an option)

Disposition of segments, image source: Wikipedia Disposition of segments plus numbers image source: Wikipedia]

Output

The output, is a percentage, given with at least 2 figures after the decimal point. It can be rounded or truncated to the closest value if you want to keep a limited number of digits.

Valid outputs: 0.74, 32.47, 7.5 (for 7.50)

Additional stuff

The clock is in 24 hours format (so 22:45 is a valid time).

We consider the clock started working at 0:00 so the challenge is effectively working out what percentage of a complete day a single segment is lit.

The first number is not lit when it is 0.

Lit segments for each number:

  • 1: b c
  • 2: a b d e g
  • 3: a b c d g
  • 4: b c f g
  • 5: a c d f g
  • 6: a c d e f g
  • 7: a b c
  • 8: a b c d e f g
  • 9: a b c d f g
  • 0: a b c d e f

Examples

  • 1.b => 58.33
  • 3.e => 33.33
  • 2.d => 70.83
  • 4.d => 70.00

Full list of outputs here

Winner

Code golf, most probably, though I am not sure yet this is the best format (I am not too interested in the input parsing and the output formatting, they might be an obstacle to golfing?)

\$\endgroup\$
5
  • 6
    \$\begingroup\$ AIUI there are 28 valid inputs, so you could provide a full list of test cases as a pastebin. I would remove "No hard coding of results": if hard-coding the 28 cases is shorter than the calculation, then IMO that's a flaw which makes the question not worth bothering with at all rather than something which can be worked around; and the issue of whether or not an answer "hard-codes the results" is likely to be grey rather than black or white. \$\endgroup\$ Mar 3, 2016 at 10:54
  • \$\begingroup\$ List of results added, I'll need to double check them. Hard coding removed, yes I don't see how "cheating" would be an issue. Any hard-coding would have to be quite constructive to be efficient. \$\endgroup\$
    – drolex
    Mar 3, 2016 at 11:29
  • \$\begingroup\$ I am removing limitations on the output as well, I don't think it brings anything to the challenge. I have started to golf a solution in python, I think there are a few interesting possibilities to optimise the use of strings describing segments used for each digit. \$\endgroup\$
    – drolex
    Mar 3, 2016 at 15:56
  • \$\begingroup\$ Can I post this abandoned proposal? \$\endgroup\$
    – user58826
    Jun 9, 2017 at 13:00
  • \$\begingroup\$ @programmer5000 certainly, just try to make it better :) \$\endgroup\$
    – drolex
    Jun 9, 2017 at 13:05
5
\$\begingroup\$

A Game of Knights

In this King of the Hill, you control 10 Knights. You need to surround your enemy before they surround you.

Each Knight has 3 possible actions:

  • Dash: Move 3 squares in a cardinal direction. If there is a knight in your path, move as far as you can.
  • Leap: Move 2 squares in a cardinal direction. If there is a knight on your destination square, don't move at all.
  • Push: Move 1 square in a cardinal direction. If there is a knight on your destination square, and no knight or wall behind him, you both move 1 square.

Each round will have a Planning Phase and then an Action Phase.

Planning:

During the planning phase, players will alternate creating a plan until both players have created 10 plans.

  • A plan consists of either an action type or a cardinal direction (not both).
  • A plan also includes the knight that will perform the action
  • All plans are revealed to both players

Action:

  • Actions occur in the same order that they were planned.
  • If you planned an action type, you will be able to choose the direction. If you planned a cardinal direction, you will be able to chose the action type you want.

After the action phase, if any knights are on the same location they started at (at the start of the round) are captured and removed from the game. You win by capturing all other knights.

Other info:

  • Cardinal means North, East, South, or West
  • The board is a 10x10 board. Your knights start as a line at the bottom of the board.
  • Walls block movement and cannot be pushed.
  • You don't have to plan an action for every knight, and a knight can take multiple actions.
  • There are a maximum of 1000 rounds. After those 1000 rounds, the winner is the player with the most knights. (A tie is allowed).
  • The starting player for a game is randomized, and that player starts every round.

You have won the game when you opponent cannot make any mobility actions.

\$\endgroup\$
7
  • 3
    \$\begingroup\$ Man, now I want to play this in person. \$\endgroup\$
    – DLosc
    Feb 18, 2017 at 21:17
  • 1
    \$\begingroup\$ Isn't it possible to form your knights into a solid 2×5 rectangle (thus immune to pushing), away from the starting squares, before the enemy can move and push it? That would guarantee you couldn't lose. Also you should clarify whether you can plan two actions for the same knight, and if you can, whether it gets to take both or only one. \$\endgroup\$
    – user62131
    Feb 18, 2017 at 23:14
  • \$\begingroup\$ @ais523 movement is not simultaneous. You start as a 1x10 line. A knight could take 10 moves on a round \$\endgroup\$ Feb 18, 2017 at 23:52
  • \$\begingroup\$ To be clear, as long as you have 10 knights, you need to move each of them every round or lose the ones you didn't move, assuming you don't push. Correct? \$\endgroup\$
    – isaacg
    Jun 23, 2017 at 21:30
  • \$\begingroup\$ Does the same player go first in each round, or does that alternate? \$\endgroup\$ Jun 23, 2017 at 21:31
  • \$\begingroup\$ @isaacg correct. \$\endgroup\$ Jun 23, 2017 at 23:51
  • \$\begingroup\$ @PeterTaylor I've debated about this, and I think the same player will go first in each round. The player that goes first will be randomized each game, but it will be consistent round to round. Otherwise, you end up with a player being able to move twice in a row. \$\endgroup\$ Jun 23, 2017 at 23:52
5
\$\begingroup\$

The Chroma Key to Success

\$\endgroup\$
5
  • \$\begingroup\$ So essentially, for every pixel in the second image, if it's #00FF00, replace it with the corresponding pixel in the first image; otherwise, don't modify that pixel? \$\endgroup\$
    – hyper-neutrino Mod
    Jul 16, 2017 at 17:33
  • \$\begingroup\$ @HyperNeutrino Yes. \$\endgroup\$
    – ckjbgames
    Jul 16, 2017 at 17:34
  • \$\begingroup\$ I don't know image formats very well, but are we guaranteed that the image will be in 3-byte-RGB format? As in, is there transparency? \$\endgroup\$
    – hyper-neutrino Mod
    Jul 16, 2017 at 17:36
  • \$\begingroup\$ @HyperNeutrino Assume the image is full opacity. \$\endgroup\$
    – ckjbgames
    Jul 16, 2017 at 17:39
  • 3
    \$\begingroup\$ Please delete this, now that it is posted. \$\endgroup\$
    – user58826
    Jul 17, 2017 at 13:05
5
\$\begingroup\$

PPCG VS CR KotH

I am working as fast as I can to get this ready. Please be patient. I have a real job and will attempt to work on this on vacation to the northern states using mobile. I give no promises.

I challenged CR to a KotH challenge. So here is the specs.

Two sides in a arena. 1000x1000. Resources scattered about. A giant area in the center filled with resources. You must build a base and defend your ancient.

You have a few kinds of buildings:

  1. Wall: a simple wall that must be broken before walking through. It has 1/2/4/8 health (upgrades). It can be changed into a gate.
  2. Gate: a gate that lets the side that made it walk through. Health: 1/2/2/4
  3. Turret: Deals 1/1/2/4 damage to the lowest health enemy within a 1/2/4/4 block radius and has 1/1/4/8 health.
  4. Resource drop: a place that you can drop resources. Has 2/2/4/6 health.
  5. Tower of vision. Gives 10 sight radius with health of five. Costs two resources to build.

Upgrades are 1/2/3/4. The first upgrade is just buying the building.

Gates when transformed stay the same level as the wall they started as, can still be upgraded.

Every bot starts with five health, one damage, and three sight radius. They can upgrade each by four for the cost of 1/2/2/2.

All upgrades take one bot. It takes 1/2/2/3 turns to upgrade an item. The bot must upgrade the item all at once or it must restart and repay the cost. Building a item takes one resource.

\$\endgroup\$
9
  • 2
    \$\begingroup\$ so, how does turn order work? is there a way to attack excluding turrets? \$\endgroup\$ Jul 29, 2017 at 2:54
  • 1
    \$\begingroup\$ Will the bots have knowledge of the entire 1000x1000 area or what is the line of sight? \$\endgroup\$ Jul 29, 2017 at 8:48
  • 2
    \$\begingroup\$ I'd recommend not making the challenge too complicated/complex. I'm not sure that skill points are necessary. \$\endgroup\$ Jul 29, 2017 at 8:49
  • \$\begingroup\$ How does it work with the Q&A site format? Or is it an off-site challenge? \$\endgroup\$
    – ugoren
    Jul 29, 2017 at 19:10
  • 2
    \$\begingroup\$ @ugoren King of the Hill challenges like this one are very much on-topic on PPCG, and in fact we even have a tag for challenges like these: koth. \$\endgroup\$
    – user41805
    Jul 29, 2017 at 19:53
  • \$\begingroup\$ @Cowsquack, You're right, it's OK. A bit strange for my taste, since the war happens off-site, but never mind. \$\endgroup\$
    – ugoren
    Jul 29, 2017 at 20:16
  • \$\begingroup\$ @SimonForsberg good point \$\endgroup\$
    – user63187
    Jul 29, 2017 at 23:26
  • \$\begingroup\$ This will not be a challenge between PPCG and CR. Challenge declined \$\endgroup\$ Aug 4, 2017 at 18:50
  • \$\begingroup\$ Since this idea is dead now, consider developing it further as a normal KoTH or deleting and editing it down to a stub. \$\endgroup\$
    – hyper-neutrino Mod
    Oct 30, 2017 at 2:24
5
\$\begingroup\$

Swap the frogs!

Given 2 integers N >= 1, representing left-side frogs, and M >= 1, representing right-side frogs, return all the steps required so that the frogs change sides with the minimum number of steps. The frogs start with one empty spot between the two sides. A frog can jump to the empty space if there is at most 1 frog from either side between the frog and the empty space. A frog can jump either forwards or backwards.


An example, with N = 3 and M = 2:

LLL.RR
LL.LRR
LLRL.R
LLRLR.
LLR.RL
L.RLRL
.LRLRL
RL.LRL
RLRL.L
RLR.LL
R.RLLL
RR.LLL

The corresponding output would be (1-indexed):

[3, 5, 6, 4, 2, 1, 3, 5, 4, 2, 3]

Each one is the index of the column that the frog that must jump is before jumping.

Rules

  • You may perform I/O in any reasonable format.
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Maybe start with a small example, because now the main output specification is tucked under a huge block of text, which will be TL;DR for many people. \$\endgroup\$
    – Sanchises
    Aug 8, 2017 at 10:52
  • \$\begingroup\$ 1. The questions in the section headed "Sandbox" make no sense because their context has been deleted. Are they still relevant? 2. "What you have to print" should presumably be "What you have to output". 3. "A frog can jump either forwards or backwards" so there should be at least one test case where all optimal paths only involve jumping forwards and at least one where all optimal paths involve jumping backwards. \$\endgroup\$ Aug 8, 2017 at 14:55
  • \$\begingroup\$ Maybe a testcase where no steps are necessary, too. \$\endgroup\$
    – Sanchises
    Aug 8, 2017 at 15:13
  • \$\begingroup\$ @PeterTaylor Please, don't be too meta :-) :p \$\endgroup\$ Aug 8, 2017 at 16:46
5
\$\begingroup\$

Learning your strengths and weaknesses

  • Fighters have a unique, random Strength (between 1 and 1000)
  • When two fighters fight, the stronger one wins.
  • Your goal is to accurately guess your fighter's Strength.

Gameplay:

  1. We start by randomly ordering all 1000 fighters.

  2. Each fighter fights his neighbor (The even fighters fight the fighter 1 above)

  3. The fighter is given two pieces of information: His opponent's last guess, and who won the fight. The fighter then guesses his strength.

  4. We perform a stable sort based on the guessed strength, and go back to step 2

  5. After 10 guesses, the fighter's score is (RealStrength - GuessedStrength)^2. Lower is better.

Other details:

  • There will be duplicate bots in a single game.

  • A stable sort is a sort that (effectively) uses the past ordering as a tiebreaker. In essence, if players [A,B,C,D] guessed [10,5,10,5] then the new order would be [B,D,A,C]

  • Bots aren't allowed to share information between each other, but are allowed to persist information within a single game.

  • I will run a large number of games. The exact number will be dependent on how much variation there is. Your final score will be all of your scores summed up.

\$\endgroup\$
7
  • \$\begingroup\$ I like this a lot. Sounds like a very interesting challenge. A couple thoughts though. 1) What's the point of squaring your score? If there was some kind of polynomial scoring I could see that, but it doesn't really have any purpose at the moment. 2) personally, I think it should be more than 10 rounds. \$\endgroup\$
    – DJMcMayhem
    Jan 29, 2018 at 17:17
  • \$\begingroup\$ @DJMcMayhem I think 10 rounds is enough for good bots to get a reasonable score without providing so much information that many will get a perfect score. I do think the process should be repeated though. Maybe a terminology change: 10 guesses per round, each round scored as indicated, average of X rounds is the final score. Also, the squaring is a way of getting absolute value (result is always positive even if guess > real) and if averaging multiple results it weights wildly incorrect guesses to increase the score by a lot. \$\endgroup\$ Jan 29, 2018 at 17:45
  • \$\begingroup\$ @DJMcMayhem Squaring means that being 50 off is far worse than being 25 off. I picked 10 rounds because log2(1000) is 9.97. This requires bots to be efficient with their time. If I make it much higher, then its going to be hard for me to differentiate the top bots. \$\endgroup\$ Jan 29, 2018 at 18:24
  • \$\begingroup\$ @KamilDrakari Updated, thanks. \$\endgroup\$ Jan 29, 2018 at 18:30
  • \$\begingroup\$ In step 3, each fighter is provided "His opponent's last guess". What will be provided during the first round? \$\endgroup\$ Jan 29, 2018 at 19:21
  • \$\begingroup\$ That'll be part of the API spec. Something like -1. \$\endgroup\$ Jan 29, 2018 at 19:31
  • \$\begingroup\$ This will probably be my last suggestion (for now): a link and/or description for "stable sort" will probably be helpful; I certainly needed to look it up, and is quite useful for informing strategies, as well as answering a question I otherwise had about handling ties. \$\endgroup\$ Jan 29, 2018 at 22:00
5
\$\begingroup\$

Can the robotic arm reach itself?

( ͡° ͜ʖ ͡°) title is kinda sketch at the moment

A robotic arm is made up of a set of line segments, each with a positive integer length. Each joint on the robot has two possible positions: straight or 90 degrees clockwise.

Here is a robotic arm with 4 line segments of sizes 4, 2, 3, 2

+---+-+--+-+

Here is the same robot arm with one joint bent

+---+-+
      |
      |
      +
      |
      +

(vertical scale is kinda messed up)

Right now, the robotic arm isn't reaching itself. By bending all of the segments, however, the arm can reach itself.

+X--+
 |  |
 +--+

So, a robotic arm of size [4,2,3,2] can reach itself.

Here is a robotic arm of size [3,1,4,3] that can't reach itself:

+--++---+--+

+
|
|+--+
+---+

Whereas a robotic arm of size [1,1,2,2,3] can reach itself.

++-+-+--+

+
X++
| |
+-+

A robotic arm of size [2,2,3,5,3,4] can also reach itself

a-b-c--d----e--f---g

e--f
|  |
|  |
|a-b
|  g
d--c

Challenge

Given list of numbers, such as [1,2,3,4,5], output a truthy value of the robotic arm can reach itself and a falsey value if it cannot.

\$\endgroup\$
9
  • \$\begingroup\$ s/touch/intersect/g? \$\endgroup\$ Feb 17, 2016 at 20:27
  • \$\begingroup\$ We've had some related challenges I think. At least codegolf.stackexchange.com/q/45059/8478 \$\endgroup\$ Feb 17, 2016 at 21:01
  • \$\begingroup\$ codegolf.stackexchange.com/q/49713/8478 \$\endgroup\$ Feb 17, 2016 at 21:03
  • 1
    \$\begingroup\$ The scenario reminds me of stretchy snakes kissing. Except, these snakes are unusually rigid ... you know what, never mind. \$\endgroup\$
    – xnor
    Feb 17, 2016 at 21:45
  • \$\begingroup\$ The first few examples would be clearer if either you also used letters to distinguish the joints or if you at least marked one end of the chain in both the straight and the folded representation. \$\endgroup\$ Feb 18, 2016 at 7:55
  • \$\begingroup\$ You could probably also use more than one example where self-intersection only happens when not all joints are bent like the [1,1,2,2,3] example. If that's possible it would be good to have one where you need to bend joints on both sides of a straight joint to make the self-intersection happen, but I don't know if that can happen. \$\endgroup\$ Feb 18, 2016 at 12:23
  • \$\begingroup\$ @El'endiaStarman s/touch/reach/g \$\endgroup\$
    – mbomb007
    Apr 7, 2016 at 15:13
  • \$\begingroup\$ Is 4,2,3,3 truthy or falsy? \$\endgroup\$
    – Zgarb
    Feb 7, 2018 at 8:12
  • 2
    \$\begingroup\$ Can the robotic arm go through itself? The [2,2,3,5,3,4] case seems to indicate so. \$\endgroup\$
    – stanri
    Feb 7, 2018 at 10:50
5
\$\begingroup\$

Efficient Tab Completion

Many tools that programmers use on a daily basis, like bash and Emacs, have tab completion.

  • Pressing Tab in certain situations will attempt to complete the text at the cursor, from a set of possibilities. The term is "completed" by filling in the remaining text.
  • For the aforementioned, if there are multiple candidates for completion, you will instead be shown a list of the candidates.
  • If there are multiple possibilities but they all start with the same substring/prefix, the rest of that substring will be filled in.

For example, to reach pydoc3, you only have to type

pyd<TAB>3

where Tab is shown as <TAB>. The tab key will insert oc, since all options starting with pyd also start with pydoc (in the set below).

a shot of bash tab completion options


The Challenge

Given a collection of strings called S and a target string called T, figure out the minimum number of keystrokes to reach T, assuming we're using tab completion and S is the set of possibilities.

  • Tab completion here is modeled after bash and Emacs, so it is case-sensitive.
  • The keystrokes/moves don't matter, only how many. A "keystroke" is:
    • Some character in the string
    • Tab
    • If S is [abc, ab] and T is ab, then either a<TAB> or ab will get there. Tab counts as one keystroke, so the method doesn't matter.
  • You may assume terms will only contain alphanumeric characters, underscore, and hyphen ([A-Za-z0-9]_-), for the purpose of this challenge.
  • You may assume no strings are empty.
  • You may assume S contains T (unsure about this one)
  • The input can be taken in whatever format is appropriate for your platform or language (array of strings, string with separators etc.) S and T are considered separate inputs.
    • I/O format is flexible and defaults apply (full program, function etc.)

This is , so shortest answer in bytes wins.


Test Cases

Set                                                 Target         Output
--------------------------------------------------  -------------  ------
[ab, abc]                                           ab             2
[lisp-mode, list-abbrevs, list-packages]            list-packages  5
[heck, hell, help_me, hello, goodbye, hello_world]  hello          5

Feedback

  • Is anything vague or underspecified?
  • Is something too specific or cumbersome?
  • Is this too similar to an existing question?
  • Should we assume S will always contain T, or require a special case?
  • Should something be changed about case-sensitivity? Assume everything is lowercase?
  • Any tags I should use?
\$\endgroup\$
6
  • \$\begingroup\$ Hmm. wouldn't it be l<TAB>t-p<TAB> for #2? Also how to get hello in 4 keystrokes? \$\endgroup\$
    – ASCII-only
    May 2, 2018 at 0:48
  • \$\begingroup\$ You're right about list-packages, I'll fix it. And it looks like hello should be 5 keystrokes at hello. Good eye. \$\endgroup\$
    – snail_
    May 2, 2018 at 0:55
  • \$\begingroup\$ You can get symbols for keys using <kbd> (<kbd>tab</kbd>) \$\endgroup\$
    – 12Me21
    May 2, 2018 at 1:41
  • \$\begingroup\$ I like this challenge! A couple of questions: is it illegal to start typing with a TAB (could be useful when all the strings in S start with the same prefix)? Why is . not in the allowed characters, since it is present in your first example of pydoc? \$\endgroup\$
    – Leo
    May 3, 2018 at 1:15
  • \$\begingroup\$ You can use <TAB> wherever you want as long as it produces the minimum keystrokes. The image is supposed to be illustrative and not necessarily representative of the challenge itself; I worry that requiring too large or too specific a subset of characters will make the challenge more complicated than it needs to be (or maybe it won't affect much at all?) \$\endgroup\$
    – snail_
    May 3, 2018 at 1:38
  • \$\begingroup\$ Assume S is [abcde] and T is abcdf. What should be the output? \$\endgroup\$
    – DELETE_ME
    May 4, 2018 at 14:02
5
\$\begingroup\$

Existential Golf

\$\endgroup\$
9
  • \$\begingroup\$ Proofs could be simple: it supports NAND, and NAND is functionally complete. \$\endgroup\$
    – tsh
    Jul 5, 2018 at 7:23
  • 1
    \$\begingroup\$ @tsh Currently there is still no winning criteria. \$\endgroup\$
    – DELETE_ME
    Jul 5, 2018 at 9:54
  • 3
    \$\begingroup\$ This is quite interesting (not sarcasm!), but what's the actual challenge? \$\endgroup\$
    – N. Virgo
    Jul 7, 2018 at 7:02
  • 3
    \$\begingroup\$ @Nathaniel When I actually finish it this will be the next installment of proof-golf. \$\endgroup\$
    – Wheat Wizard Mod
    Jul 8, 2018 at 3:19
  • 1
    \$\begingroup\$ It's great to see an unfinished idea, ready for feedback until it's postable. I see this as an important purpose of the sandbox. \$\endgroup\$ Jul 10, 2018 at 19:43
  • \$\begingroup\$ @trichoplax Not everyone think so. \$\endgroup\$
    – DELETE_ME
    Jul 11, 2018 at 3:46
  • 3
    \$\begingroup\$ I definitely disagree with having to make sandbox posts "finished". I agree with not being lazy though. I see this post as a great example of unlazy and unfinished (when posted). There was clearly effort made, and it was made available for feedback early, which can avoid going too far down a path that others already know won't work. Posting early prunes impossible or impractical challenges so challenge authors have more time for writing the challenges that will make it to main. \$\endgroup\$ Jul 12, 2018 at 19:05
  • 1
    \$\begingroup\$ @user202729 For context, I said that in response to a 1-sentence sandbox post. I also said it bothers me when people "just post the bare minimum they can get by with and edit it later". The first revision of this challenge was clearly not the bare minimum to get by with. \$\endgroup\$
    – DJMcMayhem
    Jul 12, 2018 at 19:14
  • \$\begingroup\$ @DJMcMayhem So you're measuring effort? That's usually not a good idea. \$\endgroup\$
    – DELETE_ME
    Jul 13, 2018 at 2:48
5
\$\begingroup\$

Split and recombine a number

This challenge has two related parts. Your task is to write two functions/programs as per the below specifications. You may share code across your submissions, the submissions may call one another, and you may even submit a single submission which handles both conversions. In the latter case, conversion direction may be determined by whether the input is a single number vs a list, or by an additional consistent second value (not a function) input.

Part 1

Given a floating point number, return a list with one element per digit of its integer part, if any, and if the number is a non-integer, one additional part which is the fractional part. If the number is negative, negate all elements in the output. If the number is zero, return a 1-element list with a zero in it.

When the result list is recombined as per Part 2, it must be precise to within an absolute or relative error of 10⁻¹⁰, whichever is more permissive.

Part 2

Given a list generated as per Part 1, return the number which would have generated that list in Part 1.

When the result number is split as per Part 1, each element must be precise to within an absolute or relative error of 10⁻¹⁰, whichever is more permissive.

Examples

Part 1 <-> Part 2
-123       [-1,-2,-3]
2.71828    [2,0.71828]
-800.6     [-8,0,0,-0.6]
321.7001   [3,2,1,0.7001]
-0.01      [-0.01]
100        [1,0,0]
0          [0]

\$\endgroup\$
11
  • \$\begingroup\$ (1) Technically infinities are floating point numbers (or, at least, they're not NaNs). What should the output be for an infinity? (2) How about for 1E45? (3) For numbers which are small enough to have a fractional part, what restrictions are there on the precision of the output? E.g. to take the fourth test case, 321.7001 - 321 in IEEE 754 double format gives 0.7001000000000204. \$\endgroup\$ Nov 18, 2018 at 21:18
  • \$\begingroup\$ Do we need the leading 0 in the list? Seems cleaner without it \$\endgroup\$
    – Quintec
    Nov 18, 2018 at 22:11
  • \$\begingroup\$ @Quintec What leading zero? \$\endgroup\$
    – Adám
    Nov 18, 2018 at 22:28
  • \$\begingroup\$ @PeterTaylor I'll exclude infinities. I'm not sure what do do about inexactness. I guess I could allow stopping at 16 digits of precision. Any ideas? \$\endgroup\$
    – Adám
    Nov 18, 2018 at 22:31
  • \$\begingroup\$ Now that I think about it, string output for part one doesn’t make sense. But if it did, then I meant [2, .718] instead of [2, 0.718] \$\endgroup\$
    – Quintec
    Nov 18, 2018 at 22:43
  • \$\begingroup\$ I don't see any reason to reject 0 <-> [], given that 0.01 <-> [0.01]. \$\endgroup\$
    – Bubbler
    Nov 19, 2018 at 1:21
  • \$\begingroup\$ For the precision, how about something in the line of "correct up to absolute/relative precision of 1e-16"? Partly because big numbers stored in double are not accurate even in the integer parts. \$\endgroup\$
    – Bubbler
    Nov 19, 2018 at 1:29
  • \$\begingroup\$ @user202729 Yes, I'll add that. Also, see tolerance text now. \$\endgroup\$
    – Adám
    Nov 19, 2018 at 12:36
  • \$\begingroup\$ @Bubbler Tolerance specs added. \$\endgroup\$
    – Adám
    Nov 19, 2018 at 12:41
  • \$\begingroup\$ Suggested test cases: 4.4 <-> [4,0.4] and 44.44 <-> [4,4,0.44]. Also, can we assume there will not be any unnecessary trailing zeros? I have a working solution, even with workaround for 0 <-> [0], but for 0.0 it outputs [0.0], but vice-versa for [0.0] it outputs 0 instead of 0.0. Hence the question that there won't be test cases like 0.0, 4.0 or 6.4000 with unnecessary trailing zeros. My programming language outputs 4.0 -> [4,0] -> 40 due to the implicit conversion of 0.0 to 0.. \$\endgroup\$ Dec 4, 2018 at 10:44
  • \$\begingroup\$ @Adám FWIW Composed a solution using JavaScript for the specification at this question, and a solution for the specification at the original question (that currently has a bug for two test cases at "Part 2" portion, though is not incapable of being fixed). \$\endgroup\$ Dec 17, 2018 at 23:47
5
\$\begingroup\$

Progress: Updated the rules again, and also add the timed function to the bots.

Sylver Coinage KotH

Sylver Coinage is a 2-player mathematical game that has the following rules:

  1. Two players take turns announcing a natural number each time.
  2. Each number announced must be unrepresentable as the sum of non-negative multiples of the numbers announced before.

    Eg. if the first three numbers announced are \$\{6, 11, 15\}\$, then you cannot announce any numbers representable as \$6n_1+11n_2+15n_3\$, where \$n_1,n_2,n_3\ge0\$. You can announce, for example, \$16\$, though.

  3. The player who announced a number not complying with Rule 2, or the number 1, loses.

Here is a twist -- R. L. Hutchings proved that announcing a prime number as the first play provides a winning strategy for the first player, although the detail of the strategy is not yet known. So I put a restriction here: the first player cannot announce a prime number in the first step. Now the first two numbers will be generated randomly by the driver at the beginning. No more restriction on prime numbers now.

Technical Information

A bot playing the game will have to implement a Python 3 class, extending TimedBot, with two methods: announce() and learn(). announce() should receive a list of numbers (possibly empty) and return a single integer, and learn() should receive two integers (id of the first move and second move) and the complete list of the numbers in the last game played.

Here is a sample implementation. Note: DO NOT use this as your submission -- this sample only serves as a demonstration, and it may announce numbers that violate Rule 2.

class SampleBot(TimedBot):     # must not be changed.
    def __init__(self, id):
        super().__init__()     # must not be changed.
        self.id = id

    def announce(self, list):
        import random
        return random.randint(1, 101)

    def learn(self, first, second, list):
        pass

Test Drive

class TimedBot:
    def __init__(self):
        self.time = 20.0

    def timed(func):
        def f(self, *args):
            import time
            a = time.time()
            b = func(self, *args)
            self.time -= (time.time() - a)
            print(self.time)
            return b
        return f

class SampleBot(TimedBot):
    def __init__(self, id):
        super().__init__()
        self.id = id

    @TimedBot.timed
    def announce(self, list):
        import random
        return random.randint(1, 100001)

    @TimedBot.timed
    def learn(self, first, second, list):
        pass

# very inefficient
def islinearcomb(n, l):
    if len(l):
        for i in range(0, n + 1, l[0]):
            if i == n:
                return [n // l[0]]
            elif len(l) > 1:
                isl = islinearcomb(n - i, l[1:])
                if isl:
                    return [i // l[0]] + isl
    return None

lose = -1
turn = 0
nums = []
bots = [SampleBot(0), SampleBot(1)] # replace with your bots here.

import random
while (len(nums) < 2):
    a, b, c, d = random.randint(1, 10), random.randint(1, 10), random.randint(1, 10), random.randint(1, 10)
    if 2**a * 3**b != 2**c * 3**d and 2**a * 3**b > 100000 and 2**c * 3**d > 100000 and 2**min(a,c) * 3**min(b,d) > 12:
        nums = [2**a * 3**b, 2**c * 3**d]
while lose < 0:
    v = bots[turn].announce(nums)
    print("{0}({1}) announced {2}".format(type(bots[turn]).__name__, bots[turn].id, v))
    w = islinearcomb(v, nums)
    if w:
        str = ""
        for i in range(0, len(nums)):
            if i:
                str += "+"
            str += "{0}*{1}".format(nums[i], w[i] if i < len(w) else 0)
        print("{0}({1}) announced {2} that is equal to {3}".format(type(bots[turn]).__name__, bots[turn].id, v, str))
        lose = turn
    elif v == 1:
        print("{0}({1}) announced 1".format(type(bots[turn]).__name__, bots[turn].id))
        lose = turn
    nums += [v]
    turn = 1 - turn
print("{0}({1}) wins".format(type(bots[1 - lose]).__name__, bots[1 - lose].id))

Restrictions

Each bot will have 20 seconds of time for deciding a move todo: adjustments. Running out time during the move results in a lose, and failing to finish a method within 20 seconds will lead to disqualification and rerun of all 100 rounds with the remaining bots.

Schedule

Submissions will be open until todo: date here. After that 100 complete round-robin rounds will be done. Each pair of bots will compete twice in each round, one with the first bot announcing first, and one with the second bot announcing first. Each win brings 3 points, each draw brings 1 point, and each lose brings no points. The bot with the highest points after 100 rounds wins. The tiebreaker will be as follows:

  1. Points got
  2. Wins achieved
  3. Drawing lots
\$\endgroup\$
14
  • \$\begingroup\$ Probably needs some time limit for responses to prevent solutions which attempt to generate a full game tree. \$\endgroup\$ Dec 17, 2018 at 9:26
  • \$\begingroup\$ @PeterTaylor Oh yes I thought about the time limit but I turned out forgetting to put that ;p \$\endgroup\$ Dec 17, 2018 at 9:31
  • \$\begingroup\$ "The player who announced a number not complying with Rule 2 ... loses." I've been thinking about this. I presume that your intention is that the controller will validate the responses. An alternative would be to say that you don't automatically lose, but to add a type of response where the bot can return a proof that the opponent broke rule 2. Then bot programmers have to make a decision as to how much time to spend trying to show that their opponent lost vs computing a valid response. \$\endgroup\$ Dec 17, 2018 at 20:48
  • \$\begingroup\$ What if a game goes on for a billion turns? \$\endgroup\$
    – isaacg
    Dec 18, 2018 at 1:13
  • \$\begingroup\$ @PeterTaylor I'd say my intention is that the controller will validate the responses. (in the test drive there is the code doing exactly that) \$\endgroup\$ Dec 18, 2018 at 9:29
  • \$\begingroup\$ @isaacg If the number does not start out too large a game should end quite quickly. It is because two coprime numbers already make the number of possible moves finite. But If I pose a criteria on how large a number can at most go, then I'm feared that there may be a problem that the game tree is restricted. \$\endgroup\$ Dec 18, 2018 at 9:30
  • \$\begingroup\$ I'd say: don't restrict the highest move, but give each bot a 'chess clock': they start with (say) 10s, and gain 1 second per move (and pass their clock's time as a parameter). Running over time is an automatic loss, and some percentage of time losses is a disqualification. Playing high moves will quickly exhaust their stock of time if they attempt to calculate extensive game trees, which will force the bots to either play quickly or lower their numbers. Adding a decay function to the time per move will encourage smart bots to play smaller numbers to not run out of time to think. \$\endgroup\$ Dec 24, 2018 at 16:47
  • \$\begingroup\$ @Spitemaster That's a good idea, but what I concerned about on large numbers is that the validation may take too long (because we are solving Diophantine equations in many unknowns) \$\endgroup\$ Dec 25, 2018 at 5:48
  • \$\begingroup\$ Do you realise that guaranteeing that the first two numbers are coprime guarantees that the first player will win with correct play? If you want an interesting game then you should generate the first two numbers randomly as 3-smooth numbers with a GCD which is a multiple of 6 and greater than 12. \$\endgroup\$ Jan 9, 2019 at 11:16
  • \$\begingroup\$ @PeterTaylor Great catch! During discussion only the suggestion of giving two initial numbers was achieved, so I didn't realize that. \$\endgroup\$ Jan 10, 2019 at 0:39
  • \$\begingroup\$ I would remove the submission deadline, why not keep it open and update once a new entry comes? Also you might be interested in this (I adapted the code originally written for another KoTH), the easiest thing will be to enforce a certain formatting on the first line and adapt code_matcher to that formatting, st. that it won't break because everyone is using different formatting. \$\endgroup\$ Jan 10, 2019 at 13:48
  • \$\begingroup\$ @BMO Wow that's a good one! And I saw my code in the source lol BTW for the certain formatting part you mean the lines around class FooBar(TimedBot):? \$\endgroup\$ Jan 10, 2019 at 15:19
  • 1
    \$\begingroup\$ @ShieruAsakoto: Yeah, I added the header to bots.py which will be used, all the users' code will be appended to that and written to auto_bots.py.. Basically you'll only need to checkout the few variables (lines 12-20) and the main (from line 117) to see how it works. About the formatting, yes, that's probably the most sane: Make sure every code starts with class NameOfBot(TimedBot):, all definitions are in that class and it's valid Python 3 code (I updated the code_matcher like this it should work fine). \$\endgroup\$ Jan 10, 2019 at 15:33
  • \$\begingroup\$ If they/you use that bots.py you'll need to manually update the imports or do it inside the bot itself, atm. bots could use random and time, maybe add math too. \$\endgroup\$ Jan 10, 2019 at 15:34
5
\$\begingroup\$

Interleave Invariance

There is an infinite sequence that does not change when interleaved with the natural numbers. Consider these few terms:

1 1 2 1 3 2 4 1

Interleave them with the naturals:

1   2   3   4   5   6   7   8
  1   1   2   1   3   2   4   1
-------------------------------
1 1 2 1 3 2 4 1 5 3 6 2 7 4 8 1

As you can see, there is no change in the initial eight. Now, this sequence can be extended indefinitely rather easily by repeating this interleave operation. Your task is to choose and implement one of three output formats:

  1. Take as input a nonnegative integer n and output the nth term of this sequence, zero- or one-indexed (your choice).

  2. Take as input a nonnegative integer n and output the first n terms of this sequence.

  3. Output terms in order forever, starting from the beginning.

For options 2 and 3, there must be no numeric characters and at least one non-numeric character between terms; this separator need not be consistent. 1+1=2 would be fine for input 3. Leading and trailing non-numeric characters are allowed.

Here are the first 64 terms. This is OEIS sequence A003602.

1 1 2 1 3 2 4 1 5 3 6 2 7 4 8 1 9 5 10 3 11 6 12 2 13 7 14 4 15 8 16 1 17 9 18 5 19 10 20 3 21 11 22 6 23 12 24 2 25 13 26 7 27 14 28 4 29 15 30 8 31 16 32 1

Your submission can be a program or a function; "input" and "output" are as defined by the community. Standard loopholes are forbidden.

As this is , the shortest solution (in bytes) wins! Good luck, and happy golfing!


Sandboxy Stuff

Am I clear enough on what the sequence is? Any suggestions for rewording?

Is this a duplicate? I've searched for "interleave" and "3602" and found nothing.

Anything else worth mentioning? What thoughts ya gots?

Thanks to Martin Ender for the output formats, taken almost straight from the Kolakoski challenge.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ related (not a dupe though) \$\endgroup\$
    – dzaima
    Feb 13, 2019 at 19:35
  • \$\begingroup\$ You could consider removing the option to output just the n'th term, making answers output a sequence. Otherwise I think it will be shortest in most languages to take n, halve until non-whole, then add 1/2 (ceiling), rather than use the interleaving property which I think is cooler. \$\endgroup\$
    – xnor
    Feb 15, 2019 at 0:46
  • \$\begingroup\$ It looks like n/(n&-n)/2+1 would work for a lot of languages such as Python. \$\endgroup\$
    – xnor
    Feb 15, 2019 at 1:09
  • \$\begingroup\$ @xnor Do you think removing that option will help much? I can't really think of a case where the solution won't be to wrap your expression in a looping construct if the expression was the shortest. I don't think knowing the history of this function is that helpful to finding the future value. \$\endgroup\$ Feb 25, 2019 at 22:09
  • \$\begingroup\$ @FryAmTheEggman You're probably right, answers would mostly just do the expression in some loop. In Haskell I think the interleaving definition wins out (even with option 1 allowed), but maybe that's just Haskell. \$\endgroup\$
    – xnor
    Feb 26, 2019 at 6:22
5
\$\begingroup\$

Evaluate C−− expression

Your goal is to a evaluate an expression in "C−−" (not this one) which uses only the characters are C and -. C is an variable holding an integer whose initial value you're given, and the - symbol is used in many ways including as a decrement operator:

  • C-- decrements the value stored in C, then evaluates to that value.
  • --C evaluates to C, then decrements the value stored in C.
  • -expr negates the value of the expression expr.
  • expr1-expr2 takes the difference of the two expressions.

Unfortunately, the C−− specification doesn't state how expressions are parsed or in what order parts are evaluated, saying these are "implementation dependent". So, it's up to you. For example, --C---C could be interpreted as -(-(C--)-C) or (--C)-(--C) or others, and each --C might be evaluated before or after other parts of the expression.

Input: A string consisting of C and -, and an integer initial value for C.

The string will be parseable in at least one way. You can take the string as a list of characters, but they must be exactly the characters C and -.

Output: A value this expression could evaluate to.

You don't need to worry about issues with overly large values like overflows or loss of precision.

TODO: test cases

\$\endgroup\$
7
  • 2
    \$\begingroup\$ Can --C---C be parsed as -(-(C-(-(-C))))? \$\endgroup\$
    – H.PWiz
    Apr 13, 2019 at 17:44
  • 2
    \$\begingroup\$ @H.PWiz Yes. Unfortunately, it looks like as is --C, can always be interpreted as -(-C), which is golfier but more boring, so I'll probably restrict the parsing or removing the unary negation option. \$\endgroup\$
    – xnor
    Apr 13, 2019 at 17:50
  • 1
    \$\begingroup\$ @xnor Note: -C is the same as (C-C)-C, but there are no parentheses here so nobody can say it can't also be parsed as C-(C-C). An added rule can be "You can't parse --C as -(-C), as a double negation would be meaningless." \$\endgroup\$ Apr 13, 2019 at 17:55
  • \$\begingroup\$ Do you want to include or exclude simple eval implementations? \$\endgroup\$
    – Phil H
    Apr 29, 2019 at 15:22
  • \$\begingroup\$ @PhilH That's a good question. Eval-style solutions seems pretty boring for C-style languages, but I don't know if there's a clean way to even specify what would be disallowed. I don't have time to work on this challenge, so you're welcome to spruce it up and post it if you want. \$\endgroup\$
    – xnor
    Apr 30, 2019 at 1:05
  • \$\begingroup\$ if the C-- in question is not the one linked, could you link the one you're referring to? You mentioned a C-- specification, so I assume there is one \$\endgroup\$
    – Mayube
    May 3, 2019 at 17:29
  • \$\begingroup\$ @Skidsdev I meant it as a thing I'm making up for the challenge. \$\endgroup\$
    – xnor
    May 3, 2019 at 22:02
5
\$\begingroup\$

An Auction in St. Petersburg

Setup

Mysterious packages are up for auction today. These boxes are unique in that their values are not known until they are opened, and when they are opened, their values follow a unique distribution:

probability    value
0.5               $2
0.25              $4
0.125             $8
0.0625           $16
1/(2^n)       $(2^n)

In total, there are 100 such packages up for auction, to be sold sequentially. At the start, each bot arrives with $20 in their wallet, and the goal is to walk away with more money than anyone else.

During each round in the auction, each bot simultaneously submits a bid. The bot with the highest bid (ties broken randomly) must pay the value of the second-highest bid, after which that winning bot receives an amount of money corresponding to the value of the opened package. This money can then be "reinvested" in future rounds of the auction.

The auction day ends once all 100 packages have been sold, or when any bot's wallet value exceeds 2^31.

I/O format

As input, your bot receives the following info:

  • an array of everyone's current wallet amounts
  • the history of past sale prices (the amount paid, not highest amount bid) and winners

As output, your bot returns an integer between 0 and your current wallet amount.

Tournament format

There will be N=large number of game run according to the above format (100 auction rounds each), and the finishing position of the bots will be averaged across games.

\$\endgroup\$
3
  • \$\begingroup\$ Could you explain why it ends when someone reaches 2^31? \$\endgroup\$
    – Miriam
    Apr 16, 2019 at 3:17
  • \$\begingroup\$ @ArtemisFowl It's mostly to prevent players from having to deal with numbers that don't fit into a 32-bit integer. There's only a 1 in 1 billion chance that a particular package will contain enough money to trigger this condition, I just wanted to clarify how I would handle the "infinite expected value" in practice. \$\endgroup\$
    – PhiNotPi
    Apr 16, 2019 at 14:45
  • 1
    \$\begingroup\$ This seems rather win-more. It just takes one big win to be able to guarantee that you can outbid everyone else for the rest of the game. \$\endgroup\$ Apr 20, 2019 at 21:40
5
\$\begingroup\$

Fix my stuttered words

Posted: Fix my stuttered words

\$\endgroup\$
3
  • 1
    \$\begingroup\$ "You can assume that multiple valid sets of repeated stuttered words only happen from left to right, so fixing "op op op o o o open" would result in "op op op open"." Shouldn't this result in "op open" instead? In the first rule you mention "For example "ope" and "open" both can be a stuttered word for "open"." So since the entire word is valid as stutter, I would think "op op op o o o open" becomes "([op op] op) ([o o o] open)" (([this] ... ) being the stutter, and ([...] this) being the word), thus "op open". \$\endgroup\$ Sep 13, 2019 at 9:45
  • 1
    \$\begingroup\$ I think you meant to give the example "... so fixing "op op o o o open" would result in "op op open"." (so one less op) instead? \$\endgroup\$ Sep 13, 2019 at 9:46
  • \$\begingroup\$ @KevinCruijssen: You are absolutely correct, I have updated the question. Thank you for reading with such a high precision. \$\endgroup\$
    – Night2
    Sep 13, 2019 at 12:26
5
\$\begingroup\$

Add the quotes

Background

There's a terrible problem in my console - the quotes never get included inside the arguments! So, when given this argument:

["abc","def","ghi","jkl"]

it says that the argument is like this:

[abc,def,ghi,jkl]

It would be very nice if you can fix this problem!

Body

Add double-quotes (") in order to surround a word (i.e. something that matches [a-z]+).

[[one, two, three],
[one, two, three],
[one, two, three],
[one, two, three]]

Test cases

[abc,def,ghi,jkl] -> ["abc","def","ghi","jkl"]
this is a test    -> "this" "is" "a" "test"
test              -> "test"
this "one" "contains' "quotations -> "this" ""one"" ""contains"' ""quotations"
But This One Is SpeciaL! -> B"ut" T"his" O"ne" I"s" S"pecia"L!

Rules for the input

  • The words are never capitalized.
\$\endgroup\$
7
  • \$\begingroup\$ Suggested test case: one that starts (and/or ends) with a word (i.e. this is a test -> "this" "is" "a" "test"). \$\endgroup\$ Feb 18, 2020 at 16:00
  • \$\begingroup\$ Another suggested test case: a single word without anything else (i.e. test -> "test"). \$\endgroup\$ Feb 18, 2020 at 16:10
  • \$\begingroup\$ An issue here is that since this I/O is strict, many languages can't actually execute it. GolfScript, for example, can't take in letters in any ways that isn't in quotations. \$\endgroup\$
    – Mathgeek
    Feb 19, 2020 at 20:00
  • 2
    \$\begingroup\$ @Mathgeek No you can - do you know that GolfScript takes the whole STDIN as a string? Example. \$\endgroup\$
    – user92069
    Feb 19, 2020 at 22:55
  • 3
    \$\begingroup\$ Perhaps some testcases that already contain quotes \$\endgroup\$
    – Jo King Mod
    Feb 20, 2020 at 5:24
  • \$\begingroup\$ You have to escape them: [\"abc\",\"def\",\"ghi\",\"jkl\"] \$\endgroup\$
    – S.S. Anne
    Feb 23, 2020 at 19:21
  • \$\begingroup\$ Is this challenge not simply a regex substitution; 22 bytes in vim: :%s/\([a-z]\+\)/"\1"/g? \$\endgroup\$ Feb 25, 2020 at 1:11
5
\$\begingroup\$

Is the input Bl lu ur rr ry?

This is based off this challenge.

Given an input string, check whether the string is blurry.

What's a blurry string?

Take a non-blurrified string abc as an example. You repeat every character of this twice:

aabbcc

And then insert spaces at every odd-even index.

a ab bc c

Then, remove the preceding 2 and succeeding 2 extra characters.

ab bc

As an example, all of these strings are blurry (the empty line stands for an empty string):

"a"   ->
"ab"  ->ab
"abc" ->ab bc
"abcd"->ab bc cd
...

Specification

  • The input string consists purely of printable ASCII characters. The only whitespace it will contain is the space character.
  • You don't have to remove extra characters before the check.
  • Your output can consist of any trailing whitespace, as long as it's possible to tell a truthy result from a falsy result.

Test cases

Here is a program I use to check my test cases.

""          -> True
"ab"        -> True
"ab bc"     -> True
"ab bc cd"  -> True
" b bc cd"  -> True
"ab bc c "  -> True
"a   c cd"  -> True

"a"         -> False
"abc"       -> False
"ab  bc  cd"-> False
"ab#bc#cd"  -> False
"abbccd"    -> False
"a ab bc cd"-> False
"a a ab b b"-> False
"ba cb dc"  -> False
"ba bc dc"  -> False
"FFaallssee"-> False
\$\endgroup\$
7
  • \$\begingroup\$ Shouldn't the reverse version be "un-blur the string"? This is more like a decision-problem version of the blur challenge. \$\endgroup\$ Apr 28, 2020 at 3:59
  • \$\begingroup\$ This Challenge to Blurry Vision is like Narcissist to Quine. \$\endgroup\$ Apr 28, 2020 at 4:00
  • \$\begingroup\$ @HighlyRadioactive Uh-blur the string seems too easy, therefore I made it a decision problem. So can you find a duplicate for this? \$\endgroup\$
    – user92069
    Apr 28, 2020 at 4:26
  • \$\begingroup\$ I think you meant "un-blur". No, this is probably not a dupe. \$\endgroup\$ Apr 28, 2020 at 4:28
  • \$\begingroup\$ Possible duplicate: Is it double speak? \$\endgroup\$ Apr 29, 2020 at 0:51
  • \$\begingroup\$ @mathjunkie Not really. This challenge is about consecutive characters separated by consistent spaces. (The spaces may or may not be there.) \$\endgroup\$
    – user92069
    Apr 29, 2020 at 0:54
  • \$\begingroup\$ @petStorm Yeah, I guess it's different enough \$\endgroup\$ Apr 29, 2020 at 0:57
5
\$\begingroup\$

Draw a Peano Curve with Slashes

Given a positive integer N, draw the Nth iteration of the Peano Curve, using only slashes and backslashes (and spaces). The curve will be rotated at a 45 degree angle from its usual depiction. Here's an example for the first 3 iterations:

N = 1

 /\
/ /
 / /
 \/

N = 2

       /\
      / /
     / / /\
    /  \/ /
   / /\  / /\
   \/ /  \/ /
 /\  / /\  / /\
/ / / / / / / /
 / / / / / / / /
 \/ /  \/ /  \/
   / /\  / /\
   \/ /  \/ /
     / /\  /
     \/ / /
       / /
       \/

N = 3

                         /\
                        / /
                       / / /\
                      /  \/ /
                     / /\  / /\
                     \/ /  \/ /
                   /\  / /\  / /\
                  / / / / / / / /
                 / / / / / / / / /\
                /  \/ /  \/ /  \/ /
               / /\  / /\  / /\  / /\
               \/ /  \/ /  \/ /  \/ /
             /\  / /\  / /\  / /\  / /\
            / / / / /  \/ / / / / / / /
           / / / / / /\  / / / / / / / /\
          /  \/ /  \/ /  \/ /  \/ /  \/ /
         / /\  / /\  / /\  / /\  / /\  / /\
         \/ /  \/ /  \/ /  \/ /  \/ /  \/ /
       /\  / /\  / /\  / /\  / /\  / /\  / /\
      / /  \/ / / / / / / /  \/ / / / / / / / 
     / / /\  / / / / / / / /\  / / / / / / / /\
    /  \/ /  \/ /  \/ /  \/ /  \/ /  \/ /  \/ /
   / /\  / /\  / /\  / /\  / /\  / /\  / /\  / /\
   \/ /  \/ /  \/ /  \/ /  \/ /  \/ /  \/ /  \/ /
 /\  / /\  / /\  / /\  / /\  / /\  / /\  / /\  / /\
/ / / / / / / / / / / / / / / / / / / / / / / / / /
 / / / / / / / / / / / / / / / / / / / / / / / / / /
 \/ /  \/ /  \/ /  \/ /  \/ /  \/ /  \/ /  \/ /  \/
   / /\  / /\  / /\  / /\  / /\  / /\  / /\  / /\
   \/ /  \/ /  \/ /  \/ /  \/ /  \/ /  \/ /  \/ /
     / /\  / /\  / /\  / /\  / /\  / /\  / /\  /
     \/ / / / / / / /  \/ / / / / / / /  \/ / /
       / / / / / / / /\  / / / / / / / /\  / / 
       \/ /  \/ /  \/ /  \/ /  \/ /  \/ /  \/  
         / /\  / /\  / /\  / /\  / /\  / /\    
         \/ /  \/ /  \/ /  \/ /  \/ /  \/ /    
           / /\  / /\  / /\  / /\  / /\  /     
           \/ / / / / / / /  \/ / / / / /      
             / / / / / / / /\  / / / / /       
             \/ /  \/ /  \/ /  \/ /  \/        
               / /\  / /\  / /\  / /\          
               \/ /  \/ /  \/ /  \/ /         
                 / /\  / /\  / /\  /
                 \/ / / / / / / / /
                   / / / / / / / /
                   \/ /  \/ /  \/
                     / /\  / /\
                     \/ /  \/ /
                       / /\  /
                       \/ / /
                         / /
                         \/

You may optionally mirror this horizontally, vertically, or both if you choose. Leading spaces are clearly required for this. Trailing spaces are optional. This is code-golf, so the shortest code wins.

\$\endgroup\$
4
  • \$\begingroup\$ Is this what you were looking for? It seems like a direct duplicate. \$\endgroup\$ May 22, 2020 at 5:14
  • \$\begingroup\$ @dingledooper Ah, crud. I did a search and everything and that didn't come up. Maybe because it's too old or something. Oh well - should I just delete this then? Could switch it to the Peano or Gosper Curve, but I don't know if that would be appreciably different. \$\endgroup\$ May 22, 2020 at 13:12
  • \$\begingroup\$ The Gosper curve is a duplicate of codegolf.stackexchange.com/questions/50521/…. The Peano curve doesn't seem to be a duplicate, but I'm not sure whether it's a good idea. \$\endgroup\$ May 22, 2020 at 13:51
  • \$\begingroup\$ @mypronounismonicareinstate I changed it to the Peano Curve, might still be interesting to do. \$\endgroup\$ May 22, 2020 at 14:49
5
\$\begingroup\$

Score a 1 player game of Carcassonne

\$\endgroup\$
9
  • \$\begingroup\$ @dingledooper I'm not sure I understand? \$\endgroup\$ Jun 8, 2020 at 21:35
  • \$\begingroup\$ What I mean is, there are some tiles with roads going in more than one direction, so your scoring method seems to suggest that the same tile can be scored more than once. This is different than the official rules, which state that every tile may only be scored once. \$\endgroup\$ Jun 8, 2020 at 21:53
  • \$\begingroup\$ @dingledooper The rules state, when referring to scoring a road, "each tile of that road grants you 1 point" (emphasis mine). To me, that implies that a tile containing sections of multiple roads can be scored for each of those roads \$\endgroup\$ Jun 8, 2020 at 21:59
  • \$\begingroup\$ Ok, thanks for clearing that up for me. The only reason I asked was because it contrasted from the way I played Carcassonne (or how it is normally played). \$\endgroup\$ Jun 8, 2020 at 22:05
  • \$\begingroup\$ The scoring for incomplete road seems to have at least one incorrect word. Also, how are monasteries represented in your input format? \$\endgroup\$
    – Neil
    Jun 9, 2020 at 9:49
  • \$\begingroup\$ Or indeed villages. \$\endgroup\$
    – Neil
    Jun 9, 2020 at 9:49
  • \$\begingroup\$ @Neil Yes, it does, thanks for spotting that. The monastery tiles are [0, 0, 0, 0, 0] and [0, 0, 1, 0, 0]. As no other tile can be described by these two, I didn’t think it would be necessary to add an additional value representing a monastery. It’s similar with villages; each of the tiles with a village on is unique without having to specify that it has a village on it. For instance, the tile [1, 2, 1, 1, 1] (a rotation of [2, 1, 1, 1, 1]) is guaranteed to have a village on it, so another value is unnecessary. \$\endgroup\$ Jun 9, 2020 at 14:38
  • \$\begingroup\$ Ah, so a tile with at least 3 roads always has a village, and a tile with no features apart from an optional single road always has a monastery? \$\endgroup\$
    – Neil
    Jun 9, 2020 at 16:26
  • \$\begingroup\$ @Neil Yes exactly \$\endgroup\$ Jun 9, 2020 at 16:29
5
\$\begingroup\$

Posted: Scoring Quantum Tic-Tac-Toe

\$\endgroup\$
10
  • \$\begingroup\$ Do cyclic entanglement always have len=3? \$\endgroup\$
    – l4m2
    Jun 17, 2020 at 22:26
  • \$\begingroup\$ @l4m2 A cyclic entanglement can have any length provided it fits on the board. For instance, in the case DE AB DE 1 AH CF CH CG BC 2, there is a loop of length 4 across A, B, C and H. I will make a note of this in the challenge. \$\endgroup\$
    – golf69
    Jun 17, 2020 at 23:49
  • \$\begingroup\$ @l4m2 In that same case, there is also a loop of length 2: DE .. DE \$\endgroup\$
    – golf69
    Jun 17, 2020 at 23:53
  • \$\begingroup\$ I don't think the description directly states that the other player chooses the state in all cases \$\endgroup\$ Jun 18, 2020 at 3:41
  • \$\begingroup\$ Can describing what state was chosen be done in other ways? For example, writing the number of the mark that fills the cell of the last quantum mark placed might be useful instead of the lowest cell alphabetically. \$\endgroup\$ Jun 18, 2020 at 3:44
  • \$\begingroup\$ @fireflame241 Yes, you can choose to describe the state chosen in whatever way is convenient (I added your example to the "rules" section). And thanks for pointing that out about who chooses the state, it's clear now I hope \$\endgroup\$
    – golf69
    Jun 18, 2020 at 4:17
  • \$\begingroup\$ In your illustration, does the position of the quantum marks in a single cell (e.g. in your second picture, the center cell has three marks arranged in a J shape) matter? \$\endgroup\$
    – Trebor
    Jun 18, 2020 at 7:19
  • \$\begingroup\$ @Trebor It does not, noted \$\endgroup\$
    – golf69
    Jun 18, 2020 at 8:00
  • \$\begingroup\$ Is it possible input go on there after result comes? \$\endgroup\$
    – l4m2
    Jun 19, 2020 at 4:33
  • \$\begingroup\$ @l4m2 No: "A game continues until at least one tic-tac-toe is formed or until the board is filled with classical marks." I will add that no more moves can be done after this and that multiple tic-tac-toes can only be formed simultaneously \$\endgroup\$
    – golf69
    Jun 19, 2020 at 5:01
5
\$\begingroup\$

Build an alphabetised polyglot

\$\endgroup\$
5
\$\begingroup\$

My smart phone

Posted here: My smartphone's phonebook

\$\endgroup\$
11
  • 1
    \$\begingroup\$ I like this idea. Just to make sure I've understood the challenge correctly, given a phone number, are we to find all possible strings that match it within a database of strings? \$\endgroup\$
    – lyxal
    Jul 24, 2020 at 11:45
  • \$\begingroup\$ Not quite. Given a database/list of strings, split these by space into words. Return all database entries that contain a word that matches the number. \$\endgroup\$ Jul 24, 2020 at 13:20
  • \$\begingroup\$ Duplicate? \$\endgroup\$ Jul 24, 2020 at 19:27
  • 1
    \$\begingroup\$ This question only asks return entries consisting of one word, while this asks to check for possibly multiple words per entry and then to return the complete entry. Also, this quesition had unusual and harsh restrictions. This aims to be much more simpler than that, making the puzzle more attractive. While the first reason may be splitting hairs, the second one, combined with the puzzle being ~6 yrs old, is a good reason to post this puzzle, imo. \$\endgroup\$ Jul 26, 2020 at 9:23
  • \$\begingroup\$ Add my name in! \$\endgroup\$ Aug 2, 2020 at 13:44
  • 1
    \$\begingroup\$ 'You may only take input in some form of list type, not a string.' Rigid I/O requirements are generally frowned upon. \$\endgroup\$
    – Dingus
    Aug 5, 2020 at 1:34
  • \$\begingroup\$ True, no idea why I insisted on that. I'd rather not have this challenge killed because of a probably useless constraint. \$\endgroup\$ Aug 5, 2020 at 12:51
  • 1
    \$\begingroup\$ You've probably thought of this, but you could get more names from Users. (I'm chuffed to have already made the cut, by the way!) Is there a mistake in the 34 test case - wouldn't HighlyRadioactive appear under 44? \$\endgroup\$
    – Dingus
    Aug 5, 2020 at 13:26
  • \$\begingroup\$ whoops... fixed. I'll add some more people from Users when posting, for the test cases I've just listed some people I see a lot off the top of my head. Also, if you gave some feedback, there's no reason not to put you in there. \$\endgroup\$ Aug 6, 2020 at 6:22
  • \$\begingroup\$ Why is fireflame241 there twice? \$\endgroup\$ Aug 14, 2020 at 15:14
  • \$\begingroup\$ Third-Party 'Chef' is now called petStorm - update. \$\endgroup\$ Aug 20, 2020 at 8:38
5
\$\begingroup\$

Implement the random Fibonacci sequence

\$\endgroup\$
15
  • \$\begingroup\$ Is -3 a valid output for f5? \$\endgroup\$
    – tsh
    Aug 31, 2020 at 6:27
  • \$\begingroup\$ But it is possible that f3 = 2. It is also possible that f4 = -1. \$\endgroup\$
    – tsh
    Sep 1, 2020 at 1:52
  • \$\begingroup\$ @tsh It is possible that f3 = 2 and it is possible that f4 = -1, but the two cannot be satisfied at once. So the challenge requires to record the previous results, I think (and therefore some short approaches like naive recursion can't be used). \$\endgroup\$
    – Bubbler
    Sep 1, 2020 at 2:45
  • 1
    \$\begingroup\$ It would be good to include the correct distributions of the first few entries, like f_3 to f_6, for testing purposes. \$\endgroup\$
    – Zgarb
    Sep 1, 2020 at 7:46
  • \$\begingroup\$ @cairdcoinheringaahing f3 = f2 + f1 = 1 + 1 = 2; f4 = f3 - f2 = (f2 - f1) - f2 = (1 - 1) - 1 = -1; f5 = f4 - f3 = -1 - 2 = -3; If this is not what you want, you may need to update your description to avoid ambiguous. \$\endgroup\$
    – tsh
    Sep 2, 2020 at 1:45
  • \$\begingroup\$ @tsh Took me a while, but I think I understand what you're getting at. Does my latest edit address that? \$\endgroup\$ Sep 8, 2020 at 22:18
  • \$\begingroup\$ @Zgarb Not the best when it comes to distributions, but I've added in a list of possible values for n = 1 ... 6 \$\endgroup\$ Sep 8, 2020 at 22:18
  • \$\begingroup\$ I was going to edit in the distributions, but I don't believe your last case is correct. I don't think there is a way to reach 6 or -3. I decided to edit it in anyway since I wrote it on a scrap of paper, but of course feel free to change it if I am wrong. \$\endgroup\$ Sep 9, 2020 at 21:14
  • \$\begingroup\$ If given n we just output f_n, this voids the requirement that the sequence should remember previous values, doesn't it? \$\endgroup\$
    – Luis Mendo
    Sep 12, 2020 at 16:14
  • \$\begingroup\$ @LuisMendo No, the requirement that the sequence "remembers" previous values is means that tsh's comment above would be an invalid way to construct the sequence. Because \$f_n\$ is constructed from previous terms, those terms cannot change partway through the construction of the sequence. For example, while constructing \$f_6\$, you'd have to first get the values for \$f_{1,\dots5}\$. Then, when constructing \$f_7\$, the values of \$f_{1,\dots5}\$ would be the same as when getting \$f_6\$, whether they were outputted or not. \$\endgroup\$ Sep 12, 2020 at 16:18
  • \$\begingroup\$ Ah, I see, So, "remember" means that in the construction of a given f7, every occurrence of f1 etc should have the same value. \$\endgroup\$
    – Luis Mendo
    Sep 12, 2020 at 16:44
  • \$\begingroup\$ @LuisMendo Yes exactly. If you have a better wording, I'd love to hear it, I'm not too happy with "remember" \$\endgroup\$ Sep 12, 2020 at 17:18
  • \$\begingroup\$ @cairdcoinheringaahing Here's a suggestion: Each random realization of the sequence must use consistent values. For example, if [...] \$\endgroup\$
    – Luis Mendo
    Sep 12, 2020 at 20:22
  • \$\begingroup\$ Also, when you say is chosen at random, you probably mean is chosen at random independently of previous choices? This prevents the code from randomly choosing a sign and using it for all terms, for example \$\endgroup\$
    – Luis Mendo
    Sep 12, 2020 at 20:23
  • 1
    \$\begingroup\$ @LuisMendo That wording is nice, thanks! And yes, each choice should be independent, I'll edit that it \$\endgroup\$ Sep 12, 2020 at 20:59
1
4 5
6
7 8
155

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .