Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dart/difference-of-squares] Create mentoring.md #2225

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

smanookian
Copy link
Contributor

added mentoring notes for difference of squares

@github-actions github-actions bot added track/dart Dart track type/mentor-notes Mentor notes labels Sep 20, 2022
@siebenschlaefer
Copy link

siebenschlaefer commented Sep 21, 2022

Do we really recommend the "manual" approach where numbers from 1 to n are generated and summed up?
There's a direct and much more efficient solution, and I think that's what the last paragraph in the instructions is all about ("You are not expected to discover an efficient solution to this yourself from first principles; research is allowed, indeed, encouraged. Finding the best algorithm for the problem is a key skill in software engineering.")
At least for me the core of this exercise is finding the closed formulas for squareOfSum() and sumOfSquares().

@angelikatyborska angelikatyborska changed the title Create mentoring.md Dart difference-of-squares: Create mentoring.md Sep 21, 2022
@smanookian
Copy link
Contributor Author

How do other tracks solve this exercise? Or how did you solve it?

@kotp
Copy link
Member

kotp commented Sep 21, 2022

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:

<language>/exercises/practice/difference-of-squares/.meta/example.*

@siebenschlaefer
Copy link

siebenschlaefer commented Sep 21, 2022

A good efficient solution in Python would look like this.

I don't know Dart at all but I guess a good efficient solution could look similar to this:

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);
}

@kotp
Copy link
Member

kotp commented Sep 21, 2022

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 example.dart looks currently, and it iterates, rather than using the formula that can be used.

@siebenschlaefer
Copy link

In a discussion two or three years ago I was told that the example solution from the .meta directory is not necessarily the best (easiest to read, most idiomatic, most efficient) solution, it's more like proof that the exercise can be solved, that it's possible to pass the tests.

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.

@kotp
Copy link
Member

kotp commented Sep 21, 2022

In a discussion two or three years ago I was told that the example solution from the .meta directory is not necessarily the best (easiest to read, most idiomatic, most efficient) solution, it's more like proof that the exercise can be solved, that it's possible to pass the tests.

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, example.ext1, and for concept exercises, exemplar.ext1.


1: replacing, of course, the ext for the proper extension.

@SleeplessByte
Copy link
Member

@smanookian do you feel like making changes to the notes based on the discussion above?

@SleeplessByte SleeplessByte changed the title Dart difference-of-squares: Create mentoring.md [dart/difference-of-squares] Create mentoring.md Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
track/dart Dart track type/mentor-notes Mentor notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants