From 7981d40a135877eb427873bb4de5625acbe8124d Mon Sep 17 00:00:00 2001 From: anastasiasoenjoto <54910828+anastasiasoenjoto@users.noreply.github.com> Date: Sun, 3 May 2020 16:33:07 -0700 Subject: [PATCH] create self-describing numbers lesson --- .../self-describing-numbers.english.md | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 curriculum/challenges/english/08-coding-interview-prep/rosetta-code/self-describing-numbers.english.md diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/self-describing-numbers.english.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/self-describing-numbers.english.md new file mode 100644 index 0000000000..a0d78a53f8 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/self-describing-numbers.english.md @@ -0,0 +1,99 @@ +--- +title: Self-describing numbers +id: 5eaf48389ee512d4d103684b +challengeType: 5 +--- + +## Description +
+There are several so-called "self describing" or "self-descriptive" integers.
+ +An integer is said to be "self-describing" if it has the property that, when digit positions are labeled 0 to N-1, the digit in each postion is eqal to the number of times that that digit appears in the number.
+ +For example, 2020 is a four-digit self describing number:
+ + +Self-describing numbers < 100.000.000 are: 1210, 2020, 21200, 3211000, 42101000. +
+ +## Instructions +
+ +Write a function that will check whether a given positive integer is self-describing. + +
+ +## Tests +
+ +```yml +tests: + - text: isSelfDescribing should be a function. + testString: assert(typeof isSelfDescribing=='function'); + - text: isSelfDescribing() should return a boolean. + testString: assert(typeof isSelfDescribing(2020) =='boolean'); + - text: isSelfDescribing(2020) should return true. + testString: assert.equal(isSelfDescribing(2020), true); + - text: isSelfDescribing(3021) should return false. + testString: assert.equal(isSelfDescribing(3021), false); + - text: isSelfDescribing(3211000) should return true. + testString: assert.equal(isSelfDescribing(3211000), true); +``` + +
+ +## Challenge Seed +
+ +
+ +```js +function isSelfDescribing(n) { + // Good luck! +} +``` + +
+ +
+ +## Solution +
+ + +```js +function isSelfDescribing(n) { + let digits = String(n).split(""); + digits = digits.map(function(e) {return parseInt(e)}); + let count = digits.map((x) => {return 0}) + digits.forEach((d) =>{ + if (d >= count.length) { + return false + } + count[d] += 1; + }); + + if (digits === count) { + return true; + } + if (digits.length != count.length) { + return false; + } + + for (let i=0; i< digits.length; i++){ + if (digits[i] !== count[i]) { + return false; + } + } + return true; +} + + +``` + +