├── Dealing with long sentences.dart ├── Fibonacci Series.dart ├── Manipulate list items.dart ├── Manipulate list items2.dart ├── README.md ├── Rock, paper and scissors game.dart ├── game cows and bulls .dart ├── list without repeating.dart ├── palindron_string.dart ├── prime or not.dart ├── random_numper.dart └── tow sum .dart /Dealing with long sentences.dart: -------------------------------------------------------------------------------- 1 | //Write a program that asks the user for a long string containing multiple words. Print back to the user the same string, except with the words in backwards order. 2 | //For example, say I type the string: 3 | //for_example 4 | //My name is Michele 5 | //Then I would see the string: 6 | //Michele is name My 7 | import 'dart:io'; 8 | void main() { 9 | print("enter your words : "); 10 | String input = stdin.readLineSync()!; 11 | print(input.split(" ").reversed.join(" ")); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /Fibonacci Series.dart: -------------------------------------------------------------------------------- 1 | //Write a program that asks the user how many Fibonnaci numbers to generate and then generates them 2 | //style_output 3 | // dart main.dart 4 | // Enter the number of terms: 5 | // 10 6 | // Fibonacci Series: 7 | // 0 8 | // 1 9 | // 1 10 | // 2 11 | // 3 12 | // 5 13 | // 8 14 | // 13 15 | // 21 16 | // 34 17 | 18 | import 'dart:io'; 19 | int main() { 20 | int n, t1 = 0, t2 = 1, nextTerm = 0; 21 | 22 | print ("Enter the number of terms: "); 23 | n = int.parse(stdin.readLineSync()!); 24 | 25 | print ("Fibonacci Series: ") ; 26 | 27 | for (int i = 1; i <= n; i++) { 28 | // Prints the first two terms. 29 | if(i == 1) { 30 | print(t1); 31 | continue; 32 | } 33 | if(i == 2) { 34 | print (t2); 35 | continue; 36 | } 37 | nextTerm = t1 + t2; //swap_tecnick 38 | t1 = t2;//swap_tecnick 39 | t2 = nextTerm;//swap_tecnick 40 | 41 | print(nextTerm); 42 | } 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Manipulate list items.dart: -------------------------------------------------------------------------------- 1 | // Let’s say you are given a list saved in a variable: 2 | // a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]. 3 | // Write a Dart code that takes this list and makes a new list that has only the even elements of this list in it. 4 | 5 | import 'dart:io'; 6 | void main() { 7 | List a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]; 8 | List b = []; 9 | for (int i = 0; i < a.length; i++) { 10 | if (a[i] % 2 == 0) { 11 | b.add(a[i]); 12 | } 13 | } 14 | print(b); 15 | } 16 | -------------------------------------------------------------------------------- /Manipulate list items2.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | import 'dart:math'; 3 | 4 | void main() { 5 | List numpers = [0,5, 10, 15, 20, 25,77,99,90,4] ; 6 | List empty_list = [] ; 7 | 8 | for(int i = 0 ; i< numpers.length ; i++){ 9 | if(numpers[i] == numpers[0] ){ 10 | empty_list.add(numpers[i]); 11 | empty_list.add(numpers[numpers.length-1]); 12 | 13 | } 14 | } 15 | print(empty_list); 16 | } 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

problem_solving_with_dart 2 |

3 | 1- palindrom_string
4 | Ask the user for a string and print out whether this string is a palindrome or not.
5 | A palindrome is a string that reads the same forwards and backwards.
6 | code : https://cutt.us/IyUib
7 | -------------------------------------------------- 8 |

9 | 2-Manipulate list items.dart
10 | Let’s say you are given a list saved in a variable:
11 | a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100].
12 | Write a Dart code that takes this list and makes a new list that has only the even elements of this list in it.
13 | code : https://cutt.us/lXMJO
14 | ------------------------------------------------- 15 |

16 | 3- Rock, paper and scissors game
17 | Make a two-player Rock-Paper-Scissors game against computer.
18 | Ask for player’s input, compare them, print out a message to the winner.
19 | code : https://cutt.us/6Wgye
20 | ------------------------------------------------- 21 |

22 | 4- Generate a random number between 1 and 100.
23 | Ask the user to guess the number,
24 | then tell them whether they guessed too low, too high, or exactly right.
25 | Keep track of how many guesses the user has taken, and when the game ends, print this out.
26 | code : https://cutt.us/5hUqo
27 | ------------------------------------------------- 28 |

29 | 5- Ask the user for a number and determine whether the number is prime or not.
30 | code : https://cutt.us/Un2B3
31 | ------------------------------------------------- 32 |

33 | 6-Write a program that takes a list of numbers for example
34 | a = [5, 10, 15, 20, 25]
35 | and makes a new list of only the first and last elements of the given list
36 | code : https://cutt.us/RpoYM
37 | ------------------------------------------------- 38 |

39 | 7-Write a program that asks the user how many Fibonnaci numbers to generate and then generates them
40 | Make sure to ask the user to enter the number of numbers in the sequence to generate.
41 | code : https://cutt.us/dbFFp
42 | ------------------------------------------------- 43 |

44 | 8-Write a program that takes a list
45 | and returns a new list that contains all the elements of the first list minus all the duplicates.
46 | code : https://cutt.us/8cntT
47 | ------------------------------------------------- 48 |

49 | 9-Write a program that asks the user for a long string containing multiple words. Print back to the user the same string, except with the words in backwards order.
50 | For example, say I type the string:
51 | My name is Michele
52 | Then I would see the string:
53 | Michele is name My
54 | code : https://cutt.us/ad5pN
55 | ------------------------------------------------- 56 |

57 | 10-Create a program that will play the “cows and bulls” game with the user.
58 | The game works like this:
59 | Randomly generate a 4-digit number. Ask the user to guess a 4-digit number.
60 | For every digit the user guessed correctly in the correct place, they have a “cow”.
61 | For every digit the user guessed correctly in the wrong place is a “bull.”
62 | Every time the user makes a guess, tell them how many “cows” and “bulls” they have.
63 | Once the user guesses the correct number, the game is over.
64 | Keep track of the number of guesses the user makes throughout the game and tell the user at the end.
65 | code : https://cutt.us/HOqqm
66 | ------------------------------------------------- 67 |

68 | 11- Given an array of integers nums and an integer target
69 | return indices of the two numbers such that they add up to target.
70 | You may assume that each input would have exactly one solution, and you may not use the same element twice.
71 | You can return the answer in any order.
72 | Example 1:
73 | Input: nums = [2,7,11,15], target = 9
74 | Output: [0,1]
75 | Example 2 :
76 | Input: nums = [3,3], target = 6
77 | Output: [0,1]
78 | code : https://cutt.us/HGQZz
79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /Rock, paper and scissors game.dart: -------------------------------------------------------------------------------- 1 | // Make a two-player Rock-Paper-Scissors game against computer. 2 | // Ask for player’s input, compare them, print out a message to the winner. 3 | // The first way 4 | import 'dart:io'; 5 | import 'dart:math'; 6 | 7 | void main() { 8 | print("Welcome to Rock, Paper, Scissors\nType 'exit' to stop the game"); 9 | final random = Random(); 10 | 11 | // Initial score 12 | int user = 0; 13 | int comp = 0; 14 | 15 | // Options for computer to choose 16 | List options = ["rock", "paper", "scissors"]; 17 | 18 | // Actual game 19 | while (true) { 20 | String compChoice = options[random.nextInt(options.length)]; 21 | stdout.write("\nPlease choose Rock, Paper or Scissors: "); 22 | String userChoice = stdin.readLineSync()!.toLowerCase(); 23 | print("\n"); 24 | 25 | if (userChoice == "exit") { 26 | if (user == comp || comp == user) { 27 | print( 28 | "score game is \n user : $user and comp : $comp \n A draw between the two teams "); 29 | break; 30 | } else if (user > comp && comp < user) { 31 | print( 32 | "score game is \n user : $user and comp : $comp \n user is winer "); 33 | break; 34 | } else if (comp > user && user < comp) { 35 | print( 36 | "score game is \n user : $user and comp : $comp \n comp is winer "); 37 | break; 38 | } 39 | } 40 | 41 | if (userChoice == "rock" && compChoice == "rock") { 42 | print("computer choise is $compChoice"); 43 | print("Draw without pointsd"); 44 | } else if (compChoice == "rock" && userChoice == "rock") { 45 | print(compChoice); 46 | print("Draw without points"); 47 | } else if (userChoice == "paper" && compChoice == "paper") { 48 | print("computer choise is $compChoice"); 49 | print("Draw without points"); 50 | } else if (compChoice == "paper" && userChoice == "paper") { 51 | print("computer choise is $compChoice"); 52 | print("Draw without points"); 53 | } else if (compChoice == "scissors" && userChoice == "scissors") { 54 | print("computer choise is $compChoice"); 55 | print("Draw without points"); 56 | } else if (userChoice == "scissors" && compChoice == "scissors") { 57 | print("computer choise is $compChoice"); 58 | print("Draw without points"); 59 | } else if (compChoice == "rock" && userChoice == "paper") { 60 | print("computer choise is $compChoice"); 61 | print("user is win "); 62 | user++; 63 | } else if (compChoice == "paper" && userChoice == "rock") { 64 | print("computer choise is $compChoice"); 65 | print("comp is win "); 66 | comp++; 67 | } else if (compChoice == "rock" && userChoice == "scissors") { 68 | print("computer choise is $compChoice"); 69 | print("comp is win "); 70 | comp++; 71 | } else if (compChoice == "scissors" && userChoice == "rock") { 72 | print("computer choise is $compChoice"); 73 | print("user is win "); 74 | user++; 75 | } else if (compChoice == "paper" && userChoice == "scissors") { 76 | print("computer choise is $compChoice"); 77 | print("user is win "); 78 | user++; 79 | } else if (compChoice == "scissors" && userChoice == "paper") { 80 | print("computer choise is $compChoice"); 81 | print("comp is win "); 82 | comp++; 83 | } else if (userChoice != "paper" && 84 | userChoice != "rock" && 85 | userChoice != "scissors") { 86 | print("The choice is incorrect"); 87 | continue; 88 | } 89 | } 90 | } 91 | // ------------------------------------------------------------------------------------------------- 92 | // The second way 93 | import 'dart:io'; 94 | import 'dart:math'; 95 | 96 | void main() { 97 | print("Welcome to Rock, Paper, Scissors\nType 'exit' to stop the game"); 98 | final random = Random(); 99 | 100 | // Rules of the game 101 | Map rules = { 102 | "rock": "scissors", 103 | "scissors": "paper", 104 | "paper": "rock" 105 | }; 106 | 107 | // Initial score 108 | int user = 0; 109 | int comp = 0; 110 | 111 | // Options for computer to choose 112 | List options = ["rock", "paper", "scissors"]; 113 | 114 | // Actual game 115 | while (true) { 116 | String compChoice = options[random.nextInt(options.length)]; 117 | stdout.write("\nPlease choose Rock, Paper or Scissors: "); 118 | String userChoice = stdin.readLineSync().toLowerCase(); 119 | 120 | if (userChoice == "exit") { 121 | print("\nYou: $user Computer: $comp\nBye Bye!"); 122 | break; 123 | } 124 | 125 | if (!options.contains(userChoice)) { 126 | print("Incorrect choice"); 127 | continue; 128 | } else if (compChoice == userChoice) { 129 | print("We have a tie!"); 130 | } else if (rules[compChoice] == userChoice) { 131 | print("Computer wins: $compChoice vs $userChoice"); 132 | comp += 1; 133 | } else if (rules[userChoice] == compChoice) { 134 | print("You win: $userChoice vs $compChoice"); 135 | user += 1; 136 | } 137 | } 138 | } 139 | 140 | -------------------------------------------------------------------------------- /game cows and bulls .dart: -------------------------------------------------------------------------------- 1 | // Create a program that will play the “cows and bulls” game with the user. The game works like this: 2 | // Randomly generate a 4-digit number. Ask the user to guess a 4-digit number. 3 | // For every digit the user guessed correctly in the correct place, they have a “cow”. 4 | // For every digit the user guessed correctly in the wrong place is a “bull.” 5 | // Every time the user makes a guess, tell them how many “cows” and “bulls” they have. 6 | // Once the user guesses the correct number, the game is over. 7 | // Keep track of the number of guesses the user makes throughout the game and tell the user at the end. 8 | 9 | import 'dart:io'; 10 | import 'dart:math'; 11 | 12 | void main() { 13 | /* Generate random number 14 | Range is between 1000 and 9999 */ 15 | final random = Random(); 16 | String randomNumber = (1000 + random.nextInt(9999 - 1000)).toString(); 17 | print(randomNumber); 18 | 19 | stdout.write("Welcome to Cows and Bulls\nType 'exit' to stop the game\n"); 20 | 21 | int attempts = 0; 22 | 23 | // Actual game 24 | while (true) { 25 | int cows = 0; 26 | int bulls = 0; 27 | attempts += 1; 28 | 29 | stdout.write("\nPlease choose a four digit number: "); 30 | String chosenNumber = stdin.readLineSync()!; 31 | 32 | // Conditions to check if the game is over 33 | if (chosenNumber == randomNumber) { 34 | print("Bullseye! You took $attempts attempts"); 35 | break; 36 | } else if (chosenNumber == "exit") { 37 | print("Bye bye!"); 38 | break; 39 | } else if (chosenNumber.length != randomNumber.length) { 40 | print("Incorrect number. Make sure to give 4 digit number"); 41 | continue; 42 | } 43 | 44 | /* If a digit is in the same index increase the cow 45 | If it is somewhere else increase the bull*/ 46 | for (var i = 0; i < randomNumber.length; i++) { 47 | if (chosenNumber[i] == randomNumber[i]) { 48 | cows += 1; 49 | } else if (randomNumber.contains(chosenNumber[i])) { 50 | bulls += 1; 51 | } 52 | } 53 | print("\nAttempts: $attempts \nCows: $cows, Bulls: $bulls"); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /list without repeating.dart: -------------------------------------------------------------------------------- 1 | // Write a program that takes a list and returns a new list that contains all the elements of the first list minus all the duplicates. 2 | import 'dart:io'; 3 | void main() { 4 | print("please enter your numper counter"); 5 | int counter = int.parse(stdin.readLineSync()!); 6 | List listofitem = []; 7 | for (int i = 0; i < counter; i++) { 8 | int item = int.parse(stdin.readLineSync()!); 9 | listofitem.add(item); 10 | } 11 | print(listofitem.toSet().toList()); 12 | } 13 | -------------------------------------------------------------------------------- /palindron_string.dart: -------------------------------------------------------------------------------- 1 | //Ask the user for a string and print out whether this string is a palindrome or not. 2 | //A palindrome is a string that reads the same forwards and backwards. 3 | 4 | import 'dart:io'; 5 | 6 | void main() { 7 | print("pleaes write name"); 8 | String? myString = stdin.readLineSync()!; 9 | 10 | if (myString[0] == myString[myString.length - 1]) { 11 | print("name is palindrom"); 12 | return; 13 | } 14 | 15 | print("name is not palindroms"); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /prime or not.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | import 'dart:math'; 3 | 4 | void main() { 5 | int numper, flag = 0; 6 | 7 | print("please add your numper"); 8 | numper = int.parse(stdin.readLineSync()!); 9 | for (int i = 1; i <= numper; i++) { 10 | if (numper % i == 0) { 11 | flag++; 12 | } 13 | } 14 | if (flag == 2) { 15 | print("numper is prime"); 16 | } else { 17 | print("numper is not prime"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /random_numper.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | import 'dart:math'; 3 | 4 | void main() { 5 | final random = Random(); 6 | int choise = random.nextInt(100); 7 | int user = int.parse(stdin.readLineSync()!); 8 | if (user > 100) { 9 | print("numper out of range"); 10 | } else if (user > choise) { 11 | print("random numper is $choise"); //test_random_numper 12 | print("numper is too high"); 13 | } else if (user < choise) { 14 | print("random numper is $choise"); //test_random_numper 15 | print("numper is too low"); 16 | } else { 17 | print("random numper is $choise"); //test_random_numper 18 | print("exactly right"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tow sum .dart: -------------------------------------------------------------------------------- 1 | // Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. 2 | 3 | // You may assume that each input would have exactly one solution, and you may not use the same element twice. 4 | 5 | // You can return the answer in any order. 6 | 7 | 8 | 9 | // Example 1: 10 | 11 | // Input: nums = [2,7,11,15], target = 9 12 | // Output: [0,1] 13 | 14 | // Example 2 : 15 | // Input: nums = [3,3], target = 6 16 | // Output: [0,1] 17 | 18 | import 'dart:io'; 19 | import 'dart:math'; 20 | 21 | void main() { 22 | List index = []; 23 | List items = []; 24 | 25 | print("pleas enter your target"); 26 | int target = int.parse(stdin.readLineSync()!); 27 | print("enter your length list"); 28 | int length_list = int.parse(stdin.readLineSync()!); 29 | 30 | for (int i = 0; i < length_list; i++) { 31 | print("pleaes enter your value of index $i"); 32 | int value = int.parse(stdin.readLineSync()!); 33 | items.add(value); 34 | } 35 | for (int i = 0; i < items.length; i++) { 36 | for (int j = 0; j < items.length; j++) { 37 | if (items[i] + items[j] == target) { 38 | index.addAll([i, j]); 39 | } 40 | } 41 | } 42 | print(index.toSet().toList()); 43 | } 44 | 45 | 46 | --------------------------------------------------------------------------------