[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Webdevs can't even FizzBuzz wtf?
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 123
Thread images: 8
File: 1431434986158.png (750 KB, 854x853) Image search: [Google]
1431434986158.png
750 KB, 854x853
Webdevs can't even FizzBuzz wtf?
>>
Friendly reminder that the modulo operator is useless outside of fizzbuzz.
>>
>>53574682

I use it quite a lot to do something every nth iteration of a loop
>>
>>53575028
Can't you just make your step increase for every nth?
>>
File: 1450757134729.jpg (25 KB, 440x410) Image search: [Google]
1450757134729.jpg
25 KB, 440x410
>>53575056
>implying anon is doing anything every step of the loop as well
>>
>>53575056
If computational space and complexity were free, then sure.
>>
10 PRINT "PC ARE GAY"
20 GOTO 10
RUN

name the computer os and year
>>
>>53575056

the nth loop might concat some text, the nth % 5 might print the text to the console or something as well
>>
So for the longest time I've heard of people doing job interviews and having to program FizzBuzz around campus. Most of these people didn't get the job, they said it was too hard. I just googled what its supposed to do. Took me half a minute. Fuck higher education and all the fags that exist in it.
>>
>>53574667
const saySomething = (number) => {
let isMultipleOfThree = !(number % 3);
let isMultipleOfFive = !(number % 5);

if(isMultipleOfThree && isMultipleOfFive) return "FizzBuzz";
if(isMultipleOfThree) return "Fizz";
if(isMultipleOfFive) return "Buzz";

return number;

};

for (let i = 1; i <= 100; i++)
console.log(saySomething(i));


I'm a web dev, did I win something?
>>
>>53574682
> never has to output how long something will take in a human readable format

He's like a child.
>>
>>53574682
>he has never written a function that checks if a number is even or odd
>>
>>53575452
Yeah, what's up with that.
I thought it was a joke when I first heard about it.
>>
>>53574667
She doesn't even work with pure javascript.
>>
>>53575595
c2.com/cgi/wiki?FizzBuzzTest
-read Why FizzBuzz is "hard"
>>
>>53574682
Found the codemonkey
>>
But does your FizzBuzz based in a view make a report, which is intercepted by middleware that breaks the program flow, then calls a reducer and creates a state change, which is then updates the view?
>>
>>53574682
>what is modulo arithmetic
You'll need that if you want to make your videogames when you grow up
>>
>>53575466
too verbose too many lines you lose
>>
>>53576155
Who cares, it is readable and verbosity when you don't fuck shit up won't ruin performance.
>>
File: 1458301802226.jpg (581 KB, 1329x1917) Image search: [Google]
1458301802226.jpg
581 KB, 1329x1917
>women in tech

https://css-tricks.com/tales-of-a-non-unicorn-a-story-about-the-trouble-with-job-titles-and-descriptions/
>>
>>53576155
You're right maybe I should rewrite it in machine code
>>
>>53574682
you should take a look at cryptography
>>
>>53574667
20 seg to write wut
for i in range(0,101):
if i%3 == 0 and i%5 == 0:
print('fizz buzz')
elif i%3 == 0:
print('fizz')
elif i%5 == 0:
print('buzz')
else:
print(i)
>>
>>53576182
I see no problem really.
She wrote a function that encapsulates only one function but makes it more verbose by including full English words.

That's basically how Ruby was designed.
>>
>>53574667

public string fizzBuzz(int i){

String strTMP = "";

if (0 == (strTMP % 3)){
strTMP = "fizz";
}

if (0 == (strTMP % 5)){
strTMP = strTMP + "buzz";
}

if (strTMP.length() < 1){
return strTMP;
} else {
return (String) i;
}

}
>>
>>53576388
Not bad, though LOC isn't everything you know, executing the same statement twice is considered bad practice. Try storing the return values of your arithmetic in variables, it's what they're made for
>>
>>53576676
better?
for i in range(0,101):
fizzbuzz = ''
if i%3 == 0:
fizzbuzz += 'fizz'
if i%5 == 0:
fizzbuzz += 'buzz'
if fizzbuzz == '':
fizzbuzz += str(i)
print(fizzbuzz)
>>
>>53574682
Use it plus date for shitty random numbers.
>>
>>53576788
Sort of but you're giving up readability here, I'd do variable assignment in the if statements and then run an and statement on those variables

so something like
if (a = i%3) == 0
if (b = i%5) == 0
if (a and b)

will make things a lot easier for someone to understand the code at first glance
>>
>>53577044
thx for the tips anon
>>
>>53577058
Don't listen to him. >>53576388 was good already. Putting i%3 and i%5 into valuables do not guarantee both increase in execution speed and decrease in code size.
>>
>>53577245
This. Don't do stupid things a compiler can/should do for you.
>>
>>53577245
>>53577302

It's not about optimization but readability. And not having to edit things multiple times when there's a change.
>>
>>53574682
i guess all of cryptography is just useless.
>>
Yes we can.

https://www.npmjs.com/package/fizzbuzz
>>
>>53574682

You can use it to test whether a number is even or odd.
>>
>>53575452

How is fizzbuzz even hard? My friend tells me people who have gone through college fail it all the time.

I'm a hobbiest programmer who has no schooling with this shit, and I figured this out in less than a minute too.
>>
>>53575589
>>53580131
if(num & 1){
//odd
}else{
//even
}


shoo shoo pajeet
>>
>>53580602
That doesn't work in most languages.
>>
>>53574667
Guys, please.
Stop reposting my picture. This topic is too fucking old to even talk about it
>>
>>53580165
>How is fizzbuzz even hard?
The whole point is that it is not. For anyone who can actually program- as distinct from people pasting JS snippets from stackoverflow- it is beyond trivial.

By the way, reading this thread has given me renewed confidence in myself. I have no formal education (not even a high school diploma!) and my only work experience has been remote freelance jobs. But I can competently write correct code to spec from scratch, I have experience in quite a few languages and platforms, and I learn fast. I'm considering applying for some Actual Jobs(tm), now. Can't do any worse than some of these horror stories.
>>
>>53576315
What's the use of the modulo operator (as opposed to the modulo operation on bigints) in cryptography?
>>
File: 1317584462615.jpg (13 KB, 256x220) Image search: [Google]
1317584462615.jpg
13 KB, 256x220
>>53581129
>hi gais let's have a some more constructive discussions
>>
>>53577746
Putting assignment into if condition does not increase readability. Your claim about not having to edit multiple things is also extremely dubious when applied to fizzbuzz.
>>
>>53575466
>>53574667
saySomething = function(n) {
buzz=function(isFizz) {
if (!(n % 5)) return "Buzz";
else if (isFizz) return ""; else return n;
};
if (!(n % 3)) return "Fizz"+buzz(true); else return buzz(false);
};
for (var i = 1; i <= 100; i++) console.log(saySomething(i));
>>
http://codepen.io/SachaG/pen/jCpba
>>
>tfw you fizzbuzz in random esolangs but most don't have a modulo operator so you have to keep counters like a pleb
>>
Is this good enough fizzbuzz?

#include <stdio.h>

int main() {
int i;
for (i=1; i<=100; i++) {
if ((i%3==0) && (i%5==0)) {
printf("fizzbuzz ");
} else if (i%3==0) {
printf("fizz ");
} else if (i%5==0) {
printf("buzz ");
} else {
printf("%d ", i);
}
}
printf("\n");
return(0);
}
>>
>>53581263
Biased random numbers :D
>>
>>53578055
EXACTLY! At least someone in tech gets it.
>>
>>53575126
Commodore 64 accepts BASIC like that, but I think other (especially Commodore variants) accepted that too back in the day. C64 came out early 1980s. I only know the precise date from wikipedia though. Based 6502/6510 and based expansion port with cartridges.
Good thing was imho about C64 BASIC that basicly it was an easier assembly. It's control flow and how a program looked like was quite close. GOTO/GOSUB -> JMP/CALL
Of course, you always numbered your programs like 10,20 or 10,15,20 instead of n,n+1 etc so you can do minor patching easily.
That programming experience was so my first and most eye-opening... C and x86 ASM was very easy afterwards. C giving me structure and an easier experience while ASM being a bit lower level "puzzle" where good tricks mattered. I'm still in my twenties though, but I would never trade my childhood C64 experience.
>>
>>53581263
Used a lot in RSA to both generate keys, encrypt and decrypt
>>
Scala is the winner
(1 until 100).foreach(n => println({
def buzz(isFizz: Boolean) = if (n % 5 == 0) "Buzz" else if (isFizz) "" else n
if (n % 3 == 0) "Fizz"+buzz(true) else buzz(false)
})
)
>>
>>53586364
>((i%3==0) && (i%5==0))
What is this?
>>
>>53588643
Looks like "just in case" brackets
>>
#include <iostream>
#include <thread>
#include <memory>
#include <future>
#include <vector>
#include <string>

std::vector<std::string> doFizzBuzz(uint64_t start, uint64_t end)
{
std::vector<std::string> result;
result.reserve(end - start + 1);

for (; start <= end; ++start)
{
std::string str;
if (start % 3 == 0)
str += "fizz";
if (start % 5 == 0)
str += "buzz";

if (str.empty())
str = std::to_string(start);
else
str[0] = toupper(str[0]);

result.push_back(std::move(str));
}
return result;
}

void parallelFizzbuzz(uint64_t start, uint64_t end)
{
auto threadNum = std::thread::hardware_concurrency();
std::vector<std::future<std::vector<std::string>>> futures;

uint64_t numsPerThread = (end - start) / threadNum;
auto remainder = (end - start) % threadNum;
auto lastEnding = start;

for(auto i = 0; i < threadNum; ++i)
{
auto begin = lastEnding;
auto ending = begin + numsPerThread;

if (remainder)
{
++ending;
--remainder;
}
lastEnding = ending;

futures.push_back(std::async(std::launch::async, doFizzBuzz, begin, ending));
}

for(auto&& future : futures)
{
auto&& vec = future.get();
for (auto&& string : vec)
puts(string.c_str());
}
}

int main(int argc, const char * argv[])
{
parallelFizzbuzz(1, 1000000);
}
>>
>>53574682
Fucking degenerate
>>
>>53576182
Easily the best picture Ive seen this week.
>>
>>53589483
wtf dood
is this pasta or did you write that
>>
>>53576182
I agree with that blog though.

The position was for a designer and a designer shouldn't need to know that stuff.

It's like if I go in for a PHP job and they ask me to make a custom wordpress theme. I don't know the first thing about coding wordpress and my ui skills suck ass. I would probably blog about it too.
>>
>>53574682
panjeet, pls, use a trip next time for easy filtering purposes
>>
>>53574682
Not entirely true. If you want someone for some serious backend engineering it's a bonus if they know about the modulo operator so they can google the exact definition and parameters if they ever need to use it for stuff like functional transition tables between abelian data tree models.
>>
<?php
function modulo($a, $n) {
$str = strval($a/$n);
if (strpos($str, '.') === false) {
return 0;
} else {
$decimalStr = explode('.', $str)[1];
$decimals = floatval('0.'.$decimalStr);
return intval(round($decimals*$n));
}
}


Where do I get my CS diploma?
>>
>>53575056
not if you have to do something outside the nth step.
>>
i=1;
var i = i+1;
if i/3= intgr then
i="fizz";
if i/5= intgr then
i="buzz";
if i/3=intgr , i/5=intgr then
i="fizzbuzz";
show i;

how did I do
>>
>>53574682
Friendly reminder that you actually need math to program well.
>>
Is this supposed to be hard? Did I fuck up somewhere and not see it?

String prt = "";

for (int i=1; i<= 100; i++){

prt = "";

if(i%3==0){ prt += "Fizz"; }
if(i%5==0){ prt += "Buzz"; }
if(prt.equals("")){ prt = String.valueOf(i); }

System.out.println(prt);
}
>>
>>53576788
That loop will go 101 times.
>>
Can someone tell me why on earth we keep having these FUCKING ETERNAL FIZZBUZZ THREADS?
Like what's the fucking point. There's literally nothing interesting about it and I don't need to see 3 million different retard-versions of it.

Stop shitting up /g/ you fucking faggots
>>
>>53595046
Never fucking reply to me again unless you are contributing to the thread.
>>
>>53595112
kill yourself, degenerate faggot
>>
>>53594350
Nope, you're completely correct. it's a test to fish out smooth-talking retards during job-interviews.
>>
>>53590505
Designer often isn't separated from coder when it comes to web development. The responsibilities portion of the position make clear that the employee would be involved in prototyping so I don't think she could get away with "I didn't know I'd need to know how to code!".

People hang up on the FizzBuzz question but it's really the second one that gets me.
>Write a function that takes a timecode string and turns it into seconds.
>Again I started talking through it, but it was impossible for me to figure it out with someone watching. I needed to do some serious Googling. He said I could email back my solutions. I toyed with the idea of calling up and saying, "forget it, this isn't for me" but I decided to stick it out. After spending a few hours coming up with something that semi-worked, I found the solution on StackOverflow and, in my honesty, linked to it in the code.

It's even easier than FizzBuzz and it took her HOURS and she didn't even approach it from a direction of "what function do I need?" but "what is the solution?" when she went to Google.

I bet that now, after all of it, if you asked her to do the same problems again she would have to go back to Google or the code she copied which means she didn't learn anything.
>>
>>53574682
Friendly reminder that you've never implemented a hashing algorithm in your entire life.
>>
>>53574682
Some book wanted me to make an array of the years [1950, 1960, 1970, 1980, 1990, 2010]
python doesnt have conventional for-loop so i did:
[y for y in range(1950, 2011) if y % 10 == 0]
>>
>>53574682
>he never wrote a circular buffer
Here's your (You), you earned it.
>>
>>53596755
are you trolling or don't you know that you can give a step argument?
range(1950,2011, 10) would have given you exactly that.
>>
File: 1441953749056.png (2 KB, 300x200) Image search: [Google]
1441953749056.png
2 KB, 300x200
void
fizzbuzz(unsigned int n, unsigned int limit)
{
if (n > limit)
return;
else {
std::cout << n << ": "
<< ((n % 3 == 0) ? "Fizz" : "")
<< ((n % 5 == 0) ? "Buzz" : "") << "\n";
fizzbuzz(n+1, limit);
}
}
>>
>>53596250
>It's even easier than FizzBuzz

It's not, you actually have to do some calculations, which is almost absent from FizzBuzz.

But wait... so, you get like 11:30:52 and then convert it to seconds? As in (11*60*60) + (30*60) + 52?
>>
>>53597147
What's the advantage of doing it recursively instead of using a for loop? How much is needed to burst the heap with recursive functions?
>>
>>53597303
GCC will _probably_ optimize the tail call, and I'm pretty sure clang will, however there's really no reason to do it that way in a heavily imperative language like C++.
If you wanted to show off your tail-recursion skills, use a FP language like Haskell or Lisp or OCaml


fizzBuzz :: Int -> String
fizzBuzz n = case (n `mod` 3, n `mod` 5) of
(0, 0) -> "FizzBuzz"
(0, _) -> "Fizz"
(_, 0) -> "Buzz"
(_, _) -> show n

main :: IO ()
main =
mapM_ (putStrLn . fizzBuzz) [1..100]
>>
>>53594951
Nop in python this will go 0 to 100
>>
>>53597303
>What's the advantage of doing it recursively
There is none. I've just done fizzbuzz before and I wanted to try it a new way.
It broke at iteration 1048149.
>>
Why would a web dev need to be able to program in fizzbuzz?
>>
>>53597338
Rust has `elements` of functional programming, does that count?
fn fizzbuzz() {
for i in 1..101 {
match (i % 3, i % 5) {
(0, 0) => println!("FizzBuzz"),
(0, _) => println!("Fizz"),
(_, 0) => println!("Buzz"),
(_, _) => println!("{}", i)
};
};
}
>>
>>53597389
Pattern matching would be a great choice in any language that permits it. I'm working on a C++ project right now and really regret not doing the whole thing in Rust, it looks much more tolerable.
>>
>>53593573
In the non-blocking languages department
>>
File: 1410541206497.png (84 KB, 694x732) Image search: [Google]
1410541206497.png
84 KB, 694x732
>>
>>53597369
>program in fizzbuzz

I'll have you know, fizzbuzz is a growing technology, in 5 years it's going to take over the entire web development scene. Better start learning while you can, you'll come out on top.
>>
>>53597264
Seems likely. Just splitting the string and then multiplying out for minutes,and hours.
>>
>>53597472
jesus fucking christ, that's not even programming, it's common sense. What the fuck. You only need to know how to split a fucking string and then use your regular, kardashian-show watching rotting brain to do the fucking math.
>>
Any way I can improve this?


public class FizzBuzz
{

public static void main(String[] args)
{
for (byte counter = 1; counter <= 100; counter++)
{
if (counter % 3 == 0 && counter % 5 == 0)
{
System.out.println("FizzBuzz");
}
else
{
if (counter % 3 == 0)
{
System.out.println("Fizz");
}
else
{
if (counter % 5 == 0)
{
System.out.println("Buzz");
}
else
{
System.out.println(counter);
}
}
}
}
}

}
>>
>>53597555
does java not have else if?
also common convention in java is to put opening brace on the same line, newline is a C# thing
otherwise it's functional but boring
>>
>>53597555
B R A V O
R
A
V
O
>>
>>53597555
Why are you using bytes? And you don't need to nest if-else blocks, just use and if-else if-else block.
>>
File: 1442465016296.jpg (189 KB, 576x576) Image search: [Google]
1442465016296.jpg
189 KB, 576x576
>>53574667
>Abstract Algebra
Fun class really
>>
>>53593736
it gave me cancer
>>
>>53589483
ENTERPRISE
>>
>>53597581
>Why are you using bytes?

Memory efficiency, I suppose. Doesn't really matter with a program as simple as Fizzbuzz, but I want it to turn into a habit.
>>
>>53597638
It's Java though, bytes are internally represented as integers. There's no memory saved.
>>
public static String[] fizzbuzz(int n) {
String[] ret = new String[n];
for(int i = 0; i < n; -~i) {
String tmp = "";
if(i%3 == 0) {tmp += "Fizz";}
if(i%5 == 0) {tmp += "Buzz";}
if(tmp.length() == 0) {tmp += i}
ret[i] = tmp;
}
return ret;
}
>>
>>53576388
> i%3 == 0 and i%5 == 0
> not writing that as i%15 == 0
>>
>>53597347
That's 101 times
>>
>>53597425
w-what
>>
>>53597936
Print the float to a string, then see if there's a negative sign.
>>
>>53598383
I'm more wondering as to what line of thought even lead to this kind of a solution.
>>
Y'all even C++?
#include <iostream>
#include <string>

template <int, const char*, int N>
struct String
{ };
template <const char* S, int N>
struct String<0, S, N>
{
String ()
{ std::cout.write(S, N); }
};

template <int, int>
struct Int
{ };
template <int N>
struct Int<0, N>
{
inline Int ()
{ std::cout << N; }
};

char fizz[] = "Fizz";
char buzz[] = "Buzz";

template <int N=100>
struct FizzBuzz
: FizzBuzz<N - 1>
, String<(N % 3), fizz, 4>
, String<(N % 5), buzz, 4>
, Int<!((N % 3) * (N % 5)), N>
{
inline FizzBuzz ()
{ std::cout << std::endl; }
};
template <>
struct FizzBuzz<0>
{ };

FizzBuzz<> _;
int main () {}
>>
>>53574682
what's a circular array?
>>
>>53597649
Even in C, often it doesn't save any memory because of alignment.
In the end, nobody gives a shit about 3 bytes, and it's not worth the potential overflow errors you'll have.

e.g. Find the bug, without compiling:
typedef unsigned char u8;

int main () {
for (u8 i = 0; i < 256; i++)
printf("%d\n", (int) i);
return 0;
}
>>
https://github.com/learn-co-students/rspec-fizzbuzz-q-000/compare/master...karliekloss:master
>>
>>53574682

Friendly reminder that you don't need a modulo operator to do FizzBuzz. You only need to be able to do basic addition, assignment, and control flow (if, for). If you can't even do FizzBuzz, you are objectively a shitty programmer.
>>
>>53598522
Oh god why
>>
>>53598602
Not the original guy, but I'm assuming that is in C?
Want to pick up C so let me take a stab at this...

Would it be that the u8 is in the for loop? I did a quick example search, and I don't see how that u8 would be used.

Unless, its taking i and multiplying by u8, the char, making i an unsigned char type?

In that case (Looking at my C reference book) it looks like you overflowing the i variable? I don't think that would be wrong either though, because unsigned char stops at 255.

My other guess would be that parsing i as an int without redefining does some wonky stuff.
>>
>>53598602
Had to stare at it for like five minutes before getting it. Probably wouldn't have caught anything if I didn't already know there's a bug.
>>
>>53600303
>Would it be that the u8 is in the for loop? I did a quick example search, and I don't see how that u8 would be used.
Did you miss the typedef? Look up typedef in your book.

>Unless, its taking i and multiplying by u8, the char, making i an unsigned char type?
u8 isn't a variable and this isn't your maths class. "pi r r" is a syntax error not an equation. Again, look up typedef.

>In that case (Looking at my C reference book) it looks like you overflowing the i variable? I don't think that would be wrong either though, because unsigned char stops at 255.
This is it. Unsigned char doesn't 'stop' at 255 When you store a value into an unsigned char, that number is 'truncated', or taken modulo 256. Modulo caused such a tizzy in the first reply. What's 255 + 1 mod 256? Every arithmetic integer operation in a computer is done modulo some 2^n.

>My other guess would be that parsing i as an int without redefining does some wonky stuff.
No problems with casting. I guess you could say that wonky things happen with casting in the signed case, but only if you're watching the underlying bit pattern.
>>
>>53598602
If anyone is still wondering: The maximum value an unsigned char can hold is 255.
>>
>>53600860
I was wondering if it was one or two bytes. Since it is C, I should have assumed one byte rather than two that are commonly used for Unicode today.
>>
>>53600870
A char is 1 byte. The thing is that it's unsigned.
Usually, a char can hold values from i BELIEVE -128 to 127.
An unsigned char holds values from 0 to 255. Both 1 byte, just represented differently.
>>
>>53574667
I was fizzbuzzing to get a job in a webdev company.
kek
>>
>>53600860
Yup.
What a bitch, right? What looks like a loop from 0..255 will actually be infinite.
If you're lucky you'll get a compiler warning.
>>
File: 2teeth-0911-10.jpg (21 KB, 403x336) Image search: [Google]
2teeth-0911-10.jpg
21 KB, 403x336
    for (int i = 1; i < 101; i++) {
!(i % 3 | i % 5) ? cout << "FizzBuzz\n" : !(i % 3) ? cout << "Fizz\n" : !(i % 5) ? cout << "Buzz\n" : cout << i << "\n";
}
>>
>>53574682
But what if I want to write increment the index but have it roll over? Add an if because I'm just a code monkey?

x = (x + 1) % y
vs
x++
if (x >= y)
x = 0
Thread replies: 123
Thread images: 8

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.