Exercises
- Exercise 1 :
- Exercise 2 :
- Exercise 3 :
- Exercise 4 :
Create a enum Weekday with constants for MONDAY, TUESDAY,... until SUNDAY.
Write a program that takes a day from user using its reference (1,2..7) and print "Holiday" if the day is SATURDAY or SUNDAY else print "not Holiday".
Try to use only :
- enum
- Conditional Operators : condition ? expr1 : expr2
Solution:
import 'dart:io';
enum weekDay { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
void main() {
print("Give a day reference from 1 to 7");
var day = weekDay.values[int.parse(stdin.readLineSync()) - 1];
day == weekDay.SATURDAY || day == weekDay.SUNDAY
? print("Holiday")
: print("not Holiday");
}
3
not Holiday
6
Holiday
7
Holiday
Print the result of every question
1) Declare and initialize 2 lists :
- li1 : 1,2,3,4,5,6,7,8
- li2 : 0,1,4,5,6
2) Add 9 to li1 and add 7,8,9 to li2
3) Insert 0 at the beginning of li1 and insert 2,3 after 1 in li2
4) Replace the last element in li1 with 10 and Replace the first 3 elements in li2 with 11,12,13
5) Remove 5 and the element at index 3 from li1 and remove 3 elements with index from 4 to 6 from li2
6) Print "Yes" if li2 contains 7 else print "No"
7) Shuffle li1 and clear li2
8) Create new list containing the elements from 2 to 5 of li1.
Solution:
void main() {
var li1 = [1, 2, 3, 4, 5, 6, 7, 8];
var li2 = [0, 1, 4, 5, 6];
print("li1 = $li1 & li2 = $li2");
li1.add(9);
li2.addAll([7, 8, 9]);
print("li1 = $li1 & li2 = $li2");
li1.insert(0, 0);
li2.insertAll(2, [2, 3]);
print("li1 = $li1 & li2 = $li2");
li1[li1.length - 1] = 10;
li2.replaceRange(0, 3, [11, 12, 13]);
print("li1 = $li1 & li2 = $li2");
li1.remove(5);
li1.removeAt(3);
li2.removeRange(4, 7);
print("li1 = $li1 & li2 = $li2");
li2.contains(7) ? print("Yes") : print("No");
li1.shuffle();
li2.clear();
print("li1 = $li1 & li2 = $li2");
var li3 = li1.sublist(2, 5);
print("li1 = $li1 & li2 = $li2 & li3 = $li3");
}
li1 = [1, 2, 3, 4, 5, 6, 7, 8] & li2 = [0, 1, 4, 5, 6]
li1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] & li2 = [0, 1, 4, 5, 6, 7, 8, 9]
li1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] & li2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
li1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 10] & li2 = [11, 12, 13, 3, 4, 5, 6, 7, 8, 9]
li1 = [0, 1, 2, 4, 6, 7, 8, 10] & li2 = [11, 12, 13, 3, 7, 8, 9]
Yes
li1 = [8, 6, 1, 7, 4, 0, 2, 10] & li2 = []
li1 = [8, 6, 1, 7, 4, 0, 2, 10] & li2 = [] & li3 = [1, 7, 4]
Print the result of every question
1) Declare and initialize 2 Sets :
- boys : Peter,John,Jamie,Kit,Iain,Taylor
- girls : Lena,Emilia,Taylor,Sophie,Jamie,Maisie
2) Add Daniel to boys and Anna & Betsy to girls.
3) Create new set called "all" that contains both sets.
4) Create new set called "both" that contains the unisex names(both boys and girls).
5) Create new set called "onlyBoys" that contains all male-specific name (without unisex names).
6) remove all the unisex names from boys and remove "Lena" from girls.
7) Print Yes if "only" contains "Taylor" else print No.
8) Remove all elements from "all".
9) Create a list called "listOfBoys" from "onlyBoys" and print its length.
Solution:
void main() {
var boys = {"Peter", "John", "Jamie", "Kit", "Iain", "Taylor"};
var girls = {"Lena", "Emilia", "Taylor", "Sophie", "Jamie", "Maisie"};
print("boys = $boys & girls = $girls");
boys.add("Daniel");
girls.addAll({"Anna", "Betsy"});
print("boys = $boys & girls = $girls");
var all = boys.union(girls);
print("all = $all");
var both = boys.intersection(girls);
print("both = $both");
var onlyBoys = boys.difference(girls);
print("onlyBoys = $onlyBoys");
boys.removeAll(both);
print("boys = $boys");
girls.remove("Lena");
print("girls = $girls");
onlyBoys.contains("Taylor") ? print("Yes") : print("No");
all.clear();
print("all = $all");
var listOfBoys = onlyBoys.toList();
print("listOfBoys = $listOfBoys and its length = ${listOfBoys.length}");
}
boys = {Peter, John, Jamie, Kit, Iain, Taylor} & girls = {Lena, Emilia, Taylor, Sophie, Jamie, Maisie}
boys = {Peter, John, Jamie, Kit, Iain, Taylor, Daniel} & girls = {Lena, Emilia, Taylor, Sophie, Jamie, Maisie, Anna, Betsy}
all = {Peter, John, Jamie, Kit, Iain, Taylor, Daniel, Lena, Emilia, Sophie, Maisie, Anna, Betsy}
both = {Jamie, Taylor}
onlyBoys = {Peter, John, Kit, Iain, Daniel}
boys = {Peter, John, Kit, Iain, Daniel}
girls = {Emilia, Taylor, Sophie, Jamie, Maisie, Anna, Betsy}
No
all = {}
listOfBoys = [Peter, John, Kit, Iain, Daniel] and its length = 5
Print the result of every question
1) Declare and initialize this Map :
- student :
name : "John Kane"
gender : "Male"
age : 21
id : 12345678
phone : 565689891
email : "johnkane@gmail.com"
2) Add "stateID" : "WA" and "yearEnrolled" : 2017
3) Check if there is a key called "phone" then delete it else print "NO"
4) Print all keys
5) Check if there is a value equal to 20 then print "Yes" else print "No"
6) Print all values
7) Update the "age" with 23 and "phone" with 213456789 with diffrent methods
8) Print the length of this map
9) Clear it
Solution:
void main() {
var student = {
"name": "John Kane",
"gender": "Male",
"age": 21,
"id": 12345678,
"phone": 565689891,
"emil": "johnkane@gmail.com",
};
print("student = $student");
student.addAll({"statedID": "WA", "yearEnrolled": 2017});
print("student = $student");
student.containsKey("phone") ? student.remove("phone") : print("No");
print("student = $student");
print("keys = ${student.keys}");
student.containsValue(20) ? print("Yes") : print("No");
print("values = ${student.values}");
student.update("age", (value) => 23);
student["phone"] = 213456789;
print("student = $student");
print("length = ${student.length}");
student.clear();
print("student = $student");
}
student = {name: John Kane, gender: Male, age: 21, id: 12345678, phone: 565689891, emil: johnkane@gmail.com}
student = {name: John Kane, gender: Male, age: 21, id: 12345678, phone: 565689891, emil: johnkane@gmail.com, statedID: WA, yearEnrolled: 2017}
student = {name: John Kane, gender: Male, age: 21, id: 12345678, emil: johnkane@gmail.com, statedID: WA, yearEnrolled: 2017}
keys = (name, gender, age, id, emil, statedID, yearEnrolled)
No
values = (John Kane, Male, 21, 12345678, johnkane@gmail.com, WA, 2017)
student = {name: John Kane, gender: Male, age: 23, id: 12345678, emil: johnkane@gmail.com, statedID: WA, yearEnrolled: 2017, phone: 213456789}
length = 8
student = {}