Roman numerals/Encode: Difference between revisions
Content added Content deleted
(→{{header|GW-BASIC}}: Added.) |
|||
Line 5,960: | Line 5,960: | ||
<syntaxhighlight lang="python">import roman |
<syntaxhighlight lang="python">import roman |
||
print(roman.toRoman(2022))</syntaxhighlight> |
print(roman.toRoman(2022))</syntaxhighlight> |
||
===Minimalistic structuralism=== |
|||
<syntaxhighlight lang="python">def toRoman(n): |
|||
res='' |
|||
reg=n |
|||
if reg<4000: |
|||
while reg>=1000: |
|||
res+='M' |
|||
reg-=1000 |
|||
if reg>=900: |
|||
res+='CM' |
|||
reg-=900 |
|||
if reg>=500: |
|||
res+='D' |
|||
reg-=500 |
|||
if reg>=400: |
|||
res+='CD' |
|||
reg-=400 |
|||
while reg>=100: |
|||
res+='C' |
|||
reg-=100 |
|||
if reg>=90: |
|||
res+='XC' |
|||
reg-=90 |
|||
if reg>=50: |
|||
res+='L' |
|||
reg-=50 |
|||
if reg>=40: |
|||
res+='XL' |
|||
reg-=40 |
|||
while reg>=10: |
|||
res+="X" |
|||
reg-=10 |
|||
if reg>=9: |
|||
res+='IX' |
|||
reg-=9 |
|||
if reg>=5: |
|||
res+='V' |
|||
reg-=5 |
|||
if reg>=4: |
|||
res+='IV' |
|||
reg-=4 |
|||
while reg>0: |
|||
res+='I' |
|||
reg-=1 |
|||
return res</syntaxhighlight> |
|||
===Imperative=== |
===Imperative=== |