Skip to content

Commit 25cb2cc

Browse files
committed
challenge 3
1 parent 2066940 commit 25cb2cc

File tree

6 files changed

+156
-0
lines changed

6 files changed

+156
-0
lines changed

1-evenFibonacci/submission4.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Written by Miracul!cKz#6933
2+
# 03/02/2020
3+
4+
romans = []
5+
import csv
6+
with open('roman_numerals.txt', newline='') as csvfile:
7+
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
8+
for row in spamreader:
9+
romans.append(row)
10+
romans = romans[0]
11+
#print(romans[0][1])
12+
#print(len(romans))
13+
arabec = np.zeros(len(romans))
14+
for i in range(len(romans)):
15+
for j in range(len(romans[i])):
16+
if romans[i][j] == "M":
17+
arabec[i] += 1000
18+
elif romans[i][j] == "D":
19+
arabec[i] += 500
20+
elif romans[i][j] == "C":
21+
arabec[i] += 100
22+
elif romans[i][j] == "L":
23+
arabec[i] += 50
24+
elif romans[i][j] == "X":
25+
arabec[i] += 10
26+
elif romans[i][j] == "V":
27+
arabec[i] += 5
28+
elif romans[i][j] == "I":
29+
arabec[i] += 1
30+
31+
print(sum(arabec))
32+

3-romanNumerals/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Welcome to the third #code4bytes challenge!
2+
We find ourselves in the city of Rome, you are working as a historian in a museum and are tasked with converting roman numerals (I, V, X,...) to arabic numerals (1,2,3,4,5,...). For your convenience, an intern has typed in all the roman numerals into a text file (`roman_numerals.txt`) separated by commas. Your boss asked you to manually convert them but fortunately enough you've been hanging around in the *CodingWeb* server where you met some good people who taught you a thing or two about coding, so you think you can automate this boring task!
3+
```
4+
Read the roman numerals from roman_numerals.txt and convert them into arabic numerals.
5+
What is the total sum of those arabic numerals?
6+
7+
Example:
8+
I, II, III, IV, V
9+
10+
1 + 2 + 3 + 4 + 5 = 15
11+
```
12+
This challenge is quite a bit easier than the previous ones. Because of this, the first place shal recieve 500 bytes, second place 400, thrid 300 and everybody else 200.
13+
14+
Have fun, good luck and happy coding!

3-romanNumerals/generate_numbers.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
roman = ['M', 'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I']
2+
num = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
3+
4+
rom_to_num = {"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000}
5+
num_to_rom = {1:"I", 5:"V", 10:"X", 50:"L", 100:"C", 500:"D", 1000:"M"}
6+
7+
def num_to_roman(n):
8+
result = []
9+
for i in range(len(roman)):
10+
count = int(n / num[i])
11+
result.append(roman[i] * count)
12+
n -= num[i] * count
13+
14+
r = "".join(result)
15+
return r
16+
17+
def roman_to_num(r):
18+
count = 0
19+
for i in range(len(r)):
20+
try:
21+
if(rom_to_num[r[i+1]] > rom_to_num[r[i]]):
22+
count -= rom_to_num[r[i]]
23+
else:
24+
count += rom_to_num[r[i]]
25+
26+
except:
27+
count += rom_to_num[r[i]]
28+
return count
29+
30+
import random
31+
32+
33+
for i in range(1,3999):
34+
x = num_to_roman(i)
35+
if(i != roman_to_num(x)):
36+
print("WOOPS {} {}".format(x, i))
37+
38+
with open("roman_numerals.txt", "w") as file:
39+
count = 0
40+
for i in range(100):
41+
x = random.randint(1,3999)
42+
count += x
43+
s = num_to_roman(x)
44+
file.write(s + ",")
45+
46+
print(count)

3-romanNumerals/roman_numerals.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MCDLVIII,MDCLXIV,DXVIII,MMCMXXVI,CCCLXXIV,DCCLXVIII,MMCXCI,MCCCXXIV,MMCMXXIX,MMMCCLXXVII,MMCDXL,MMMCCCLXXIII,MMDCCCLVIII,MMMCXXXV,DCXL,MMCMLXX,DCCXXXVI,CLII,MMMCMLXXIII,MMMCDXLII,MMCCL,DLXXXIV,DCLXXIX,MMMDXLVII,MDCXXV,MMMXCVII,MCCXCVII,MMDCCCLV,MMMCCCLXII,MCCCXLII,MMMCLXXI,MMMCMXL,CCCXXXIV,MMMCDXLII,DCLX,CDXX,CXIX,CDLXXXV,MCCXXIV,MMMDCLI,MMMCXLII,DXIV,CMXCVIII,DCLXXXIV,MDCCCLIX,MCMLXX,DCCCXVIII,MDLVII,MMMCMXCIX,MMMCMXLV,MCMXCIII,CCXVI,MMMDCCCXLIV,MMMCXL,MMCCXLIX,MDXXIII,DCCXXIV,MMLXII,DXXXVII,DLXXV,MLII,MDCLXIV,MMMCCLXXIII,MCMLXXIV,MMCLX,MMMDCCXXXIX,CCXXV,MCIII,CXL,CCLXXXIX,MMMXCII,MMCDLVII,DCCLXXXI,CCXLVII,MMDV,MCCCLI,MMDXLVIII,MMDXCII,MI,DCCCLXXIV,MMMDLXXIV,MMMLXVI,MMCDXX,DXLII,DLXXXIII,MMMCDLXVII,MDCCCLXXXVI,MMMCDLXV,CLXVI,CLXI,MMMDXLIX,CLXIII,MMDCCCLIX,MMDCCCXIX,MMCLIV,MMMDLXIX,MCVII,MMIV,MMMDCCCLXXII,MMMDCLXIV

3-romanNumerals/submission1.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Written by manish#9999
2+
# 02/02/2020
3+
4+
def roman_to_int(roman):
5+
values = {'M': 1000,
6+
'D': 500,
7+
'C': 100,
8+
'L': 50,
9+
'X': 10,
10+
'V': 5,
11+
'I': 1
12+
}
13+
numbers = [values[ch] for ch in roman]
14+
total = 0
15+
n2 = 0
16+
for n1, n2 in zip(numbers, numbers[1:]):
17+
if n1 >= n2:
18+
total += n1
19+
else:
20+
total -= n1
21+
return total + n2
22+
23+
with open("roman_numerals.txt", "r") as f:
24+
data = f.read()
25+
f.close()
26+
27+
total = 0
28+
for each in data.split(","):
29+
total += roman_to_int(each)
30+
print(total)

3-romanNumerals/submission2.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Written by frog#7088
2+
# 02/02/2020
3+
4+
import csv
5+
romanNumerals = {
6+
"I" : 1,
7+
"V" : 5,
8+
"X" : 10,
9+
"L" : 50,
10+
"C" : 100,
11+
"D" : 500,
12+
"M" : 1000
13+
}
14+
romanNumeralsData= [row for row in csv.reader(open("roman_numerals.txt", newline=""))][0]
15+
16+
def parseRomanNumerals(string):
17+
string = list(string)
18+
for x in range(len(string)):
19+
string[x] = romanNumerals[string[x]]
20+
addingNumber = 0
21+
for x in range(len(string)):
22+
try:
23+
if string[x+1] > string[x]:
24+
addingNumber += -string[x]
25+
else:
26+
addingNumber += string[x]
27+
except:
28+
addingNumber += string[x]
29+
return addingNumber
30+
adding = 0
31+
for x in romanNumeralsData:
32+
adding += parseRomanNumerals(x)
33+
print(adding)

0 commit comments

Comments
 (0)