Filip_Markoski

All

Dec 22nd, 2017
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.09 KB | None | 0 0
  1.  
  2. class ComplexNumber<T extends Number, U extends Number>
  3.         implements Comparable<ComplexNumber<?, ?>> {
  4.     T real;
  5.     U imag;
  6.  
  7.     public ComplexNumber(T real, U imag) {
  8.         this.real = real;
  9.         this.imag = imag;
  10.     }
  11.  
  12.     public T getReal() {
  13.         return real;
  14.     }
  15.  
  16.     public void setReal(T real) {
  17.         this.real = real;
  18.     }
  19.  
  20.     public U getImaginary() {
  21.         return imag;
  22.     }
  23.  
  24.     public void setImaginary(U imag) {
  25.         this.imag = imag;
  26.     }
  27.  
  28.     public double modul() {
  29.         /* sqrt(x^2 + y^2) */
  30.         return Math.sqrt(Math.pow(real.doubleValue(), 2) + Math.pow(imag.doubleValue(), 2));
  31.     }
  32.  
  33.     @Override
  34.     public int compareTo(ComplexNumber<?, ?> that) {
  35.         return Double.compare(this.modul(), that.modul());
  36.     }
  37.  
  38.     @Override
  39.     public String toString() {
  40.         return String.format("%.2f%+.2fi", real.doubleValue(), imag.doubleValue());
  41.     }
  42. }
  43.  
  44. ===============================================================================
  45. class Node<T> {
  46.     T element;
  47.     Node<T> next;
  48.  
  49.     public Node(T element, Node<T> next) {
  50.         this.element = element;
  51.         this.next = next;
  52.     }
  53.  
  54.     public T getElement() {
  55.         return element;
  56.     }
  57.  
  58.     public void setElement(T element) {
  59.         this.element = element;
  60.     }
  61.  
  62.     public Node<T> getNext() {
  63.         return next;
  64.     }
  65.  
  66.     public void setNext(Node<T> next) {
  67.         this.next = next;
  68.     }
  69. }
  70.  
  71. class Queue<T> {
  72.     Node<T> front;
  73.     Node<T> rear;
  74.     int length;
  75.  
  76.     public Queue() {
  77.         this.front = null;
  78.         this.rear = null;
  79.         this.length = 0;
  80.     }
  81.  
  82.     public boolean isEmpty() {
  83.         return length == 0;
  84.     }
  85.  
  86.     public void enqueue(T element) {
  87.         Node<T> toBeAdded = new Node<T>(element, null);
  88.         /* Initial Scenario */
  89.         if (front == null) {
  90.             front = toBeAdded;
  91.             rear = toBeAdded;
  92.         }
  93.  
  94.         /* Running Scenario */
  95.         else {
  96.             rear.setNext(toBeAdded);
  97.             rear = toBeAdded;
  98.         }
  99.         length++;
  100.     }
  101.  
  102.     public T dequeue() throws EmptyQueueException {
  103.         /* Initial Scenario */
  104.         if (isEmpty()) {
  105.             throw new EmptyQueueException();
  106.         }
  107.         /* Running Scenario */
  108.         T toBeRemoved = front.getElement();
  109.         front = front.getNext();
  110.         length--;
  111.         return toBeRemoved;
  112.     }
  113.  
  114.     public T peek() throws EmptyQueueException {
  115.         /* Initial Scenario */
  116.         if (isEmpty()) {
  117.             throw new EmptyQueueException();
  118.         }
  119.         /* Running Scenario */
  120.         return front.getElement();
  121.     }
  122.  
  123.     public T inspect() throws EmptyQueueException {
  124.         /* Initial Scenario */
  125.         if (isEmpty()) {
  126.             throw new EmptyQueueException();
  127.         }
  128.         /* Running Scenario */
  129.         return rear.getElement();
  130.     }
  131.  
  132.     public int count() {
  133.         return length;
  134.     }
  135. }
  136.  
  137. class EmptyQueueException extends Exception {
  138.  
  139. }
  140.  
  141. ===============================================================================
  142.  
  143.  
  144. class BinaryTreeSet<T extends Comparable<T>> {
  145.  
  146.     class Node<T extends Comparable<T>> {
  147.         T element;
  148.         Node<T> left;
  149.         Node<T> right;
  150.         boolean exist;
  151.  
  152.         public Node(T element, Node<T> left, Node<T> right) {
  153.             this.element = element;
  154.             this.left = left;
  155.             this.right = right;
  156.             this.exist = true;
  157.         }
  158.     }
  159.  
  160.     Node<T> root;
  161.  
  162.     BinaryTreeSet() {
  163.         this.root = null;
  164.     }
  165.  
  166.     Node<T> insert(T element, Node<T> node) {
  167.         /* Initial Scenario */
  168.         if (node == null) {
  169.             node = new Node<>(element, null, null);
  170.         }
  171.         /* Running Scenario */
  172.         if (element.compareTo(node.element) < 0) {
  173.             node.left = insert(element, node.left);
  174.         } else if (element.compareTo(node.element) > 0) {
  175.             node.right = insert(element, node.right);
  176.         } // If it's a duplicate we don't do anything
  177.         return node;
  178.     }
  179.  
  180.     void addElement(T element) {
  181.         root = insert(element, root);
  182.     }
  183.  
  184.     boolean contains(T element, Node<T> node) {
  185.         /* Initial Scenario */
  186.         if (node == null) {
  187.             return false;
  188.         }
  189.         /* Running Scenario */
  190.         if (element.compareTo(node.element) == 0) {
  191.             return true;
  192.         } else if (element.compareTo(node.element) < 0) {
  193.             return contains(element, node.left);
  194.         } else if (element.compareTo(node.element) > 0) {
  195.             return contains(element, node.right);
  196.         }
  197.         return false;
  198.     }
  199.  
  200.     boolean contains(T element) {
  201.         return contains(element, root);
  202.     }
  203.  
  204.     boolean remove(T element, Node<T> node) {
  205.         /* Initial Scenario */
  206.         if (node == null) {
  207.             return false;
  208.         }
  209.  
  210.         /* Running Scenario */
  211.         if (element.compareTo(node.element) == 0) {
  212.             node.exist = false;
  213.             return true;
  214.         } else if (element.compareTo(node.element) < 0) {
  215.             return remove(element, node.left);
  216.         } else if (element.compareTo(node.element) > 0) {
  217.             return remove(element, node.right);
  218.         }
  219.         return false;
  220.     }
  221.  
  222.     boolean removeElement(T element) {
  223.         return remove(element, root);
  224.     }
  225.  
  226.     boolean isEmpty() {
  227.         return root == null;
  228.     }
  229.  
  230.     /*    Left Root Right     */
  231.     void inorder(Node<T> node, StringBuffer sb) {
  232.         if (node != null) {
  233.             inorder(node.left, sb);
  234.             sb.append(node.element + " ");
  235.             inorder(node.right, sb);
  236.         }
  237.     }
  238.  
  239.     @Override
  240.     public String toString() {
  241.         if (isEmpty()) return "EMPTY";
  242.  
  243.         StringBuffer sb = new StringBuffer();
  244.         inorder(root, sb);
  245.         sb.setLength(sb.length() - 1);
  246.         return sb.toString();
  247.     }
  248. }
  249.  
  250. ==============================================================================
  251.  
  252. class IntegerList {
  253.     List<Integer> integers;
  254.  
  255.     IntegerList() {
  256.         this.integers = new ArrayList<>();
  257.     }
  258.  
  259.     IntegerList(Integer... numbers) {
  260.         this.integers = new ArrayList<>(Arrays.asList(numbers));
  261.     }
  262.  
  263.     void add(int el, int idx) {
  264.         if (idx > integers.size()) {
  265.             IntStream.range(0, integers.size() - idx)
  266.                     .forEach(i -> integers.add(0));
  267.             integers.add(idx, el);
  268.         }
  269.         integers.add(idx, el);
  270.     }
  271.  
  272.     boolean validateIndex(int index) {
  273.         if (index < 0 || index > integers.size()) {
  274.             throw new ArrayIndexOutOfBoundsException();
  275.         }
  276.         return false;
  277.     }
  278.  
  279.     int remove(int idx) {
  280.         validateIndex(idx);
  281.         return integers.remove(idx);
  282.     }
  283.  
  284.     void set(int el, int idx) {
  285.         validateIndex(idx);
  286.         integers.add(idx, el);
  287.     }
  288.  
  289.     int get(int idx) {
  290.         validateIndex(idx);
  291.         return integers.get(idx);
  292.     }
  293.  
  294.     int size() {
  295.         return integers.size();
  296.     }
  297.  
  298.     int count(int el) {
  299.         return Collections.frequency(integers, el);
  300.         /*return (int) integers.stream()
  301.                 .filter(i -> i.equals(el))
  302.                 .count();*/
  303.     }
  304.  
  305.     void removeDuplicates() {
  306.         /*integers = IntStream.range(0, integers.size())
  307.                 .map(i -> integers.size() - 1 - i)
  308.                 .mapToObj(this::get)
  309.                 .distinct()
  310.                 .collect(Collectors.toList());
  311.         Collections.reverse(integers);*/
  312.         Collections.reverse(integers);
  313.         integers = integers.stream().distinct().collect(Collectors.toList());
  314.         Collections.reverse(integers);
  315.  
  316.     }
  317.  
  318.     int sumFirst(int k) {
  319.         return integers.stream()
  320.                 .limit(k)
  321.                 .mapToInt(Integer::valueOf)
  322.                 .sum();
  323.     }
  324.  
  325.     int sumLast(int k) {
  326.         return integers.stream()
  327.                 .skip(integers.size() - k)
  328.                 .mapToInt(Integer::valueOf)
  329.                 .sum();
  330.     }
  331.  
  332.     void shiftRight(int idx, int k) {
  333.         validateIndex(idx);
  334.         int shiftIndex = (idx + k) % integers.size();
  335.         Integer removed = integers.remove(idx);
  336.         integers.add(shiftIndex, removed);
  337.     }
  338.  
  339.     void shiftLeft(int idx, int k) {
  340.         validateIndex(idx);
  341.         int shiftIndex = (idx - k) % integers.size();
  342.         if (shiftIndex < 0) {
  343.             shiftIndex += integers.size();
  344.         }
  345.         Integer removed = integers.remove(idx);
  346.         integers.add(shiftIndex, removed);
  347.     }
  348.  
  349.     IntegerList addValue(int value) {
  350.         Integer numbers[] = integers.stream()
  351.                 .map(i -> i + value)
  352.                 .toArray(Integer[]::new);
  353.         return new IntegerList(numbers);
  354.     }
  355. }
  356.  
  357.  
  358.  
  359. class NoSuchRoomException extends Exception {
  360.     public NoSuchRoomException(String roomName) {
  361.         super(roomName);
  362.     }
  363. }
  364.  
  365. class NoSuchUserException extends Exception {
  366.     public NoSuchUserException(String userName) {
  367.         super(userName);
  368.     }
  369. }
  370.  
  371. class ChatRoom {
  372.     String name;
  373.     Set<String> usernames;
  374.  
  375.     public ChatRoom(String name) {
  376.         this.name = name;
  377.         this.usernames = new TreeSet<>();
  378.     }
  379.  
  380.     public void addUser(String username) {
  381.         usernames.add(username);
  382.     }
  383.  
  384.     public void removeUser(String username) {
  385.         usernames.remove(username);
  386.     }
  387.  
  388.     public boolean hasUser(String username) {
  389.         return usernames.contains(username);
  390.     }
  391.  
  392.     public int numUsers() {
  393.         return usernames.size();
  394.     }
  395.  
  396.     @Override
  397.     public String toString() {
  398.         StringBuffer sb = new StringBuffer();
  399.         sb.append(name + "\n");
  400.         if (usernames.isEmpty()) {
  401.             sb.append("EMPTY\n");
  402.         } else {
  403.             String text = usernames.stream().collect(Collectors.joining("\n"));
  404.             sb.append(text);
  405.         }
  406.         return sb.toString();
  407.     }
  408.  
  409.     public String getName() {
  410.         return name;
  411.     }
  412. }
  413.  
  414. class ChatSystem {
  415.     Map<String, ChatRoom> chatrooms;
  416.     Set<String> usernames;
  417.  
  418.     public ChatSystem() {
  419.         this.chatrooms = new TreeMap<>();
  420.         this.usernames = new HashSet<>();
  421.     }
  422.  
  423.     public void addRoom(String roomName) {
  424.         chatrooms.put(roomName, new ChatRoom(roomName));
  425.     }
  426.  
  427.     public void removeRoom(String roomName) {
  428.         chatrooms.remove(roomName);
  429.     }
  430.  
  431.     public ChatRoom getRoom(String roomName) throws NoSuchRoomException {
  432.         return Optional
  433.                 .ofNullable(chatrooms.get(roomName))
  434.                 .orElseThrow(() -> new NoSuchRoomException(roomName));
  435.     }
  436.  
  437.     public void register(String userName) {
  438.         usernames.add(userName);
  439.         if (!chatrooms.isEmpty()) {
  440.             ChatRoom leastPopulatedRoom = chatrooms.values().stream()
  441.                     .min(Comparator.comparing(ChatRoom::numUsers))
  442.                     .orElse(null);
  443.             if (leastPopulatedRoom != null) {
  444.                 leastPopulatedRoom.addUser(userName);
  445.             }
  446.         }
  447.     }
  448.  
  449.     void validateUserAndRoom(String userName, String roomName) throws NoSuchRoomException, NoSuchUserException {
  450.         if (!chatrooms.containsKey(roomName)) throw new NoSuchRoomException(roomName);
  451.         if (!usernames.contains(userName)) throw new NoSuchUserException(userName);
  452.     }
  453.  
  454.     public void joinRoom(String userName, String roomName) throws NoSuchRoomException, NoSuchUserException {
  455.         validateUserAndRoom(userName, roomName);
  456.         chatrooms.get(roomName).addUser(userName);
  457.     }
  458.  
  459.     public void leaveRoom(String userName, String roomName) throws NoSuchRoomException, NoSuchUserException {
  460.         validateUserAndRoom(userName, roomName);
  461.         chatrooms.get(roomName).removeUser(userName);
  462.     }
  463.  
  464.     public void registerAndJoin(String userName, String roomName) throws NoSuchRoomException, NoSuchUserException {
  465.         usernames.add(userName);
  466.         joinRoom(userName, roomName);
  467.     }
  468.  
  469.     public void followFriend(String userName, String friendUserName) throws NoSuchUserException {
  470.         if (!usernames.contains(userName)) throw new NoSuchUserException(userName);
  471.  
  472.         chatrooms.values().stream()
  473.                 .filter(i -> i.hasUser(friendUserName))
  474.                 .forEach(chatRoom -> chatRoom.addUser(userName));
  475.  
  476.     }
  477. }
  478.  
  479. ==========================================================
  480.  
  481. class Scheduler<T> {
  482.     TreeMap<Date, T> map;
  483.  
  484.     Scheduler() {
  485.         map = new TreeMap<>();
  486.     }
  487.  
  488.     void add(Date d, T t) {
  489.         map.put(d, t);
  490.     }
  491.  
  492.     boolean remove(Date d) {
  493.         return map.remove(d) != null;
  494.     }
  495.  
  496.     T next() {
  497.         Date now = new Date();
  498.         return map.ceilingEntry(now).getValue();
  499.     }
  500.  
  501.     T last() {
  502.         Date now = new Date();
  503.         return map.floorEntry(now).getValue();
  504.     }
  505.  
  506.     ArrayList<T> getAll(Date begin, Date end) {
  507.         return new ArrayList<T>(map.subMap(begin, end).values());
  508.     }
  509.  
  510.     T getFirst() {
  511.         return map.firstEntry().getValue();
  512.     }
  513.  
  514.     T getLast() {
  515.         return map.lastEntry().getValue();
  516.     }
  517. }
Advertisement
Add Comment
Please, Sign In to add comment