Codewarsテーマ解析
最近Codewarsのテーマをいくつか塗って、以下のように整理して、みんなと分かち合います!
1.Roman Numerals Encoder
Create a function taking a positive integer as its parameter and returning a string containing the Roman Numeral representation of that integer.
Modern Roman numerals are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC. 2008 is written as 2000=MM, 8=VIII; or MMVIII. 1666 uses each Roman symbol in descending order: MDCLXVI.
Example:
Help:
Remember that there can't be more than 3 identical symbols in a row.
正数nを与えて、この数をローマ数字で表す.
考え方分析:英語を読んだ後のあなたは、最初は必ず霧がかかっています.問題の意味が分かってから、まず疑問なのは、この転化規則はどのように整頓されているのかということです.心の中でひそかに出題者本人に挨拶して、それから考え込んでいます...
このルールを理解するために、問題を見て、ローマの数字の列には3つの繰り返しの数字がないと言って、例を挙げ始めました.
1-I
2-II
3-III
4-IV
発見していないで、また本当にこのようにして、更に例を挙げ続けます
5-V
6-VI
7-VII
8-VIII
9-IX
えっ?どうしてこう表すのですか.さらに10-Xを見てみると、このような数字は9=10-1であるべきだと推測し、9を表すときは10と1を逆さにして表す.推測を検証するために,900−CM,90−XCを見て,推測が正しく,法則を表す謎が解けた.
では、このような変換をプログラムで行うにはどうすればいいのでしょうか.私たちはまだ古い考えで、特殊から一般まで:
例えば私たちの数字は15ですが、どうやって彼をXVに変換することができますか?15=10+5であることから,15の数字組成を減算して得ることが考えられる.15-10=5なので、15が表すローマ数字は必ずX、5-5=0で終わるので、15=XVです.ディクショナリでは、数字と文字の対応関係を設定できます.最大の数字から減らさなければならないことに注意してください.例えば、15-5=10ではなく、前の数字がVで、明らかに間違っています.そのため、dictionaryの値をソートします.sorted関数を用いてソートした.
コードは比較的簡単で、詳しく説明していません.
2.Sum of Digits/Digital Root
In this kata, you must create a
A digital root is the recursive sum of all the digits in a number. Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. This is only applicable to the natural numbers.
Here's how it works:
実は問題の意味は明らかで、例はすでに私たちに関数の機能が何であるかを教えてくれました.
考え方:与えられた整数の各ビット数を個別に分割して累積し,すでにビット数であるか否かを判断する.考え方は簡単ですが、pythonの文法に慣れていないと迂回されます.
注意:
6/10=0.6
6//10=0
pythonにはこのような文法規則があり、最初の構想コードは以下の通りです.
2つ目の考え方:
python内蔵関数sumとmapを用いて、コードは以下の通りです.
1.Roman Numerals Encoder
Create a function taking a positive integer as its parameter and returning a string containing the Roman Numeral representation of that integer.
Modern Roman numerals are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC. 2008 is written as 2000=MM, 8=VIII; or MMVIII. 1666 uses each Roman symbol in descending order: MDCLXVI.
Example:
solution(1000) # should return 'M'
Help:
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1,000
Remember that there can't be more than 3 identical symbols in a row.
正数nを与えて、この数をローマ数字で表す.
考え方分析:英語を読んだ後のあなたは、最初は必ず霧がかかっています.問題の意味が分かってから、まず疑問なのは、この転化規則はどのように整頓されているのかということです.心の中でひそかに出題者本人に挨拶して、それから考え込んでいます...
このルールを理解するために、問題を見て、ローマの数字の列には3つの繰り返しの数字がないと言って、例を挙げ始めました.
1-I
2-II
3-III
4-IV
発見していないで、また本当にこのようにして、更に例を挙げ続けます
5-V
6-VI
7-VII
8-VIII
9-IX
えっ?どうしてこう表すのですか.さらに10-Xを見てみると、このような数字は9=10-1であるべきだと推測し、9を表すときは10と1を逆さにして表す.推測を検証するために,900−CM,90−XCを見て,推測が正しく,法則を表す謎が解けた.
では、このような変換をプログラムで行うにはどうすればいいのでしょうか.私たちはまだ古い考えで、特殊から一般まで:
例えば私たちの数字は15ですが、どうやって彼をXVに変換することができますか?15=10+5であることから,15の数字組成を減算して得ることが考えられる.15-10=5なので、15が表すローマ数字は必ずX、5-5=0で終わるので、15=XVです.ディクショナリでは、数字と文字の対応関係を設定できます.最大の数字から減らさなければならないことに注意してください.例えば、15-5=10ではなく、前の数字がVで、明らかに間違っています.そのため、dictionaryの値をソートします.sorted関数を用いてソートした.
def solution(n):
# TODO convert int to roman string
finalStr=""
translationRule={1000:'M',900:'CM',500:'D',400:'CD',100:'C',90:'XC',50:'L',40:'XL',10:'X',9:'IX',5:'V',4:'IV',1:'I'}
for x in sorted(translationRule.keys(),reverse=True):#reverse=true
while n>=x:
finalStr+=translationRule[x]
n-=x
return finalStr
コードは比較的簡単で、詳しく説明していません.
2.Sum of Digits/Digital Root
In this kata, you must create a
digital root
function. A digital root is the recursive sum of all the digits in a number. Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. This is only applicable to the natural numbers.
Here's how it works:
digital_root(16)
=> 1 + 6
=> 7
digital_root(942)
=> 9 + 4 + 2
=> 15 ...
=> 1 + 5
=> 6
digital_root(132189)
=> 1 + 3 + 2 + 1 + 8 + 9
=> 24 ...
=> 2 + 4
=> 6
digital_root(493193)
=> 4 + 9 + 3 + 1 + 9 + 3
=> 29 ...
=> 2 + 9
=> 11 ...
=> 1 + 1
=> 2
実は問題の意味は明らかで、例はすでに私たちに関数の機能が何であるかを教えてくれました.
考え方:与えられた整数の各ビット数を個別に分割して累積し,すでにビット数であるか否かを判断する.考え方は簡単ですが、pythonの文法に慣れていないと迂回されます.
注意:
6/10=0.6
6//10=0
pythonにはこのような文法規則があり、最初の構想コードは以下の通りです.
def digital_root(n):
while n//10!=0 :
temp=n;
n=0;
while temp!=0 :
n=n+temp%10
temp=temp//10
return n
2つ目の考え方:
python内蔵関数sumとmapを用いて、コードは以下の通りです.
def digital_root(n):
# ...
while n>9:
n=sum(map(int,str(n)))#sum , : 、 、 .
return n
#map 9 ,