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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions strings/palindrome.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import 'package:test/test.dart';

void main() {
palindrome();
}

void palindrome() {
print('Enter Words or number');
// User enter a string or a number
String original = 'hannah';

// then we will reverse the input
String reverseUsingLoop = reverseStringUsingLoop(original);
String reverseUsingSplit = reverseStringUsingSplit(original);
String reverseUsingRunes = reverseStringUsingRunes(original);
String reverseUsingCodeUnits = reverseStringUsingCodeUnits(original);

// then we will compare (Use any one of the four)
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');
}

Comment on lines +19 to +42
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?

// test
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)));
});
});
Comment on lines +44 to +61
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

}

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);
}
Comment on lines +64 to +85
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.