Skip to content

Commit 9f4a743

Browse files
committed
refactor: all katas use only one template do-kata
1 parent 1b2c890 commit 9f4a743

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+618
-32
lines changed

basic-statistics/init_code.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# code-kata
2+
class Array
3+
def mean
4+
end
5+
6+
def median
7+
end
8+
9+
def standard_deviation
10+
end
11+
12+
def modes
13+
end
14+
end

basic-statistics/introduction.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
basic-statistic
2+
3+
Statistics is a field of mathematics that pertains to data analysis. Statistical methods and equations can be applied to a data set in order to analyze and interpret results, explain variations in the data, or predict future data.
4+
5+
A few examples of statistical information we can calculate are:
6+
7+
+ Average value (mean)
8+
+ Most frequently occurring value (mode)
9+
+ On average, how much each measurement deviates from the mean (standard deviation of the mean)
10+
+ Span of values over which your data set occurs (range), and
11+
+ Midpoint between the lowest and highest value of the set (median)
12+
13+
For example:
14+
15+
the mean of [1,2,2,3,5] is 2.6
16+
the median of [1,2,2,3,5] is 2
17+
the standard_deviation of [1,2,2,3,5] is 1.52
18+
the only mode of [1,1,2,2,3,4,4,4,4,4] is 4
19+
modes of [1,1,2,2,3,4,4] are [1,2,4]
20+
there is no modes in [1,2,3]
21+

basic-statistics/test_cases.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
assert_equal [1,2,2,3,5].mean, 2.6
2+
assert_equal [100,100,100,100.1].mean, 100.025
3+
assert_equal [1,2,2,3,5].median, 2
4+
assert_equal [2,3,-100,100].median, 2.5
5+
assert_equal [1, 1, 10, 100, 1000].median, 10
6+
assert_equal [1,2,2,3,5].standard_deviation, 1.52
7+
assert_equal [1,1,2,2,3,4,4,4,4,4].modes, [4]
8+
assert_equal [1,1,2,2,3,4,4].modes, [1,2,4]
9+
assert_equal [1,2,3].modes, []

counting-days/init_code.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# code-kata
2+
def days_in_month(month, year)
3+
4+
end

counting-days/introduction.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
counting-days
2+
3+
Write a method that return number of days of the given month/year.
4+
5+
For example:
6+
7+
days_in_month(1, 2024) # 31
8+
days_in_month(2, 2024) # 29
9+
days_in_month(2, 2025) # 28
10+

counting-days/test_cases.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
assert_equal days_in_month(1, 2024), 31
2+
assert_equal days_in_month(6, 2024), 30
3+
assert_equal days_in_month(7, 2024), 31
4+
assert_equal days_in_month(0, 2024), 0
5+
assert_equal days_in_month(13, 2024), 0
6+
assert_equal days_in_month(2, 2024), 29
7+
assert_equal days_in_month(2, 2025), 28
8+
assert_equal days_in_month(2, 2025), 28
9+
assert_equal days_in_month(2, 0), 29
10+
assert_equal days_in_month(2, -1), 28
11+
assert_equal days_in_month(7, -2024), 31

do-kata.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<title>Kata</title>
4+
<title>ruby-kata</title>
55
<meta name="viewport" content="width=device-width,initial-scale=1">
66
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/6.65.7/codemirror.min.css">
77
<link rel="stylesheet" href="./codekata.css">
@@ -14,6 +14,8 @@
1414
const urlParams = new URLSearchParams(queryString);
1515
const kata = urlParams.get('kata')
1616

17+
document.getElementById('kata-name').textContent = `/${kata}`
18+
1719
import { DefaultRubyVM } from "https://cdn.jsdelivr.net/npm/@ruby/[email protected]/dist/browser/+esm";
1820
import { Application, Controller } from "https://unpkg.com/@hotwired/stimulus/dist/stimulus.js"
1921

@@ -27,7 +29,7 @@
2729
}
2830

2931
load() {
30-
fetch(`https://ruby-kata.github.io/${kata}/${this.fileValue}`)
32+
fetch(`https://ruby-kata.github.io/${kata}/${this.fileValue}`, {mode: "no-cors"})
3133
.then(response => response.text())
3234
.then(content => {
3335
this.element.value = content
@@ -52,7 +54,7 @@
5254
}
5355
});
5456

55-
const initCodeResponse = await fetch(`https://ruby-kata.github.io/${kata}/init_code.rb`)
57+
const initCodeResponse = await fetch(`https://ruby-kata.github.io/${kata}/init_code.rb`, {mode: "no-cors"})
5658
const initCode = await initCodeResponse.text()
5759
this.editor.setValue(initCode)
5860

@@ -63,7 +65,7 @@
6365
}
6466

6567
async run() {
66-
const testHelperResponse = await fetch("https://ruby-kata.github.io/test_helper/assertion.rb")
68+
const testHelperResponse = await fetch("https://ruby-kata.github.io/test_helper/assertion.rb", {mode: "no-cors"})
6769
const testHelperCode = await testHelperResponse.text()
6870
const code = this.editor.getValue()
6971
const testcases = document.getElementById('testcases').value
@@ -73,7 +75,7 @@
7375
7476
${testHelperCode}
7577
76-
run('${testcases}')
78+
run(%(${testcases}))
7779
rescue => error
7880
error
7981
end
@@ -100,7 +102,7 @@
100102
</div>
101103

102104
<div class="footer">
103-
<a href="./index.html">./ruby-kata-list</a><span>/fizzbuzz</span>
105+
<a href="./index.html">./ruby-kata-list</a><span id="kata-name"></span>
104106
</div>
105107
</body>
106108
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# code-kata
2+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
doing-math-with-roman-numbers
2+
Ruby Cookbook
3+
4+
You want to convert between Arabic and Roman numbers,
5+
or do arithmetic with Roman numbers and get Roman numbers as your result.
6+
7+
For example:
8+
9+
72.to_roman # => LXXII
10+
444.to_roman # => CDXLIV
11+
1979.to_roman # => MCMLXXIX
12+
'MCMXLVIII'.to_roman # => MCMXLVIII
13+
Roman.to_arabic('MCMLXXIX') # => 1979
14+
'MMI'.to_roman.to_arabic # => 2001
15+
'MMI'.to_roman + 3 # => MMIV
16+
'MCMXLVIII'.to_roman # => MCMXLVIII
17+
612.to_roman * 3.to_roman # => MDCCCXXXVI
18+
(612.to_roman * 3).divmod('VII'.to_roman) # => [CCLXII, II]
19+
612.to_roman * 10000 # => 6120000 # Too big
20+
612.to_roman * 0 # => 0 # Too small
21+
'MCMXCIX'.to_roman.succ # => MM
22+
('I'.to_roman..'X'.to_roman).collect # => [I, II, III, IV, V, VI, VII, VIII, IX, X]
23+
'IIII'.to_roman # ArgumentError: Invalid Roman number: IIII
24+
'IVI'.to_roman # ArgumentError: Invalid Roman number: IVI
25+
'IXV'.to_roman # ArgumentError: Invalid Roman number: IXV
26+
'MCMM'.to_roman # ArgumentError: Invalid Roman number: MCMM
27+
'CIVVM'.to_roman # ArgumentError: Invalid Roman number: CIVVM
28+
-10.to_roman # RangeError: -10 can't be represented as a Roman number.
29+
50000.to_roman # RangeError: 50000 can't be represented as a Roman number.
30+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
assert_equal 72.to_roman.to_s, 'LXXII'
2+
assert_equal 444.to_roman.to_s, 'CDXLIV'
3+
assert_equal 1979.to_roman.to_s, 'MCMLXXIX'
4+
assert_equal Roman.to_arabic('MCMLXXIX'), 1979
5+
assert_equal 'MMI'.to_roman.to_arabic, 2001
6+
assert_equal ('MMI'.to_roman + 3).to_s, 'MMIV'
7+
assert_equal (612.to_roman * 3.to_roman).to_s, 'MDCCCXXXVI'
8+
assert_equal 'MCMXCIX'.to_roman.succ.to_s, 'MM'
9+
assert_raises(ArgumentError.new('Invalid Roman number: IIII')) { 'IIII'.to_roman }
10+
assert_raises(ArgumentError.new('Invalid Roman number: IVI')) { 'IVI'.to_roman }
11+
assert_raises(RangeError.new('-10 can't be represented as a Roman number.')) { -10.to_roman }

fibonacci-sequence/init_code.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# code-kata
2+
class Fibonacci
3+
4+
end

fibonacci-sequence/introduction.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
fibonacci-sequence
2+
3+
You want to generate a sequence of fibonacci numbers
4+
5+
fib = Fibonacci.new
6+
fib.succ # => 1
7+
fib.succ # => 1
8+
fib.succ # => 2
9+
fib.succ # => 3
10+
11+
Fibonacci[0..3] # [1,1,2,3]
12+
Fibonacci[..4] # [1,1,2,3,5]
13+
Fibonacci[6] # => 13
14+

fibonacci-sequence/test_cases.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fib = Fibonacci.new
2+
assert_equal fib.succ, 1
3+
assert_equal fib.succ, 1
4+
assert_equal fib.succ, 2
5+
assert_equal fib.succ, 3
6+
assert_equal Fibonacci[0..3], [1,1,2,3]
7+
assert_equal Fibonacci[..4], [1,1,2,3,5]
8+
assert_equal Fibonacci[6], 13

fizzbuzz/init_code.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# code-kata
12
def fizzbuzz(n)
23
# your implement
34
end

implementing-enumerable/init_code.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# code-kata
2+
class LinkedList
3+
4+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
implementing-enumerable
2+
Ruby Cookbook
3+
4+
You want to give a class all the useful iterator and iteration-related features of Ruby’s
5+
arrays (sort, detect, inject, and so on), but your class can’t be a subclass of Array.
6+
You don’t want to define all those methods yourself.
7+
8+
ll = LinkedList.new
9+
ll << 1 << 2 << 3 << 4
10+
ll.collect { |x| x.even? } # => [false, true, false, true]
11+
ll.detect { |x| x > 3 } # => 4
12+
ll.map { |x| x ** 2 } # => [1, 4, 9, 16]
13+
ll.each_with_index { |x, i| puts "Element #{i} is #{x}" }
14+
# Element 0 is 1
15+
# Element 1 is 2
16+
# Element 2 is 3
17+
# Element 3 is 4

implementing-enumerable/test_cases.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ll = LinkedList.new
2+
ll << 1 << 2 << 3 << 4
3+
assert_equal ll.collect { |x| x.even? }, [false, true, false, true]
4+
assert_equal ll.detect { |x| x > 3 }, 4
5+
assert_equal ll.select(&:odd?), [1, 3]
6+
assert_equal ll.map { |x| x ** 2 }, [1, 4, 9, 16]
7+
assert_equal ll.map.with_index { |x, i| [i, x] }, [[0,1], [1,2], [2,3], [3,4]]

index.html

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ <h1 style="color: red; margin-left: 20px;">Ruby Kata List</h1>
22

33
<ol>
44
<li><a href="./do-kata.html?kata=fizzbuzz">fizzbuzz</a></li>
5-
<li><a href="./basic-statistics.html">basic-statistics</a></li>
6-
<li><a href="./implementing-enumerable.html">implementing-enumerable</a></li>
7-
<li><a href="./fibonacci-sequence.html">fibonacci-sequence</a></li>
8-
<li><a href="./primes-sequence.html">primes-sequence</a></li>
9-
<li><a href="./search-and-replace.html">search-and-replace</a></li>
10-
<li><a href="./money-in-words.html">money-in-words</a></li>
11-
<li><a href="./word-wrapping-lines-of-text.html">word-wrapping-lines-of-text</a></li>
12-
<li><a href="./keep-array-stays-sorted.html">keep-array-stays-sorted</a></li>
13-
<li><a href="./sorting-an-array-by-frequency-of-appearance.html">sorting-an-array-by-frequency-of-appearance</a></li>
14-
<li><a href="./karate-chop.html">karate-chop</a></li>
15-
<li><a href="./rake-tasks.html">rake-tasks</a></li>
16-
<li><a href="./counting-days.html">counting-days</a></li>
17-
<li><a href="./open-hours.html">open-hours</a></li>
18-
<li><a href="./time-duration.html">time-duration</a></li>
19-
<li><a href="./text-full-justify.html">text-full-justify</a></li>
20-
<li><a href="./solving-a-system-of-linear-equations.html">solving-a-system-of-linear-equations</a></li>
21-
<li><a href="./doing-math-with-roman-numbers.html">doing-math-with-roman-numbers</a></li>
5+
<li><a href="./do-kata.html?kata=basic-statistics">basic-statistics</a></li>
6+
<li><a href="./do-kata.html?kata=implementing-enumerable">implementing-enumerable</a></li>
7+
<li><a href="./do-kata.html?kata=fibonacci-sequence">fibonacci-sequence</a></li>
8+
<li><a href="./do-kata.html?kata=primes-sequence">primes-sequence</a></li>
9+
<li><a href="./do-kata.html?kata=search-and-replace">search-and-replace</a></li>
10+
<li><a href="./do-kata.html?kata=money-in-words">money-in-words</a></li>
11+
<li><a href="./do-kata.html?kata=word-wrapping-lines-of-text">word-wrapping-lines-of-text</a></li>
12+
<li><a href="./do-kata.html?kata=keep-array-stays-sorted">keep-array-stays-sorted</a></li>
13+
<li><a href="./do-kata.html?kata=sorting-an-array-by-frequency-of-appearance">sorting-an-array-by-frequency-of-appearance</a></li>
14+
<li><a href="./do-kata.html?kata=karate-chop">karate-chop</a></li>
15+
<li><a href="./do-kata.html?kata=rake-tasks">rake-tasks</a></li>
16+
<li><a href="./do-kata.html?kata=counting-days">counting-days</a></li>
17+
<li><a href="./do-kata.html?kata=open-hours">open-hours</a></li>
18+
<li><a href="./do-kata.html?kata=time-duration">time-duration</a></li>
19+
<li><a href="./do-kata.html?kata=text-full-justify">text-full-justify</a></li>
20+
<li><a href="./do-kata.html?kata=solving-a-system-of-linear-equations">solving-a-system-of-linear-equations</a></li>
21+
<li><a href="./do-kata.html?kata=doing-math-with-roman-numbers">doing-math-with-roman-numbers</a></li>
2222
</ol>

karate-chop/init_code.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# code-kata
2+
def chop(num, arr)
3+
4+
end

karate-chop/introduction.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Kata02: Karate Chop
2+
http://codekata.com/kata/kata02-karate-chop/
3+
4+
A binary chop (sometimes called the more prosaic binary search) finds the position of value in a sorted array of values. It achieves some efficiency by halving the number of items under consideration each time it probes the values: in the first pass it determines whether the required value is in the top or the bottom half of the list of values. In the second pass in considers only this half, again dividing it in to two. It stops when it finds the value it is looking for, or when it runs out of array to search. Binary searches are a favorite of CS lecturers.
5+
6+
This Kata is straightforward. Implement a binary search routine (using the specification below) in the language and technique of your choice. Tomorrow, implement it again, using a totally different technique. Do the same the next day, until you have five totally unique implementations of a binary chop. (For example, one solution might be the traditional iterative approach, one might be recursive, one might use a functional style passing array slices around, and so on).
7+
8+
Goals
9+
10+
This Kata has three separate goals:
11+
12+
1. As you’re coding each algorithm, keep a note of the kinds of error you encounter. A binary search is a ripe breeding ground for “off by one” and fencepost errors. As you progress through the week, see if the frequency of these errors decreases (that is, do you learn from experience in one technique when it comes to coding with a different technique?).
13+
14+
2. What can you say about the relative merits of the various techniques you’ve chosen? Which is the most likely to make it in to production code? Which was the most fun to write? Which was the hardest to get working? And for all these questions, ask yourself “why?”.
15+
16+
3. It’s fairly hard to come up with five unique approaches to a binary chop. How did you go about coming up with approaches four and five? What techniques did you use to fire those “off the wall” neurons?
17+
18+
Specification
19+
20+
Write a binary chop method that takes an integer search target and a sorted array of integers. It should return the integer index of the target in the array, or -1 if the target is not in the array. The signature will logically be:
21+
22+
`chop(int, array_of_int) -> int`
23+
24+
You can assume that the array has less than 100,000 elements. For the purposes of this Kata, time and memory performance are not issues (assuming the chop terminates before you get bored and kill it, and that you have enough RAM to run it).

karate-chop/test_cases.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
assert_equal(-1, chop(3, []))
2+
assert_equal(-1, chop(3, [1]))
3+
assert_equal(0, chop(1, [1]))
4+
5+
assert_equal(0, chop(1, [1, 3, 5]))
6+
assert_equal(1, chop(3, [1, 3, 5]))
7+
assert_equal(2, chop(5, [1, 3, 5]))
8+
assert_equal(-1, chop(0, [1, 3, 5]))
9+
assert_equal(-1, chop(2, [1, 3, 5]))
10+
assert_equal(-1, chop(4, [1, 3, 5]))
11+
assert_equal(-1, chop(6, [1, 3, 5]))
12+
13+
assert_equal(0, chop(1, [1, 3, 5, 7]))
14+
assert_equal(1, chop(3, [1, 3, 5, 7]))
15+
assert_equal(2, chop(5, [1, 3, 5, 7]))
16+
assert_equal(3, chop(7, [1, 3, 5, 7]))
17+
assert_equal(-1, chop(0, [1, 3, 5, 7]))
18+
assert_equal(-1, chop(2, [1, 3, 5, 7]))
19+
assert_equal(-1, chop(4, [1, 3, 5, 7]))
20+
assert_equal(-1, chop(6, [1, 3, 5, 7]))
21+
assert_equal(-1, chop(8, [1, 3, 5, 7]))

keep-array-stays-sorted/init_code.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# code-kata
2+
class SortedArray
3+
4+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
keep-array-stays-sorted
2+
Ruby Cookbook
3+
Kata11: Sorting It Out (http://codekata.com/kata/kata11-sorting-it-out/)
4+
5+
You want to make sure an array stays sorted, even as you replace its elements or add
6+
new elements to it.
7+
8+
a = SortedArray.new([3,2,1]) # => [1, 2, 3]
9+
10+
a << -1 # => [-1, 1, 2, 3]
11+
a << 1.5 # => [-1, 1, 1.5, 2, 3]
12+
a.push(2.5) # => [-1, 1, 1.5, 2, 2.5, 3]
13+
a.unshift(1.6) # => [-1, 1, 1.5, 1.6, 2, 2.5, 3]
14+
a.collect! { |x| x * -1 } # => [-3, -2.5, -2, -1.6, -1.5, -1, 1]
15+
a[3] = 25
16+
a # => [-3, -2.5, -2, -1.5, -1, 1, 25]
17+
a[1..2] = [6000, 10, 600, 6]
18+
a # => [-3, -1.5, -1, 1, 6, 10, 25, 600, 6000]
19+
a += [0, -10] # => [-10, -3, -1.5, -1, 0, 1, 6, 10, 25, 600, 6000]
20+

keep-array-stays-sorted/test_cases.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
a = SortedArray.new([3,2,1])
2+
assert_equal a, [1,2,3]
3+
a << -1
4+
assert_equal a, [-1,1,2,3]
5+
a << 1.5
6+
assert_equal a, [-1, 1, 1.5, 2, 3]
7+
a.push(2.5)
8+
assert_equal a, [-1, 1, 1.5, 2, 2.5, 3]
9+
a.unshift(1.6)
10+
assert_equal a, [-1, 1, 1.5, 1.6, 2, 2.5, 3]
11+
a.collect! { |x| x * -1 }
12+
assert_equal a, [-3, -2.5, -2, -1.6, -1.5, -1, 1]
13+
a[3] = 25
14+
assert_equal a, [-3, -2.5, -2, -1.6, -1.5, -1, 1, 25]
15+
a[1..2] = [6000, 10, 600, 6]
16+
assert_equal a, [-3, -1.5, -1, 1, 6, 10, 25, 600, 6000]
17+
a += [0, -10]
18+
assert_equal a, [-10, -3, -1.5, -1, 0, 1, 6, 10, 25, 600, 6000]

money-in-words/init_code.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# code-kata
2+
def money_in_words(money)
3+
4+
end

0 commit comments

Comments
 (0)