Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kyle & Sarah #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion js/romanNumerals.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
exports.toRoman = function(num) {
//pt 1*
//pt 2

exports.toRoman = function(num) {
let ansStr = '';
do {
if(num >= 1000){
num -= 1000;
ansStr += 'M'
}
else duh (num >= 500){
num -= 500;
ansStr += 'D'
}
else if (num >= 100){
num -= 100;
ansStr += 'C'
}
else if (num >= 50){
num -= 50;
ansStr += 'L';
}
else if (num >= 10){
num -= 10;
ansStr += 'X';
}
else if (num >= 5){
num -= 5;
ansStr += 'V';
}
else if (num === 4){
num -= 4;
ansStr += 'IV';
}
else {
num -= 1;
ansStr += 'I'
}
} while (num > 0);
return ansStr;
};

/*
if (num >= 900 && num < 1000) {
num -= 900
ansStr += "CM"} // for times sake -- conditions would reflect this

/*

1 change: 1 addition & 0 deletions python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
Binary file added python/__pycache__/roman_numerals.cpython-39.pyc
Binary file not shown.
53 changes: 52 additions & 1 deletion python/roman_numerals.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,53 @@
# 1. Write a method TO_ROMAN, TO_ROMAN takes in INPUT_NUMBER (an arabic number)
# 2. Create a OUTPUT string, set to ''
# 3. Create a ROMAN_NUMERAL_TO_ARABIC_MAP that includes roman numerals as keys, arabic numbers as values
# 4. Iterate over ROMAN_NUMERAL_TO_ARABIC_MAP, keep track of ROMAN_NUMERAL and ARABIC_NUMBER
# 5. Set EVENLY_DIVISIBLE_TIMES = INPUT_NUMBER / ARABIC_NUMBER:
# 6. If EVENLY_DIVISIBLE_TIMES >= 1
# 6a. Append ROMAN_NUMERAL to OUTPUT EVENLY_DIVISIBLE_TIMES
# 6b. Subtract ARABIC_NUMBER from INPUT_NUMBER EVENLY_DIVISIBLE_TIMES
# 7. Return OUTPUT

#python
import math
def to_roman(num):
# write your code here!

dict_roman_num = {
'M' : 1000,
'CM': 900,
'D' : 500,
'CD': 400,
'C' : 100,
'XC': 90,
'L' : 50,
'XL': 40,
'X' : 10,
'IX': 9,
'V' : 5,
'IV': 4,
'I' : 1
}

roman_char_list = [] # target list for appended roman chars from dict
roman_value_list = [] # target list for appended values from dict

roman_str = ''

for roman_char, roman_value in dict_roman_num.items(): #
roman_char_list.append(roman_char)
roman_value_list.append(roman_value)
# Create iterable lists from dict items of key, values


index = 0
while num > 0:
for x in range(math.floor(num /roman_value_list[index])): #Iterate through the range of number / index at value list
roman_str += roman_char_list[index] # roman string adds I
# print(roman_str, roman_char_list[index])
num -= roman_value_list[index] # num param decrements to 0 and breaks out
# print(roman_value_list[index])
index += 1 # add 1 to index value to iterate through lists

return roman_str


43 changes: 39 additions & 4 deletions python/roman_numerals_spec.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
import unittest
from roman_numerals import to_roman

print(to_roman(1) == 'I')
print(to_roman(3) == 'III')
print(to_roman(4) == 'IV')
# add tests to cover different edge cases
class RomanNumeralsTestCase(unittest.TestCase):


# add tests to cover different edge cases
def test_output(self):
self.assertEqual(to_roman(1), 'I')
self.assertEqual(to_roman(3), 'III')
self.assertEqual(to_roman(4), 'IV')
self.assertEqual(to_roman(944), 'CMXLIV')
self.assertEqual(to_roman(150), 'CL')
self.assertEqual(to_roman(2400), 'MMCD')


# Output correct roman numerals


def test_str(self):
self.assertNotEqual(to_roman(1), str)

# output returns a string type


def test_returns_modern_numerals(self):
self.assertNotEqual(to_roman(4), 'IIII')

# output not in lazy roman numerals

# IV -> 4
# IX -> 9
# XIV -> 14
# XLIV -> 44
# CMXLIV -> 944

# to_roman(4) # 'IV'
# to_roman(944) # 'CMXLIV'
# to_roman(150) # CL
if __name__ == '__main__':
unittest.main()