Script47 Posted October 27, 2014 Share Posted October 27, 2014 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 Quote Link to comment Share on other sites More sharing options...
KyleMassacre Posted October 27, 2014 Share Posted October 27, 2014 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"; } Quote Link to comment Share on other sites More sharing options...
Script47 Posted October 27, 2014 Author Share Posted October 27, 2014 (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 October 27, 2014 by Script47 Quote Link to comment Share on other sites More sharing options...
Zettieee Posted October 27, 2014 Share Posted October 27, 2014 This is acutally awesome O.o Nice work :) Quote Link to comment Share on other sites More sharing options...
Sim Posted October 27, 2014 Share Posted October 27, 2014 Awesome, I was going to try that next, also if you're interested in another one try to generate the Fibonacci Sequence. I remember writing a Fibonacci thing in highschool. Wow oh wow that was like 15yrs ago... Quote Link to comment Share on other sites More sharing options...
Script47 Posted October 29, 2014 Author Share Posted October 29, 2014 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. Quote Link to comment Share on other sites More sharing options...
SRB Posted October 29, 2014 Share Posted October 29, 2014 (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 October 29, 2014 by Guest Quote Link to comment Share on other sites More sharing options...
Script47 Posted October 29, 2014 Author Share Posted October 29, 2014 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; } } Quote Link to comment Share on other sites More sharing options...
Alan Posted October 29, 2014 Share Posted October 29, 2014 [MENTION=50378]Guest[/MENTION]: 86 characters; not too bad but I'm sure I can shave a few off that - how about 62 - http://codepad.org/jSBqb1ms Quote Link to comment Share on other sites More sharing options...
Script47 Posted October 29, 2014 Author Share Posted October 29, 2014 [MENTION=50378]Guest[/MENTION]: 86 characters; not too bad but I'm sure I can shave a few off that - how about 62 - http://codepad.org/jSBqb1ms Wow haha, good shaving! :D Quote Link to comment Share on other sites More sharing options...
SRB Posted October 29, 2014 Share Posted October 29, 2014 @Guest: 86 characters; not too bad but I'm sure I can shave a few off that - how about 62 - http://codepad.org/jSBqb1ms A quote from Skype earlier in relation to this thread; I’m thinking only Alan (friend of mine) could do it, but we will see. Quote Link to comment Share on other sites More sharing options...
KyleMassacre Posted October 30, 2014 Share Posted October 30, 2014 Haha hawt dayum I didn't even know it can work like that. Heck, I can't even read it right Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.