Skip to content

Instantly share code, notes, and snippets.

@webmstk
Created December 13, 2022 20:43
Show Gist options
  • Select an option

  • Save webmstk/1026cd8119d5c5caf7268c02b8653a3d to your computer and use it in GitHub Desktop.

Select an option

Save webmstk/1026cd8119d5c5caf7268c02b8653a3d to your computer and use it in GitHub Desktop.

Revisions

  1. webmstk created this gist Dec 13, 2022.
    113 changes: 113 additions & 0 deletions 2-4-dart_vars_and_types.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,113 @@
    // 1.
    int a = 13;

    // 2.
    double b = 12.8;

    // 3.
    var text = 'moon';

    // Error: A value of type 'String' can't be assigned to a variable of type 'int'.
    // a = text;

    // 4.
    dynamic dyn = 13;
    // Успешно, потому что dynamic позволяет записывать и перезаписывать в себя любой тип
    dyn = text;

    // 5.
    final fin = DateTime.now();
    const con = 9;

    // Error: Can't assign to the final variable 'fin'.
    // fin = 10.0;

    // Error: Can't assign to the const variable 'con'
    // con = 8.8;

    // const позволяет определить константу значением, известным на этапе компиляции.
    // final позволяет определеять константу в рантайме.

    // 6.
    int a2 = 3;
    print(a2.isEven);

    // 7.
    print('I \u2665 dart');

    // 8.
    // a.
    var list = [1, 2, 3, 4, 5, 6, 7, 8];
    // b.
    print(list.length);
    // c.
    var sortedList = [...list];
    sortedList.sort((a, b) => b.compareTo(a));
    print(sortedList);
    // d.
    var newList = list.sublist(0, 3);
    print(newList);
    // e.
    print(list.indexOf(5));
    // f.
    list.removeWhere((element) => [5, 8].contains(element));
    print(list);
    // g.
    list = list.map((e) {
    if (e < 4) {
    return e * 10;
    }
    return e;
    }).toList();
    print(list);

    // 9.
    // a.
    var numberBook = <String, String>{};
    numberBook['Иван'] = '2264865';
    numberBook['Татьяна'] = '89523366684';
    numberBook['Олег'] = '84952256575';
    // b.
    print(numberBook);
    // c.
    numberBook['Екатерина'] = '2359942';
    // d.
    final keys = numberBook.keys.toList();
    keys.sort();
    for (var key in keys.reversed) {
    print('$key: ${numberBook[key]}');
    }

    // 10.
    // a.
    var mySet = {'Москва', 'Вашингтон', 'Париж'};
    print(mySet);
    // b.
    mySet.add('Вашингтон');
    // Длина = 3, потому что Set хранит уникальные значения
    print(mySet.length);
    // c.
    var verse = '''
    She sells sea shells on the sea shore
    The shells that she sells are sea shells I am sure.
    So if she sells sea shells on the sea shore
    I am sure that the shells are sea shore shells
    ''';

    var words = verse
    .replaceAll('\n', ' ')
    .replaceAll('.', '')
    .trim()
    .split(' ')
    .map((word) => word.toLowerCase())
    .toList();

    var result = words.fold({}, (acc, value) {
    if (acc.containsKey(value)) {
    acc[value]++;
    } else {
    acc[value] = 1;
    }
    return acc;
    });
    print(result);