turntaker
Well-Known Member
- Joined
- May 29, 2013
- Messages
- 3,908
- Gender
- Undisclosed
- HSC
- 2015
he is shouting HSP so its ok if he doesn't repWaiting for the rep
he is shouting HSP so its ok if he doesn't repWaiting for the rep
Java:Question 2: write a program that finds the median of an unsorted array of 10 integer elements. You can generate the array however you wish, e.g. via command-line arguments, STDIN, looping.
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;
}
nice, neve4r seen Priority Queue beforeJava:
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; }
Cheeky heapsortnice, neve4r seen Priority Queue before
Java:1. Given a linked list, find the k-th element from the end, using only ONE single pass.
For example, 1->2->3->4->5->X, the 0th element from the end is 5, 1st element from the end is 4 etc.
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();
}
This is an awesome question, don't you agree?
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%
There is bucket as well lolIsn't there two ways to sort array of numbers; Bubble and Selection ?
you should try the first challengeI hope I learn something here, being an IT student I kinda suck at coding lel.
I don't see a conversion to an array of integers necessary, since you're just using modes. This also handles the case of having multiple modes (like "0 1 1 0 3" will give [0,1])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.
public static List<Integer> modes(String line)
{
if(line == null || line == "") throw new IllegalArgumentException();
Map<String, Integer> map = new HashMap<>();
int count = 1;
for(String key : line.split(" "))
{
Integer temp = map.containsKey(key) ? map.get(key) + 1 : 1;
map.put(key, temp);
if(temp > count)
{
count = temp;
}
}
List<Integer> modes = new ArrayList<>();
for(Entry<String, Integer> e : map.entrySet())
{
if(e.getValue() == count)
{
modes.add(Integer.parseInt(e.getKey()));
}
}
return modes;
}
There are so many. For sorting integers I think Radix sort is pretty cool.There is bucket as well lol
This tbh, sorting is just used to introduce algorithms + analysis. If you just use what's provided by the framework/language you get code which is likely highly optimised and more likely to be correct. They also often use hybrid approaches like: https://en.wikipedia.org/wiki/IntrosortUnless your sorting algorithm is better than O(nlogn) you're better off using Arrays#sort (in Java, at least)
For those that don't know there's a function called isspace() that finds whether there is a space at a specific position. ( Which you can use for this question)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%
Hint: Regular expressions will make your code much easier to write but aren't necessary
This was in todays comp1911 exam.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.
IMO, it's much simpler to use String.Split (in Java, other languages have similar methods) or split using regex so you can deal with each word on its own.For those that don't know there's a function called isspace() that finds whether there is a space at a specific position. ( Which you can use for this question)
Yup, example of that in my solution to the previous questionIMO, it's much simpler to use String.Split (in Java, other languages have similar methods) or split using regex so you can deal with each word on its own.
What's the solution, seems really tricky. Specifically the fact that there's numbers >9.This was in todays comp1911 exam.
Here is my solution:Question 1 : Write a C program (or whatever language you use) that takes a single integer as argument and prints out a letter F of size n using * characters in EXACTLY the format shown below. You should also print error messages if there is invalid input.
Example:
./letterF 4
* * * *
*
*
* * * *
*
*
./letterF 5
* * * * *
*
*
* * * * *
*
*
*
./letterF 6
* * * * * *
*
*
*
* * * * * *
*
*
*
Let the games begin.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void printFPattern (int size);
int main (int argc, char *argv[]) {
assert (argc > 1);
int size = atoi (argv[1]);
printFPattern (size);
return EXIT_SUCCESS;
}
void printFPattern (int size) {
int row = 0;
int col = 0;
while (row < size+2) {
while (col < size) {
if (row == 0 || row == (size/2)+1 || col == 0) {
printf("*");
}
else {
printf(" ");
}
col++;
}
printf("\n");
row++;
col = 0;
}
}
Any standard test framework would be able to do this, it's just a string comparison.Anyone know how I would make tests for these sorts of programs?