Skip to content
Open
Show file tree
Hide file tree
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
29 changes: 27 additions & 2 deletions exercise1.dart
Original file line number Diff line number Diff line change
@@ -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<String> args) {

}
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");
}
41 changes: 41 additions & 0 deletions exercise10.dart
Original file line number Diff line number Diff line change
@@ -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<String> 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<int> 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");
}
29 changes: 27 additions & 2 deletions exercise2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> args) {

}
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");
}
}

//}
31 changes: 29 additions & 2 deletions exercise3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,32 @@
*/

main(List<String> args) {

}
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<int> 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
]);
}
29 changes: 27 additions & 2 deletions exercise4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,31 @@
* For example, 13 is a divisor of 26 because 26 / 13 has no remainder.
*/

import 'dart:io';

main(List<String> args) {

}
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);
}
}
}
21 changes: 20 additions & 1 deletion exercise5.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,24 @@
*/

main(List<String> args) {
example();
}

void example(){

List<int> a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89];
List<int> b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 89];
Set<int> 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());
}

}

26 changes: 26 additions & 0 deletions exercise6.dart
Original file line number Diff line number Diff line change
@@ -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<String> 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");
}
38 changes: 38 additions & 0 deletions exercise7.dart
Original file line number Diff line number Diff line change
@@ -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<String> 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<int> a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100];
int i = 0;
List<int> 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
]);
}
52 changes: 52 additions & 0 deletions exercise8.dart
Original file line number Diff line number Diff line change
@@ -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<String> args) {
maindin();

}


void maindin() {
print("Welcome to Rock, Paper, Scissors\nType 'exit' to stop the game");
final random = Random();
// Rules of the game
Map<String, String> rules = {
"rock": "scissors",
"scissors": "paper",
"paper": "rock"
};
// Initial score
int user = 0;
int comp = 0;
// Options for computer to choose
List<String> 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;
}
}
}
48 changes: 48 additions & 0 deletions exercise9.dart
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}
}
}