diff --git a/exercise1.dart b/exercise1.dart index 36a79b9..859cefa 100644 --- a/exercise1.dart +++ b/exercise1.dart @@ -1,9 +1,34 @@ +import 'dart:io'; + /** * Exercise 1 * Create a program that asks the user to enter their name and their age. * Print out a message that tells how many years they have to be 100 years old. */ +import 'dart:io'; + +//my answer main(List args) { - -} \ No newline at end of file + maina(); + mainu(); +} + +void maina() { + print("Enter your name here:"); + String name = stdin.readLineSync(); + + print("Enter your Age here:"); + int n = int.parse(stdin.readLineSync()); + int sum = 100 - n; + print("Hey,$name! your have to add $sum to your age to be 100 years"); +} + +void mainu() { + stdout.write("What's your name? "); + String name = stdin.readLineSync(); + print("Hi, $name! What is your age?"); + int age = int.parse(stdin.readLineSync()); + int yearsToHunderd = 100 - age; + print("$name, You have $yearsToHunderd years to be 100"); +} diff --git a/exercise10.dart b/exercise10.dart new file mode 100644 index 0000000..e26e5d9 --- /dev/null +++ b/exercise10.dart @@ -0,0 +1,41 @@ +/** + * Exercise 10 + * Ask the user for a number and determine whether the number is prime or not. + * Do it using a function + */ +import 'dart:io'; + +main(List args) { + // prime(); + mainbu(); +} + +// void prime() { +// print("Enter your number here:"); +// int j = int.parse(stdin.readLineSync()); +// if (j % 2 == 1 || j % 3 ==1) { +// print("The Number is a PRIME Number"); +// } else if (j % 2 != 1 || j % 3 != 1) { +// print("The Number is NOT a PRIME Number"); +// }//else if(j % 3 == 0){ +// print("The Number is NOT a PRIME Number"); +// } +//} + +void mainbu() { + stdout.write("Please give us a number: "); + int chosenNumber = int.parse(stdin.readLineSync()); + checkPrime(chosenNumber); +} + +void checkPrime(int number) { + // List comprehensions + List a = [ + for (var i = 1; i <= number; i++) + if (number % i == 0) i + ]; + // Check for prime + a.length == 2 + ? print("The chosen number is a prime") + : print("The chosen number is not a prime"); +} diff --git a/exercise2.dart b/exercise2.dart index 5434e44..ca93e2b 100644 --- a/exercise2.dart +++ b/exercise2.dart @@ -3,7 +3,32 @@ * Ask the user for a number. * Depending on whether the number is even or odd, print out an appropriate message to the user. */ +import 'dart:io'; main(List args) { - -} \ No newline at end of file + maind(); + maina(); +} +//my answer +maind() { + print("Enter your number here:"); + int b = int.parse(stdin.readLineSync()); + //for(var i in b){ + if (b % 2 == 0) { + print("The Number you entered is an EVEN number"); + } else { + print("The Number you entered is an ODD number"); + } +} + +void maina() { + stdout.write("Hi, please choose a number: "); + int number = int.parse(stdin.readLineSync()); + if (number % 2 == 0) { + print("Chosen number is even"); + } else { + print("Chosen number is odd"); + } +} + +//} diff --git a/exercise3.dart b/exercise3.dart index e2700f1..5eccb91 100644 --- a/exercise3.dart +++ b/exercise3.dart @@ -6,5 +6,32 @@ */ main(List args) { - -} \ No newline at end of file + exercise(); + mainj(); +} + +void exercise() { + /// EXERCISE 33333 + var lista = [1, 1, 2, 3, 8, 13, 21, 34, 55, 89]; + var ert = 5; + + for (var ctr in lista) { + if (ctr < ert) { + print(ctr); + } + } +} + +void mainj() { + List a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]; + for (var i in a) { + if (i < 5) { + print(i); + } + } + // One liner + print([ + for (var i in a) + if (i < 5) i + ]); +} diff --git a/exercise4.dart b/exercise4.dart index dcb70d6..994a7ac 100644 --- a/exercise4.dart +++ b/exercise4.dart @@ -6,6 +6,31 @@ * For example, 13 is a divisor of 26 because 26 / 13 has no remainder. */ +import 'dart:io'; + main(List args) { - -} \ No newline at end of file + fuji(); + mainle(); +} + +void fuji() { + print("Enter your number here:"); + int deg = int.parse(stdin.readLineSync()); + dynamic i = new List(); + for (var b = 1; b <= deg; b++) { + if (deg % b == 0) { + i.add(b); + } + } + print(i); +} + +void mainle() { + stdout.write("Please choose a number: "); + int number = int.parse(stdin.readLineSync()); + for (var i = 1; i <= number; i++) { + if (number % i == 0) { + print(i); + } + } +} diff --git a/exercise5.dart b/exercise5.dart index 91c05bf..f92cfba 100644 --- a/exercise5.dart +++ b/exercise5.dart @@ -8,5 +8,24 @@ */ main(List args) { + example(); +} + +void example(){ + + List a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]; + List b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 89]; + Set c = {}; + for (var i in a) { + for (var j in b) { + if (i == j) { + c.add(i); + } + } + } + print(c.toList()); + // One liner using set intersections + print(Set.from(a).intersection(Set.from(b)).toList()); +} -} \ No newline at end of file + diff --git a/exercise6.dart b/exercise6.dart new file mode 100644 index 0000000..9467e46 --- /dev/null +++ b/exercise6.dart @@ -0,0 +1,26 @@ +/** + * Exercise 6 + * Ask the user for a string and print out whether this string is a palindrome or not. + * A palindrome is a string that reads the same forwards and backwards. + */ +import 'dart:io'; + +main(List args) { + //jumie(); + juke(); +} + +// void jumie() { +// print("Enter your word here:"); +// String name = stdin.readLineSync(); +// } + +void juke(){ + stdout.write("Please give a word: "); + String input = stdin.readLineSync().toLowerCase(); + String revInput = input.split('').reversed.join(''); + // Ternary operator + input == revInput + ? print("The word is palindrome") + : print("The word is not a palindrome"); +} diff --git a/exercise7.dart b/exercise7.dart new file mode 100644 index 0000000..4cdd5bc --- /dev/null +++ b/exercise7.dart @@ -0,0 +1,38 @@ +/** + * Exercise 7 + * Let’s say you are given a list saved in a variable: + * a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]. + * Write a Dart code that takes this list and makes a new list that has only the even + * elements of this list in it. + */ + +main(List args) { + fresh(); + tabe(); +} + +void fresh() { + var a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]; + for (var ctr in a) { + if (ctr % 2 == 0) { + print(ctr); + } + } +} + +void tabe() { + List a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]; + int i = 0; + List l = []; + for (var e in a) { + if (++i % 2 == 0) { + l.add(e); + } + } + print(l); + // One liner + print([ + for (var e in a) + if (++i % 2 == 0) e + ]); +} diff --git a/exercise8.dart b/exercise8.dart new file mode 100644 index 0000000..36b8924 --- /dev/null +++ b/exercise8.dart @@ -0,0 +1,52 @@ +/** + * Exercise 8 + * Make a two-player Rock-Paper-Scissors game against computer. + * Ask for player’s input, compare them, print out a message to the winner. + */ + +import 'dart:io'; +import 'dart:math'; + +main(List args) { + maindin(); + +} + + +void maindin() { + print("Welcome to Rock, Paper, Scissors\nType 'exit' to stop the game"); + final random = Random(); + // Rules of the game + Map rules = { + "rock": "scissors", + "scissors": "paper", + "paper": "rock" + }; + // Initial score + int user = 0; + int comp = 0; + // Options for computer to choose + List options = ["rock", "paper", "scissors"]; + // Actual game + while (true) { + String compChoice = options[random.nextInt(options.length)]; + stdout.write("\nPlease choose Rock, Paper or Scissors: "); + String userChoice = stdin.readLineSync().toLowerCase(); + if (userChoice == "exit") { + print("\nYou: $user Computer: $comp\nBye Bye!"); + break; + } + if (!options.contains(userChoice)) { + print("Incorrect choice"); + continue; + } else if (compChoice == userChoice) { + print("We have a tie!"); + } else if (rules[compChoice] == userChoice) { + print("Computer wins: $compChoice vs $userChoice"); + comp += 1; + } else if (rules[userChoice] == compChoice) { + print("You win: $userChoice vs $compChoice"); + user += 1; + } + } +} \ No newline at end of file diff --git a/exercise9.dart b/exercise9.dart new file mode 100644 index 0000000..1d7b145 --- /dev/null +++ b/exercise9.dart @@ -0,0 +1,48 @@ +/** + * Exercise 9 + * Generate a random number between 1 and 100. Ask the user to guess the number, + * then tell them whether they guessed too low, too high, or exactly right. + * Keep track of how many guesses the user has taken, and when the game ends, print this out. + */ + +import 'dart:io'; +import 'dart:math'; + +main(List args) { + tube(); +} + +void tube() { + print("Type exit to quit the game"); + guessingGame(); +} + +guessingGame() { + final random = Random(); + int randNumber = random.nextInt(100); + int attempt = 0; + while (true) { + attempt += 1; + stdout.write("Please choose a number between 0 and 100: "); + String chosenNumber = stdin.readLineSync(); + // Make sure user does not go out of limits + if (chosenNumber.toLowerCase() == "exit") { + print("\nBye"); + break; + } else if (int.parse(chosenNumber) > 100) { + print("Please do not go over 100"); + continue; + } + // Main logic + if (int.parse(chosenNumber) == randNumber) { + print("Bingo! You tried $attempt times\n"); + continue; + } else if (int.parse(chosenNumber) > randNumber) { + print("You are higher"); + continue; + } else { + print("You are lower"); + continue; + } + } +}