Jump to content
MakeWebGames

Recommended Posts

Posted

Recently I've been looking in to a lot of programming puzzles for fun and I came across one of the versions of FizzBuzz.

Rules

 

  • If a string starts with "f" then you display "Fizz".
  • If a string ends with "b" then you display "Buzz".
  • If a string starts with "f" and ends with "b" then you display "FizzBuzz".

 

Spoiler

Posted

I made one for PHP. If the line is a divisor of 3 it displays "Fizz", if it's a divisor of 5 it displays "Buzz", and if it's a divisor of both it displays "FizzBuzz".

for($i = 1; $i <= 100; $i++){    
   echo ($i%3==0 ? ($i%5==0?"FizzBuzz":"Fizz") : ($i%5==0?"Buzz": $i))."\n";
}
Posted (edited)
I made one for PHP. If the line is a divisor of 3 it displays "Fizz", if it's a divisor of 5 it displays "Buzz", and if it's a divisor of both it displays "FizzBuzz".
for($i = 1; $i <= 100; $i++){    
   echo ($i%3==0 ? ($i%5==0?"FizzBuzz":"Fizz") : ($i%5==0?"Buzz": $i))."\n";
}

Awesome, I was going to try that next, also if you're interested in another one try to generate the Fibonacci Sequence.

Edited by Script47
Posted

So, I've been trying to make a random string generator, I've done this in PHP before, it was a lot easier it PHP than in Java, but this is what I came up with, it isn't the best.

 

public static String generateString(boolean numbersToo) {
	Random rGen = new Random();
	String string = "";
	String charsAndNums[][] = {{"A" + rGen.nextInt(99), "B" + rGen.nextInt(99), "C" + rGen.nextInt(99), "D" + rGen.nextInt(99), "E" + rGen.nextInt(99), "F" + rGen.nextInt(99), "G" + rGen.nextInt(99), "H" + rGen.nextInt(99), "I" + rGen.nextInt(99), "J" + rGen.nextInt(99), "K" + rGen.nextInt(99), "L" + rGen.nextInt(99), "M" + rGen.nextInt(99), "N" + rGen.nextInt(99), "O" + rGen.nextInt(99), "P" + rGen.nextInt(99), "Q" + rGen.nextInt(99), "R" + rGen.nextInt(99), "S" + rGen.nextInt(99), "T" + rGen.nextInt(99), "U" + rGen.nextInt(99), "V" + rGen.nextInt(99), "W" + rGen.nextInt(99), "X" + rGen.nextInt(99), "Y" + rGen.nextInt(99), "Z" + rGen.nextInt(99)}};

	for(int i = rGen.nextInt(10) + 4; i <= rGen.nextInt(26) + 5; i++) {	
		string += charsAndNums[0][i];
	}
	return string;
}

 

Any ideas to improve are welcome.

Posted (edited)
I made one for PHP. If the line is a divisor of 3 it displays "Fizz", if it's a divisor of 5 it displays "Buzz", and if it's a divisor of both it displays "FizzBuzz".
for($i = 1; $i <= 100; $i++){    
   echo ($i%3==0 ? ($i%5==0?"FizzBuzz":"Fizz") : ($i%5==0?"Buzz": $i))."\n";
}

Pfft, you n00b!

Little shorter next time, please.

My attempt in the last couple minutes -- challenge is to beat it.

EDIT: Forums breaks it with smiley codes *ugh*

Link: https://paste.ee/p/f7KrG

Edit 2: If my server would have accepted \n instead of <br> I could have chopped another 2 characters *boo*

Edited by Guest
Posted

I tried the Fibonacci Sequence before and came up with this.

 

public static void fibonacciSequence() {
	int x = 2;
	int y = 3;

	System.out.println(1 + "\n" + 1 + "\n" + 2 + "\n" + 3);

	for(int i = 0; i <= 7; i++) {
		int z = x + y;
		System.out.println(z);
		x = y;
		y = z;
	}
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...