Skip to content

Instantly share code, notes, and snippets.

View Mastersam07's full-sized avatar
๐Ÿš€
building amazing things

Samuel Abada Mastersam07

๐Ÿš€
building amazing things
View GitHub Profile
@Mastersam07
Mastersam07 / main.dart
Last active March 30, 2020 23:34
a function that returns the index of a particular resistor color
void main() {
print(getIndex('Red'));
}
// a function that returns the index of a particular resistor color
// taking the color as argument
int getIndex(color) {
var dict = {
'Black': 0,
'Brown': 1,
@Mastersam07
Mastersam07 / day0130.dart
Last active March 31, 2020 00:07
a function that prints out the reverse of a string
void main() {
print(reverse('30days of code')); // should pass
print(reverse('30daysofcode')); // should pass
}
// a function that prints out the reverse of a string
// here we split the string, reverse the array and join
// we should also remove white spaces.
String reverse(word) {
String newString = word.replaceAll(" ", "").split('').reversed.join();
@Mastersam07
Mastersam07 / day0230.dart
Last active March 31, 2020 00:18
leap year function in dart
void main() {
print(isleapyear(1996)); // a true leap year
print(isleapyear(2000)); // a true leap year
print(isleapyear(1900)); // a false leap year
}
bool isleapyear(int year) =>
(year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
@Mastersam07
Mastersam07 / day0330.dart
Last active March 31, 2020 05:51
Isogram function
void main() {
print(isIsogram('')); // should return true
print(isIsogram('30days - of code')); // should return false
print(isIsogram('as -d f- gh')); // should return true as hyphen and spaces are allowed multiple times
print(isIsogram('a')); // single letter should return true
print(isIsogram('36797')); // single number should return false
}
// String letters = 'abcdefghijklmnopqrstuvwxyz';
@Mastersam07
Mastersam07 / day0430.dart
Last active March 31, 2020 07:11
word count
void main() {
print(countWords( "That's the password: PASSWORD 123!, cried the Special Agent.\nSo I fled.")); // should do a proper count
print(countWords("You you YOU")); // should return you:3 as it is case insensitive
print(countWords("3,3,3")); // should return 3:3 as stated in the problem
print(countWords("333")); // should return 333:1 as stated in the problem
}
// Hold the input in a map for key value pairs
// Using RegExp to ensure it matches the format in the prob statement
import 'dart:math';
void main() {
// uncomment below for list n difference of squares with i < n+1
// for (int i = 0; i < 11; i++) { // for a list of difference from 0 to 10
// print(differenceOfSquares(i));
// }
print(differenceOfSquares(10)); // returns 3025 - 385 = 2640.
}
void main() {
num number = 60;
print('prime factors of $number: ${primeFactors(number)}');
}
primeFactors(number) {
var factors = [], i = 2;
while (number > 1) {
if (number % i == 0) {
factors.add(i); number /= i; i = 2;
@Mastersam07
Mastersam07 / day0730.dart
Last active March 31, 2020 09:50
pascal triangle
// pascal triangle
void main() {
print(pascalTriangle(5)); // [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]
print("--------------------------");
print(pascalTriangle(1)); // should return 1
print("--------------------------");
print(pascalTriangle(0)); // should return empty []
print("--------------------------");
print(pascalTriangle(-6)); // should return -1
@Mastersam07
Mastersam07 / day0830.dart
Last active April 9, 2020 08:35
oop approach to checking type of triangle
void main() {
Triangle firstTriangle = Triangle(10, 10, 15);
Triangle secondTriangle = Triangle(10, 15, 20);
Triangle thirdTriangle = Triangle(10, 10, 10);
print(firstTriangle.triangleType);
print(secondTriangle.triangleType);
print(thirdTriangle.triangleType);
}
class Triangle {
@Mastersam07
Mastersam07 / day0930.dart
Last active April 9, 2020 09:03
Determine if a sentence is a pangram.
void main() {
String s = 'The quick brown fox jumps over the lazy dog';
print(isPanagram(s));
}
bool isPanagram(String phrase) {
List<String> alphas = 'abcdefghijklmnopqrstuvwxyz'.split(''); // all alphabets
var newPhrase = phrase
.replaceAll(' ', '')
.toLowerCase(); // remove spaces and take to lower case