Skip to content

Commit

Permalink
Add Roman Numeral in Beef (#3730)
Browse files Browse the repository at this point in the history
  • Loading branch information
rzuckerm authored Jan 22, 2024
1 parent 9cbb926 commit 3b6737e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
72 changes: 72 additions & 0 deletions archive/b/beef/RomanNumeral.bf
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;

namespace RomanNumeral;

class Program
{
public static void Usage()
{
Console.WriteLine("Usage: please provide a string of roman numerals");
Environment.Exit(0);
}

public static void Error()
{
Console.WriteLine("Error: invalid string of roman numerals");
Environment.Exit(0);
}

public static Result<int> RomanNumeral(StringView str)
{
int total = 0;
int prevDigit = 0;
for (char8 ch in str)
{
int digit = 0;
switch (ch)
{
case 'I': digit = 1;
case 'V': digit = 5;
case 'X': digit = 10;
case 'L': digit = 50;
case 'C': digit = 100;
case 'D': digit = 500;
case 'M': digit = 1000;
default: return .Err;
}

total += digit;

// If there is a previous digit and digit is greater than previous digit,
// subtract two times previous digit from total to compensate for addition of
// previous digit
if (prevDigit > 0 && digit > prevDigit)
{
total -= 2 * prevDigit;
prevDigit = 0;
}

prevDigit = digit;
}

return .Ok(total);
}

public static int Main(String[] args)
{
if (args.Count < 1)
{
Usage();
}

switch (RomanNumeral(args[0]))
{
case .Ok(let result):
Console.WriteLine(result);
case .Err:
Error();
}

return 0;
}
}
2 changes: 1 addition & 1 deletion archive/b/beef/testinfo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ folder:

container:
image: "rzuckerm/beef"
tag: "0.43.5a2-4"
tag: "0.43.5a4-6"
build: |
sh -c "BeefBuild -new -workspace={{ source.name }} && \
cp {{ source.name }}{{ source.extension }} {{ source.name }}/src/Program.bf && \
Expand Down

0 comments on commit 3b6737e

Please sign in to comment.