<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>roman numeral Archives - Creatronix</title>
	<atom:link href="https://creatronix.de/tag/roman-numeral/feed/" rel="self" type="application/rss+xml" />
	<link>https://creatronix.de/tag/roman-numeral/</link>
	<description>My adventures in code &#38; business</description>
	<lastBuildDate>Sat, 05 Nov 2022 08:05:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Code Kata: Roman Numeral &#8211; Part 1</title>
		<link>https://creatronix.de/code-kata-roman-numeral-part-1/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Wed, 28 Dec 2016 22:18:59 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[kata]]></category>
		<category><![CDATA[roman numeral]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=220</guid>

					<description><![CDATA[<p>Christmas is over, so we get rid of the Christmas Tree. Today I want to show You another Code Kata: Roman Numerals. The task seems to be quite easy. Write a program which converts a decimal number into a string which contains the equivalent as a roman literal. E.g. convert 1984 into MCMLXXXIV. The requirements&#8230;</p>
<p>The post <a href="https://creatronix.de/code-kata-roman-numeral-part-1/">Code Kata: Roman Numeral &#8211; Part 1</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Christmas is over, so we get rid of the Christmas Tree.<br />
Today I want to show You another Code Kata: Roman Numerals. The task seems to be quite easy. Write a program which converts a decimal number into a string which contains the equivalent as a roman literal. E.g. convert 1984 into <span class="pl-s">MCMLXXXIV.</span></p>
<p><strong>The requirements</strong></p>
<p>We just use the characters from I to M.</p>
<table>
<tbody>
<tr>
<th>Symbol</th>
<td>I</td>
<td>V</td>
<td>X</td>
<td>L</td>
<td>C</td>
<td>D</td>
<td>M</td>
</tr>
<tr>
<th>Value</th>
<td>1</td>
<td>5</td>
<td>10</td>
<td>50</td>
<td>100</td>
<td>500</td>
<td>1,000</td>
</tr>
</tbody>
</table>
<p>We first write a simple conversion function which ignores the abbreviation syntax, so instead of IX for 9 we write VIIII. To do so we use integer division and modulo operation.  We start with the highest number and work our way down. Have a look:<span id="more-220"></span></p>
<pre><code>def convert_number_simple(number):
    roman_literal = ""
    thousands = number / 1000
    roman_literal += "M" * thousands
    five_hundreds = number % 1000 / 500
    roman_literal += "D" * five_hundreds
    hundreds = number % 1000 % 500 / 100
    roman_literal += "C" * hundreds
    fifties = number % 1000 % 500 % 100 / 50
    roman_literal += "L" * fifties
    tens = number % 1000 % 500 % 100 % 50 / 10
    roman_literal += "X" * tens
    fives = number % 1000 % 500 % 100 % 50 % 10 / 5
    roman_literal += "V" * fives
    ones = number % 1000 % 500 % 100 % 50 % 10 % 5
    roman_literal += "I" * ones
return roman_literal
</code></pre>
<p>This is by far not the most efficient way to convert a number. You could introduce a variable which holds the last calculation for the modulo operation, but I chose this representation for the sake of readability.</p>
<p>After we have written the simple conversion we can introduce the complex format by replacing string sequences which represent<br />
900, 90, 9 and 4. It is important that we do it again in an descendant order because if we got &#8220;VIIII&#8221; and would replace&#8221;IIII&#8221; we would end up with VIV which is a non-valid numeral.</p>
<pre><code>def convert_number(number):
    easy = convert_number_simple(number)
    easy = easy.replace("DCCCC", "CM") # 900
    easy = easy.replace("LXXXX", "XC") # 90
    easy = easy.replace("VIIII", "IX") # 9
    easy = easy.replace("IIII", "IV")  # 4
return easy
</code></pre>
<p>To test drive our code, Python has an assert function build in so we can write our main function as follows:</p>
<pre><code>def main():
    assert("M" == convert_number_simple(1000))
    assert("MM" == convert_number_simple(2000))
    assert("D" == convert_number_simple(500))
    assert("MD" == convert_number_simple(1500))
    assert("C" == convert_number_simple(100))
    assert("L" == convert_number_simple(50))
    assert("X" == convert_number_simple(10))
    assert("V" == convert_number_simple(5))
    assert("I" == convert_number_simple(1))

    assert("MDCCCCLXXXIIII" == convert_number_simple(1984))
    assert("MCMLXXXIV" == convert_number(1984))
    assert("IV" == convert_number(4))
    assert("IX" == convert_number(9))


if __name__ == '__main__':
    main()
</code></pre>
<p><strong>So far the solution in Python &#8211; stay tuned for Part 2 in which we do it in Rust 🙂<br />
</strong></p>
<p>The post <a href="https://creatronix.de/code-kata-roman-numeral-part-1/">Code Kata: Roman Numeral &#8211; Part 1</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Code Kata: Christmas Tree</title>
		<link>https://creatronix.de/code-kata-christmas-tree/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Sat, 24 Dec 2016 21:32:00 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[kata]]></category>
		<category><![CDATA[roman numeral]]></category>
		<category><![CDATA[rust]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=3</guid>

					<description><![CDATA[<p>Inspired by a blog post of Dave Thomas I started to implement my technology learning roadmap by writing little code katas. To kill two birds with one stone I first solve a programming puzzle with my lingua franca Python. That helped me to concentrate on solving the algorithmic part of the puzzle because I don&#8217;t&#8230;</p>
<p>The post <a href="https://creatronix.de/code-kata-christmas-tree/">Code Kata: Christmas Tree</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Inspired by a <a href="http://codekata.com/kata/codekata-how-it-started/">blog post</a> of Dave Thomas I started to implement my technology learning roadmap by writing little code katas.</p>
<p>To kill two birds with one stone I first solve a programming puzzle with my lingua franca Python. That helped me to concentrate on solving the algorithmic part of the puzzle because I don&#8217;t have to constantly worry about syntax and semantics.</p>
<p>Then I did the whole thing again this time in the language I want to learn: Rust</p>
<p>To give You an example how that works I will elaborate on the Christmas tree kata. <span id="more-3"></span> Ok, what is the Christmas tree? It&#8217;s a neat little programming exercise where You have to print a Christmas tree on the console.</p>
<p>e.g. a tree with height 2 looks like this:</p>
<pre><code> X
XXX
 |
</code></pre>
<p>and a tree with height 3 looks like this:</p>
<pre><code>  X
 XXX
XXXXX
  |
</code></pre>
<p>How do You solve this? First of all You have to understand how many whitespaces and &#8220;X&#8221;s You will find in each line depending on the level.<br />
So for a tree with height 2 there is one whitespace and one X in the first and no whitespace and 3 &#8220;X&#8221; in the second row and two whitespaces and one &#8220;|&#8221; in the third line.</p>
<p>Let&#8217;s make a little table to see where this is going:</p>
<pre>row  WS   X   |
0    1    1   0
1    0    3   0
2    1    0   1
</pre>
<p>Same thing again for height 3</p>
<pre>row  WS   X   |
0    2    1   0
1    1    3   0
2    0    5   0
3    2    0   1
</pre>
<p>I hope it becomes quite easy to see how the characters have to be printed out:<br />
for whitespaces it is &#8220;height &#8211; current_row &#8211; 1&#8221;,<br />
for &#8220;X&#8221; it is &#8220;2 * current_row + 1&#8221;<br />
and the last line is always &#8220;height &#8211; 1&#8221; whitespaces plus the &#8220;|&#8221; character.</p>
<p>In Python it looks like this:</p>
<pre><code>height = 3
for line in range(height):
    print (" " * (height-line-1)) + "X" * (2*line+1)
print (" " * (height-1)) + "|"
</code></pre>
<p>And now for something completely different: Rust<br />
Rust is a strongly and statically typed language, so there is a bit more effort in writing this Kata down. It looks like this:</p>
<pre><code>
fn draw_christmas_tree(height: i32){
    let mut val: String = String::new();
    for i in 0..height {
        for j in 0..height-i{
            val.push_str(" ");
        }
         for k in 0..(2*i+1){
            val.push_str("X");
        }
        val.push_str("\n");
    }
    for j in 0..height{
        val.push_str(" ");
    }
    val.push_str("|");
    println!("{}", val);

}
fn main() {
    draw_christmas_tree(3);
}
</code></pre>
<p>Because in Rust You don&#8217;t have the smooth &#8220;repeat a string n times&#8221; syntax You have to iterate more often. I also tried to append the characters to a string buffer before printing them out so it looks a bit more different then the solution in python.</p>
<p>The post <a href="https://creatronix.de/code-kata-christmas-tree/">Code Kata: Christmas Tree</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
