The Code Marathon. (1 Viewer)

KingOfActing

lukewarm mess
Joined
Oct 31, 2015
Messages
1,016
Location
Sydney
Gender
Male
HSC
2016
What's the solution, seems really tricky. Specifically the fact that there's numbers >9.
I posted a Java solution for it earlier :p

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?
 

brent012

Webmaster
Webmaster
Joined
Feb 26, 2008
Messages
5,291
Gender
Male
HSC
2011
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:
My question is still unanswered ;)
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();
	}
}
 
Last edited:

FizzBuzz

New Member
Joined
Jun 17, 2016
Messages
2
Gender
Undisclosed
HSC
N/A
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"
    );
}
 

KingOfActing

lukewarm mess
Joined
Oct 31, 2015
Messages
1,016
Location
Sydney
Gender
Male
HSC
2016
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);
}
 

brent012

Webmaster
Webmaster
Joined
Feb 26, 2008
Messages
5,291
Gender
Male
HSC
2011
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";
 

KingOfActing

lukewarm mess
Joined
Oct 31, 2015
Messages
1,016
Location
Sydney
Gender
Male
HSC
2016
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 :p)
 

brent012

Webmaster
Webmaster
Joined
Feb 26, 2008
Messages
5,291
Gender
Male
HSC
2011
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.
 

turntaker

Well-Known Member
Joined
May 29, 2013
Messages
3,908
Gender
Undisclosed
HSC
2015
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?
 

brent012

Webmaster
Webmaster
Joined
Feb 26, 2008
Messages
5,291
Gender
Male
HSC
2011
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.
 

turntaker

Well-Known Member
Joined
May 29, 2013
Messages
3,908
Gender
Undisclosed
HSC
2015
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.

And thanks for the advice.
 

Drsoccerball

Well-Known Member
Joined
May 28, 2014
Messages
3,650
Gender
Undisclosed
HSC
2015
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.
 

Drsoccerball

Well-Known Member
Joined
May 28, 2014
Messages
3,650
Gender
Undisclosed
HSC
2015
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;
}
 

keepLooking

Active Member
Joined
Aug 25, 2014
Messages
477
Gender
Male
HSC
2015
@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;
}
EDIT: Add 8/8 paint explanation:
 

Attachments

Last edited:

FizzBuzz

New Member
Joined
Jun 17, 2016
Messages
2
Gender
Undisclosed
HSC
N/A
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;
}
 

porcupinetree

not actually a porcupine
Joined
Dec 12, 2014
Messages
664
Gender
Male
HSC
2015
Write a method/program which:

  • Tests whether an input word/string is a palindrome
  • If not, then determines whether or not there is a possible arrangement of the word/string such that it is a palindrome, and then gives one of these arrangements
 

turntaker

Well-Known Member
Joined
May 29, 2013
Messages
3,908
Gender
Undisclosed
HSC
2015
Write a method/program which:

  • Tests whether an input word/string is a palindrome
  • If not, then determines whether or not there is a possible arrangement of the word/string such that it is a palindrome, and then gives one of these arrangements
ill do this after my last exam, but my guess is convert string to an array of char and iterate backwards to check for the word again.

Not sure how you would go about the second part. ill think about it later
 

brent012

Webmaster
Webmaster
Joined
Feb 26, 2008
Messages
5,291
Gender
Male
HSC
2011
my guess is convert string to an array of char and iterate backwards to check for the word again.
Alternatively, in Java, StringBuilder/StringBuffer have a reverse() method.
 

brent012

Webmaster
Webmaster
Joined
Feb 26, 2008
Messages
5,291
Gender
Male
HSC
2011
ah yeah lol.
I am new to java and very unfamiliar with built in methods
StringBuilder and StringBuffer (same interface iirc) have a lot of built in string operations. BigInteger also has isProbablePrime and gcd which are sometimes useful for these style of questions.
 

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

Top