# The Code Marathon. (1 Viewer)

#### turntaker

##### Well-Known Member
Waiting for the rep
he is shouting HSP so its ok if he doesn't rep

#### KingOfActing

##### lukewarm mess
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.
Java:
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;
}``````
Runs in O(n) complexity. Can easily be changed to any sized array by changing the 5 to depend on array size.

#### turntaker

##### Well-Known Member
Java:
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;
}``````
Runs in O(n) complexity. Can easily be changed to any sized array by changing the 5 to depend on array size.
nice, neve4r seen Priority Queue before

#### KingOfActing

##### lukewarm mess
nice, neve4r seen Priority Queue before
Cheeky heapsort

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.
Java:
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();
}``````
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: (, ' ? ! . )

Example input:
Code:
``This is an awesome question, don't you agree?``
Expected output:
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:

#### UNSWStudent

##### New Member
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:

#### Rhinoz8142

##### Well-Known Member
Isn't there two ways to sort array of numbers; Bubble and Selection ?

#### turntaker

##### Well-Known Member
Isn't there two ways to sort array of numbers; Bubble and Selection ?
There is bucket as well lol

#### Rhinoz8142

##### Well-Known Member
I hope I learn something here, being an IT student I kinda suck at coding lel.

#### turntaker

##### Well-Known Member
I hope I learn something here, being an IT student I kinda suck at coding lel.
you should try the first challenge

nah

#### KingOfActing

##### lukewarm mess
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.
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])

Java:
Code:
``````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)
{
}
}
return modes;
}``````

------------
My question from before still stands unanswered

#### brent012

##### Webmaster
Webmaster
There is bucket as well lol
There are so many. For sorting integers I think Radix sort is pretty cool.

Unless your sorting algorithm is better than O(nlogn) you're better off using Arrays#sort (in Java, at least)
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/Introsort

#### Drsoccerball

##### Well-Known Member
Cheeky heapsort

Java:
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();
}``````
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: (, ' ? ! . )

Example input:
Code:
``This is an awesome question, don't you agree?``
Expected output:
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
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)

#### Drsoccerball

##### Well-Known Member
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.
This was in todays comp1911 exam.

#### brent012

##### Webmaster
Webmaster
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)
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.

#### KingOfActing

##### lukewarm mess
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.
Yup, example of that in my solution to the previous question

#### Flop21

##### Well-Known Member
This was in todays comp1911 exam.
What's the solution, seems really tricky. Specifically the fact that there's numbers >9.

#### Flop21

##### Well-Known Member
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.
Here is my solution:

Code:
``````#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;
}
}``````
Anyone know how I would make tests for these sorts of programs? I had one in an exam and it was able to test if the shape was exact or not.

Also any criticism or style corrections/suggestions are welcome.

#### Flop21

##### Well-Known Member
Here's another one a bit harder IMO, from a past comp1917 exam.

// you can assume size is an odd positive value
// this function is to print on the screen a cross
// made of asterisks which fits inside a square of
// side length "size"
// void drawCross (int size);
/*

#### brent012

##### Webmaster
Webmaster
Anyone know how I would make tests for these sorts of programs?
Any standard test framework would be able to do this, it's just a string comparison.

For quiz questions, it's likely that the person that set the exam wrote a correct solution and used that to generate test data, or used it directly in the assertions.

If you were doing this kind of question using TDD, you'd directly use any input/output specified and then make some of your own test cases by hand.