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

4629 Answers 4629

1
79 80
81
82 83
155
1
\$\begingroup\$

Play 1D minesweeper

\$\endgroup\$
1
  • \$\begingroup\$ What should I output? \$\endgroup\$
    – tsh
    Feb 18, 2022 at 4:00
1
\$\begingroup\$

Page Selector

\$\endgroup\$
8
  • \$\begingroup\$ Personally, I think that the last two examples (from a UI standpoint) should be < 1 2 3 [4] 5 ... 11 > and < 1 ... 6 [7] 8 ... 11 >. Regardless, you should clarify if there is some priority for how numbers should be distributed (e.g. should the center have more numbers?) \$\endgroup\$
    – AnttiP
    Feb 22, 2022 at 19:11
  • \$\begingroup\$ Slightly related codegolf.stackexchange.com/questions/147318/goto-the-nth-page \$\endgroup\$
    – tsh
    Feb 23, 2022 at 2:39
  • \$\begingroup\$ Adding to AnttiP's comment, a worked out example or examples would be nice. \$\endgroup\$
    – pajonk
    Feb 23, 2022 at 6:39
  • \$\begingroup\$ @AnttiP Good point, clarified slightly. \$\endgroup\$
    – Ginger
    Feb 23, 2022 at 15:06
  • 1
    \$\begingroup\$ What should be the output for 11 6 7? < 1 ... 6 [7] ... 11 > or < 1 ... [7] 8 ... 11 > or something else? (this should be clarified in the specs) \$\endgroup\$
    – pajonk
    Feb 23, 2022 at 19:43
  • \$\begingroup\$ @pajonk Clarified. \$\endgroup\$
    – Ginger
    Feb 28, 2022 at 13:48
  • \$\begingroup\$ Ok, what about 11 6 6? (That problem with specs was intended in my previous comment) \$\endgroup\$
    – pajonk
    Feb 28, 2022 at 19:32
  • \$\begingroup\$ @pajonk Added (and updated specs) \$\endgroup\$
    – Ginger
    Mar 3, 2022 at 1:31
1
\$\begingroup\$

Can the Tune be Played?

I changed the question slightly from when it was posted on the Sandbox.

\$\endgroup\$
2
  • \$\begingroup\$ I don't get it. Could you explain in detail eg. the [1, 3, 2] is valid - the notes would be played in the order [3, 1, 2] statement? \$\endgroup\$
    – pajonk
    Feb 27, 2022 at 16:35
  • \$\begingroup\$ I might change the wording because I think I have explained it in a confusing way. A more mathematical explanation is: if you shift back each number n in a list by n spaces, do any of the numbers end up in the same space? If so, the list is invalid. So: shifting 1 back by 1 space, it ends up at position -1; shifting 3 back by 3 spaces, it ends up at position -2, and shifting 2 back by 2 spaces, it ends up at position 0. Since none of these are the same position, the list [1, 3, 2] is valid. \$\endgroup\$
    – Lecdi
    Feb 27, 2022 at 16:46
1
\$\begingroup\$

The Nineteenth Bakery

\$\endgroup\$
1
\$\begingroup\$

Reveal all clues of Black Box

Black Box is a board game, Your task is to reveal all clues.

What is black box

Black box is a board game with hidden atoms, Your task is given input, All atoms, reveal all clues.

I/O

Input

The atoms can be any 1-char that is not newline (Used for separator) Like this.

O....
...O.
..O..
.....
....O

Output

When the code reveals all clues (See Below) the output of The given input above can be:

 HRHH1
H     1
R     H
H     H
2     R
H     H
 H2HRH

or

[["H", "R", "H", "H", "1"], ["1", "H", "H", "R", "H"], ["H", "R", "H", "2", "H"], ["H", "2", "H", "R", "H"]]

Ignore the number, This doesn't mean anything, the number can be non-unique

Task

Hit

Atoms interact with rays in three ways. A direct impact on an atom by a ray is a "hit".

.........
.........
.O.....O.
.........
.........
...O.....H 1
.........
.......O.

Thus, ray 1 fired into the box configuration at left strikes an atom directly, generating a "hit", designated by an "H". A ray which hits an atom does not emerge from the box.

Deflection

The interaction resulting from a ray which does not actually hit an atom, but which passes directly to one side of the ball is called a "deflection". The angle of deflection for this ray/atom interaction is 90 degrees. Ray 2 is deflected by the atom at left, exiting the box as shown.

    2
.........
.........
.O.....O.
.........
.........2
...O.....
.........
.......O.

Reflection

The final type of interaction of a ray with an atom is a "reflection", designated by an "R". This occurs in two circumstances. If an atom is at the edge of the grid, any ray which is aimed into the grid directly beside it causes a reflection.

.........
.........
.O.....O.
.........
.........
...O.....
.........
.......O.
      RHR
      354

Rays 3 and 4 at left would each generate a reflection, due to the atom at the edge. Ray 5 would be a hit on the atom.

Double deflection

The other circumstance leading to a reflection is when two deflections cancel out. In the grid at left, ray 6 results in a reflection due to its interaction with the atoms in the grid.

   6

   R
.....
..O.O
.....
.....
.....

Detour

Rays that don't result in hits or reflections are called "detours". These may be single or multiple deflections, or misses. A detour has an entry and an exit location, while hits and reflections only have an entry location for a hit, and a single entry/exit location for a reflection.

  8   8  
.........
.........
.O.....O.
.........9
.........
...O.....
.........9
.......O.

Of course, more complex situations result when these behaviors interact. Ray 8 results in two deflections, as does ray 9.

Some rays travel a twisted course, like ray 1 at left.

 .........
 .........
 .O.....O.
 .........
1.........
 ...O.....
 .........
 .......O.
     1

Notice that this complex set of five deflections above looks exactly like a single deflection, as shown by ray 2 at left. Things are not always as simple as they seem within a black box.

 .........
 .........
 .........
 .....O...
1.........
 .........
 .........
 .........
     1

Reflections and hits can be more complex, too. Ray 2 gets deflected by the first atom, reflected by the next two atoms and again deflected by the original atom, yielding a reflection.

.O...
.....R 2
.....
.O.O.
.....

Ray 3 below gets deflected by the first atom, then by the second atom, and then hits the third atom, yielding a hit.

   ...O.
   O....
   .....
3 H.....
   ...O.

Test cases

O....
...O.
..O..
.....
....O
->
 HRHH1
H     1
R     H
H     H
2     R
H     H
 H2HRH

O
->
 H
H H
 H

OOO
O O
OOO
->
 HHH
H   H
H   H
H   H
 HHH

..O..
O...O
..O..
->
 HRHRH
R     R
H     H
R     R
 HRHRH

...
...
...
->
 123
4   4
5   5
6   6
 123

....
O...
...O
....
->
 H1HH
R    1
H    R
R    H
2    R
 HH2H

Meta:

  • Any feedback?
\$\endgroup\$
4
  • \$\begingroup\$ I think I would first explain what Black Box is and then specify the task and I/O. Right now the Input and Output sections are hard to grasp without context and one needs to go back to those sections after reading specs. \$\endgroup\$
    – pajonk
    Feb 25, 2022 at 19:29
  • 1
    \$\begingroup\$ I'd use a capital O in your demos, rather than a #. It makes it visually clearer, imo. \$\endgroup\$
    – wizzwizz4
    Feb 26, 2022 at 1:29
  • \$\begingroup\$ @wizzwizz4 Ok, I will do later \$\endgroup\$
    – Fmbalbuena
    Mar 9, 2022 at 21:01
  • \$\begingroup\$ @wizzwizz4 It's a bit ugly because the clues are letters, I will rollback if someone doesn't like this. \$\endgroup\$
    – Fmbalbuena
    Mar 9, 2022 at 21:09
1
\$\begingroup\$

Negative of an ASCII photo

\$\endgroup\$
3
  • \$\begingroup\$ interesting challenge although this might be a dupe im not sure \$\endgroup\$
    – DialFrost
    Mar 14, 2022 at 2:38
  • \$\begingroup\$ @DialFrost I had a browse and couldn't find one, but obviously if it is I won't post \$\endgroup\$
    – jezza_99
    Mar 14, 2022 at 2:45
  • 2
    \$\begingroup\$ ah ic although u might wanna state that the negative photo has to be of same "size" as the original one as suggested by ur ["#",""] and ["","#"] but i dont know if it rly matters \$\endgroup\$
    – DialFrost
    Mar 14, 2022 at 2:47
1
\$\begingroup\$

Overlay the Ukrainian Flag

A pseudo-sequel to this challenge.

The Challenge

As of the posting of this challenge, my PFP has a blue-and-yellow overlay in support of Ukraine. Your task is to, given the path to an input image, overlay a similar image on top.

Here is the flag: Ukrainian Flag

  • The flag is divided horizontally across the middle into two equally sized stripes.
  • Stripe colors: (0, 87, 183) or #0057B7 (blue, top) and (255, 215, 0) or #FFD700 (yellow, bottom).
  • You must, given the path to an image as input, overlay two semi-transparent rectangles of those colors onto it. The rectangles must each take up half the vertical area of the image, and be as wide as the image.
  • The rectangles must have 70% opacity.
  • Colors must be exact if possible, otherwise use the closest available blue and yellow.
  • The image can be saved to a file or piped raw to STDOUT in any common image file format, or it can be displayed in a window.
  • If you're saving to a file, you may either overwrite the old image or create a new one.
  • Built-in flag images, flag-drawing libraries, or horrendously upscaling the Ukrainian flag emoji are prohibited.
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Can we choose the input file format or you have some restrictions on that? Do we have to input via a path to file or other means of inputting graphics is fine (raw bytes, 3D array of RGB values etc.)? May we assume the image has even height (in pixels or in some convenient unit)? \$\endgroup\$
    – pajonk
    Mar 22, 2022 at 11:41
1
\$\begingroup\$

Overlay Two Images

This challenge is as simple as it gets: Just take two images as input (in any reasonable format) and overlay the first atop the second.

  • The overlay must have 70% opacity (if possible, if not use the closest available analog), or in other words, multiply the overlay's pixel values by 0.3 and the base by 0.7.
  • The overlay must be centered over the base image.
  • If the overlay is bigger, it must be cropped to fit within the base.
  • If the base image is bigger, the overlay does not need to be enlarged.
\$\endgroup\$
3
  • \$\begingroup\$ Maybe you could explain wat 70% opacity actually means. \$\endgroup\$
    – math scat
    Mar 28, 2022 at 5:52
  • 1
    \$\begingroup\$ @mathcat 70% of the pixels are transparent or have an alpha value of 70? Then I guess if you take a sample on the final image the colour should be top_red*0.3+bottom_red*0.7, top_blue*0.3+bottom_blue*0.7,top_green*0.3+bottom_green*0.7... or however colours work \$\endgroup\$ Mar 28, 2022 at 12:13
  • \$\begingroup\$ I think adding this would be important \$\endgroup\$
    – math scat
    Mar 28, 2022 at 18:42
1
\$\begingroup\$

Optimally break a string into substrings

\$\endgroup\$
5
  • \$\begingroup\$ What do we need to optimize? if it's "smallest number of pieces", then [potato][c][a][r][rot] also scores 5. \$\endgroup\$
    – Nitrodon
    Apr 1, 2022 at 17:45
  • \$\begingroup\$ @Nitrodon Yeah it's smallest number of pieces, forgot to add that. \$\endgroup\$ Apr 1, 2022 at 18:19
  • \$\begingroup\$ Could we please have a few more testcases? \$\endgroup\$
    – ophact
    Apr 2, 2022 at 6:26
  • \$\begingroup\$ @ophact Sure, I'll add some \$\endgroup\$ Apr 2, 2022 at 22:54
  • 2
    \$\begingroup\$ Title suggestion: "substrings" -> "words" (as the substrings in question, other than single characters, have to come from a dictionary of sorts) \$\endgroup\$ Apr 8, 2022 at 1:34
1
\$\begingroup\$

Simplified Piet Interpreter

\$\endgroup\$
6
  • \$\begingroup\$ 1) Long challenges are fine, especially when the challenge is complex like this. Complete specification is much more important. 5) From quick skimming, the example looks mostly correct to me, except: at the pointer instruction near the start, right + clockwise 9 times is down, not up. An easy fix is to change the initial number to, say, 7. 6) My choice would be to add something like "Reference: official Piet specification (link)" at the bottom. \$\endgroup\$
    – Bubbler
    Apr 7, 2022 at 6:21
  • \$\begingroup\$ @Bubbler Thanks for the feedback! For the example, I think I just mixed up clockwise and counterclockwise lol. \$\endgroup\$
    – Aiden Chow
    Apr 7, 2022 at 7:00
  • \$\begingroup\$ Seems interesting!! I'd say my one complaint is that this does miss the other big feature of Piet, which is that instructions are defined by the difference between the blocks you're travelling between, not the specific value of either of them, although that is probably acceptable for simplicity reasons. Also should provide some specifics on how the final output is to be formatted. \$\endgroup\$
    – des54321
    Apr 7, 2022 at 17:41
  • \$\begingroup\$ @des54321 Yeah, I decided to remove the hue and lightness aspect of Piet because I thought that it would add unnecessary complexity to the challenge, though if you think that it would make the challenge more interesting, I could add that aspect to the challenge as well. \$\endgroup\$
    – Aiden Chow
    Apr 7, 2022 at 18:24
  • \$\begingroup\$ @AidenChow the hue/lightness feature would definitely make an interesting (if possibly harder) challenge, and the instruction set you have now would fit nicely in a 3 hue 3 lightness cycle, but its certainly ultimately your decision whether to include it \$\endgroup\$
    – des54321
    Apr 7, 2022 at 18:26
  • \$\begingroup\$ @des54321 I will think about it. The problem is that if I were to change the specs now, I would have to completely rewrite my worked example. \$\endgroup\$
    – Aiden Chow
    Apr 7, 2022 at 18:37
1
\$\begingroup\$

Draw the Norwegian flag

\$\endgroup\$
4
  • \$\begingroup\$ link is wrong. correct link \$\endgroup\$
    – naffetS
    Apr 8, 2022 at 21:04
  • \$\begingroup\$ @Steffan oh wow, never knew stack exchange was that stubborn \$\endgroup\$ Apr 8, 2022 at 21:15
  • \$\begingroup\$ I don't think it would be considered a duplicate as there are already tons of flag challenges \$\endgroup\$
    – naffetS
    Apr 11, 2022 at 0:03
  • \$\begingroup\$ Perhaps it's not considered a duplicate, but I'd hold the opinion that printing the Norwegian flag isn't particularly interesting, only because many flag challenges already do exist (or maybe this is the unpopular opinion and many people enjoy having these flag challenges). \$\endgroup\$ Apr 15, 2022 at 3:48
1
\$\begingroup\$

Draw New Posts' Profile!

\$\endgroup\$
6
  • \$\begingroup\$ @DLosc Whoops, fixed. \$\endgroup\$
    – Ginger
    Apr 19, 2022 at 21:20
  • 1
    \$\begingroup\$ Also possibly loosen the input from boolean to any two distinct values? Although that might make it too easy if someone specifies the inputs to be their lang's color values for blue/black \$\endgroup\$
    – des54321
    Apr 19, 2022 at 21:24
  • \$\begingroup\$ Also presumably this is code-golf \$\endgroup\$
    – des54321
    Apr 19, 2022 at 21:26
  • \$\begingroup\$ @des54321 of course \$\endgroup\$
    – Ginger
    Apr 19, 2022 at 21:27
  • 1
    \$\begingroup\$ As this image is 48x48 and binary in nature, I suggest embedding in the post a binary matrix reflecting on/off pixels of the picture. \$\endgroup\$
    – pajonk
    Apr 20, 2022 at 9:31
  • 1
    \$\begingroup\$ Note that NP and SP's profile pictures are slightly "blurred", they are not perfect 48×48 pixel images. So you might want to clarify any non-white pixels are the same blue/black. \$\endgroup\$ Apr 27, 2022 at 1:25
1
\$\begingroup\$

Create a program that may or may not determine if a list is in this sequence

Your task is to write a program which returns another program which has a nonzero chance of correctly determining if an inputted list is in a sequence described below. This can either be done through randomly generating a program, which will occasionally do the correct task, or through hardcoding a program that always works and returning that.

The sequence

The sequence you'll need to identify starts with the following items:

well the sequence I was gonna use turns out to have a closed form formula so frick

\$\endgroup\$
2
  • \$\begingroup\$ What was the idea here? Was it going to be something uncomputable? Could this still be salvaged with a new sequence? Also, what stops you from submitting like print(random(0,1)) or whatever \$\endgroup\$ May 16, 2022 at 20:06
  • 1
    \$\begingroup\$ @thejonymyster Basically, you try to write a program that will randomly generate (sometimes) executable code. So there's a very small chance you'll get something which actually does solve the problem (checking if something's in a sequence, in this case), which is longer than the program which randomly generated it. \$\endgroup\$ May 16, 2022 at 20:18
1
\$\begingroup\$

Binary Palindromes

Take an input of a positive integer and determine if the binary representation of that number is palindromic. Except for 0 (which is not in the scope of this problem), all binary palindromes are odd, so you do not have to worry about leading/trailing zeroes.

OEIS Sequence: https://oeis.org/A006995

This is code-golf, so shortest answer in bytes wins.

Test Cases

1 -> True (1)
4 -> False (100)
85 -> True (1010101)
131 -> False (10000011)
\$\endgroup\$
4
  • \$\begingroup\$ I personally think this question is trivial, but I may be wrong so I didn't downvote it. \$\endgroup\$ May 10, 2022 at 14:23
  • \$\begingroup\$ take this as an answer, str(bin(n))[2:][::-1]==str(bin(n))[2:] \$\endgroup\$ May 10, 2022 at 14:26
  • \$\begingroup\$ Very closely related: codegolf.stackexchange.com/questions/216929/based-palindromes. May even be considered duplicate by some? Better wait for input from other users and/or ask in chat. \$\endgroup\$
    – pajonk
    May 10, 2022 at 19:30
  • \$\begingroup\$ Didn't see that one because I searched specifically for binary. Will ask in chat. \$\endgroup\$
    – Romanp
    May 10, 2022 at 19:32
1
\$\begingroup\$

King of the Hill: Greed Control

What is Greed Control

Greed control is a multiplayer round-based game in which in every round, a player bet a number inside a specific range, say 1~100. Say 2 people betted 100, then they'd both get 100/2 which is 50 points in the game. Basically, the players that choose the same number split the scores evenly (no rounding) and the total of their net score gain is the number they chose.

After a set number of rounds, players compare scores. The highest score generally wins the game overall.

Your challenge

Build a bot in python, specifically, python 3 , that when given input, discussed in the section input, they give the number as an integer they bet as output. It must be afunction.

Input

information, points, round_number, sum_of_all, user_num. information is a dictionary with integer keys in which each key's value is the number of bots that chose that key's number last round. points is the list of integers you can choose from. round_number is the round number. sum_of_all is like information, except it is the summation of all rounds. user_num is the number of players playing.

We guarantee that

We guarantee that points stay the same every round and is strictly increasing, all integers. However, it may not be consecutive.

Controller function


from random import choice

def random_better(information, points, round_number, sum_of_all, user_num):
    return choice(points)

def greedy_better(information, points, round_number, sum_of_all, user_num):
    return points[-1]

def calculator(information, points, round_number, sum_of_all, user_num):
    return sorted(points, key=lambda x:x/(information[x]+0.000000000001))[0]

def smarty(information, points, round_number, sum_of_all, user_num):
    return sorted(points, key=lambda x:x/(information[x]+0.000000000001))[choice(range(1,40))]

users = [random_better, greedy_better, calculator, smarty] * 10

user_names = [user.__name__.replace('_',' ') for user in users]
rounds = 1000
points = sorted(list(range(1,101)))
user_num = len(users)
bots = [user for user in users]
scores = [0.0 for user in users]
information = {i:0 for i in points}
sum_of_all = {i:0 for i in points}
print('\n'*100)

for round_number in range(rounds):
    choices = [bot(information, points, round_number, sum_of_all, user_num) for bot in bots]
    information = {score:choices.count(score) for score in points}
    scores = [scores[index] + choices[index]/information[choices[index]] for index in range(user_num)]

    print(f'\n\n\nRound #{round_number+1} Reports: ')

    print('\nBots Report: ')
    print('\n'.join([f'{user_names[index].ljust(20)} '
                     f'chose {choices[index]} '
                     f'and got {choices[index]/information[choices[index]]} additional points, '
                     f'making it now have {scores[index]} points!!! ' for index in range(user_num)]))

    print('\nDistribution Report: ')
    print('\n'.join([f'The number of bots who chose {num} is: {information[num]}!!! ' for num in points]))

    print('\nLeaderboard Report: ')
    sorted_list = sorted(range(user_num), key=lambda x: -scores[x])

    print(f'\n{user_names[sorted_list[0]].ljust(20)} with {scores[sorted_list[0]]} points, '
          f'\n{user_names[sorted_list[1]].ljust(20)} with {scores[sorted_list[1]]} points, and'
          f'\n{user_names[sorted_list[2]].ljust(20)} with {scores[sorted_list[2]]} points!!! ')

    sum_of_all = {i:sum_of_all[i]+information[i] for i in points}

print('\n\n\nFinal Report Card: \n')
print('\n'.join([f'{user_names[sorted_list[index]].ljust(20)}: '
                 f'{scores[sorted_list[index]]} points. ' for index in range(user_num)]))

The first part is imports. You may only depend on default python built-ins or the python standard library. Or if you manage to hide it and I out of coincidence have the package...

The second part is your functions. Naming your function bot_name_with_underscores is encouraged.

The third part are game parameters. The number of rounds, the accessible outputs, etc.

The fourth part is running the program!!!

An example output is as follows:


Round #1 Reports: 

Bots Report: 
random better        chose 3 and got 1.5 additional points, making it now have 1.5 points!!! 
greedy better        chose 10 and got 10.0 additional points, making it now have 10.0 points!!! 
calculator           chose 1 and got 1.0 additional points, making it now have 1.0 points!!! 
smarty               chose 3 and got 1.5 additional points, making it now have 1.5 points!!! 

Distribution Report: 
The number of bots who chose 1 is: 1!!! 
The number of bots who chose 2 is: 0!!! 
The number of bots who chose 3 is: 2!!! 
The number of bots who chose 4 is: 0!!! 
The number of bots who chose 5 is: 0!!! 
The number of bots who chose 6 is: 0!!! 
The number of bots who chose 7 is: 0!!! 
The number of bots who chose 8 is: 0!!! 
The number of bots who chose 9 is: 0!!! 
The number of bots who chose 10 is: 1!!! 

Leaderboard Report: 

greedy better        with 10.0 points, 
random better        with 1.5 points, and
smarty               with 1.5 points!!! 



Round #2 Reports: 

Bots Report: 
random better        chose 7 and got 7.0 additional points, making it now have 8.5 points!!! 
greedy better        chose 10 and got 5.0 additional points, making it now have 15.0 points!!! 
calculator           chose 1 and got 1.0 additional points, making it now have 2.0 points!!! 
smarty               chose 10 and got 5.0 additional points, making it now have 6.5 points!!! 

Distribution Report: 
The number of bots who chose 1 is: 1!!! 
The number of bots who chose 2 is: 0!!! 
The number of bots who chose 3 is: 0!!! 
The number of bots who chose 4 is: 0!!! 
The number of bots who chose 5 is: 0!!! 
The number of bots who chose 6 is: 0!!! 
The number of bots who chose 7 is: 1!!! 
The number of bots who chose 8 is: 0!!! 
The number of bots who chose 9 is: 0!!! 
The number of bots who chose 10 is: 2!!! 

Leaderboard Report: 

greedy better        with 15.0 points, 
random better        with 8.5 points, and
smarty               with 6.5 points!!! 



Round #3 Reports: 

Bots Report: 
random better        chose 4 and got 4.0 additional points, making it now have 12.5 points!!! 
greedy better        chose 10 and got 10.0 additional points, making it now have 25.0 points!!! 
calculator           chose 1 and got 1.0 additional points, making it now have 3.0 points!!! 
smarty               chose 2 and got 2.0 additional points, making it now have 8.5 points!!! 

Distribution Report: 
The number of bots who chose 1 is: 1!!! 
The number of bots who chose 2 is: 1!!! 
The number of bots who chose 3 is: 0!!! 
The number of bots who chose 4 is: 1!!! 
The number of bots who chose 5 is: 0!!! 
The number of bots who chose 6 is: 0!!! 
The number of bots who chose 7 is: 0!!! 
The number of bots who chose 8 is: 0!!! 
The number of bots who chose 9 is: 0!!! 
The number of bots who chose 10 is: 1!!! 

Leaderboard Report: 

greedy better        with 25.0 points, 
random better        with 12.5 points, and
smarty               with 8.5 points!!! 



Round #4 Reports: 

Bots Report: 
random better        chose 6 and got 6.0 additional points, making it now have 18.5 points!!! 
greedy better        chose 10 and got 10.0 additional points, making it now have 35.0 points!!! 
calculator           chose 1 and got 1.0 additional points, making it now have 4.0 points!!! 
smarty               chose 2 and got 2.0 additional points, making it now have 10.5 points!!! 

Distribution Report: 
The number of bots who chose 1 is: 1!!! 
The number of bots who chose 2 is: 1!!! 
The number of bots who chose 3 is: 0!!! 
The number of bots who chose 4 is: 0!!! 
The number of bots who chose 5 is: 0!!! 
The number of bots who chose 6 is: 1!!! 
The number of bots who chose 7 is: 0!!! 
The number of bots who chose 8 is: 0!!! 
The number of bots who chose 9 is: 0!!! 
The number of bots who chose 10 is: 1!!! 

Leaderboard Report: 

greedy better        with 35.0 points, 
random better        with 18.5 points, and
smarty               with 10.5 points!!! 



Round #5 Reports: 

Bots Report: 
random better        chose 2 and got 2.0 additional points, making it now have 20.5 points!!! 
greedy better        chose 10 and got 10.0 additional points, making it now have 45.0 points!!! 
calculator           chose 1 and got 1.0 additional points, making it now have 5.0 points!!! 
smarty               chose 6 and got 6.0 additional points, making it now have 16.5 points!!! 

Distribution Report: 
The number of bots who chose 1 is: 1!!! 
The number of bots who chose 2 is: 1!!! 
The number of bots who chose 3 is: 0!!! 
The number of bots who chose 4 is: 0!!! 
The number of bots who chose 5 is: 0!!! 
The number of bots who chose 6 is: 1!!! 
The number of bots who chose 7 is: 0!!! 
The number of bots who chose 8 is: 0!!! 
The number of bots who chose 9 is: 0!!! 
The number of bots who chose 10 is: 1!!! 

Leaderboard Report: 

greedy better        with 45.0 points, 
random better        with 20.5 points, and
smarty               with 16.5 points!!! 



Round #6 Reports: 

Bots Report: 
random better        chose 3 and got 3.0 additional points, making it now have 23.5 points!!! 
greedy better        chose 10 and got 10.0 additional points, making it now have 55.0 points!!! 
calculator           chose 1 and got 1.0 additional points, making it now have 6.0 points!!! 
smarty               chose 2 and got 2.0 additional points, making it now have 18.5 points!!! 

Distribution Report: 
The number of bots who chose 1 is: 1!!! 
The number of bots who chose 2 is: 1!!! 
The number of bots who chose 3 is: 1!!! 
The number of bots who chose 4 is: 0!!! 
The number of bots who chose 5 is: 0!!! 
The number of bots who chose 6 is: 0!!! 
The number of bots who chose 7 is: 0!!! 
The number of bots who chose 8 is: 0!!! 
The number of bots who chose 9 is: 0!!! 
The number of bots who chose 10 is: 1!!! 

Leaderboard Report: 

greedy better        with 55.0 points, 
random better        with 23.5 points, and
smarty               with 18.5 points!!! 



Round #7 Reports: 

Bots Report: 
random better        chose 8 and got 8.0 additional points, making it now have 31.5 points!!! 
greedy better        chose 10 and got 10.0 additional points, making it now have 65.0 points!!! 
calculator           chose 1 and got 1.0 additional points, making it now have 7.0 points!!! 
smarty               chose 3 and got 3.0 additional points, making it now have 21.5 points!!! 

Distribution Report: 
The number of bots who chose 1 is: 1!!! 
The number of bots who chose 2 is: 0!!! 
The number of bots who chose 3 is: 1!!! 
The number of bots who chose 4 is: 0!!! 
The number of bots who chose 5 is: 0!!! 
The number of bots who chose 6 is: 0!!! 
The number of bots who chose 7 is: 0!!! 
The number of bots who chose 8 is: 1!!! 
The number of bots who chose 9 is: 0!!! 
The number of bots who chose 10 is: 1!!! 

Leaderboard Report: 

greedy better        with 65.0 points, 
random better        with 31.5 points, and
smarty               with 21.5 points!!! 



Round #8 Reports: 

Bots Report: 
random better        chose 1 and got 0.5 additional points, making it now have 32.0 points!!! 
greedy better        chose 10 and got 5.0 additional points, making it now have 70.0 points!!! 
calculator           chose 1 and got 0.5 additional points, making it now have 7.5 points!!! 
smarty               chose 10 and got 5.0 additional points, making it now have 26.5 points!!! 

Distribution Report: 
The number of bots who chose 1 is: 2!!! 
The number of bots who chose 2 is: 0!!! 
The number of bots who chose 3 is: 0!!! 
The number of bots who chose 4 is: 0!!! 
The number of bots who chose 5 is: 0!!! 
The number of bots who chose 6 is: 0!!! 
The number of bots who chose 7 is: 0!!! 
The number of bots who chose 8 is: 0!!! 
The number of bots who chose 9 is: 0!!! 
The number of bots who chose 10 is: 2!!! 

Leaderboard Report: 

greedy better        with 70.0 points, 
random better        with 32.0 points, and
smarty               with 26.5 points!!! 



Round #9 Reports: 

Bots Report: 
random better        chose 3 and got 3.0 additional points, making it now have 35.0 points!!! 
greedy better        chose 10 and got 5.0 additional points, making it now have 75.0 points!!! 
calculator           chose 1 and got 1.0 additional points, making it now have 8.5 points!!! 
smarty               chose 10 and got 5.0 additional points, making it now have 31.5 points!!! 

Distribution Report: 
The number of bots who chose 1 is: 1!!! 
The number of bots who chose 2 is: 0!!! 
The number of bots who chose 3 is: 1!!! 
The number of bots who chose 4 is: 0!!! 
The number of bots who chose 5 is: 0!!! 
The number of bots who chose 6 is: 0!!! 
The number of bots who chose 7 is: 0!!! 
The number of bots who chose 8 is: 0!!! 
The number of bots who chose 9 is: 0!!! 
The number of bots who chose 10 is: 2!!! 

Leaderboard Report: 

greedy better        with 75.0 points, 
random better        with 35.0 points, and
smarty               with 31.5 points!!! 



Round #10 Reports: 

Bots Report: 
random better        chose 2 and got 2.0 additional points, making it now have 37.0 points!!! 
greedy better        chose 10 and got 5.0 additional points, making it now have 80.0 points!!! 
calculator           chose 1 and got 1.0 additional points, making it now have 9.5 points!!! 
smarty               chose 10 and got 5.0 additional points, making it now have 36.5 points!!! 

Distribution Report: 
The number of bots who chose 1 is: 1!!! 
The number of bots who chose 2 is: 1!!! 
The number of bots who chose 3 is: 0!!! 
The number of bots who chose 4 is: 0!!! 
The number of bots who chose 5 is: 0!!! 
The number of bots who chose 6 is: 0!!! 
The number of bots who chose 7 is: 0!!! 
The number of bots who chose 8 is: 0!!! 
The number of bots who chose 9 is: 0!!! 
The number of bots who chose 10 is: 2!!! 

Leaderboard Report: 

greedy better        with 80.0 points, 
random better        with 37.0 points, and
smarty               with 36.5 points!!! 



Final Report Card: 

greedy better       : 80.0 points. 
random better       : 37.0 points. 
smarty              : 36.5 points. 
calculator          : 9.5 points. 

If any bot runs into an error,

it fails. So CHECK IT!!! Make it FOOLPROOF!!!

Winner

This is .

\$\endgroup\$
3
  • 2
    \$\begingroup\$ I feel like you need a bit better explanation of how Greed Control scoring works, but otherwise, it looks pretty good. \$\endgroup\$
    – Romanp
    May 12, 2022 at 14:52
  • 4
    \$\begingroup\$ Also, please reduce the number of exclamation points and bold chars in your post. IMO it makes it look childish and something like the stereotypical plz send teh codez \$\endgroup\$
    – Seggan
    May 12, 2022 at 15:17
  • \$\begingroup\$ I'm childish... I'm 13... Hahaha \$\endgroup\$ May 13, 2022 at 1:20
1
\$\begingroup\$

Cross of Numbers

Given a number n as input, output an ASCII cross composed of numbers counting up to that length. Counting starts at 0 in the center, and numbers with more than one digit simply have their digits printed in order in the correct direction.

Example for n=12:

               2
               1
               1
               1
               0
               1
               9
               8
               7
               6
               5
               4
               3
               2
               1
2111019876543210123456789101112
               1
               2
               3
               4
               5
               6
               7
               8
               9
               1
               0
               1
               1
               1
               2

Meta

Is this appropriate for ?

\$\endgroup\$
1
\$\begingroup\$

Categorising categories

A category is an abstract mathematical object whose actual meaning is not really important here. A category can be viewed as a directed graph with the following restrictions:

  • every node in the graph has at least one edge from and to itself
  • for every pair of edges which share an intermediate node (like \$ a \rightarrow b \$ and \$ b \rightarrow c \$), there must exist an edge which is their composition, like \$ a \rightarrow c \$

More mathematically, where \$ E(a, b) \$ means "there exists an edge from \$ a \$ to \$ b \$": $$ \forall a \in G. E(a, a) \\ \forall a, b, c \in G. E(a, b) \vee E(b, c) \implies E(a,c) $$

Your task is to determine whether a given directed graph satisfies the above restrictions.

I/O

You may take input using any reasonable representation of a graph, such as:

  • an edge list
  • an adjacency list
  • an adjacency matrix
  • a built-in directed graph type

You may assume there are no duplicate edges (but note that the edge \$ a \rightarrow b \$ is not the same as \$ b \rightarrow a \$)

Nodes will be represented by positive integers starting from \$ 1 \$, i.e., elements of the range \$ [1, n] \$, where \$ n \$ is the number of nodes. You may optionally also take \$ n \$ as a second input.

You should output one of two distinct values of your choosing, which correspond to true and false results.

Rules


Meta

  • Should I remove the first requirement ("every node in the graph has at least one edge from and to itself")? I think it doesn't add much to the challenge, but then it's less directly related to category theory and I can't use the funny title
  • Is this clear enough?
  • Any other feedback?
\$\endgroup\$
1
  • \$\begingroup\$ I know it's not really relevant to the challenge itself, but "A category can be viewed as a directed graph with the following restrictions:", this might be technically true, but it certainly is misleading. It's sort of like saying that a group can be viewed as a set. It's missing sort of the most important property. \$\endgroup\$
    – Wheat Wizard Mod
    May 17, 2022 at 12:50
1
\$\begingroup\$

Make an Apartment Building

\$\endgroup\$
1
\$\begingroup\$

LOOOOONG TEEEEEXT

\$\endgroup\$
2
  • \$\begingroup\$ How do you determine what a vowel is? I assume the question cares about letters not phonemes, so is it aeiou? You test cases imply that y and w, which are sometimes vowel letters, are not treated as vowel however is this a universal? \$\endgroup\$
    – Wheat Wizard Mod
    May 12, 2022 at 8:52
  • \$\begingroup\$ Oops, guess I should specify that... \$\endgroup\$ May 12, 2022 at 9:16
1
\$\begingroup\$

Solve the Zany car game

\$\endgroup\$
1
  • \$\begingroup\$ I think letting people choose input format may lead to more interesting solutions. \$\endgroup\$
    – pajonk
    May 16, 2022 at 7:48
1
\$\begingroup\$

Bold Words for Profit

The challenge is simple. Given a webpage in html, make the first syllable of each word bold.

Example:

<body>Accessibility is a right, not a paid service.</body>

-->

<body><b>A</b>ccessibility <b>i</b>s <b>a</b> <b>ri</b>ght, 
<b>no</b>t <b>a</b> <b>pai</b>d <b>se</b>rvice.</body>

We will use the heuristic that the first syllable of a word w is the prefix of w that goes up to and including the last vowel ([aeiouy]). If there are no vowels, bold the first letter ([a-z]).

Examples (first syllable):

disability -> di
isnt -> is
for -> fo
profit -> pro

You may bold text in any way you see fit -- the <b> tag used above is just for example. Whitespace in the output HTML need not be preserved. Since we want to make the most profit off of this tool as possible, and each character typed is a hundred dollars paid to our software developers, the code must be as short as possible.

Happy golfing!

Bionic Reading

\$\endgroup\$
1
  • 1
    \$\begingroup\$ It's worth taking into account that this technique is (unfortunately) patented by bionic-reading.com. I'm not sure what the legal implications of posting a question about it on StackExchange are, and I'd be especially worried about the implementations posted as answers. \$\endgroup\$
    – pxeger
    May 27, 2022 at 20:04
1
\$\begingroup\$

Is it a good chord?

\$\endgroup\$
1
\$\begingroup\$

Sell cinema tickets

Or in other words, print out a numbered grid from bottom-middle to top-middle.

Challenge

Most people think that, in a cinema, the back seats and the middle ones are the best.
You'll be receiving the number of seats across and vertically in the theater, your job is to sell the tickets in prioritized order, starting from the best seats to the worst.
The seats will be numbered from top-left to bottom-right.

Example

In: 3, 4

Seats:
1  2  3
4  5  6
7  8  9
10 11 12

Out: 11, 12, 10, 8, 9, 7, 5, 6, 4, 2, 3, 1

The order of the outer seats doesn't matter, as long as the distance to the middle seat in the row is the same.

Another Possible output: 11, 10, 12, 8, 7, 9, 5, 4, 6, 2, 1, 3
12: Priority 7
11: Priority 8
10: Priority 7
09: Priority 5
08: Priority 6
07: Priority 5
06: Priority 3
05: Priority 4
04: Priority 3
03: Priority 1
02: Priority 2
01: Priority 1

Rules

  • The number of seats across and vertically (in any order) should be received as the input
  • Output should be the seats numbered, sorted by their priority
  • Default Loopholes apply
\$\endgroup\$
1
\$\begingroup\$

But is it a pretty place?

\$\endgroup\$
1
\$\begingroup\$

Extract the contained powers of two!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ The title is somewhat misleading, because the output is a list of matches, not "how many" (which would be the length of the list). \$\endgroup\$
    – pxeger
    May 30, 2022 at 15:54
  • 1
    \$\begingroup\$ I also suggest a test case which has a power of two as a subsequnce, but not a contiguous substring. e.g. 106 -> [1], not [1, 16]. Also, can the output may be in any order? \$\endgroup\$
    – pxeger
    May 30, 2022 at 15:56
  • \$\begingroup\$ Thanks!! I have added your suggested test case, and improved the title. \$\endgroup\$
    – Qaziquza
    May 30, 2022 at 17:48
1
\$\begingroup\$

Two In One: Guess That Language - Cops

Two In One: Guess That Language - Robbers

\$\endgroup\$
3
  • \$\begingroup\$ I suggest restrictions on languages (what is a valid language and what are different languages) to be taken from this challenge - this seems to be a nice and working consensus. \$\endgroup\$
    – pajonk
    May 30, 2022 at 10:01
  • \$\begingroup\$ Done @pajonk .. \$\endgroup\$ May 31, 2022 at 0:26
  • \$\begingroup\$ I've edited this down to a stub now that it's been posted to save space \$\endgroup\$ Jun 4, 2022 at 2:47
1
\$\begingroup\$

Sort my Cups

\$\endgroup\$
1
\$\begingroup\$

Parenthetical names

Your challenge is to build a program that puts parentheses around the 'title' part of names. For example, Sonic the Hedgehog is converted to Sonic (the Hedgehog).

Rules:

  • Inputs will only contain words (alphabetical characters and spaces).
  • Your program must be case insensitive.
  • Leading and trailing thes are ignored.
  • With two thes in a row, only the first is parenthesized.
  • Nested thes mean nested parentheses.
  • Remaining thes are skipped unless surrounded by spaces. For example, the the in their would not be parenthesized.

Test cases:

'the pig'               => 'the pig'
'pig the'               => 'pig the'
'pig hog'               => 'pig hog'
'pig the hog'           => 'pig (the hog)'
'the the pig'           => 'the the pig'
'pig the hog pig        => 'pig (the hog pig)'
'the pig the hog the'   => 'the pig (the hog the)'
'pig the hog the pig'   => 'pig (the hog (the pig))'
'the hog the pig'       => 'the hog (the pig)'
'pig the the hog'       => 'pig (the the hog)'
'hog their pig the hog' => 'hog their pig (the hog)'

Inspired by this xkcd comic: https://xkcd.com/2252/

This is code-golf, so least number of bytes wins.

\$\endgroup\$
1
\$\begingroup\$

Parse Specification Data Structure Diagrams

Many protocol specifications illustrate the structure of data packets or file formats with ASCII tables. This helps to visualize the layout of the data in memory, a file, or how bits are transmitted "on the wire".

For example RFC 1035 section 4.1.1 describes the header of a DNS query with the following diagram:

                                1  1  1  1  1  1
  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                      ID                       |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    QDCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    ANCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    NSCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    ARCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

RFC-793 Tranmission Control Protocol contains the following diagram of a TCP packet header:

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          Source Port          |       Destination Port        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                        Sequence Number                        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Acknowledgment Number                      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Data |           |U|A|P|R|S|F|                               |
| Offset| Reserved  |R|C|S|S|Y|I|            Window             |
|       |           |G|K|H|T|N|N|                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|           Checksum            |         Urgent Pointer        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Options                    |    Padding    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                             data                              |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  • Wouldn't it be useful if we could automate the parsing of data buffers into the various fields based on these diagrams?

Challenge

Your job is to write code that can interpret these diagrams to parse a data buffer into the data fields.

  • Inputs will be a multi-line ASCII string following the format shown above, either an array of lines or a single string with linefeeds, and a data packet as an array of bytes.
  • The input diagram includes the two initial rows that number the bit offsets (note that zeros in the first row may or may not be present).
  • Bytes are in Big Endian ("network") order
  • Bit offsets are from MSb to LSb
  • The number of bits per row will be a power of 2 and at least one octet (byte). I.e. 8, 16, 32, 64.
  • For this challenge a single field will not span bytes from one row to the next.
  • Some cases will require multiple lines of input for a single row of bits to accommodate the field names. Field names are concatenated from the data in the field column over multiple rows. For example see the fourth row in the RFC-793 TCP packet header. Note when field name values butt up to the edge of the field, no space is inserted between the characters on consecutive rows, otherwise a single space is between the field name values on each row. See "Data Offset" vs. "ACK".
  • A single bit position could take two characters as in the ASCII diagram for the TCP packet header, or three as in the DNS query header. I.e. +-+ vs +--+
  • Output can be in decimal or hexadecimal with a '0x' prefix.

Example Input and Output

Example 1 - DNS Query Header

Input:

String diagram = """
                                1  1  1  1  1  1
  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                      ID                       |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    QDCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    ANCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    NSCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                    ARCOUNT                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
""";
byte [] data = { 0x00, 0x00, 0x84, 0x80, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00 };

Output (in hexadecimal):

ID = 0x0
QR = 0x1
OpCode = 0x0
AA = 0x1
TC = 0x0
RD = 0x0
RA = 0x1
Z = 0x0
RCODE = 0x0
QDCOUNT = 0x0
ANCOUNT = 0x6
NSCOUNT = 0x0
ARCOUNT = 0x0

Example 2 - TCP Packet Header:

Input:

String diagram = """
 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          Source Port          |       Destination Port        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                        Sequence Number                        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Acknowledgment Number                      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Data |           |U|A|P|R|S|F|                               |
| Offset| Reserved  |R|C|S|S|Y|I|            Window             |
|       |           |G|K|H|T|N|N|                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|           Checksum            |         Urgent Pointer        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Options                    |    Padding    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                             data                              |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
""";
byte [] data = { 0xc0, 0x01, 0xa4, 0x74,
                 0x00, 0x00, 0x00, 0x02,
                 0x00, 0x00, 0x00, 0x01,
                 0x60, 0x10, 0x00, 0x04,
                 0x39, 0x3a, 0x00, 0x00,
                 0x00, 0x00, 0x00, 0x00,
                 0x01, 0x02, 0x03, 0x04 };

Output (in decimal):

Source Port = 49253
Destination Port = 42100
Sequence Number = 2
Acknowledgment Number = 1
Data Offset = 24
Reserved = 0
URG = 0
ACK = 1
PSH = 0
PST = 0
SYN = 0
FIN = 0
Window = 4
Checksum = 14650
Urgent Pointer = 0
Options = 0
Padding = 0
data = 16909060

Understanding the tables:

+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|                      ID                       |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

The above table section indicates that the 'ID' field is composed of the first 16-bits of the data packet (Big Endian), The 'QR' field is the high bit of the next byte, 'OpCode' comes from the next four highest bits of the same byte containing 'QR', The lower three bits of that byte contain the single-bit values for 'AA', 'TC', and 'RD'. 'RA' is the highest bit of the next byte, followed by 3-bits for 'Z'. The lower four bits of that byte hold the value for 'RCODE'.

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Data |           |U|A|P|R|S|F|                               |
| Offset| Reserved  |R|C|S|S|Y|I|            Window             |
|       |           |G|K|H|T|N|N|                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

The above table has several fields whose field names could not fit within the table using only a single line for that row of bits. There are nine fields in these 32 bits. The upper four bits of the first byte is 'Data Offset', followed by six bits (the remaining four of the first byte, and the upper two bits of the next byte) for 'Reserved' (i.e. unused). The lower six bits of the second byte are for the six one-bit fields 'URG', 'ACK', 'PSH', 'RST, 'SYN', and finally 'FIN'. 'Window' is a 16-bit value made from the following two bytes (Big Endian).

i.e. pseudo code to unpack the data values for the above line:

Data_Offset = (data[0] & 0xf0) >> 4;
Reserved = ((data[0] & 0x0f) << 2) + ((data[1] & 0xc0) >> 6);
URG = (data[1] & 0x20) >> 5;
ACK = (data[1] & 0x10) >> 4;
PSH = (data[1] & 0x08) >> 3;
RST = (data[1] & 0x04) >> 2;
SYN = (data[1] & 0x02) >> 1;
FIN = data[1] & 0x01;
Window = (data[2] << 8) + data[3];

The field names themselves come from concatenating the characters within each "box". Where spaces are trimmed from both ends, and all other white space is collapsed to a single space. You may also use an underscore, a hyphen, or a dot ('_', '-', '.') instead of a space to separate words of a field name. You must use some separator between words, conversion to CamelCase or straight concatenation of words is not allowed. You must not alter the case of the field names as it may be significant.

The output must be printed or in the form of a single string that clearly pairs the field names and values. Consider readability for humans. I.e. a list of field names, followed by a list of values is NOT acceptable as this is too inconvenient for a human to find the right value for each field. You may use one field per line and '=' as I have shown above, a two column table, a text format similar to JSON, CSV, etc.. There must be distinct delimiters between the name and the value and between different field-value pairings, e.g. I used "=" and newlines. If you use a space between field name and field value, you must not use a space between words in the field name unless they are quoted or otherwise escaped so the output is unambiguous.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Could you please add a worked out example (maybe a simpler one)? I'm not familiar with the structure of the tables and I don't quite get what's going on. Also, please specify a winning criteria (code-golf I presume?) and consider relaxing I/O requirements (i.e. add "or any reasonable equivalent" that would include e.g. array of characters for input or binary for output). \$\endgroup\$
    – pajonk
    Jun 10, 2022 at 6:13
  • \$\begingroup\$ @pajonk I've editing for clarity. How is it now? \$\endgroup\$
    – swpalmer
    Jun 10, 2022 at 15:48
  • \$\begingroup\$ Yes, looks better now. What types of output are acceptable apart from the format you show? - I suggest anything that will unambiguously identify the names and values (including but not limited to: named list, names and values space/comma separated, two lists (one with names, second with values), dataframe with two columns) \$\endgroup\$
    – pajonk
    Jun 10, 2022 at 18:11
  • \$\begingroup\$ @pajonk I've clarified the output requirements. \$\endgroup\$
    – swpalmer
    Jun 10, 2022 at 22:01
1
\$\begingroup\$

Flatten from the inside out

Say we have a ragged list

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

And we want to flatten it by a layer, decreasing its depth by 1. We could flatten it from the outside:

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

Or, we could flatten it from the inside.

To do this, take all subarrays with maximal depth, and disperse each into its parent array. For example, with the array [[1, 2], [3, [4, 5]]], the [4, 5] has maximal depth, so the array becomes [[1, 2], [3, 4, 5]].

Your challenge is to, given a list, flatten it from the inside by one level. The list will only contain nonnegative integers, and will not contain empty lists.

This is , shortest wins!

Testcases

[[1, 2], [3, [4, 5]]] -> [[1, 2], [3, 4, 5]]
[[6, 3, [1, 3, 4]], 4, [2, 3, 9, [5, 6]]] -> [[6, 3, 1, 3, 4], 4, [2, 3, 9, 5, 6]]
[[[3]], [4, [5]]] -> [[3], [4, 5]]
[[[[[[[[1]]]]]]]] -> [[[[[[[1]]]]]]]
\$\endgroup\$
0
1
79 80
81
82 83
155

You must log in to answer this question.

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