Categories
JavaScript Телерик

JS OOP изпит – сутрешна група

Решение на задачата на сутрешната група на изпита по JavaScript OOP

// item.js
define(function() {
    'use strict';
    var Item;

    Item = (function() {
        var types = ['accessory', 'smart-phone', 'notebook', 'pc', 'tablet'];

        function Item(type, name, price) {
            if (types.indexOf(type) == -1) {
                throw "invalid item type";
            };

            if (name.length < 6 || name.length > 40) {
                throw "invalid item name";
            };

            if (price < 0.0) {
                throw "invalid item price";
            };
            this.type = type;
            this.name = name;
            this.price = price;
        }
        return Item;
    })();
    return Item;
});

// store.js 

define(function() {
    'use strict';
    var Store;
    Store = (function() {
        function Store(storeName) {
            if (storeName.length < 6 || storeName.length > 40) {
                throw "invalid store name";
            }

            this._name = storeName;
            this._items = [];
        };

        Store.prototype.addItem = function(item) {
            if (item.constructor.name !== "Item") {
                throw "invalid item type";
            }

            this._items.push(item);

            return this;
        };

        Store.prototype.getAll = function() {
            var sortedItems = compareBy(this._items, "name");

            return sortedItems;
        };

        Store.prototype.getSmartPhones = function() {
            var items = [];

            this._items.forEach(function(item) {
                if (item.type === 'smart-phone') {
                    items.push(item);
                }
            });

            var sortedItems = compareBy(items, "name");

            return sortedItems;
        };

        Store.prototype.getMobiles = function() {
            var items = [];

            this._items.forEach(function(item) {
                if (item.type === 'smart-phone' || item.type === 'tablet') {
                    items.push(item);
                }
            });

            var sortedItems = compareBy(items, "name");

            return sortedItems;
        };

        Store.prototype.getComputers = function() {
            var items = [];

            this._items.forEach(function(item) {
                if (item.type === 'pc' || item.type === 'notebook') {
                    items.push(item);
                }
            });

            var sortedItems = compareBy(items, "name");

            return sortedItems;
        };

        Store.prototype.filterItemsByType = function(type) {
            var items = [];

            this._items.forEach(function(item) {
                if (item.type === type) {
                    items.push(item);
                }
            });

            var sortedItems = compareBy(items, "name");

            return sortedItems;
        };

        Store.prototype.filterItemsByPrice = function(options) {
            var minPrice = 0;
            var maxPrice = 999999999.99;
            var items = [];

            if (options) {
                minPrice = options.min || minPrice;
                maxPrice = options.max || maxPrice;
            }

            this._items.forEach(function(item) {
                if (item.price >= minPrice && item.price <= maxPrice) {
                    items.push(item);
                }
            });
            var sortedItems = compareBy(items, "price");
            return sortedItems;
        };
        Store.prototype.countItemsByType = function() {
            var itemTypes = [];
            for (var i = 0; i < this._items.length; i++) {
                if (itemTypes[this._items[i].type]) {
                    itemTypes[this._items[i].type]++;
                } else {
                    itemTypes[this._items[i].type] = 1;
                }
            }
            return itemTypes;
        };
        Store.prototype.filterItemsByName = function(part) {
            var items = [];
            this._items.forEach(function(item) {
                if (item.name.toLowerCase().indexOf(part.toLowerCase()) >= 0) {
                    items.push(item);
                }
            });

            var sortedItems = compareBy(items, "name");

            return sortedItems;
        };

        function compareBy(data, field) {
            data.sort(function(a, b) {
                if (a[field] < b[field]) return -1;
                if (a[field] > b[field])
                    return 1;
                return 0;
            });

            return data;
        };

        return Store;
    })();

    return Store;
});
Categories
HTML / CSS

Резултат от изпита по CSS

Точки: 259 от 300

Точки по задачи

  1. 64 от 100 точки
  2. 95 от 100 точки
  3. 100 от 100 точки

Общ брой участници: 698

Класиране: 163 място

Categories
Visual C# Телерик

Задача: Nightmare On Code Street

Решение

using System;

namespace NightmareOnCodeStreet {
  class Program {
    static void Main() {
      string number = Console.ReadLine();
      char[] numbersList = number.ToCharArray();
      int size = numbersList.Length;
      if (numbersList[0] - '0' < 0) {
        for (int i = 0; i < numbersList.Length - 1; i++) {
          numbersList[i] = numbersList[i + 1];
        }
        size--;
      }
      int counter = 0;
      int sum = 0;
      for (int i = 0; i < size; i++) {
        if (i % 2 != 0) {
          if (Char.IsDigit(numbersList[i])) {
            counter++;
            sum += (int)(numbersList[i] - '0');
          }
        }
      }
      Console.WriteLine("{0} {1}", counter, sum);
    }
  }
}

Categories
Visual C# Телерик

Задача: Kaspichania Boats

Решение

using System;

namespace KaspichaniaBoats {
  class Program {
    static void Main() {
      int n = int.Parse(Console.ReadLine());
      int boatWidth = 2 * n + 1;

      int dots = 1;
      for (int i = 0; i < n; i++) {
        Console.Write(new string('.', n - i));
        if (i == 0) {
          Console.Write(new string('*', 1));
        } else if (i == 1) {
          Console.Write(new string('*', 3));
        } else {
          Console.Write("*");
          Console.Write(new string('.', dots));
          Console.Write("*");
          Console.Write(new string('.', dots));
          Console.Write("*");
          dots++;
        }
        Console.Write(new string('.', n - i));
        Console.WriteLine();
      }
      Console.WriteLine(new string('*', boatWidth));
      int counter = 1;
      dots = n - 2;
      for (int i = 1; i <= n / 2; i++) {
        Console.Write(new string('.', i));
        Console.Write(new string('*', 1));
        Console.Write(new string('.', dots));
        Console.Write(new string('*', 1));
        Console.Write(new string('.', dots));
        Console.Write(new string('*', 1));
        Console.Write(new string('.', i));
        dots--;
        counter++;
        Console.WriteLine();
      }
      Console.Write(new string('.', counter));
      Console.Write(new string('*', boatWidth - 2 * counter));
      Console.Write(new string('.', counter));
    }
  }
}
Categories
Visual C# Телерик

Задача: 2-4-8

Решение

using System;

namespace _2_4_8 {
  class Program {
    static void Main() {
      decimal a = decimal.Parse(Console.ReadLine());
      decimal b = decimal.Parse(Console.ReadLine());
      decimal c = decimal.Parse(Console.ReadLine());

      decimal r = 0;
      switch ((int) b) {
      case 2:
        r = a % c;
        break;
      case 4:
        r = a + c;
        break;
      case 8:
        r = a * c;
        break;
      }

      int remainder = r % 4;
      if (remainder == 0) {
        Console.WriteLine(r / 4);
      } else {
        Console.WriteLine(remainder);
      }
      Console.WriteLine(r);
    }
  }
}
Categories
Visual C# Телерик

Задача: Tribonacci Triangle

Решение

using System;

namespace TribonacciTriangle {
  class Program {
    static void Main() {
      long firstNumber = long.Parse(Console.ReadLine());
      long secondNumber = long.Parse(Console.ReadLine());
      long thirdNumber = long.Parse(Console.ReadLine());
      int linesCount = int.Parse(Console.ReadLine());

      Console.WriteLine(firstNumber);
      Console.WriteLine(secondNumber + " " + thirdNumber);

      int membersCount = 0;
      for (int i = 1; i <= linesCount; i++) {
        membersCount += i;
      }
      int nextLine = 3;
      int lineSize = 0;
      for (int i = 4; i <= membersCount; i++) {
        long currentNumber = firstNumber + secondNumber + thirdNumber;
        firstNumber = secondNumber;
        secondNumber = thirdNumber;
        thirdNumber = currentNumber;
        Console.Write(currentNumber + " ");
        lineSize++;
        if (lineSize == nextLine) {
          Console.WriteLine();
          nextLine += 1;
          lineSize = 0;
        }
      }
    }
  }
}
Categories
Visual C# Телерик

Задача: Sheets

Решение

using System;
using System.Collections.Generic;

namespace Sheets {
  class Program {
    static void Main() {
      int n = int.Parse(Console.ReadLine());
      int[] piecesList = new int[11];
      Dictionary sheets = new Dictionary();
      sheets.Add(0, "A0");
      sheets.Add(1, "A1");
      sheets.Add(2, "A2");
      sheets.Add(3, "A3");
      sheets.Add(4, "A4");
      sheets.Add(5, "A5");
      sheets.Add(6, "A6");
      sheets.Add(7, "A7");
      sheets.Add(8, "A8");
      sheets.Add(9, "A9");
      sheets.Add(10, "A10");

      piecesList[10] = 1;
      for (int i = 9; i >= 0; i--) {
        piecesList[i] = piecesList[i + 1] * 2;
      }

      for (int i = 0; i < piecesList.Length; i++) {
        if (n >= piecesList[i]) {
          n -= piecesList[i];
          sheets.Remove(i);
        }
      }

      foreach(var item in sheets) {
        Console.WriteLine(item.Value);
      }
    }
  }
}
Categories
Visual C# Телерик

Задача: Next Day

Решение

using System;

namespace TelerikExamples {
  class FireInTheMatrix {
    static void Main() {
      int fireWidth = int.Parse(Console.ReadLine());
      int sideDots = (fireWidth - 2) / 2;
      int middleDots = 0;

      for (int i = sideDots; i >= 0; i--) {
        Console.Write(new string('.', i));
        Console.Write(new string('#', 1));
        Console.Write(new string('.', middleDots));
        Console.Write(new string('#', 1));
        Console.Write(new string('.', i));
        Console.WriteLine();

        middleDots += 2;
      }

      middleDots = fireWidth - 2;

      for (int i = 0; i < fireWidth / 4; i++) {
        Console.Write(new string('.', i));
        Console.Write(new string('#', 1));
        Console.Write(new string('.', middleDots));
        Console.Write(new string('#', 1));
        Console.Write(new string('.', i));
        Console.WriteLine();
        middleDots -= 2;
      }
      Console.WriteLine(new string('-', fireWidth));
      int middleSlashes = fireWidth / 2;
      for (int i = 0; i < fireWidth / 2; i++) {
        Console.Write(new string('.', i));
        Console.Write(new string('\\', middleSlashes));
        Console.Write(new string('/', middleSlashes));
        Console.Write(new string('.', i));
        Console.WriteLine();
        middleSlashes--;
      }
    }
  }
}
Categories
Visual C# Телерик

Задача: Formula Bit 1

Решение

using System;

namespace FormulaBit1 {
  class Program {
    static int gridSize = 8;
    static int[, ] grid = new int[gridSize, gridSize];
    static int trackLength = 0;
    static int turnsCount = 0;
    static char previousDirection = 'D';
    static bool finished = false;

    static void Main() {
      for (int i = 0; i < gridSize; i++) {
        int number = int.Parse(Console.ReadLine());
        for (int j = 0; j < gridSize; j++) {
          int mask = 1 << j;
          int numberAndMask = number & mask;
          int bit = numberAndMask >> j;
          grid[i, gridSize - j - 1] = (bit == 1) ? 1 : 0;
        }
      }

      buildTrack(0, 7, 'D');

      if (finished == false) {
        Console.WriteLine("No {0}", trackLength);
      }
    }

    static void checkForTrackExit(int row, int column) {
      if (row == 7 && column == 0) {
        finished = true;
        Console.WriteLine("{0} {1}", trackLength, turnsCount);
      }
    }

    static void buildTrack(int row, int column, char currentDirection) {
      if (row < 0 || column < 0 || row >= grid.GetLength(0) || column >= grid.GetLength(1)) {
        return;
      }

      if (grid[row, column] == 1) {
        return;
      }

      switch (currentDirection) {
      case 'D':
        for (int i = row; i < gridSize; i++) {
          if (grid[i, column] == 0) {
            trackLength += 1;
            checkForTrackExit(i, column);
            if (i == (gridSize - 1)) {
              turnsCount++;
              previousDirection = 'D';
              buildTrack(i, column - 1, 'L');
            }
            continue;
          } else {
            turnsCount++;
            previousDirection = 'D';
            buildTrack(i - 1, column - 1, 'L');
            break;
          }
        }
        break;
      case 'L':
        for (int i = column; i >= 0; i--) {
          if (grid[row, i] == 0) {
            trackLength += 1;
            checkForTrackExit(row, i);
            if (i == 0) {
              if (previousDirection == 'D') {
                turnsCount++;
                previousDirection = 'L';
                buildTrack(row - 1, i, 'U');
              } else {
                turnsCount++;
                previousDirection = 'L';
                buildTrack(row + 1, i, 'D');
              }
            }
            continue;
          } else {
            if (previousDirection == 'U') {
              turnsCount++;
              previousDirection = 'L';
              buildTrack(row + 1, i + 1, 'D');
              break;
            } else {
              turnsCount++;
              previousDirection = 'L';
              buildTrack(row - 1, i + 1, 'U');
              break;
            }
          }

        }
        break;
      case 'U':
        for (int i = row; i >= 0; i--) {
          if (grid[i, column] == 0) {
            trackLength += 1;
            checkForTrackExit(i, column);
            if (i == 0) {
              turnsCount++;
              previousDirection = 'U';
              buildTrack(i, column - 1, 'L');
            }
            continue;
          } else {
            turnsCount++;
            previousDirection = 'U';
            buildTrack(i + 1, column - 1, 'L');
            break;
          }
        }
        break;
      }
    }
  }
}
Categories
Visual C# Телерик

Задача: Carpets

Решение

using System;

namespace Carpets {
  class Program {
    static void Main() {
      int fullSize = int.Parse(Console.ReadLine());
      int halfSize = fullSize / 2;

      for (int i = 1; i <= halfSize; i++) {
        Console.Write(new string('.', halfSize - i));
        string str = new string(' ', 2 * i);
        char[] chr = str.ToCharArray();
        for (int j = 0; j < chr.Length / 2; j++) {
          if (j % 2 == 0) {
            chr[j] = '/';
            chr[chr.Length - j - 1] = '\\';
          }
        }
        str = new string(chr);
        Console.Write(str);
        Console.Write(new string('.', halfSize - i));
        Console.WriteLine();
      }
      for (int i = 1; i <= halfSize; i++) {
        Console.Write(new string('.', i - 1));
        string str = new string(' ', 2 * (halfSize + 1) - 2 * i);
        char[] chr = str.ToCharArray();
        for (int j = 0; j < chr.Length / 2; j++) {
          if (j % 2 == 0) {
            chr[j] = '\\';
            chr[chr.Length - j - 1] = '/';
          }
        }
        str = new string(chr);
        Console.Write(str);
        Console.Write(new string('.', i - 1));
        Console.WriteLine();
      }
    }
  }
}