I have a solution but its wayyyyyyyy to long. It involves sorting the array an then finding the middle term. (I havn't done sorting before so maybe thats why im so bad at it)
The naive solution most 1917 student's would give would probably be sort it then take the median as you are doing. Just bare in mind in computing it is never the first solution that comes in mind that is the most efficient, you will learn this if you take further comp courses in unsw.
Unless your sorting algorithm is better than O(nlogn) you're better off using Arrays#sort (in Java, at least)
2015 HSC
Mathematics 2U
2016 HSC
Advanced English, Drama, Maths 3U, Maths 4U, Music 2, Extension Music, Chemistry, Software Design & Development
Currently studying
Advanced Mathematics (Hons)/Computer Science @ UNSW (2017– )
2015 HSC
Mathematics 2U
2016 HSC
Advanced English, Drama, Maths 3U, Maths 4U, Music 2, Extension Music, Chemistry, Software Design & Development
Currently studying
Advanced Mathematics (Hons)/Computer Science @ UNSW (2017– )
Ooooh this is a good idea for a thread
Bachelor of Science (Advanced Mathematics) @ USYD
Java:
Runs in O(n) complexity. Can easily be changed to any sized array by changing the 5 to depend on array size.Code:public static double median(int[] a) { PriorityQueue<Integer> queue = new PriorityQueue<>(5); for(int i : a) { queue.offer(i); if(queue.size() > 5) { queue.poll(); } } return (queue.poll()+queue.poll())/2D; }
2015 HSC
Mathematics 2U
2016 HSC
Advanced English, Drama, Maths 3U, Maths 4U, Music 2, Extension Music, Chemistry, Software Design & Development
Currently studying
Advanced Mathematics (Hons)/Computer Science @ UNSW (2017– )
Cheeky heapsort
Java:Question 4: Given a sentence in the format of a String, find the vowel/total-letter ratio for each word. Your output should be given with one word per line, as a percentage rounded up to 2 decimal places. Input can consist of letters (A-Z,a-z), spaces, and these 5 punctuation symbols: (, ' ? ! . )Code:public static <T> T nthFromEnd(LinkedList<T> list, int n) { if(n < 0 || n > list.size()) throw new IllegalArgumentException("Invalid value of n - " + n + " for list of size " + list.size()); Iterator<T> iterator = list.descendingIterator(); for(int i = 0; i < n; i++) { iterator.next(); } return iterator.next(); }
Example input:
Expected output:Code:This is an awesome question, don't you agree?
Code:This 25.00% is 50.00% an 50.00% awesome 57.14% question, 50.00% don't 25.00% you 66.67% agree? 60.00%
_{Hint: Regular expressions will make your code much easier to write but aren't necessary}
Last edited by KingOfActing; 17 Jun 2016 at 8:20 PM.
2015 HSC
Mathematics 2U
2016 HSC
Advanced English, Drama, Maths 3U, Maths 4U, Music 2, Extension Music, Chemistry, Software Design & Development
Currently studying
Advanced Mathematics (Hons)/Computer Science @ UNSW (2017– )
Wow, this is AWESOME! Write a program that takes a list of numbers as input (can be a string of space or comma separated ints entered by user or can be a file), converts the input into an array (or linked list depending on language) of numbers (must also deal with negative numbers) and outputs which number is the most recurring. If there are no recurring numbers, i.e. all unique numbers, then the output should be the maximum number.
Examples:
If input is "1 2 -21 3 3 7 7 7 3 7 7 -123 7 7 9 -7" then the output should be 7.
If the input is "1 -123 7 2 8 -2 9 4 5 6 3 -7" then the output should be 9.
If input is "" output should be an error message.
Last edited by UNSWStudent; 16 Jun 2016 at 5:48 AM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks