1. ## Re: The Code Marathon.

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)

2. ## Re: The Code Marathon.

Originally Posted by Flop21
You are confused by 'linked lists'? Or something else? Because it's understandable if you're confused with linked lists if you haven't been taught that concept yet.

It's a way of storing data, you point to a 'node' which stores the value and the pointer to the next node... so on until it reaches the end (points to nothing/NULL).
yeah I haven't done Linked lists yet

3. ## Re: The Code Marathon.

Originally Posted by Drsoccerball
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)
use a sorting algorithm

4. ## Re: The Code Marathon.

Originally Posted by Drsoccerball
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)
That's understandable sorting is 2nd semester work. This is a solution, however this isn't the optimal one.

5. ## Re: The Code Marathon.

Originally Posted by leehuan
turntaker goes to UoW.
How is this related?

6. ## Re: The Code Marathon.

Originally Posted by SpiralFlex
That's understandable sorting is 2nd semester work. This is a solution, however this isn't the optimal one.
Is there a simple 1st sem solution

7. ## Re: The Code Marathon.

Originally Posted by Drsoccerball
Is there a simple 1st sem solution
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.

8. ## Re: The Code Marathon.

Unless your sorting algorithm is better than O(nlogn) you're better off using Arrays#sort (in Java, at least)

9. ## Re: The Code Marathon.

Originally Posted by Drsoccerball
How is this related?
he probably implies that I may have covered sorting algorithms, which I have.

10. ## Re: The Code Marathon.

Originally Posted by KingOfActing
Unless your sorting algorithm is better than O(nlogn) you're better off using Array#sort (in Java, at least)
yea, but I think thats kinda cheating lol (since C probably doesn't have a method that sorts)

11. ## Re: The Code Marathon.

Alright I should take a break from this since I have an exam tomorrow and I haven't really studied for it.
But I will be back after exams

12. ## Re: The Code Marathon.

Originally Posted by turntaker
yea, but I think thats kinda cheating lol (since C probably doesn't have a method that sorts)
Why rewrite code that already exists? Besides, C has qsort that's basically the same thing

13. ## Re: The Code Marathon.

Originally Posted by KingOfActing
Why rewrite code that already exists? Besides, C has qsort that's basically the same thing
On yeah true haha

14. ## Re: The Code Marathon.

Ooooh this is a good idea for a thread

15. ## Re: The Code Marathon.

Originally Posted by porcupinetree
Ooooh this is a good idea for a thread
Waiting for the rep

16. ## Re: The Code Marathon.

Originally Posted by Drsoccerball
Waiting for the rep
he is shouting HSP so its ok if he doesn't rep

17. ## Re: The Code Marathon.

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.

18. ## Re: The Code Marathon.

Originally Posted by KingOfActing
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

19. ## Re: The Code Marathon.

Originally Posted by turntaker
nice, neve4r seen Priority Queue before
Cheeky heapsort

Originally Posted by SpiralFlex
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

20. ## Re: The Code Marathon.

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.

21. ## Re: The Code Marathon.

Isn't there two ways to sort array of numbers; Bubble and Selection ?

22. ## Re: The Code Marathon.

Originally Posted by Rhinoz8142
Isn't there two ways to sort array of numbers; Bubble and Selection ?
There is bucket as well lol

23. ## Re: The Code Marathon.

I hope I learn something here, being an IT student I kinda suck at coding lel.

24. ## Re: The Code Marathon.

Originally Posted by Rhinoz8142
I hope I learn something here, being an IT student I kinda suck at coding lel.
you should try the first challenge

25. ## Re: The Code Marathon.

nah

Page 2 of 6 First 1234 ... Last

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•