Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ComplexNumber<T extends Number, U extends Number>
- implements Comparable<ComplexNumber<?, ?>> {
- T real;
- U imag;
- public ComplexNumber(T real, U imag) {
- this.real = real;
- this.imag = imag;
- }
- public T getReal() {
- return real;
- }
- public void setReal(T real) {
- this.real = real;
- }
- public U getImaginary() {
- return imag;
- }
- public void setImaginary(U imag) {
- this.imag = imag;
- }
- public double modul() {
- /* sqrt(x^2 + y^2) */
- return Math.sqrt(Math.pow(real.doubleValue(), 2) + Math.pow(imag.doubleValue(), 2));
- }
- @Override
- public int compareTo(ComplexNumber<?, ?> that) {
- return Double.compare(this.modul(), that.modul());
- }
- @Override
- public String toString() {
- return String.format("%.2f%+.2fi", real.doubleValue(), imag.doubleValue());
- }
- }
- ===============================================================================
- class Node<T> {
- T element;
- Node<T> next;
- public Node(T element, Node<T> next) {
- this.element = element;
- this.next = next;
- }
- public T getElement() {
- return element;
- }
- public void setElement(T element) {
- this.element = element;
- }
- public Node<T> getNext() {
- return next;
- }
- public void setNext(Node<T> next) {
- this.next = next;
- }
- }
- class Queue<T> {
- Node<T> front;
- Node<T> rear;
- int length;
- public Queue() {
- this.front = null;
- this.rear = null;
- this.length = 0;
- }
- public boolean isEmpty() {
- return length == 0;
- }
- public void enqueue(T element) {
- Node<T> toBeAdded = new Node<T>(element, null);
- /* Initial Scenario */
- if (front == null) {
- front = toBeAdded;
- rear = toBeAdded;
- }
- /* Running Scenario */
- else {
- rear.setNext(toBeAdded);
- rear = toBeAdded;
- }
- length++;
- }
- public T dequeue() throws EmptyQueueException {
- /* Initial Scenario */
- if (isEmpty()) {
- throw new EmptyQueueException();
- }
- /* Running Scenario */
- T toBeRemoved = front.getElement();
- front = front.getNext();
- length--;
- return toBeRemoved;
- }
- public T peek() throws EmptyQueueException {
- /* Initial Scenario */
- if (isEmpty()) {
- throw new EmptyQueueException();
- }
- /* Running Scenario */
- return front.getElement();
- }
- public T inspect() throws EmptyQueueException {
- /* Initial Scenario */
- if (isEmpty()) {
- throw new EmptyQueueException();
- }
- /* Running Scenario */
- return rear.getElement();
- }
- public int count() {
- return length;
- }
- }
- class EmptyQueueException extends Exception {
- }
- ===============================================================================
- class BinaryTreeSet<T extends Comparable<T>> {
- class Node<T extends Comparable<T>> {
- T element;
- Node<T> left;
- Node<T> right;
- boolean exist;
- public Node(T element, Node<T> left, Node<T> right) {
- this.element = element;
- this.left = left;
- this.right = right;
- this.exist = true;
- }
- }
- Node<T> root;
- BinaryTreeSet() {
- this.root = null;
- }
- Node<T> insert(T element, Node<T> node) {
- /* Initial Scenario */
- if (node == null) {
- node = new Node<>(element, null, null);
- }
- /* Running Scenario */
- if (element.compareTo(node.element) < 0) {
- node.left = insert(element, node.left);
- } else if (element.compareTo(node.element) > 0) {
- node.right = insert(element, node.right);
- } // If it's a duplicate we don't do anything
- return node;
- }
- void addElement(T element) {
- root = insert(element, root);
- }
- boolean contains(T element, Node<T> node) {
- /* Initial Scenario */
- if (node == null) {
- return false;
- }
- /* Running Scenario */
- if (element.compareTo(node.element) == 0) {
- return true;
- } else if (element.compareTo(node.element) < 0) {
- return contains(element, node.left);
- } else if (element.compareTo(node.element) > 0) {
- return contains(element, node.right);
- }
- return false;
- }
- boolean contains(T element) {
- return contains(element, root);
- }
- boolean remove(T element, Node<T> node) {
- /* Initial Scenario */
- if (node == null) {
- return false;
- }
- /* Running Scenario */
- if (element.compareTo(node.element) == 0) {
- node.exist = false;
- return true;
- } else if (element.compareTo(node.element) < 0) {
- return remove(element, node.left);
- } else if (element.compareTo(node.element) > 0) {
- return remove(element, node.right);
- }
- return false;
- }
- boolean removeElement(T element) {
- return remove(element, root);
- }
- boolean isEmpty() {
- return root == null;
- }
- /* Left Root Right */
- void inorder(Node<T> node, StringBuffer sb) {
- if (node != null) {
- inorder(node.left, sb);
- sb.append(node.element + " ");
- inorder(node.right, sb);
- }
- }
- @Override
- public String toString() {
- if (isEmpty()) return "EMPTY";
- StringBuffer sb = new StringBuffer();
- inorder(root, sb);
- sb.setLength(sb.length() - 1);
- return sb.toString();
- }
- }
- ==============================================================================
- class IntegerList {
- List<Integer> integers;
- IntegerList() {
- this.integers = new ArrayList<>();
- }
- IntegerList(Integer... numbers) {
- this.integers = new ArrayList<>(Arrays.asList(numbers));
- }
- void add(int el, int idx) {
- if (idx > integers.size()) {
- IntStream.range(0, integers.size() - idx)
- .forEach(i -> integers.add(0));
- integers.add(idx, el);
- }
- integers.add(idx, el);
- }
- boolean validateIndex(int index) {
- if (index < 0 || index > integers.size()) {
- throw new ArrayIndexOutOfBoundsException();
- }
- return false;
- }
- int remove(int idx) {
- validateIndex(idx);
- return integers.remove(idx);
- }
- void set(int el, int idx) {
- validateIndex(idx);
- integers.add(idx, el);
- }
- int get(int idx) {
- validateIndex(idx);
- return integers.get(idx);
- }
- int size() {
- return integers.size();
- }
- int count(int el) {
- return Collections.frequency(integers, el);
- /*return (int) integers.stream()
- .filter(i -> i.equals(el))
- .count();*/
- }
- void removeDuplicates() {
- /*integers = IntStream.range(0, integers.size())
- .map(i -> integers.size() - 1 - i)
- .mapToObj(this::get)
- .distinct()
- .collect(Collectors.toList());
- Collections.reverse(integers);*/
- Collections.reverse(integers);
- integers = integers.stream().distinct().collect(Collectors.toList());
- Collections.reverse(integers);
- }
- int sumFirst(int k) {
- return integers.stream()
- .limit(k)
- .mapToInt(Integer::valueOf)
- .sum();
- }
- int sumLast(int k) {
- return integers.stream()
- .skip(integers.size() - k)
- .mapToInt(Integer::valueOf)
- .sum();
- }
- void shiftRight(int idx, int k) {
- validateIndex(idx);
- int shiftIndex = (idx + k) % integers.size();
- Integer removed = integers.remove(idx);
- integers.add(shiftIndex, removed);
- }
- void shiftLeft(int idx, int k) {
- validateIndex(idx);
- int shiftIndex = (idx - k) % integers.size();
- if (shiftIndex < 0) {
- shiftIndex += integers.size();
- }
- Integer removed = integers.remove(idx);
- integers.add(shiftIndex, removed);
- }
- IntegerList addValue(int value) {
- Integer numbers[] = integers.stream()
- .map(i -> i + value)
- .toArray(Integer[]::new);
- return new IntegerList(numbers);
- }
- }
- class NoSuchRoomException extends Exception {
- public NoSuchRoomException(String roomName) {
- super(roomName);
- }
- }
- class NoSuchUserException extends Exception {
- public NoSuchUserException(String userName) {
- super(userName);
- }
- }
- class ChatRoom {
- String name;
- Set<String> usernames;
- public ChatRoom(String name) {
- this.name = name;
- this.usernames = new TreeSet<>();
- }
- public void addUser(String username) {
- usernames.add(username);
- }
- public void removeUser(String username) {
- usernames.remove(username);
- }
- public boolean hasUser(String username) {
- return usernames.contains(username);
- }
- public int numUsers() {
- return usernames.size();
- }
- @Override
- public String toString() {
- StringBuffer sb = new StringBuffer();
- sb.append(name + "\n");
- if (usernames.isEmpty()) {
- sb.append("EMPTY\n");
- } else {
- String text = usernames.stream().collect(Collectors.joining("\n"));
- sb.append(text);
- }
- return sb.toString();
- }
- public String getName() {
- return name;
- }
- }
- class ChatSystem {
- Map<String, ChatRoom> chatrooms;
- Set<String> usernames;
- public ChatSystem() {
- this.chatrooms = new TreeMap<>();
- this.usernames = new HashSet<>();
- }
- public void addRoom(String roomName) {
- chatrooms.put(roomName, new ChatRoom(roomName));
- }
- public void removeRoom(String roomName) {
- chatrooms.remove(roomName);
- }
- public ChatRoom getRoom(String roomName) throws NoSuchRoomException {
- return Optional
- .ofNullable(chatrooms.get(roomName))
- .orElseThrow(() -> new NoSuchRoomException(roomName));
- }
- public void register(String userName) {
- usernames.add(userName);
- if (!chatrooms.isEmpty()) {
- ChatRoom leastPopulatedRoom = chatrooms.values().stream()
- .min(Comparator.comparing(ChatRoom::numUsers))
- .orElse(null);
- if (leastPopulatedRoom != null) {
- leastPopulatedRoom.addUser(userName);
- }
- }
- }
- void validateUserAndRoom(String userName, String roomName) throws NoSuchRoomException, NoSuchUserException {
- if (!chatrooms.containsKey(roomName)) throw new NoSuchRoomException(roomName);
- if (!usernames.contains(userName)) throw new NoSuchUserException(userName);
- }
- public void joinRoom(String userName, String roomName) throws NoSuchRoomException, NoSuchUserException {
- validateUserAndRoom(userName, roomName);
- chatrooms.get(roomName).addUser(userName);
- }
- public void leaveRoom(String userName, String roomName) throws NoSuchRoomException, NoSuchUserException {
- validateUserAndRoom(userName, roomName);
- chatrooms.get(roomName).removeUser(userName);
- }
- public void registerAndJoin(String userName, String roomName) throws NoSuchRoomException, NoSuchUserException {
- usernames.add(userName);
- joinRoom(userName, roomName);
- }
- public void followFriend(String userName, String friendUserName) throws NoSuchUserException {
- if (!usernames.contains(userName)) throw new NoSuchUserException(userName);
- chatrooms.values().stream()
- .filter(i -> i.hasUser(friendUserName))
- .forEach(chatRoom -> chatRoom.addUser(userName));
- }
- }
- ==========================================================
- class Scheduler<T> {
- TreeMap<Date, T> map;
- Scheduler() {
- map = new TreeMap<>();
- }
- void add(Date d, T t) {
- map.put(d, t);
- }
- boolean remove(Date d) {
- return map.remove(d) != null;
- }
- T next() {
- Date now = new Date();
- return map.ceilingEntry(now).getValue();
- }
- T last() {
- Date now = new Date();
- return map.floorEntry(now).getValue();
- }
- ArrayList<T> getAll(Date begin, Date end) {
- return new ArrayList<T>(map.subMap(begin, end).values());
- }
- T getFirst() {
- return map.firstEntry().getValue();
- }
- T getLast() {
- return map.lastEntry().getValue();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment