-
-
Notifications
You must be signed in to change notification settings - Fork 445
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
base: master
Are you sure you want to change the base?
Conversation
please add the label 'hacktoberfest-accepted' when accepting the pr request |
There was a problem hiding this 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.
strings/palindrome.dart
Outdated
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'); | ||
} |
There was a problem hiding this comment.
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.
strings/palindrome.dart
Outdated
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'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
write tests.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
strings/palindrome.dart
Outdated
@@ -0,0 +1,16 @@ | |||
void main(){ |
There was a problem hiding this comment.
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.
strings/palindrome.dart
Outdated
String? original = 'hannah'; | ||
|
||
// then we will reverse the input | ||
String? reverse = original.split('').reversed.join(''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
avoid printing
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 |
There was a problem hiding this 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.
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'); | ||
} | ||
|
There was a problem hiding this comment.
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?
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))); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add negative cases
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); | ||
} |
There was a problem hiding this comment.
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.
Welcome to Dart community
Describe your change:
Checklist:
Fixes: #{$ISSUE_NO}
.