1. ## Re: The Code Marathon.

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

2. ## Re: The Code Marathon.

Originally Posted by turntaker
There is bucket as well lol
There are so many. For sorting integers I think Radix sort is pretty cool.

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

3. ## Re: The Code Marathon.

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

4. ## Re: The Code Marathon.

Originally Posted by UNSWStudent
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.

5. ## Re: The Code Marathon.

Originally Posted by Drsoccerball
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.

6. ## The Code Marathon.

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

7. ## Re: The Code Marathon.

Originally Posted by Drsoccerball
This was in todays comp1911 exam.
What's the solution, seems really tricky. Specifically the fact that there's numbers >9.

8. ## Re: The Code Marathon.

Originally Posted by Drsoccerball
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.

9. ## Re: The Code Marathon.

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);
/*

10. ## Re: The Code Marathon.

Originally Posted by Flop21
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.

11. ## Re: The Code Marathon.

Originally Posted by Flop21
What's the solution, seems really tricky. Specifically the fact that there's numbers >9.
I posted a Java solution for it earlier

Originally Posted by Flop21
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);
Java:
Code:
```public static void drawCross(int size)
{
for(int i = 0; i < size; i++)
{
char[] array = new char[size];
for(int j = 0; j < size; j++)
{
array[j] = ' ';
}
array[i] = '*';
array[size - i - 1] = '*';
System.out.println(new String(array));
}
}```

My question is still unanswered Side note: anyone know how to do java syntax highlighting (if it's possible) on this forum?

12. ## Re: The Code Marathon.

A common whiteboard interview question to weed people out who don't know what they are doing:

"Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”."

EDIT:
Originally Posted by KingOfActing
Since no one wants to do it lol...

Could be optimised, but good enough haha.

Code:
```
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] inputWords = in.nextLine().split(" ");

for (String word : inputWords) {
int total = 0, vowels = 0;
for (char c : word.toCharArray()) {
if (Character.isAlphabetic(c)) {
if ("eaiou".indexOf(c) >= 0) {
vowels++;
}

total++;
}
}

double percent = (vowels / (double) total) * 100;
System.out.printf("%s %.2f%%\n", word, percent);
}

in.close();
}
}```

13. ## Re: The Code Marathon.

Originally Posted by brent012
A common whiteboard interview question to weed people out who don't know what they are doing:

"Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”."

Code:
```#include <stdio.h>
int main(void) {
puts("1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\n"
"FizzBuzz\n16\n17\nFizz\n19\nBuzz\nFizz\n22\n23\nFizz\nBuzz\n26\n"
"Fizz\n28\n29\nFizzBuzz\n31\n32\nFizz\n34\nBuzz\nFizz\n37\n38\nFizz\n"
"Buzz\n41\nFizz\n43\n44\nFizzBuzz\n46\n47\nFizz\n49\nBuzz\nFizz\n52\n"
"53\nFizz\nBuzz\n56\nFizz\n58\n59\nFizzBuzz\n61\n62\nFizz\n64\nBuzz\n"
"Fizz\n67\n68\nFizz\nBuzz\n71\nFizz\n73\n74\nFizzBuzz\n76\n77\nFizz\n"
"79\nBuzz\nFizz\n82\n83\nFizz\nBuzz\n86\nFizz\n88\n89\nFizzBuzz\n91\n"
"92\nFizz\n94\nBuzz\nFizz\n97\n98\nFizz\nBuzz"
);
}```

14. ## Re: The Code Marathon.

Originally Posted by FizzBuzz
Code:
```#include <stdio.h>
int main(void) {
puts("1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\n"
"FizzBuzz\n16\n17\nFizz\n19\nBuzz\nFizz\n22\n23\nFizz\nBuzz\n26\n"
"Fizz\n28\n29\nFizzBuzz\n31\n32\nFizz\n34\nBuzz\nFizz\n37\n38\nFizz\n"
"Buzz\n41\nFizz\n43\n44\nFizzBuzz\n46\n47\nFizz\n49\nBuzz\nFizz\n52\n"
"53\nFizz\nBuzz\n56\nFizz\n58\n59\nFizzBuzz\n61\n62\nFizz\n64\nBuzz\n"
"Fizz\n67\n68\nFizz\nBuzz\n71\nFizz\n73\n74\nFizzBuzz\n76\n77\nFizz\n"
"79\nBuzz\nFizz\n82\n83\nFizz\nBuzz\n86\nFizz\n88\n89\nFizzBuzz\n91\n"
"92\nFizz\n94\nBuzz\nFizz\n97\n98\nFizz\nBuzz"
);
}```
Ahahah I love this

Alternatively:
Code:
```for(int i = 1; i <= 100; i++)
{
System.out.println(i%3 ==0? i%5==0? "FizzBuzz" : "Fizz" : i%5==0 ? "Buzz" : i);
}```

15. ## Re: The Code Marathon.

Originally Posted by KingOfActing
Ahahah I love this

Alternatively:
Code:
```for(int i = 1; i <= 100; i++)
{
System.out.println(i%3 ==0? i%5==0? "FizzBuzz" : "Fizz" : i%5==0 ? "Buzz" : i);
}```
Is that regex

16. ## Re: The Code Marathon.

Nah, that's just nested ternaries/conditional operators. IMO, they become pretty messy when you nest them.

It returns either the left or right value depending on the condition. i.e. someBool ? "is true" : " is false";

17. ## Re: The Code Marathon.

Originally Posted by brent012
Nah, that's just nested ternaries/conditional operators. IMO, they become pretty messy when you nest them.

It returns either the left or right value depending on the condition. i.e. someBool ? "is true" : " is false";
For such simple operations I usually prefer them over nested ifs since I always do brace = new line, and always braces after if/else, so that would become heaps messy (not to mention redundant "System.out#println" everywhere )

18. ## Re: The Code Marathon.

I was taught to avoid it as a general rule of thumb haha, it doesn't read well - you have to read it carefully/step through it to understand what's going to happen. For the same reasoning, I avoid multiple returns in the one function unless it's a switch or to short circuit a loop.

But, i'm a C# dev - so a lot of the misuse of ternaries is probably happening alongside LINQ queries which would amplify that problem.

19. ## Re: The Code Marathon.

Originally Posted by brent012
I was taught to avoid it as a general rule of thumb haha, it doesn't read well - you have to read it carefully/step through it to understand what's going to happen. For the same reasoning, I avoid multiple returns in the one function unless it's a switch or to short circuit a loop.

But, i'm a C# dev - so a lot of the misuse of ternaries is probably happening alongside LINQ queries which would amplify that problem.
Hey man do you have any advice for finding a job out of uni?

20. ## Re: The Code Marathon.

Originally Posted by turntaker
Hey man do you have any advice for finding a job out of uni?
As in once graduated, or outside of uni?

Best way of getting a job as a developer while still studying is to get involved with extra curricular programming stuff. Like programming competitions and hackathons. Not only will your skills improve, but often these events will be sponsored by companies that offer internships or are looking to hire students.

There's also probably also some networking events etc.

21. ## Re: The Code Marathon.

Originally Posted by brent012
As in once graduated, or outside of uni?

Best way of getting a job as a developer while still studying is to get involved with extra curricular programming stuff. Like programming competitions and hackathons. Not only will your skills improve, but often these events will be sponsored by companies that offer internships or are looking to hire students.

There's also probably also some networking events etc.
Both.

22. ## Re: The Code Marathon.

Originally Posted by Flop21
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.
Maybe you should assert that argc == 2 rather than > 1.

23. ## Re: The Code Marathon.

I can't figure out the right bottom arm of the x oh well :P
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int main(int argc, char *argv[]){
assert (argc == 2);
int count = 0;
for(int y = 0; y < atoi(argv[1]); y++){
count = count + y;
for(int i = y; i < atoi(argv[1]) - 1; i++){
if(i >= count){
printf("*");
count = 100000;
}
else{
printf(" ");
}
}

count = y + 1;

printf("*");
printf("\n");
}
return 0;
}```

24. ## Re: The Code Marathon.

@Drsoccerball, what does your code actually do? ( I can't seem to figure out lol.)

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int main( int argc, char *argv[ ] ) {
assert (argc == 2);
int x = 0;
int y = 0;
int counter = 0;
int size = atoi( argv[ 1 ] );
while( y < size ) {
while( x < size ) {
if( x == counter || x == size - counter - 1 ) {
printf( "*" );
} else {
printf( " " );
}
x++;
}
counter++;
x = 0;
printf( "\n" );
y++;
}
return EXIT_SUCCESS;
}```

25. ## Re: The Code Marathon.

To print an X using odd num input you can just print a square like,

*****
*****
*****
*****
*****

only print * if it's on the diagonals, and print a space otherwise!

Code:
```#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[]){
int i;
int j;
int size=atoi(argv[1]);
for (i=0;i<size;i++){
for (j=0;j<size;j++){
if (i==j||i+j+1==size){
printf("*");}
else{
printf(" ");}}
printf("\n");}
return 0;
}```

Page 3 of 6 First 12345 ... 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
•