[dart/difference-of-squares] Create mentoring.md#2225
[dart/difference-of-squares] Create mentoring.md#2225smanookian wants to merge 1 commit intoexercism:mainfrom
Conversation
|
Do we really recommend the "manual" approach where numbers from |
|
How do other tracks solve this exercise? Or how did you solve it? |
|
The question of "How do other tracks solve this exercise" is easy to answer, as you can look in each tracks repository and find their solution in each of the files. For each track that has the exercise you will find the solution in: |
|
A I don't know Dart at all but I guess a import "dart:math" show pow;
class DifferenceOfSquares {
/// Calculate the square of the sum of 1 .. [n].
///
/// See https://en.wikipedia.org/wiki/Triangular_number
int squareOfSum(int n) => pow((n + 1) * n ~/ 2, 2) as int;
/// Calculate the sum of the squares 1^2 .. [n]^2.
///
/// See https://en.wikipedia.org/wiki/Square_pyramidal_number
int sumOfSquares(int n) => n * (n + 1) * (2 * n + 1) ~/ 6;
/// Calculate the difference between the square of the sum and the sum of the squares.
int differenceOfSquares(int n) => squareOfSum(n) - sumOfSquares(n);
} |
import "dart:math" show pow;
class DifferenceOfSquares {
int squareOfSum(int input) => pow(_sum(input), 2).toInt();
int sumOfSquares(int input) => _range(input).map((i) => pow(i, 2).toInt()).reduce((r, i) => r + i);
int differenceOfSquares(int input) => squareOfSum(input) - sumOfSquares(input);
num _sum(int input) => input * (input + 1) ~/ 2;
List<int> _range(int length) => new List<int>.generate(length, (i) => i + 1);
}This is how the |
|
In a discussion two or three years ago I was told that the example solution from the I just wanted to comment that an efficient implementation exists and to draw attention to the last paragraph of the instructions, IMHO they clearly hint to such an efficient implementation. But I have no skin in this game, I'm neither a Dart mentor nor do I know what Dart programmers would consider idiomatic. I'm fine with whatever you guys decide for these Dart mentoring notes. |
This is true for this exercise, if this is a practice exercise and not a concept exercise. I think that concept exercises should have an exemplar solution. I think, also, that there is the difference of that specific file being named, for practice, 1: replacing, of course, the |
|
@smanookian do you feel like making changes to the notes based on the discussion above? |
added mentoring notes for difference of squares