exercism / dart

Exercism exercises in Dart.
https://exercism.org/tracks/dart
MIT License
57 stars 94 forks source link

Armstrong Numbers: do we want to add BigInt? #434

Closed kytrinyx closed 1 year ago

kytrinyx commented 1 year ago

The armstrong-numbers exercise has two new tests in problem-specifications.

New test specifications ``` { "uuid": "5ee2fdf8-334e-4a46-bb8d-e5c19c02c148", "description": "Armstrong number containing seven zeroes", "comments": [ "The numeric size of this input number is 108 bits. Consider skipping this test if appropriate." ], "scenarios": [ "big-integer" ], "property": "isArmstrongNumber", "input": { "number": 186709961001538790100634132976990 }, "expected": true } { "uuid": "12ffbf10-307a-434e-b4ad-c925680e1dd4", "description": "The largest and last Armstrong number", "comments": [ "The numeric size of this input number is 127 bits. Consider skipping this test if appropriate." ], "scenarios": [ "big-integer" ], "property": "isArmstrongNumber", "input": { "number": 115132219018763992565095597973971522401 }, "expected": true } ```

If I'm not mistaken this would require handling the new cases with BigInt.

Do you think this would be a good addition for this track?

Right now armstrong-numbers is listed as difficulty 1 (scale is 1-10). Would adding BigInt complicate matters enough that we would have to bump the difficulty up a bit? Do we want to do that?

I don't have a strong opinion, but at the moment I'm leaning slightly in favor of leaving the exercise as is. I'm happy to be convinced otherwise, though.

@Stargator @devkabiir Thoughts?

devkabiir commented 1 year ago

If I'm not mistaken this would require handling the new cases with BigInt.

You are correct, int in Dart is limited to 64 bits. We have to use BigInt

Do you think this would be a good addition for this track?

Yes, I have been bitten by not using the correct numeric type in my Databases and code in the past. It's helpful to know. As far as I know, we don't cover this anywhere? @Stargator

Right now armstrong-numbers is listed as difficulty 1 (scale is 1-10). Would adding BigInt complicate matters enough that we would have to bump the difficulty up a bit? Do we want to do that?

The complexity would not increase. It is a matter of changing types. If we must increase the difficulty, I would say increase by 0.5.

I don't have a strong opinion, but at the moment I'm leaning slightly in favor of leaving the exercise as is. I'm happy to be convinced otherwise, though.

The existing solutions would break for sure. However, it would be good dogfood for your test generator and see how it reacts?. Generating tests for this exercise would not be trivial!


To make the tests spec compliant and working, following changes would need to be made

example.dart ```dart class ArmstrongNumbers { /// The parameters and variables within the method that are set to final in order to prevent the accidentally modification of the value. /// As those variables are not needed to be changed. bool isArmstrongNumber(final String input) { final numOfDigits = input.length; var sum = BigInt.from(0); for (var count = 0; count < numOfDigits; count++) { final digitAsString = input.substring(count, count + 1); final digit = BigInt.parse(digitAsString); sum += digit.pow(numOfDigits); } return input == sum.toString(); } } ```
armstrong_numbers_test.dart Instead of using number literals, we need to use `''` ```diff - final bool result = armstrongNumbers.isArmstrongNumber(0); + final result = armstrongNumbers.isArmstrongNumber('0'); ```
kytrinyx commented 1 year ago

Excellent. Difficulties are ints, not floats, so we could bump it to 2.

I will give this a shot.

Stargator commented 1 year ago

As far as I know, we don't cover this anywhere? @Stargator

Correct. We don't use BigInt anywhere nor do we have anything related to databases.

I agree with devkabiir's response. This shouldn't be a change, despite breaking the current solutions.