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

Create palindrome.dart #187

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

satyamnoob
Copy link

@satyamnoob satyamnoob commented Oct 7, 2022

Welcome to Dart community

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Dart files are placed inside an existing directory.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@satyamnoob
Copy link
Author

please add the label 'hacktoberfest-accepted' when accepting the pr request

@satyamnoob satyamnoob closed this Oct 7, 2022
@satyamnoob satyamnoob reopened this Oct 7, 2022
Copy link
Member

@akashgk akashgk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the requested changes.

Comment on lines 2 to 14
print('Enter Words or number');
// User enter a string or a number
String? original = 'hannah';

// then we will reverse the input
String? reverse = original.split('').reversed.join('');
// then we will compare
if(original == reverse)
{
print('Its A Palindrome');
}else{
print('Its A Not Palindrome');
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we optimise this? can we have an algorithm with lesser space complexity?

Also reduce the use of inbuilt functions like reversed. split and join. The Algorithm will be quite inefficient.

Comment on lines 2 to 11
print('Enter Words or number');
// User enter a string or a number
String? original = 'hannah';

// then we will reverse the input
String? reverse = original.split('').reversed.join('');
// then we will compare
if(original == reverse)
{
print('Its A Palindrome');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write tests.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am very new to this. What are tests? I will be updating everything as you requested

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check out other files. to see how tests are implemented.

You can also read up from the official docs:
https://pub.dev/packages/test

@@ -0,0 +1,16 @@
void main(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create a function outside main which checks for palindrome.

String? original = 'hannah';

// then we will reverse the input
String? reverse = original.split('').reversed.join('');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid printing

@satyamnoob
Copy link
Author

I have added the tests and other requirements. Do I need to do another pull request or you can see the changes? Sorry for asking dumb questions

Copy link
Member

@akashgk akashgk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address the change requests.

Comment on lines +19 to +42
if (original == reverseUsingLoop) {
// print('Its A Palindrome');
} else {
// print('Its A Not Palindrome');
}

if (original == reverseUsingSplit) {
// print('Its A Palindrome');
} else {
// print('Its A Not Palindrome');
}

if (original == reverseUsingRunes) {
// print('Its A Palindrome');
} else {
// print('Its A Not Palindrome');
}

if (original == reverseUsingCodeUnits) {
// print('Its A Palindrome');
} else {
// print('Its A Not Palindrome');
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the use of this code section?

Comment on lines +44 to +61
group('Palindrome Check', () {
test('Using loop to reverse a string.', () {
var string = 'hannah';
expect(string, equals(reverseStringUsingLoop(string)));
});
test('Using split to reverse a string.', () {
var string = 'hannah';
expect(string, equals(reverseStringUsingSplit(string)));
});
test('Using runes to reverse a string.', () {
var string = 'hannah';
expect(string, equals(reverseStringUsingRunes(string)));
});
test('Using code units to reverse a string.', () {
var string = 'hannah';
expect(string, equals(reverseStringUsingCodeUnits(string)));
});
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add negative cases

Comment on lines +64 to +85
String reverseStringUsingLoop(String input) {
String reversed = "";
for (int index = input.length - 1; index >= 0; index--) {
reversed += input[index];
}
return reversed;
}

String reverseStringUsingSplit(String input) {
var chars = input.split('');
return chars.reversed.join();
}

String reverseStringUsingRunes(String input) {
var chars = input.runes.toList();

return String.fromCharCodes(chars.reversed);
}

String reverseStringUsingCodeUnits(String input) {
return String.fromCharCodes(input.codeUnits.reversed);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the reversal logic are running on O(N) space. can we optimise to make it run on O(1) space.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants