dart プロ生ちゃん#カレンダープログラミングプチコンテスト2014

プロ生ちゃん#カレンダープログラミングプチコンテスト2014

calendar.dart
void main() {
  var today = new DateTime.now();
  var calendar = new List.generate(6, (i) => new List.filled(7, "  "));
  for (var i = 1, week = 0; i <= new DateTime(today.year, today.month + 1, 0).day; i++) {
    var day = new DateTime(today.year, today.month, i);
    calendar[week][day.weekday % 7] += i.toString();
    if (day.weekday == DateTime.SATURDAY) week++;
  }
  print(calendar.where((w) => w.any((d) => d.trim() != "")).map(
      (w) => w.map((d) => d.substring(d.length - 2)).join(" ")).join("\r\n"));
}

dart 1000储物柜数学问题 - 在飞镖中解决

1000储物柜数学问题 - 在飞镖中解决

gistfile1.dart
/*
  There are 1000 lockers in a high school with 1000 students.  The
  problem begins with the first student opening all 1000 lockers; next
  the second student closes lockers 2,4,6,8,10 and so on to locker 1000;
  the third student changes the state (opens lockers closed, closes
  lockers open) on lockers 3,6,9,12,15 and so on; the fourth student
  changes the state of lockers 4,8,12,16 and so on. This goes on until
  every student has had a turn.

  How many lockers will be open at the end? What is the formula?
 */
import 'dart:io';

int numberOfLockers = 1000;

// Lockers start all open because of the first student
List<String> lockers = new List.filled(numberOfLockers + 1, 'open');

main() {

  for (var student = 2; student <= numberOfLockers; student++) {

    // For the student, open the locker if closed, or close if open
    for (var locker = student; locker <= numberOfLockers; locker += student) {
      lockers[locker] = lockers[locker] == 'open' ? 'closed' : 'open';
    }

  }

  // Count of lockers remaining open
  int total = 0;

  // List the lockers that remain open
  for (var locker = 1; locker <= numberOfLockers; locker ++ ) {
    if (lockers[locker] == 'open') {
      stdout.write("${locker} ");
      total += 1;
    }
  }

  print("\nThere are ${total} lockers remaining open.");
}

// 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961 
// There are 31 lockers remaining open.

dart 飞镖のいろんなオブジェクトの真伪値としての振る舞い

飞镖のいろんなオブジェクトの真伪値としての振る舞い

object_as_bool_test.dart
import 'package:unittest/unittest.dart';

main() {

  /// NoSuchMethodError: method not found 'get:obj'
  test("undefined", () {
    if (obj) {
      expect(true, isFalse);
    } else {
      expect(false, isFalse);
    }
  });
  /// Passed : null => false
  test("null", () {
    var obj;
    if (obj) {
      expect(true, isFalse);
    } else {
      expect(false, isFalse);
    }
  });
  group("number", () {
    /// Passed : 0 => false
    test("0", () {
      if (0) {
        expect(true, isFalse);
      } else {
        expect(false, isFalse);
      }
    });
    /// Passed : -0 => false
    test("-0", () {
      if (-0) {
        expect(true, isFalse);
      } else {
        expect(false, isFalse);
      }
    });
    /// Failed : -1 => false
    test("-1", () {
      if (-1) {
        expect(true, isTrue);
      } else {
        expect(false, isTrue);
      }
    });
    /// Failed : 1 => false
    test("1", () {
      if (1) {
        expect(true, isTrue);
      } else {
        expect(false, isTrue);
      }
    });
    /// Passed : NaN => false
    test("NaN", () {
      if (double.NAN) {
        expect(true, isFalse);
      } else {
        expect(false, isFalse);
      }
    });
  });
  group("string", () {
    /// Passed : "" => false
    test("empty", () {
      if ("") {
        expect(true, isFalse);
      } else {
        expect(false, isFalse);
      }
    });
    /// Failed : "true" => false
    test("true string", () {
      if ("true") {
        expect(true, isTrue);
      } else {
        expect(false, isTrue);
      }
    });
  });
  group("object", () {
    /// Passed : {} => false
    test("empty object", () {
      if ({
      }) {
        expect(true, isFalse);
      } else {
        expect(false, isFalse);
      }
    });
    /// Failed : object => false
    test("not empty object", () {
      if ({
          "name" : "laco",
          "flag" : false
      }) {
        expect(true, isTrue);
      } else {
        expect(false, isTrue);
      }
    });
  });
}

dart gistfile1.dart

gistfile1.dart
/**
 * Example of a strategy pattern
 * We implement different behaviours for a robot
 */

class Robot {
  Behaviour behaviour;
  Robot(this.behaviour);
  move() => print(behaviour.move());
}

abstract class Behaviour {
  move();
}

class AgressiveBehaviour implements Behaviour{
  move() => 'Agressive Behaviour: if find another robot attack it';
}

class DefensiveBehaviour implements Behaviour{
  move() => 'Defensive Behaviour: if find another robot run from it';
}

class NormalBehaviour implements Behaviour{
  move() => 'Normal Behaviour: if find another robot ignore it';
}