• Congratulations to the Class of 2024 on your results!
    Let us know how you went here
    Got a question about your uni preferences? Ask us here

The Code Marathon. (4 Viewers)

KingOfActing

lukewarm mess
Joined
Oct 31, 2015
Messages
1,016
Location
Sydney
Gender
Male
HSC
2016
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
Joined
May 29, 2013
Messages
3,908
Gender
Undisclosed
HSC
2015
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
Joined
Oct 31, 2015
Messages
1,016
Location
Sydney
Gender
Male
HSC
2016
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
Joined
May 24, 2016
Messages
12
Gender
Male
HSC
2015
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
Joined
Jul 25, 2013
Messages
1,334
Location
Sydney
Gender
Male
HSC
2014
Uni Grad
2018
Isn't there two ways to sort array of numbers; Bubble and Selection ?
 

Rhinoz8142

Well-Known Member
Joined
Jul 25, 2013
Messages
1,334
Location
Sydney
Gender
Male
HSC
2014
Uni Grad
2018
I hope I learn something here, being an IT student I kinda suck at coding lel.
 

KingOfActing

lukewarm mess
Joined
Oct 31, 2015
Messages
1,016
Location
Sydney
Gender
Male
HSC
2016
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)
		{
			modes.add(Integer.parseInt(e.getKey()));
		}
	}
	return modes;
}

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

brent012

Webmaster
Webmaster
Joined
Feb 26, 2008
Messages
5,291
Gender
Male
HSC
2011
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) :p
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
Joined
May 28, 2014
Messages
3,650
Gender
Undisclosed
HSC
2015
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
Joined
May 28, 2014
Messages
3,650
Gender
Undisclosed
HSC
2015
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
Joined
Feb 26, 2008
Messages
5,291
Gender
Male
HSC
2011
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
Joined
Oct 31, 2015
Messages
1,016
Location
Sydney
Gender
Male
HSC
2016
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 :p
 

Flop21

Well-Known Member
Joined
May 12, 2013
Messages
2,807
Gender
Female
HSC
2015
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
Joined
May 12, 2013
Messages
2,807
Gender
Female
HSC
2015
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
Joined
Feb 26, 2008
Messages
5,291
Gender
Male
HSC
2011
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.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 4)

Top