Exercises
- Exercise 1:
- Exercise 2:
- Exercise 3:
- Exercise 4:
- Exercise 5:
- Exercise 6:
- Exercise 7:
- Exercise 8:
- Exercise 9:
- Exercise 10:
Write a Dart function that checks whether a passed string is palindrome or not?
A palindrome is word, phrase, or sequence that reads the same backward as forward, e.g., madam or nursesrun.
Solution:
bool palindrome(String txt) {
int index = 0;
while ((index < txt.length ~/ 2) &&
(txt[index] == txt[txt.length - index - 1])) index++;
return index == txt.length ~/ 2;
}
bool palindrome(String txt) {
for (int index = 0; index < txt.length ~/ 2; index++) {
if (txt[index] != txt[txt.length - index - 1]) return false;
}
return true;
}
Write a Dart function that accepts three integer n ,min (optional parameter and have a default value of 1) and max then return a list of n random numbers between min and max.
Solution:
import 'dart:math';
List randoms(int n, int max, {int min = 0}) {
List list = [];
for (int i = 0; i < n; i++) {
var x = Random().nextInt(max - min + 1) + min;
list.add(x);
}
return list;
}
Write a Dart function that accepts a String as a parameter and delete all vowels from it (A, E, I, O, and U).
Solution:
String deleteVowels(String txt) {
String newTxt = "";
for (int i = 0; i < txt.length; i++) {
if (!['A', 'E', 'I', 'O', 'U'].contains(txt[i].toUpperCase())) {
newTxt += txt[i];
}
}
return newTxt;
}
Write a Dart function which will take an array of numbers stored and find the second lowest and second greatest numbers, respectively.
Sample array : [7,2,10,41,35]
Expected Output : 7,35
Solution:
void second(List list) {
//finding the max and the min
int min = list[0];//initialization of the min and the max
int max = list[0];//with the first value in the list
for (int i = 1; i < list.length; i++) {
if (list[i] > max) max = list[i];
if (list[i] < min) min = list[i];
}
//finding the second max and the second min
int secondMin = max;
int secondMax = min;
for (int i = 0; i < list.length; i++) {
if ((list[i] > secondMax) && (list[i] != max)) secondMax = list[i];
if ((list[i] < secondMin) && (list[i] != min)) secondMin = list[i];
}
//printing the result
print("Second Min = $secondMin and Second Max = $secondMax");
}
Write a Dart function that accepts a string as a parameter and converts the first letter of each word of the string in upper case.
Example string : 'the best day ever'
Expected Output : 'The Best Day Ever '
Solution:
String firstUpper(String txt) {
var newTxt = " ";
if (txt[0] != ' ') newTxt = txt[0].toUpperCase();
for (int i = 1; i < txt.length; i++) {
if ((txt[i - 1] == ' ') && (txt[i] != ' ')) {
newTxt += txt[i].toUpperCase();
} else
newTxt += txt[i];
}
return newTxt;
}
Write a Dart function that accepts a string as a parameter and find the longest word within the string.
Example string : 'we are the new generation'
Expected Output : 'generation'
Assume one space between words in the passed string.
Solution:
String longest(String ch) {
var chList = ch.split(' ');
int max = 0;
for (var element in chList) {
if (element.length > chList[max].length) max = chList.indexOf(element);
}
return chList[max];
}
Write a Dart function that returns a passed lowercase string with letters in alphabetical order.
Example string : 'flutter'
Expected Output : 'eflrttu'
Assume punctuation and numbers symbols are not included in the passed string.
Solution:
String order(String ch) {
var chList = ch.split('');
var result = '';
//0 <= ASCII <= 128
var chCodes = ch.codeUnits.toList();
for (int i = 0; i < chList.length; i++) {
var min = 0;
for (int j = 0; j < chList.length; j++) {
if (chCodes[j] < chCodes[min]) min = j;
}
result += chList[min];
//999>128 now this element can't be min again so we eliminate it
chCodes[min] = 999;
}
return result;
}
Write a Dart function that accepts a string and calculate the number of upper case letters, lower case letters,numbers and symbols(we don't take in count the space).
Solution:
void calculate(String txt) {
var upper = 0, lower = 0, numbers = 0, symbols = 0;
for (int i = 0; i < txt.length; i++) {
if (txt[i] != ' ') {
if ((txt.codeUnitAt(i) >= 'A'.codeUnitAt(0)) &&
(txt.codeUnitAt(i) <= 'Z'.codeUnitAt(0)))
upper++;
else if ((txt.codeUnitAt(i) >= 'a'.codeUnitAt(0)) &&
(txt.codeUnitAt(i) <= 'z'.codeUnitAt(0)))
lower++;
else if ((txt.codeUnitAt(i) >= '0'.codeUnitAt(0)) &&
(txt.codeUnitAt(i) <= '9'.codeUnitAt(0)))
numbers++;
else
symbols++;
}
}
print(
"upper case letters:$upper, lower case letters:$lower, numbers:$numbers and symbols:$symbols");
}
Write a Dart function that prints the characters that occurs two times in a given string.
Example String : 'Firebase and Flutter'
Expected Output : 'F R A T'
Solution:
String occursTwoTime(String txt) {
var result = "";
int occ;
for (int i = 0; i < txt.length; i++) {
occ = 0;
for (int j = 0; j < txt.length; j++) {
if (txt[j].toUpperCase() == txt[i].toUpperCase()) occ++;
}
if ((occ == 2) && (!result.contains(txt[i].toUpperCase())))
result += " " + txt[i].toUpperCase();
}
return result;
}
Write a Dart recursive function to compute the factors of a positive integer.
Solution:
int fact(int x) {
if (x == 1)
return 1;
else
return x * fact(x - 1);
}