Advertisement
AntonioWF

zad1

Nov 2nd, 2023
1,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.43 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. @SuppressWarnings("unchecked")
  5. class Array<E> {
  6.     private E data[]; // declared to be an Object since it would be too
  7.     // complicated with generics
  8.     private int size;
  9.  
  10.     public Array(int capacity) {
  11.         this.data = (E[]) new Object[capacity];
  12.         this.size = 0;
  13.     }
  14.  
  15.     public void insertLast(E o) {
  16.         //check if there is enough capacity, and if not - resize
  17.         if(size + 1 > data.length)
  18.             this.resize();
  19.         data[size++] = o;
  20.     }
  21.  
  22.     public void insert(int position, E o) {
  23.         // before all we check if position is within range
  24.         if (position >= 0 && position <= size) {
  25.             //check if there is enough capacity, and if not - resize
  26.             if(size + 1 > data.length)
  27.                 this.resize();
  28.             //copy the data, before doing the insertion
  29.             for(int i=size;i>position;i--) {
  30.                 data[i] = data[i-1];
  31.             }
  32.             data[position] = o;
  33.             size++;
  34.         } else {
  35.             System.out.println("Ne mozhe da se vmetne element na taa pozicija");
  36.         }
  37.     }
  38.  
  39.     public void set(int position, E o) {
  40.         if (position >= 0 && position < size)
  41.             data[position] = o;
  42.         else
  43.             System.out.println("Ne moze da se vmetne element na dadenata pozicija");
  44.     }
  45.  
  46.     public E get(int position) {
  47.         if (position >= 0 && position < size)
  48.             return data[position];
  49.         else
  50.             System.out.println("Ne e validna dadenata pozicija");
  51.         return null;
  52.     }
  53.  
  54.     public int find(E o) {
  55.         for (int i = 0; i < size; i++) {
  56.             if(o.equals(data[i]))
  57.                 return i;
  58.         }
  59.         return -1;
  60.     }
  61.  
  62.     public int getSize() {
  63.         return size;
  64.     }
  65.  
  66.     public void delete(int position) {
  67.         // before all we check if position is within range
  68.         if (position >= 0 && position < size) {
  69.             // first resize the storage array
  70.             E[] newData = (E[]) new Object[size - 1];
  71.             // copy the data prior to the delition
  72.             for (int i = 0; i < position; i++)
  73.                 newData[i] = data[i];
  74.             // move the data after the deletion
  75.             for (int i = position + 1; i < size; i++)
  76.                 newData[i - 1] = data[i];
  77.             // replace the storage with the new array
  78.             data = newData;
  79.             size--;
  80.         }
  81.     }
  82.  
  83.     public void resize() {
  84.         // first resize the storage array
  85.         E[] newData = (E[]) new Object[size*2];
  86.         // copy the data
  87.         for (int i = 0; i < size; i++)
  88.             newData[i] = data[i];
  89.         // replace the storage with the new array
  90.         this.data = newData;
  91.     }
  92.  
  93.     @Override
  94.     public String toString() {
  95.         String ret = new String();
  96.         if(size>0) {
  97.             ret = "{";
  98.             ret += data[0];
  99.             for(int i=1;i<size;i++) {
  100.                 ret += "," + data[i];
  101.             }
  102.             ret+="}";
  103.             return ret;
  104.         }
  105.         else {
  106.             ret = "Prazna niza!";
  107.         }
  108.         return ret;
  109.     }
  110.  
  111. }
  112.  
  113. public class ArrayMeanWordLength {
  114.  
  115.     //TODO: implement function
  116.         public static String wordClosestToAverageLength(Array<String> arr) {
  117.     int sumOfLengths = 0;
  118.     for (int i = 0; i < arr.getSize(); i++) {
  119.         sumOfLengths += arr.get(i).length();
  120.     }
  121.  
  122.     double averageLength = (double) sumOfLengths / arr.getSize();
  123.  
  124.     String closestWord = "";
  125.     double closestLengthDifference = Integer.MAX_VALUE;
  126.  
  127.     for (int i = 0; i < arr.getSize(); i++) {
  128.         String word = arr.get(i);
  129.         double lengthDifference = Math.abs(word.length() - averageLength);
  130.         double currentWordLength = closestWord.length();
  131.  
  132.         if (lengthDifference < closestLengthDifference || (lengthDifference == closestLengthDifference && word.length() > currentWordLength)) {
  133.             closestWord = word;
  134.             closestLengthDifference = lengthDifference;
  135.         }
  136.     }
  137.  
  138.     return closestWord;
  139. }
  140.  
  141.    
  142.  
  143.     public static void main(String[] args) {
  144.         Scanner input = new Scanner(System.in);
  145.  
  146.         int N = input.nextInt();
  147.         Array<String> arr = new Array<>(N);
  148.         input.nextLine();
  149.  
  150.         for(int i=0;i<N;i++) {
  151.             arr.insertLast(input.nextLine());
  152.         }
  153.  
  154.         System.out.println(wordClosestToAverageLength(arr));
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement