CodeWars符号化問題2021/01/24-Upside down numbers
[質問]
Consider the numbers 6969
and 9116
. When you rotate them 180 degrees
(upside down), these numbers remain the same. To clarify, if we write them down on a paper and turn the paper upside down, the numbers will be the same. Try it and see! Some numbers such as 2
or 5
don't yield numbers when rotated.
Given a range, return the count of upside down numbers within that range. For example, solve(0,10) = 3
, because there are only 3
upside down numbers >= 0 and < 10
. They are 0, 1, 8
.
More examples in the test cases.
(要約)所定の範囲内の数字を時計回りに180度回転させたとき、元の数字と同じ数字の個数を見つけます.
function solve(x, y) {
let answer = 0;
let check = true;
const updown = {
'1': '1',
'6': '9',
'8': '8',
'9': '6',
'0': '0'
};
for(let i = x; i < y; i++) {
const numToStr = `${i}`;
if(numToStr.includes('2') || numToStr.includes('3') || numToStr.includes('4') || numToStr.includes('5') || numToStr.includes('7')) {
continue;
}
else {
const changeNum = `${i}`.split('').reverse().join('');
check = true;
for(let k in changeNum) {
if(`${i}`[k] !== updown[changeNum[k]]) {
check = false;
break;
}
}
if(check) answer++;
}
}
return answer;
};
まず、反転した数値をオブジェクトにし、繰り返し文を使用して所定の範囲内で数値を使用します。
数値を反転させる効果を得るには、数値の順序を反転させ、各数値を反転時に変更した数値に変更します。
そして同じものしか見つからず、その数を返します。
Reference
この問題について(CodeWars符号化問題2021/01/24-Upside down numbers), 我々は、より多くの情報をここで見つけました
https://velog.io/@hemtory/CodeWars20210124
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
Reference
この問題について(CodeWars符号化問題2021/01/24-Upside down numbers), 我々は、より多くの情報をここで見つけました https://velog.io/@hemtory/CodeWars20210124テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol