How should I implement the classic bubble sort algorithm? I'd particularly like to see a C++ implementation, but any language (even pseudocode) would be helpful.
P.S. Not a homework.
What are your favourite short, mind-blowing snippets in functional languages?
My two favourite ones are (Haskell):
powerset = filterM (const [True, False])
foldl f v xs = foldr (\x g a -> g (f a x)) id xs v -- from Hutton's tutorial
(I tagged the question as Haskell, but examples in all languages - including non-FP ones - are welcome as…
Here's my (code golf) challenge:
Take two arrays of bytes and determine if the second array is a substring of the first. If it is, output the index at which the contents of the second array appear in the first. If you do not find the second array in the first, then output -1.
Example Input:
{ 63, 101, 245, 215, 0 } { 245, 215 }
Expected…
Code Golf: Rotating Maze
Make a program that takes in a file consisting of a maze. The maze has walls given by '#'. The maze must include a single ball, given by a 'o' and any number of holes given by a '@'. The maze file can either be entered via command line or read in as a line through standard input. Please specify which in your…
The challenge
The shortest code by character count that will output the numeric equivalent of an Excel column string.
For example, the A column is 1, B is 2, so on and so forth. Once you hit Z, the next column becomes AA, then AB and so on.
Test cases:
A: 1
B: 2
AD: 30
ABC: 731
WTF: 16074
ROFL: 326676
Code count…
Guidelines for code-golf on SO
We've all seen phone numbers that are put into words: 1-800-BUY-MORE, etc.
What is the shortest amount of code you can write that will produce all the possible combinations of words for a 7 digit US phone number.
Input will be a seven digit integer (or string, if that is simpler), and…
A digital root, according to Wikipedia, is "the number obtained by adding all the digits, then adding the digits of that number, and then continuing until a single-digit number is reached."
For instance, the digital root of 99 is 9, because 9 + 9 = 18 and 1 + 8 = 9.
My Haskell solution -- and I'm no expert -- is as…
The challenge
The shortest code by character count to output a spider web with rings equal to user's input.
A spider web is started by reconstructing the center ring:
\_|_/
_/ \_
\___/
/ | \
Then adding rings equal to the amount entered by the user. A ring is another level of a "spider circles" made…
Generate the Fibonacci sequence in the fewest amount of characters possible. Any language is OK, except for one that you define with one operator, f, which prints the Fibonacci numbers.
Starting point: 25 characters in Haskell:
f=0:1:zipWith(+)f(tail f)
This is not really a programming question
Question :
Sometimes you have to learn a new language consider this situation for example :
you have been programming in C# for some years and then one day you need to code in java. Now being a programmer you already know the programming concepts its just the syntax you…
I want to see all the different ways you can come up with, for a factorial subroutine, or program. The hope is that anyone can come here and see if they might want to learn a new language.
Ideas:
Procedural
Functional
Object Oriented
One liners
Obfuscated
Oddball
Bad Code
Polyglot
Basically I want to see an…
Print all 12 verses of the popular holiday song.
By 12 verses I mean the repetition of each line as is sung in the song, ie
Verse One:
On the first day of Christmas my true love gave to me
a partridge in a pear tree.
Verse Two
On the second day of Christmas my true love gave to me
two turtle doves
and a…
The point of this question is to collect a list of examples of hashtable implementations using arrays in different languages. It would also be nice if someone could throw in a pretty detailed overview of how they work, and what is happening with each example.
Edit:
Why not just use the built in hash…
What is the least amount of code you can write to create, sort (ascending), and print a list of 100 random positive integers? By least amount of code I mean characters contained in the entire source file, so get to minifying.
I'm interested in seeing the answers using any and all programming…
Blast from the past. This is one of the puzzles from my early days:
Can you write a method (a function) which when called outputs its own source - literally including all the quotes, indentations, etc?
No cheating - no reading from a file
The challenge
The shortest code by character count that will output the numeric solution, given a number and a valid string pattern, using the Ghost Leg method.
Examples
Input: 3,
"| | | | | | | |
|-| |=| | | | |
|-| | |-| |=| |
| |-| |-| | |-|"
Output: 2
Input: 2,
"| | |=| |
|-| |-|…
Perl and PHP do this with backticks. For example:
$output = `ls`;
This code returns a directory listing into the variable $output. A similar function, system("ls"), returns the operating system return code for the given command. I'm talking about a variant that returns whatever the…
Given a list of lists like this
[[1,2,3],[a,b,c,d],[x,y]]
generate all permutations of the flattened list, [1,2,3,a,b,c,d,x,y], such that the elements of each sublist occur in the same order.
For example, this one is okay
[a,1,b,2,x,y,3,c,d]
but this one is not
[y,1,2,3,a,b,c,x,d]…
Today I read the following problem:
Use the digits 2, 0, 1, 1 and the
operations +, -, x, ÷, sqrt, ^ , !,
(), combinations, and permutations to
write equations for the counting
numbers 1 through 100.
All four digits must be used in each expression.
Only the digits…
Description:
The input are multiple lines (terminated by a newline) which describe a 'field'. There are 'numbers' scattered across this field:
the numbers always start at 1
they follow the ordering of the natural numbers: every 'next number' is incremented with 1
every number is…
Here's a pretty simple idea, in this pastebin I've posted some pair of numbers. These represent Nodes of a directed graph. The input to stdin will be of the form, (they'll be numbers, i'll be using an example here)
c d
q r
a b
b c
d e
p q
so x y means x is connected to y (not…
I am usually a Java programmer, and have used textmate for that almost exclusively, but lately I started using C++ with it. but when i use even the most basic programs and incorporate the cin keyword, and run the program, I dont get an oppurtunity to put in anything during…
I am transforming data from this legacy table:
MovTime (IdMov INT, IdPerson NVARCHAR(20), Date1 datetime, Type1 nvarchar(30) )
IdMov IdPerson Date1 Type
----------- -------------------- -----------------------…
I'm developing a 2D tile based game and I have a few questions regarding it.
First I would like to know if this is the correct way to structure my Tile class:
namespace TileGame.Engine {
public enum TileType {
Air,
Stone
}…
I have chrooted sftp setup as below.
# Package generated configuration file
# See the sshd_config(5) manpage for details
# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will…