Cyber-dojo.org是编程操练者的乐园。下面是这个网站上的43个编程操练题目,供编程操练爱好者参考。
100 doors
100 doors in a row are all initially closed. You make 100 passes by the doors. The first time through, you visit every door and toggle the door (if the door is closed, you open it; if it is open, you close it). The second time you only visit every 2nd door (door #2, #4, #6, ...). The third time, every 3rd door (door #3, #6, #9, ...), etc, until you only visit the 100th door.
Question: What state are the doors in after the last pass? Which are open, which are closed?
[Source http://rosettacode.org]
Anagrams
Write a program to generate all potential anagrams of an input string.
For example, the potential anagrams of "biro" are
biro bior brio broi boir bori
ibro ibor irbo irob iobr iorb
rbio rboi ribo riob roib robi
obir obri oibr oirb orbi orib
[Please refer to TDD the Anagrams Kata]
Balanced Parentheses
Write a program to determine if the the parentheses (), the brackets [], and the braces {}, in a string are balanced.
For example:
{{)(}} is not balanced because ) comes before (
({)} is not balanced because ) is not balanced between {}
and similarly the { is not balanced between ()
[({})] is balanced
{}([]) is balanced
{()}[[{}]] is balanced
Bowling Game
Write a program to score a game of Ten-Pin Bowling.
Input: string (described below) representing a bowling game
Ouput: integer score
- The scoring rules:
- Each game, or "line" of bowling, includes ten turns, or "frames" for the bowler.
- In each frame, the bowler gets up to two tries to knock down all ten pins.
- If the first ball in a frame knocks down all ten pins, this is called a "strike". The frame is over. The score for the frame is ten plus the total of the pins knocked down in the next two balls.
- If the second ball in a frame knocks down all ten pins, this is called a "spare". The frame is over. The score for the frame is ten plus the number of pins knocked down in the next ball.
- If, after both balls, there is still at least one of the ten pins standing the score for that frame is simply the total number of pins knocked down in those two balls.
- If you get a spare in the last (10th) frame you get one more bonus ball. If you get a strike in the last (10th) frame you get two more bonus balls. These bonus throws are taken as part of the same turn. If a bonus ball knocks down all the pins, the process does not repeat. The bonus balls are only used to calculate the score of the final frame.
- The game score is the total of all frame scores.
- Examples:
X indicates a strike
/ indicates a spare
- indicates a miss
| indicates a frame boundary
The characters after the || indicate bonus balls- X|X|X|X|X|X|X|X|X|X||XX
Ten strikes on the first ball of all ten frames.
Two bonus balls, both strikes.
Score for each frame == 10 + score for next two balls == 10 + 10 + 10 == 30
Total score == 10 frames x 30 == 300 - 9-|9-|9-|9-|9-|9-|9-|9-|9-|9-||
Nine pins hit on the first ball of all ten frames.
Second ball of each frame misses last remaining pin.
No bonus balls.
Score for each frame == 9
Total score == 10 frames x 9 == 90 - 5/|5/|5/|5/|5/|5/|5/|5/|5/|5/||5
Five pins on the first ball of all ten frames.
Second ball of each frame hits all five remaining pins, a spare.
One bonus ball, hits five pins.
Score for each frame == 10 + score for next one ball == 10 + 5 == 15
Total score == 10 frames x 15 == 150 - X|7/|9-|X|-8|8/|-6|X|X|X||81
Total score == 167
- X|X|X|X|X|X|X|X|X|X||XX
Calc Stats
Your task is to process a sequence of integer numbers
to determine the following statistics:
o) minimum value
o) maximum value
o) number of elements in the sequence
o) average value
For example: [6, 9, 15, -2, 92, 11]
o) minimum value = -2
o) maximum value = 92
o) number of elements in the sequence = 6
o) average value = 21.833333
Combined Number
Write a function accepting a list of non negative integers,
and returning their largest possible combined number
as a string. For example
given [50, 2, 1, 9] it returns "95021" (9 + 50 + 2 + 1)
given [5, 50, 56] it returns "56550" (56 + 5 + 50)
given 420, 42, 423] it returns "42423420" (42 + 423 + 420)
Source [https://blog.svpino.com/about]
Count Coins
There are four types of common coins in US currency:
quarters (25 cents)
dimes (10 cents)
nickels (5 cents)
pennies (1 cent)
There are 6 ways to make change for 15 cents:
A dime and a nickel;
A dime and 5 pennies;
3 nickels;
2 nickels and 5 pennies;
A nickel and 10 pennies;
15 pennies.
How many ways are there to make change for a dollar
using these common coins? (1 dollar = 100 cents).
[Source http://rosettacode.org]
Diversion
Think of binary numbers: sequences of 0's and 1's.
How many n-digit binary numbers are there that
don't have two adjacent 1 bits?
For example, for three-digit numbers, Five of the
possible eight combinations meet the criteria:
000, 001, 010, 011, 100, 101, 110, 111.
What is the number for sequences of length 4, 5, 10, n?
Having worked out the pattern, there's a second part to
the question: can you prove why that relationship exists?
(Source http://codekata.pragprog.com,
Code Kata Fifteen -- A Diversion)
Eight Queens
Place eight chess queens on an 8x8 chessboard so that
no two queens threaten each other. Thus, a solution
requires that no two queens share the same row,
column, or diagonal.
[source: http://en.wikipedia.org/wiki/Eight_queens_puzzle]
Fizz Buzz
Write a program that prints the numbers from 1 to 100.
But for multiples of three print "Fizz" instead of the
number and for the multiples of five print "Buzz". For
numbers which are multiples of both three and five
print "FizzBuzz".
Sample output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
... etc up to 100
Fizz Buzz Plus
Write a program that prints the numbers from 1 to 100, but...
numbers that are exact multiples of 3,
or that contain 3, must print a string containing "Fizz"
For example 9 -> "...Fizz..."
For example 31 -> "...Fizz..."
numbers that are exact multiples of 5,
or that contain 5, must print a string containing "Buzz"
For example 10 -> "...Buzz..."
For example 51 -> "...Buzz..."
Friday 13th
Write a program to show that the 13th day of the month
falls more often on a Friday than any other day of the
week. The 1st of January 1973 was a Monday.
You should aim at producing the clearest possible
program, not the fastest.
[source: BCPL the language and its compiler
by Martin Richards and Colin Whitby-Strevens]
Game of Life
Your task is to write a program to calculate the next
generation of Conway's game of life, given any starting
position.
You start with a two dimensional grid of cells, where
each cell is either alive or dead. The grid is finite,
and no life can exist off the edges. When calculating
the next generation of the grid, follow these four rules:
- Any live cell with fewer than two live neighbours
dies, as if caused by underpopulation. - Any live cell with more than three live neighbours
dies, as if by overcrowding. - Any live cell with two or three live neighbours
lives on to the next generation. - Any dead cell with exactly three live neighbours
becomes a live cell.
Examples: * indicates live cell, . indicates dead cell
Example input: (4 x 8 grid)
4 8
........
.......
...*...
........
Example output:
4 8
........
......
......
........
Gray Code
Create functions to encode a number to and decode
a number from Gray code. Display the normal binary
representations, Gray code representations, and
decoded Gray code values for all 5-bit binary
numbers (0-31 inclusive, leading 0's not necessary).
There are many possible Gray codes. The following
encodes what is called "binary reflected Gray code."
Encoding (MSB is bit 0, b is binary, g is Gray code):
if b[i-1] = 1
g[i] = not b[i]
else
g[i] = b[i]
Decoding (MSB is bit 0, b is binary, g is Gray code):
b[0] = g[0]
for other bits:
b[i] = g[i] xor b[i-1]
[Source http://rosettacode.org]
Haiku Review
Haiku is an ancient form of Japanese poetry. A haiku
is a three-line poem with seventeen syllables, where
the first line must contain five syllables, the second
line must contain seven syllables, and the third line
must contain five syllables. The lines do not have to
rhyme. Here is an example, where slashes separate the
lines:
Computer programs/The bugs try to eat my code/I must
not let them.
You must write a program that will review a haiku and
check that each line contains the correct number of
syllables.
Input
The input contains one or more lines, each of which
contains a single haiku. A haiku will contain at least
three words, and words will be separated by either a
single space or a slash ('/'). Slashes also separate
the three lines of a haiku, so each haiku will contain
exactly two slashes. (The three lines of the haiku will
be contained within one physical line of the file.)
A haiku will contain only lowercase letters ('a'-'z'),
forward slashes ('/'), and spaces, and will be no more
than 200 characters long (not counting the end-of-line
characters).
Each haiku is guaranteed to contain three lines, and
each line will contain at least one word. Your job is
to determine whether each line has the correct number
of syllables (5/7/5). For the purposes of this problem,
every contiguous sequence of one or more vowels counts
as one syllable, where the vowels are
a, e, i, o, u, and y. Every word will contain at least
one syllable.
(Note that this method of counting syllables does not
always agree with English conventions. In the second
example below, your program must consider the word
'code' to have two syllables because the 'o' and the
'e' are not consecutive. However, in English the 'e'
is silent and so 'code' actually has only one syllable.)
Output
For each haiku, output a comma-separated single line
that contains the number of syllables in each haiku,
together with the letter Y if it is a haiku, or N if
it is not a haiku (see below).
Sample Input
happy purple frog/eating bugs in the marshes/get indigestion
computer programs/the bugs try to eat my code/i will not let them
Sample Output
5,7,5,Yes
5,8,5,No
[Source: http://uva.onlinejudge.org/]
Harry Potter
To try and encourage more sales of the 5 different Harry
Potter books they sell, a bookshop has decided to offer
discounts of multiple-book purchases.
One copy of any of the five books costs 8 EUR.
If, however, you buy two different books, you get a 5%
discount on those two books.
If you buy 3 different books, you get a 10% discount.
If you buy 4 different books, you get a 20% discount.
If you go the whole hog, and buy all 5, you get a huge 25%
discount.
Note that if you buy, say, four books, of which 3 are
different titles, you get a 10% discount on the 3 that
form part of a set, but the fourth book still costs 8 EUR.
Your mission is to write a piece of code to calculate the
price of any conceivable shopping basket (containing only
Harry Potter books), giving as big a discount as possible.
For example, how much does this basket of books cost?
2 copies of the first book
2 copies of the second book
2 copies of the third book
1 copy of the fourth book
1 copy of the fifth book
One way of group these 8 books is:
1 group of 5 --> 25% discount (1st,2nd,3rd,4th,5th)
+1 group of 3 --> 10% discount (1st,2nd,3rd)
This would give a total of
5 books at a 25% discount
+3 books at a 10% discount
Giving
5 x (8 - 2.00) == 5 x 6.00 == 30.00
+3 x (8 - 0.80) == 3 x 7.20 == 21.60
For a total of 51.60
However, a different way to group these 8 books is:
1 group of 4 books --> 20% discount (1st,2nd,3rd,4th)
+1 group of 4 books --> 20% discount (1st,2nd,3rd,5th)
This would give a total of
4 books at a 20% discount
+4 books at a 20% discount
Giving
4 x (8-1.60) == 4 x 6.40 == 25.60
+4 x (8-1.60) == 4 x 6.40 == 25.60
For a total of 51.20
And 51.20 is the price with the biggest discount.
ISBN
ISBN - International Standard Book Number
There are two ISBN standards: ISBN-10 and ISBN-13.
Support for ISBN-13 is essential, whereas support
for ISBN-10 is optional.
Here are some valid examples of each:
ISBN-10: 0471958697
0 471 60695 2
0-470-84525-2
0-321-14653-0
ISBN-13: 9780470059029
978 0 471 48648 0
978-0596809485
978-0-13-149505-0
978-0-262-13472-9
ISBN-10 is made up of 9 digits plus a check digit (which
may be 'X') and ISBN-13 is made up of 12 digits plus a
check digit. Spaces and hyphens may be included in a code,
but are not significant. This means that 9780471486480 is
equivalent to 978-0-471-48648-0 and 978 0 471 48648 0.
The check digit for ISBN-10 is calculated by multiplying
each digit by its position (i.e., 1 x 1st digit, 2 x 2nd
digit, etc.), summing these products together and taking
modulo 11 of the result (with 'X' being used if the result
is 10).
The check digit for ISBN-13 is calculated by multiplying
each digit alternately by 1 or 3 (i.e., 1 x 1st digit,
3 x 2nd digit, 1 x 3rd digit, 3 x 4th digit, etc.), summing
these products together, taking modulo 10 of the result
and subtracting this value from 10, and then taking the
modulo 10 of the result again to produce a single digit.
Basic task:
Create a function that takes a string and returns true
if that is a valid ISBN-13 and false otherwise.
Advanced task:
Also return true if the string is a valid ISBN-10.
LCD Digits
Your task is to create an LCD string representation of an
integer value using a 3x3 grid of space, underscore, and
pipe characters for each digit. Each digit is shown below
(using a dot instead of a space)
.. ... .. .. ... .. .. .. .. ..
|.| ..| .| .| || |. |. ..| || ||
|| ..| |. .| ..| .| || ..| |_| ..|
Example: 910
.. ... ..
|| ..| |.|
..| ..| ||
Leap Years
Write a function that returns true or false depending on
whether its input integer is a leap year or not.
A leap year is defined as one that is divisible by 4,
but is not otherwise divisible by 100 unless it is
also divisible by 400.
For example, 2001 is a typical common year and 1996
is a typical leap year, whereas 1900 is an atypical
common year and 2000 is an atypical leap year.
Magic Square
This puzzle comes from Lewis Carroll.
The magic part is when the values on a square are arranged
so that adding them up in any direction results in a constant sum.
You have the following values:
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
You need to arrange them in a 3 x 3 matrix so that:
The sums of numbers in each row = magic number
The sums of numbers in each column = magic number
The sums of numbers in each diagonal = magic number
Source: https://github.com/gigasquid/wonderland-clojure-katas
Mars Rover
The instructions for this exercise can be found here
https://code.google.com/p/marsrovertechchallenge/
Mine Field
A field of N x M squares is represented by N lines
of exactly M characters each.
The character '*' represents a mine.
The character '.' represents no-mine.
Example input (a 3 x 4 mine-field of 12 squares,
2 of which are mines)
3 4
...
...
....
Your task is to write a program to accept this input
and produce as output a hint-field of identical
dimensions where each square is a * for a mine or
the number of adjacent mine-squares if the square
does not contain a mine.
Example output (for the above input)
211
121
0111
Monty Hall
Suppose you're on a game show and you're given the
choice of three doors. Behind one door is a car; behind
the others, goats. The car and the goats were
placed randomly behind the doors before the show.
The rules of the game show are as follows:
After you have chosen a door, the door remains closed
for the time being. The game show host, Monty Hall, who
knows what is behind the doors, now has to open one of
the two remaining doors, and the door he opens must have
a goat behind it. If both remaining doors have goats
behind them, he chooses one randomly. After Monty Hall
opens a door with a goat, he will ask you to decide
whether you want to stay with your first choice or to
switch to the last remaining door.
For example:
Imagine that you chose Door 1 and the host opens Door 3,
which has a goat. He then asks you "Do you want to switch
to Door Number 2?" Is it to your advantage to change your
choice?
Note that the player may initially choose any of the
three doors (not just Door 1), that the host opens a
different door revealing a goat (not necessarily Door 3),
and that he gives the player a second choice between the
two remaining unopened doors.
Simulate at least a thousand games using three doors for
each strategy and show the results in such a way as to make
it easy to compare the effects of each strategy.
[Source http://rosettacode.org]
Number Chains
Given a number, we can form a number chain by
- arranging its digits in descending order
- arranging its digits in ascending order
- subtracting the number obtained in (2) from the number
obtained (1) to form a new number - and repeat these steps unless the new number has already
appeared in the chain
Note that 0 is a permitted digit. The number of distinct
numbers in the chain is the length of the chain. You are to
write a program that reads numbers and outputs the number
chain and the length of that chain for each number read.
Input and Output
The input consists of a positive number, less than 10^9.
The output consists of the number chain generated by the
input number, followed by its lengths exactly in the format
indicated below.
Example-1
Input
123456789
Output
Original number was 123456789
987654321 - 123456789 = 864197532
987654321 - 123456789 = 864197532
Chain length 2
Example-2
Input
1234
Output
Original number was 1234
4321 - 1234 = 3087
8730 - 378 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
Chain length 4
Example-3
Input
444
Output
Original number was 444
444 - 444 = 0
0 - 0 = 0
Chain length 2
[Source: http://uva.onlinejudge.org/]
Number Names
Spell out a number. For example
99 --> ninety nine
300 --> three hundred
310 --> three hundred and ten
1501 --> one thousand, five hundred and one
12609 --> twelve thousand, six hundred and nine
512607 --> five hundred and twelve thousand,
six hundred and seven
43112603 --> forty three million, one hundred and
twelve thousand,
six hundred and three
[Source http://rosettacode.org]
Phone Numbers
Given a list of phone numbers, determine if it is
consistent. In a consistent phone list no number
is a prefix of another. For example:
o) Bob 91 12 54 26
o) Alice 97 625 992
o) Emergency 911
In this case, it is not possible to call Bob because
the phone exchange would direct your call to the
emergency line as soon as you dialled the first three
digits of Bob's phone number. So this list would not
be consistent.
Poker Hands
A poker deck contains 52 cards - each card has a suit which
is one of clubs, diamonds, hearts, or spades
(denoted C, D, H, and S in the input data).
Each card also has a value which is one of
2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, ace
(denoted 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A).
For scoring purposes, the suits are unordered while the
values are ordered as given above, with 2 being the lowest
and ace the highest value.
A poker hand consists of 5 cards dealt from the deck. Poker
hands are ranked by the following partial order from lowest
to highest.
High Card: Hands which do not fit any higher category are
ranked by the value of their highest card. If the highest
cards have the same value, the hands are ranked by the next
highest, and so on.
Pair: 2 of the 5 cards in the hand have the same value.
Hands which both contain a pair are ranked by the value of
the cards forming the pair. If these values are the same,
the hands are ranked by the values of the cards not
forming the pair, in decreasing order.
Two Pairs: The hand contains 2 different pairs. Hands
which both contain 2 pairs are ranked by the value of
their highest pair. Hands with the same highest pair
are ranked by the value of their other pair. If these
values are the same the hands are ranked by the value
of the remaining card.
Three of a Kind: Three of the cards in the hand have the
same value. Hands which both contain three of a kind are
ranked by the value of the 3 cards.
Straight: Hand contains 5 cards with consecutive values.
Hands which both contain a straight are ranked by their
highest card.
Flush: Hand contains 5 cards of the same suit. Hands which
are both flushes are ranked using the rules for High Card.
Full House: 3 cards of the same value, with the remaining 2
cards forming a pair. Ranked by the value of the 3 cards.
Four of a kind: 4 cards with the same value. Ranked by the
value of the 4 cards.
Straight flush: 5 cards of the same suit with consecutive
values. Ranked by the highest card in the hand.
Your job is to rank pairs of poker hands and to indicate
which, if either, has a higher rank.
Examples:
Input: Black: 2H 3D 5S 9C KD White: 2C 3H 4S 8C AH
Output: White wins - high card: Ace
Input: Black: 2H 4S 4C 2D 4H White: 2S 8S AS QS 3S
Output: Black wins - full house
Input: Black: 2H 3D 5S 9C KD White: 2C 3H 4S 8C KH
Output: Black wins - high card: 9
Input: Black: 2H 3D 5S 9C KD White: 2D 3H 5C 9S KH
Output: Tie
Prime Factors
Factorize a positive integer number into its prime factors.
For example:
2 -> [2]
3 -> [3]
4 -> [2,2]
6 -> [2,3]
9 -> [3,3]
12 -> [2,2,3]
15 -> [3,5]
Print Diamond
Given a letter print a diamond starting with 'A'
with the supplied letter at the widest point.
For example: print-diamond 'E' prints
A
B B
C C
D D
E E
D D
C C
B B
A
For example: print-diamond 'C' prints
A
B B
C C
B B
A
Recently Used List
Develop a recently-used-list class to hold strings
uniquely in Last-In-First-Out order.
o) A recently-used-list is initially empty.
o) The most recently added item is first, the least
recently added item is last.
o) Items can be looked up by index, which counts from zero.
o) Items in the list are unique, so duplicate insertions
are moved rather than added.
Optional extras
o) Null insertions (empty strings) are not allowed.
o) A bounded capacity can be specified, so there is an upper
limit to the number of items contained, with the least
recently added items dropped on overflow.
Reordering
Given a set of integer numbers your task is to
reorder them as follows:
-
Move a range of elements from one position to another,
preserving their order and the order of the other
elements.e.g. Given the set
{ 4, 2, 7, 5, 9, 8, 6, 4, 3, 2 }
^ ^ ^
s e pmoving the range of elements starting at element 2 (s)
and ending at element 4 (e) to the position before
element 9 (p) will give{ 4, 9, 8, 6, 4, 2, 7, 5, 3, 2 }
^ ^
s e -
Move all elements whose value would produce a Fizz,
Buzz or FizzBuzz output after a specified element,
preserving their order and the order of the other
elements.e.g. Given the set
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20 }and specifying element 4 the result would be
{ 1, 2, 4, 3, 5, 6, 9, 10, 12, 15, 18, 20, 7, 8,
11, 13, 14, 16, 17, 19 }
Reverse Roman
Given a Roman number as a string (eg "XX") determine
its integer value (eg 20).
You cannot write numerals like IM for 999.
Wikipedia states "Modern Roman numerals are written by
expressing each digit separately starting with the
leftmost digit and skipping any digit with a value of zero."
Examples:
"I" -> 1 | "X" -> 10 | "C" -> 100 | "M" -> 1000
"II" -> 2 | "XX" -> 20 | "CC" -> 200 | "MM" -> 2000
"III" -> 3 | "XXX" -> 30 | "CCC" -> 300 | "MMM" -> 3000
"IV" -> 4 | "XL" -> 40 | "CD" -> 400 | "MMMM" -> 4000
"V" -> 5 | "L" -> 50 | "D" -> 500 |
"VI" -> 6 | "LX" -> 60 | "DC" -> 600 |
"VII" -> 7 | "LXX" -> 70 | "DCC" -> 700 |
"VIII" -> 8 | "LXXX" -> 80 | "DCCC" -> 800 |
"IX" -> 9 | "XC" -> 90 | "CM" -> 900 |
"MCMXC" -> 1990 ("M" -> 1000 + "CM" -> 900 + "XC" -> 90)
"MMVIII" -> 2008 ("MM" -> 2000 + "VIII" -> 8)
"XCIX" -> 99 ("XC" -> 90 + "IX" -> 9)
"XLVII" -> 47 ("XL" -> 40 + "VII" -> 7)
Reversi
Reversi is a board game for two players. The board contains
8x8 squares. The players place Black or White counters onto
the board, one counter per square. More information can be
found on Wikipedia en.wikipedia.org/wiki/Reversi?. Your task
is to write a program that takes a current board position
together with information about whose turn it is, and returns
a list of the legal moves for that player. A move is only
legal if it results in at least one of the opponent's
counters being flipped.
Example input: (the final B indicates it is Black's turn)
........
........
........
...BW...
...WB...
........
........
........
B
Example output: (each zero indicates a legal move for Black)
........
........
....0...
...BW0..
..0WB...
...0....
........
........
B
Roman Numerals
Given a positive integer number (eg 42) determine
its Roman numeral representation as a String (eg "XLII").
You cannot write numerals like IM for 999.
Wikipedia states "Modern Roman numerals are written by
expressing each digit separately starting with the
leftmost digit and skipping any digit with a value of zero."
Examples:
1 -> "I" | 10 -> "X" | 100 -> "C" | 1000 -> "M"
2 -> "II" | 20 -> "XX" | 200 -> "CC" | 2000 -> "MM"
3 -> "III" | 30 -> "XXX" | 300 -> "CCC" | 3000 -> "MMM"
4 -> "IV" | 40 -> "XL" | 400 -> "CD" | 4000 -> "MMMM"
5 -> "V" | 50 -> "L" | 500 -> "D" |
6 -> "VI" | 60 -> "LX" | 600 -> "DC" |
7 -> "VII" | 70 -> "LXX" | 700 -> "DCC" |
8 -> "VIII" | 80 -> "LXXX" | 800 -> "DCCC" |
9 -> "IX" | 90 -> "XC" | 900 -> "CM" |
1990 -> "MCMXC" (1000 -> "M" + 900 -> "CM" + 90 -> "XC")
2008 -> "MMVIII" (2000 -> "MM" + 8 -> "VIII")
99 -> "XCIX" (90 -> "XC" + 9 -> "IX")
47 -> "XLVII" (40 -> "XL" + 7 -> "VII")
Saddle Points
Write a program to search for the "saddle points" in
a 5 by 5 array of integers. A saddle point is a cell
whose value is greater than or equal to any in its
row, and less than or equal to any in its column.
There may be more than one saddle point in the array.
Print out the coordinates of any saddle points your
program finds. Print out "No saddle points" if there
are none.
[source: http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html]
Tennis
You task is to implement a tennis scoring program.
Summary of tennis scoring:
A game is won by the first player to have won at
least four points in total and at least two points
more than the opponent.The running score of each game is described in a
manner peculiar to tennis: scores from zero to three
points are described as "love", "fifteen", "thirty",
and "forty" respectively.If at least three points have been scored by each
player, and the scores are equal, the score is "deuce".If at least three points have been scored by each
side and a player has one more point than his opponent,
the score of the game is "advantage" for the player
in the lead.
[source http://en.wikipedia.org/wiki/Tennis#Scoring]
TinyMaze
Alice found herself very tiny and wandering around Wonderland.
Even the grass around her seemed like a maze.
This is a tiny maze solver.
A maze is represented by a matrix
[[:S 0 1]
[1 0 1]
[1 0 :E]]
S : start of the maze
E : end of the maze
1 : This is a wall that you cannot pass through
0 : A free space that you can move through.
The goal is the get to the end of the maze.
A solved maze will have a :x in the start, the path,
and the end of the maze, like this.
[[:x :x 1]
[1 :x 1]
[1 :x :x]]
Source: https://github.com/gigasquid/wonderland-clojure-katas
Unsplice
Alice found herself very tiny and wandering around Wonderland.
Even the grass around her seemed like a maze.
This is a tiny maze solver.
A maze is represented by a matrix
[[:S 0 1]
[1 0 1]
[1 0 :E]]
S : start of the maze
E : end of the maze
1 : This is a wall that you cannot pass through
0 : A free space that you can move through.
The goal is the get to the end of the maze.
A solved maze will have a :x in the start, the path,
and the end of the maze, like this.
[[:x :x 1]
[1 :x 1]
[1 :x :x]]
Source: https://github.com/gigasquid/wonderland-clojure-katas
Unsplice
Given a string, strip all occurences of consecutively
occuring backslash and newline characters. For example,
assuming that:
"\" represents '' and
"\n" represents '\n'
"ab\\ncd\\nef" --> "abcdef" (two stripped out)
"abc\\ndef" --> "abcdef" (one stripped out)
"abc\n\def" --> unchanged (wrong order)
"abc\def" --> unchanged (no \n)
"abc\ndef" --> unchanged (no )
"abcdef" --> unchanged
Wonderland Number
You must find a way to generate the wonderland number.
It has six digits.
If you multiply it by 2,3,4,5, or 6, the resulting
number has all the same digits in at as the original number.
The only difference is the position that they are in.
Source: https://github.com/gigasquid/wonderland-clojure-katas
Word Wrap
Your task is to write a function that takes two arguments,
a string and an integer width.
The function returns the string, but with line breaks
inserted at just the right places to make sure that no line
is longer than the column number.
You try to break lines at word boundaries.
Like a word processor, break the line by replacing
the last space in a line with a newline.
Yatzy
The game of yatzy is a simple dice game. Each player
rolls five six-sided dice. The player places the roll in
a category, such as ones, twos, fives, pair, two pairs
etc (see below).
If the roll is compatible with the category, the player
gets a score for the roll according to the rules. If the
roll is not compatible with the category, the player scores
zero for the roll.
For example, if a player rolls 5,6,5,5,2 and scores the
dice in the fives category they would score 15 (three fives).
Your task is to score a GIVEN roll in a GIVEN category.
You do NOT have to program the random dice rolling.
You do NOT have to program re-rolls (as in the real game).
You do NOT play by letting the computer choose the highest
scoring category for a given roll.
Yatzy Categories and Scoring Rules
Chance:
The player scores the sum of all dice,
no matter what they read.
For example,
1,1,3,3,6 placed on "chance" scores 14 (1+1+3+3+6)
4,5,5,6,1 placed on "chance" scores 21 (4+5+5+6+1)
Yatzy:
If all dice have the same number,
the player scores 50 points.
For example,
1,1,1,1,1 placed on "yatzy" scores 50
5,5,5,5,5 placed on "yatzy" scores 50
1,1,1,2,1 placed on "yatzy" scores 0
Ones, Twos, Threes, Fours, Fives, Sixes:
The player scores the sum of the dice that reads one,
two, three, four, five or six, respectively.
For example,
1,1,2,4,4 placed on "fours" scores 8 (4+4)
2,3,2,5,1 placed on "twos" scores 4 (2+2)
3,3,3,4,5 placed on "ones" scores 0
Pair:
If exactly two dice have the same value then
the player scores the sum of the two highest matching dice.
For example, when placed on "pair"
3,3,3,4,4 scores 8 (4+4)
1,1,6,2,6 scores 12 (6+6)
3,3,3,4,1 scores 0
3,3,3,3,1 scores 0
Two pairs:
If exactly two dice have the same value and exactly
two dice have a different value then the
player scores the sum of these four dice.
For example, when placed on "two pairs"
1,1,2,3,3 scores 8 (1+1+3+3)
1,1,2,3,4 scores 0
1,1,2,2,2 scores 0
Three of a kind:
If there are exactly three dice with the same number
then the player scores the sum of these dice.
For example, when placed on "three of a kind"
3,3,3,4,5 scores 9 (3+3+3)
3,3,4,5,6 scores 0
3,3,3,3,1 scores 0
Four of a kind:
If there are exactly four dice with the same number
then the player scores the sum of these dice.
For example, when placed on "four of a kind"
2,2,2,2,5 scores 8 (2+2+2+2)
2,2,2,5,5 scores 0
2,2,2,2,2 scores 0
Small straight:
When placed on "small straight", if the dice read
1,2,3,4,5, the player scores 15 (the sum of all the dice).
Large straight:
When placed on "large straight", if the dice read
2,3,4,5,6, the player scores 20 (the sum of all the dice).
Full house:
If the dice are two of a kind and three of a different kind
then the player scores the sum of all five dice.
For example, when placed on "full house"
1,1,2,2,2 scores 8 (1+1+2+2+2)
2,2,3,3,4 scores 0
4,4,4,4,4 scores 0
Yatzy Cutdown
The game of yatzy is a simple dice game. Each player
rolls five six-sided dice. The player places the roll in
a category, such as one-pair, two-pair, small-straight etc
(see below).
If the roll is compatible with the category, the player
gets a score for the roll according to the rules. If the
roll is not compatible with the category, the player scores
zero for the roll.
For example, if a player rolls 5,6,5,6,2 and scores the
dice in the two-pairs category they would score 22 (5+5+6+6).
Your task is to score a GIVEN roll in a GIVEN category.
You do NOT have to program the random dice rolling.
You do NOT have to program re-rolls (as in the real game).
You do NOT play by letting the computer choose the highest
scoring category for a given roll.
Yatzy Categories and Scoring Rules
Yatzy:
If all dice have the same number,
the player scores 50 points, otherwise 0.
For example,
1,1,1,1,1 placed on "yatzy" scores 50
5,5,5,5,5 placed on "yatzy" scores 50
1,1,1,2,1 placed on "yatzy" scores 0
Pair:
If exactly two dice have the same value then
the player scores the sum of the two highest matching dice.
For example, when placed on "pair"
3,3,3,4,4 scores 8 (4+4)
1,1,6,2,6 scores 12 (6+6)
3,3,3,4,1 scores 0
3,3,3,3,1 scores 0
Two pairs:
If exactly two dice have the same value and exactly
two dice have a different value then the
player scores the sum of these four dice.
For example, when placed on "two pairs"
1,1,2,3,3 scores 8 (1+1+3+3)
1,1,2,3,4 scores 0
1,1,2,2,2 scores 0
Three of a kind:
If there are exactly three dice with the same number
then the player scores the sum of these dice.
For example, when placed on "three of a kind"
3,3,3,4,5 scores 9 (3+3+3)
3,3,4,5,6 scores 0
3,3,3,3,1 scores 0
Four of a kind:
If there are exactly four dice with the same number
then the player scores the sum of these dice.
For example, when placed on "four of a kind"
2,2,2,2,5 scores 8 (2+2+2+2)
2,2,2,5,5 scores 0
2,2,2,2,2 scores 0
Small straight:
When placed on "small straight", if the dice read
1,2,3,4,5, the player scores 15 (the sum of all the dice).
Large straight:
When placed on "large straight", if the dice read
2,3,4,5,6, the player scores 20 (the sum of all the dice).
Full house:
If the dice are two of a kind and three of a different kind
then the player scores the sum of all five dice.
For example, when placed on "full house"
1,1,2,2,2 scores 8 (1+1+2+2+2)
2,2,3,3,4 scores 0
4,4,4,4,4 scores 0
Zeckendorf Number
Just as numbers can be represented in a positional
notation as sums of multiples of the powers of ten
(decimal) or two (binary); all the positive integers
can be represented as the sum of one or zero times
the distinct members of the Fibonacci series.
Recall that the first six distinct Fibonacci numbers
are: 1, 2, 3, 5, 8, 13.
The decimal number eleven can be written as
013 + 18 + 05 + 13 + 02 + 01
or 010100 in positional notation where the columns
represent multiplication by a particular member of the
sequence. Leading zeroes are dropped so that eleven
decimal becomes 10100.
10100 is not the only way to make eleven from the
Fibonacci numbers however;
013 + 18 + 05 + 03 + 12 + 11
or 010011 would also represent decimal 11. For a true
Zeckendorf number there is the added restriction that
no two consecutive Fibonacci numbers can be used which
leads to the former unique solution.
Your task is to generate and show here a table of the
Zeckendorf number representations of the decimal numbers
zero to twenty, in order.
[Source http://rosettacode.org]