Dataset Viewer
Auto-converted to Parquet Duplicate
instruction
stringlengths
34
53.1k
completion
stringlengths
0
40.2k
__original_row_idx
int64
0
12.5k
conversations
listlengths
2
2
Classic code golf challenge. Write the most obscure Perl program to print "Just another Perl hacker". Here's the Wikipedia article on it. Bonus points if it fits in 3 lines / 70 characters each.
Here's an obscure Perl program that fits within the character limit: ```perl $_=J;print+("us".(t,a,n,d)),"\x20",a,n,("o",th,e,r),"\x20",P,e,r,l," ",h,a,c,k,e,r print$_," ",s,t,y,l,e Just an~.r,p,r,i,n,t ``` However, this approach doesn't exactly meet the character limit and seems less obscure due to its repetitive na...
0
[ { "from": "human", "value": "Classic code golf challenge. Write the most obscure Perl program to print \"Just another Perl hacker\". Here's the Wikipedia article on it. Bonus points if it fits in 3 lines / 70 characters each.\n" }, { "from": "gpt", "value": "Here's an obscure Perl program that f...
Recreate '99 bottles of beers on the wall'. The desired output is this: 99 bottles of beer on the wall, 99 bottles of beer. Take one down and pass it around, 98 bottles of beer on the wall. 98 bottles of beer on the wall, 98 bottles of beer. Take one down and pass it around, 97 bottles of beer on the wall. 97 bott...
### '99 Bottles of Beer' Simulation I've chosen Python as the language to simulate the '99 Bottles of Beer' song. The code utilizes a simple `while` loop to decrement the number of bottles and print the corresponding lyrics. ```python def sing_bottles_of_beer(): bottles = 99 while True: # Handle plura...
1
[ { "from": "human", "value": "Recreate '99 bottles of beers on the wall'. \nThe desired output is this:\n99 bottles of beer on the wall, 99 bottles of beer.\nTake one down and pass it around, 98 bottles of beer on the wall.\n\n98 bottles of beer on the wall, 98 bottles of beer.\nTake one down and pass it a...
Task Read a string of characters and produce the ROT13 of it. All characters besides [a-zA-Z] should be output verbatim. Each letter in the output may be in any case you wish (you do not have to preserve it). ROT13 is a simple substitution cipher where each letter of the alphabet is rotated by 13 places (it doesn't mat...
**ROT13 Implementation in Python** Here's a Python function that implements the ROT13 cipher: ```python def rot13(s): """ Applies the ROT13 cipher to a given string. :param s: The input string. :return: The ROT13 encoded string. """ result = "" for char in s: if char.isalpha()...
2
[ { "from": "human", "value": "Task\nRead a string of characters and produce the ROT13 of it. All characters besides [a-zA-Z] should be output verbatim. Each letter in the output may be in any case you wish (you do not have to preserve it).\nROT13 is a simple substitution cipher where each letter of the alpha...
Hamming numbers are numbers which evenly divide a power of 60. Equivalently, their prime factors are all \$ \le 5 \$. Given a positive integer, print that many Hamming numbers, in order. Rules: Input will be a positive integer \$n \le 1,000,000 \$ Output should be the first \$n\$ terms of https://oeis.org/A051037 Exec...
**Hamming Numbers Generator** The following code generates the first `n` Hamming numbers, where `n` is a given positive integer. ### Python Solution ```python import heapq def hamming_numbers(n): """Generate the first n Hamming numbers.""" h = [1] i2, i3, i5 = 0, 0, 0 for _ in range(n - 1): n...
3
[ { "from": "human", "value": "Hamming numbers are numbers which evenly divide a power of 60. Equivalently, their prime factors are all \\$ \\le 5 \\$.\nGiven a positive integer, print that many Hamming numbers, in order.\nRules:\n\nInput will be a positive integer \\$n \\le 1,000,000 \\$\nOutput should be th...
In chess, a queen can move as far as as the board extends horizontally, vertically, or diagonally. Given a NxN sized chessboard, print out how many possible positions N queens can be placed on the board and not be able to hit each other in 1 move.
**N-Queens Problem Solution** The N-Queens problem is a classic problem in computer science and chess. The goal is to place N queens on an NxN chessboard such that no two queens can attack each other in one move. ### Algorithm To solve this problem, we will use a backtracking algorithm. The idea is to place the quee...
4
[ { "from": "human", "value": "In chess, a queen can move as far as as the board extends horizontally, vertically, or diagonally.\nGiven a NxN sized chessboard, print out how many possible positions N queens can be placed on the board and not be able to hit each other in 1 move.\n" }, { "from": "gpt",...
You are provided with a function Rand5(). This function returns perfectly random (equal distribution) integers between 1 and 5. Provide the function Rand7(), which uses Rand5() to produce perfectly random integers between 1 and 7.
**Rand7() Implementation Using Rand5()** ### Problem Description We are given a function `Rand5()` that generates perfectly random integers between 1 and 5. We need to implement a function `Rand7()` that uses `Rand5()` to produce perfectly random integers between 1 and 7. ### Solution To solve this problem, we can ...
5
[ { "from": "human", "value": "You are provided with a function Rand5(). This function returns perfectly random (equal distribution) integers between 1 and 5.\nProvide the function Rand7(), which uses Rand5() to produce perfectly random integers between 1 and 7.\n" }, { "from": "gpt", "value": "**...
Challenge Write the shortest program or function to calculate the Luhn Algorithm for verifying (credit card) numbers. Luhn algorithm explained From RosettaCode, this algorithm for the purposes of this challenge is specified as such, with the example input of 49927398716: Reverse the digits, make an array: 6, 1, 7, ...
Here is a short Python function that implements the Luhn algorithm: ```python def luhn(n): s = [int(x) for x in str(n)[::-1]] for i in range(1, len(s), 2): s[i] = sum(map(int, str(s[i] * 2))) return sum(s) % 10 == 0 ``` Here's an explanation of the code: 1. We first convert the input number `n` to ...
6
[ { "from": "human", "value": "Challenge\nWrite the shortest program or function to calculate the Luhn Algorithm for verifying (credit card) numbers.\nLuhn algorithm explained\nFrom RosettaCode, this algorithm for the purposes of this challenge is specified as such, with the example input of 49927398716:\nRev...
The Fizz Buzz problem is a very basic problem to solve that is used by some to weed out interviewees that don't know how to program. The problem is: Set N = [0,100] Set F = x in N where x % 3 == 0 Set B = x in N where x % 5 == 0 Set FB = F intersect B For all N: if x in F: print fizz if x in B: print buzz if x ...
```cpp #include <iostream> #include <vector> #include <string> // Define a type trait to check if a number is divisible by 3 template <int N> struct IsDivisibleBy3 : std::false_type {}; template <> struct IsDivisibleBy3<0> : std::true_type {}; template <int N> struct IsDivisibleBy3 : std::integral_constant<bool, (N ...
7
[ { "from": "human", "value": "The Fizz Buzz problem is a very basic problem to solve that is used by some to weed out interviewees that don't know how to program. The problem is:\nSet N = [0,100]\nSet F = x in N where x % 3 == 0\nSet B = x in N where x % 5 == 0\nSet FB = F intersect B\n\nFor all N:\n if x ...
Let's suppose there is a finite unknown number n (n can be very large, does not necessarily fit 64-bits; think of BigInteger in Java or long in Python) and a function cmp(x) that returns true if x <= n. Write a program to determine n in minimum number of calls to cmp.
**Solution** To find the unknown number `n` in the minimum number of calls to `cmp(x)`, we can use a binary search approach. This is because the `cmp(x)` function provides a way to compare a given number `x` with `n`, which is the property required for binary search. Here is a step-by-step algorithm: ### Algorithm ...
8
[ { "from": "human", "value": "Let's suppose there is a finite unknown number n (n can be very large, does not necessarily fit 64-bits; think of BigInteger in Java or long in Python) and a function cmp(x) that returns true if x <= n.\nWrite a program to determine n in minimum number of calls to cmp.\n" }, ...
Given the time in 24 hour format (2359 = 11:59pm) return the angle between the minute and hour hands on a standard clock (on the face plane, so don't just output 0). Angles are to be returned in the unit of your choice, should be the smallest possible, and should be a positive number (negative angle converted to a posi...
### Problem Solution The problem requires calculating the angle between the minute and hour hands on a standard clock given the time in 24-hour format. #### Approach We will use the following steps to calculate the angle: 1. Calculate the angle of the hour hand from the 12 o'clock position. 2. Calculate the angle o...
9
[ { "from": "human", "value": "Given the time in 24 hour format (2359 = 11:59pm) return the angle between the minute and hour hands on a standard clock (on the face plane, so don't just output 0).\nAngles are to be returned in the unit of your choice, should be the smallest possible, and should be a positive ...
The task is to find a way to draw a horizontal line in an array of 16-bit integers. We're assuming a 256x192 pixel array with 16 pixels per word. A line is a contiguous run of set (1) bits. Lines can start in the middle of any word, overlap any other words, and end in any word; they may also start and end in the same ...
Drawing a Horizontal Line in a 16-Bit Integer Array ===================================================== ### Problem Description The task is to draw a horizontal line in a 256x192 pixel array, where each pixel is represented by a 16-bit integer. The line is defined by its start and end x-coordinates (`x0` and `x1`) ...
10
[ { "from": "human", "value": "The task is to find a way to draw a horizontal line in an array of 16-bit integers. \nWe're assuming a 256x192 pixel array with 16 pixels per word. A line is a contiguous run of set (1) bits. Lines can start in the middle of any word, overlap any other words, and end in any word...
From AZSPCS: Suppose you have a deck containing n cards. Each card contains a number from 1 to n, and each number appears on exactly one card. You look at the number on the top card -- let's says it's k -- and then reverse the order of the top k cards. You continue this procedure -- reading the top number and then reve...
**Problem Overview** =============== The problem involves a deck of `n` cards, each containing a unique number from 1 to `n`. The goal is to write an efficient program to calculate the number of reversals required to reach the top card being 1, following a specific procedure: 1. Read the top card number `k`. 2. Rever...
11
[ { "from": "human", "value": "From AZSPCS:\nSuppose you have a deck containing n cards. Each card contains a number from 1 to n, and each number appears on exactly one card. You look at the number on the top card -- let's says it's k -- and then reverse the order of the top k cards. You continue this procedu...
What general tips do you have for golfing in Python? I'm looking for ideas which can be applied to code-golf problems and which are also at least somewhat specific to Python (e.g. "remove comments" is not an answer). Please post one tip per answer.
### Using Built-in Functions for String Concatenation When concatenating multiple strings or variables into a single string, use Python's `join()` method or f-strings, which are more concise than using the `+` operator. ```python # Before s = "Hello, " + "world!" # After (using join) s = "".join(["Hello, ", "world!...
12
[ { "from": "human", "value": "What general tips do you have for golfing in Python? I'm looking for ideas which can be applied to code-golf problems and which are also at least somewhat specific to Python (e.g. \"remove comments\" is not an answer).\nPlease post one tip per answer.\n" }, { "from": "gp...
Using your language of choice, golf a quine. A quine is a non-empty computer program which takes no input and produces a copy of its own source code as its only output. No cheating -- that means that you can't just read the source file and print it. Also, in many languages, an empty file is also a quine: that isn't c...
**Quine in Python** ```python s = 's = %r\nprint(s %% s)' print(s % s) ``` This quine works by storing the source code as a string, then using string formatting to replace the placeholder `%r` with the string itself. **Explanation** 1. `s = 's = %r\nprint(s %% s)'`: Define the string `s` containing the source code. T...
13
[ { "from": "human", "value": "Using your language of choice, golf a quine.\n\nA quine is a non-empty computer program which takes no input and produces a copy of its own source code as its only output.\n\nNo cheating -- that means that you can't just read the source file and print it. Also, in many languages...
Define a function, s, which takes a number and returns the square root. No use of library functions, such as Java's Math.sqrt() or PHP's built in sqrt(), allowed.
### Implementation of Square Root Function #### Overview We can calculate the square root of a number using the Babylonian method for square root, which is an ancient algorithm for computing the square root of a number. This algorithm works by making an initial guess and then iteratively improving that guess until it i...
14
[ { "from": "human", "value": "Define a function, s, which takes a number and returns the square root.\nNo use of library functions, such as Java's Math.sqrt() or PHP's built in sqrt(), allowed.\n" }, { "from": "gpt", "value": "### Implementation of Square Root Function\n#### Overview\nWe can calc...
Write the shortest program in your favourite language to interpret a brainfuck program. The program is read from a file. Input and output are standard input and standard output. Cell size: 8bit unsigned. Overflow is undefined. Array size: 30000 bytes (not circled) Bad commands are not part of the input Comments begin ...
Here is a short Python program that interprets Brainfuck code from a file: ```python def brainfuck(filename): with open(filename, 'r') as f: code = f.read() # Remove comments code = '\n'.join(line.split('#')[0].rstrip() for line in code.split('\n')) # Initialize memory and pointer memory =...
15
[ { "from": "human", "value": "Write the shortest program in your favourite language to interpret a brainfuck program. The program is read from a file. Input and output are standard input and standard output.\n\nCell size: 8bit unsigned. Overflow is undefined.\nArray size: 30000 bytes (not circled)\nBad comma...
The Fibonacci sequence is a sequence of numbers, where every number in the sequence is the sum of the two numbers preceding it. The first two numbers in the sequence are both 1. Here are the first few terms: 1 1 2 3 5 8 13 21 34 55 89 ... Write the shortest code that either: Generates the Fibonacci sequence without...
```python def fibonacci(n): if n <= 0: return "Input should be a positive integer." elif n == 1: return 1 elif n == 2: return 1 else: a, b = 1, 1 for _ in range(2, n): a, b = b, a + b return b # Generate the Fibonacci sequence without end def ...
16
[ { "from": "human", "value": "The Fibonacci sequence is a sequence of numbers, where every number in the sequence is the sum of the two numbers preceding it. The first two numbers in the sequence are both 1. Here are the first few terms:\n1 1 2 3 5 8 13 21 34 55 89 ...\n\n\nWrite the shortest code that eith...
Create the shortest possible obfuscated FizzBuzz implementation. To be considered obfuscated, it should satisfy at least one of the following: Does not contain any of the words "Fizz", "Buzz", or "FizzBuzz" Does not contain the numbers 3, 5, or 15. Use any of the above in a misleading way. Remember: The goal is to be...
Here is a short and obfuscated FizzBuzz implementation in Python: ```python x=0 while x<100: x+=1 print("".join(["" if i%2 else str(x) for i in [7*x%11,5*x%9]])) ``` Explanation: * The numbers 7 and 11 are used as multipliers to generate the Fizz pattern (every 3rd number), and 5 and 9 are used to generate the Buzz...
17
[ { "from": "human", "value": "Create the shortest possible obfuscated FizzBuzz implementation.\nTo be considered obfuscated, it should satisfy at least one of the following:\n\nDoes not contain any of the words \"Fizz\", \"Buzz\", or \"FizzBuzz\"\nDoes not contain the numbers 3, 5, or 15.\nUse any of the abo...
You and your brother have inherited a rectanglar block of land. This block of land's corners are read in from stdio as TLX,TLY,TRX,TRY,BLX,BLY,BRX,BRY (all decimal numbers without units, T=Top, L=Left, B=Bottom, R=Right) Unfortunately the city has seen fit to appropriate a small rectanglar section from inside your you ...
To solve this problem, we need to first find the area of the entire block of land, then subtract the area of the city's appropriation. We'll use this to calculate the total area that needs to be divided between the two brothers. Then, we'll find the midpoint of the diagonal of the block of land and use this as a refere...
18
[ { "from": "human", "value": "You and your brother have inherited a rectanglar block of land. This block of land's corners are read in from stdio as TLX,TLY,TRX,TRY,BLX,BLY,BRX,BRY (all decimal numbers without units, T=Top, L=Left, B=Bottom, R=Right)\nUnfortunately the city has seen fit to appropriate a smal...
Write the shortest code for finding the sum of primes between \$a\$ and \$b\$ (inclusive). Input \$a\$ and \$b\$ can be taken from command line or stdin (space seperated) Assume \$1 \le a \le b \le 10^8\$ Output Just print the sum with a newline character. Bonus Points If the program accepts multiple ranges (print o...
**Prime Sum Solution** ======================= Below is a concise Python solution using the Sieve of Eratosthenes algorithm to find the sum of primes between two given numbers. ```python import sys def sieve_of_eratosthenes(limit): """Generate a sieve of Eratosthenes up to the given limit.""" sieve = [True] ...
19
[ { "from": "human", "value": "Write the shortest code for finding the sum of primes between \\$a\\$ and \\$b\\$ (inclusive).\nInput\n\n\\$a\\$ and \\$b\\$ can be taken from command line or stdin (space seperated)\nAssume \\$1 \\le a \\le b \\le 10^8\\$\n\nOutput\nJust print the sum with a newline character.\...
Write the shortest code that traverses a tree, breadth-first. Input any way you like Output a list of "names" of the nodes in correct order.
### Breadth-First Tree Traversal Here is a Python solution for breadth-first tree traversal: ```python from collections import deque class Node: def __init__(self, name, children=None): self.name = name self.children = children if children is not None else [] def breadth_first_traversal(root): ...
20
[ { "from": "human", "value": "Write the shortest code that traverses a tree, breadth-first.\nInput\nany way you like\nOutput\na list of \"names\" of the nodes in correct order.\n" }, { "from": "gpt", "value": "### Breadth-First Tree Traversal\n\nHere is a Python solution for breadth-first tree tr...
EDIT: In the interest of increasing the complexity, i've added more to the challenge. In mathematics, a vampire number (or true vampire number) is a composite natural number v, with an even number of digits n, that can be factored into two integers x and y each with n/2 digits and not both with trailing ze...
# Vampire and Pseudovampire Number Checker ========================================= This program checks whether a given number is a vampire number, pseudovampire number, or neither. It accepts numbers from the command line or stdin. ### Code ```python from itertools import permutations from math import sqrt def is...
21
[ { "from": "human", "value": "EDIT: In the interest of increasing the complexity, i've added more to the challenge.\n\nIn mathematics, a vampire number (or\n true vampire number) is a composite\n natural number v, with an even number\n of digits n, that can be factored into\n two integers x and y each wi...
What is the shortest function (implemented in C) that will return a random draw from a Poisson distribution (given the mean or λ parameter)? int random_poisson(double lambda);
**Poisson Distribution Random Draw Function in C** ===================================================== The following function uses Knuth's algorithm to generate a random draw from a Poisson distribution. This algorithm is based on the fact that a Poisson distribution can be represented as a sum of exponential distri...
22
[ { "from": "human", "value": "What is the shortest function (implemented in C) that will return a random draw from a Poisson distribution (given the mean or λ parameter)?\nint random_poisson(double lambda);\n\n" }, { "from": "gpt", "value": "**Poisson Distribution Random Draw Function in C**\n===...
Write the shortest program to transform the standard input into Morse code. Characters not in the table should be printed as they are.
**Morse Code Translator** ========================= Below is a Python program that reads from standard input and translates it into Morse code. ```python # Morse Code Translator # Morse Code Mapping morse_code = { 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.', 'G': '--.', 'H': '.......
23
[ { "from": "human", "value": "Write the shortest program to transform the standard input into Morse code. Characters not in the table should be printed as they are.\n\n" }, { "from": "gpt", "value": "**Morse Code Translator**\n=========================\n\nBelow is a Python program that reads from...
What general tips do you have for golfing in C++? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to C++ (e.g. "remove comments" is not an answer). Please post one tip per answer. (Tips that apply to C as well can be found in Tips for golfing in C - but not...
### Using Lambda Functions In C++, lambda functions can be a concise way to define small, one-time-use functions. They can be used to reduce the character count by avoiding the need to declare a separate named function. Example: ```cpp // Long way auto f=[](int x){return x*x;}; // Alternatively auto f=[&](int x){retu...
24
[ { "from": "human", "value": "What general tips do you have for golfing in C++?\nI'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to C++ (e.g. \"remove comments\" is not an answer). Please post one tip per answer.\n(Tips that apply to C as well ca...
The task of this challenge is as following: Write a program that reads a file of reasonable size (let's say <16 MB) from stdin or anywhere else (however you like, but must not be hardcoded), and put's the compressed output onto stdout. The output must be a valid gzip compressed file and if the compressed file runs thro...
### Gzip Compression Program This solution is implemented in Python and uses the LZ77 compression algorithm and Huffman coding, which are the core components of the gzip compression format. ```python import sys # Dictionary for storing codes codes = {} # Deflate algorithm constants WINDOW_SIZE = 32768 def calculat...
25
[ { "from": "human", "value": "The task of this challenge is as following:\nWrite a program that reads a file of reasonable size (let's say <16 MB) from stdin or anywhere else (however you like, but must not be hardcoded), and put's the compressed output onto stdout. The output must be a valid gzip compressed...
Write a program/script that decompresses and passes any compressed data appended at the end of the binary/file as standard input to a program specified as arguments. $ gzip --to-stdout input >> your_solution $ your_solution program args... should have the same effects as: $ program args... < input
To achieve the desired functionality, we will use a Python script that can handle both compression and decompression using the `gzip` library. The script will read the compressed data from the end of its own file, decompress it, and then pass the decompressed data as standard input to the specified program along with a...
26
[ { "from": "human", "value": "Write a program/script that decompresses and passes any compressed data appended at the end of the binary/file as standard input to a program specified as arguments.\n$ gzip --to-stdout input >> your_solution\n$ your_solution program args...\n\nshould have the same effects as:\n...
End of preview. Expand in Data Studio
README.md exists but content is empty.
Downloads last month
14