Skip to content

Instantly share code, notes, and snippets.

@gcasa
Created April 26, 2022 07:38
Show Gist options
  • Save gcasa/413aa8ad98b3d742915571fefcd68773 to your computer and use it in GitHub Desktop.
Save gcasa/413aa8ad98b3d742915571fefcd68773 to your computer and use it in GitHub Desktop.
Just a quick set of funcitons
// Released under the terms of the LGPL 2.1
#import <Foundation/Foundation.h>
// Written with a little help from Copilot... this is a test of that functionality
// Create a function to convert a roman numeral to an integer
NSNumber *convertRomanToInt(NSString *string) {
// Create a dictionary of roman numerals and their values
NSDictionary *romanNumerals = @{
@"I": @1,
@"V": @5,
@"X": @10,
@"L": @50,
@"C": @100,
@"D": @500,
@"M": @1000
};
// Create a variable to hold the value of the roman numeral
NSNumber *value = [romanNumerals objectForKey:string];
// If the value is nil, return nil
if (value == nil) {
return nil;
}
// Return the value
return value;
}
NSNumber *convertRomanNumeralStringToNumber(NSString *string) {
// Create a variable to hold the value of the roman numeral
NSNumber *value = [NSNumber numberWithInt:0];
// Create a variable to hold the previous value of the roman numeral
NSNumber *previousValue = [NSNumber numberWithInt:0];
// Loop through the string
for (int i = 0; i < string.length; i++) {
// Get the current character
NSString *character = [string substringWithRange:NSMakeRange(i, 1)];
// Get the value of the current character
NSNumber *currentValue = convertRomanToInt(character);
// If the current value is nil, return nil
if (currentValue == nil) {
return nil;
}
// If the current value is less than the previous value, subtract the current value from the total
if ([currentValue intValue] < [previousValue intValue]) {
value = [NSNumber numberWithInt:([value intValue] - [currentValue intValue])];
}
// Otherwise, add the current value to the total
else {
value = [NSNumber numberWithInt:([value intValue] + [currentValue intValue])];
}
// Set the previous value to the current value
previousValue = currentValue;
}
// Return the value
return value;
}
NSString *convertNumberToRomanNumeralString(NSNumber *n) {
// Create a dictionary of roman numerals and their values
NSDictionary *romanNumerals = @{
@1: @"I",
@5: @"V",
@10: @"X",
@50: @"L",
@100: @"C",
@500: @"D",
@1000: @"M"
};
// Create a variable to hold the roman numeral string
NSString *romanNumeralString = @"";
// Create a variable to hold the current value
NSNumber *currentValue = [NSNumber numberWithInt:0];
// Create a variable to hold the previous value
NSNumber *previousValue = [NSNumber numberWithInt:0];
// Loop through the values
for (int i = 0; i < [n intValue]; i++) {
// Set the current value to the next value
currentValue = [NSNumber numberWithInt:([currentValue intValue] + 1)];
// If the current value is less than the previous value, subtract the current value from the total
if ([currentValue intValue] < [previousValue intValue]) {
currentValue = [NSNumber numberWithInt:([currentValue intValue] - [currentValue intValue])];
}
// Get the roman numeral for the current value
NSString *romanNumeral = [romanNumerals objectForKey:currentValue];
// Add the roman numeral to the roman numeral string
romanNumeralString = [romanNumeralString stringByAppendingString:romanNumeral];
// Set the previous value to the current value
previousValue = currentValue;
}
// Return the roman numeral string
return romanNumeralString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment