Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
Dammit Rivorous you're drunk with power

Adbot
ADBOT LOVES YOU

Volte
Oct 4, 2004

woosh woosh

That Turkey Story posted:

I wanted to go and do it anyway:

C++ code:
#include <iostream>
#include <type_traits>

template< char... C >
struct tmp_string
{
  typedef tmp_string type;
  static char const c_string[ sizeof...( C ) + 1 ];
};

template< char... C >
char const tmp_string< C... >::c_string[ sizeof...( C ) + 1 ]
  = { C..., '\0' };

template< class... Strings > struct combine;

template< char... LC, char... RC >
struct combine< tmp_string< LC... >, tmp_string< RC... > >
  : tmp_string< LC..., RC... > {};

template< class L, class R, class... Strings >
struct combine< L, R, Strings... >
  : combine< typename combine< L, R >::type, Strings... > {};

template< unsigned Digit > struct digit_to_string
  : tmp_string< static_cast< char >( '0' + Digit ) > {};

template< unsigned Value, class CurrString >
struct value_to_string_impl
  : value_to_string_impl
    < Value / 10
    , typename combine
      < typename digit_to_string< Value % 10 >::type
      , CurrString
      >::type
    > {};

template< class CurrString >
struct value_to_string_impl< 0, CurrString > : CurrString {};

template< unsigned Value >
struct value_to_string : value_to_string_impl< Value, tmp_string<> > {};

template<> struct value_to_string< 0 > : tmp_string< '0' > {};

template< unsigned Curr, unsigned End >
struct fizzbuzz_impl;

template< unsigned Curr, unsigned End >
struct fizzbuzz_impl_fizz_or_buzz
  : combine
    < typename std::conditional
      < Curr % 3 == 0, tmp_string< 'F', 'i', 'z', 'z' >, tmp_string<> >::type
    , typename std::conditional
      < Curr % 5 == 0, tmp_string< 'B', 'u', 'z', 'z' >, tmp_string<> >::type
    , tmp_string< ' ' >
    , typename fizzbuzz_impl< Curr + 1, End >::type
    > {};

template< unsigned Curr, unsigned End >
struct fizzbuzz_impl_not_fizz_or_buzz
  : combine
    < typename value_to_string< Curr >::type
    , tmp_string< ' ' >
    , typename fizzbuzz_impl< Curr + 1, End >::type
    > {};

template< unsigned Curr, unsigned End >
struct fizzbuzz_impl
  : std::conditional< Curr % 3 == 0 || Curr % 5 == 0
                    , fizzbuzz_impl_fizz_or_buzz< Curr, End >
                    , fizzbuzz_impl_not_fizz_or_buzz< Curr, End >
                    >::type {};

template< unsigned Curr >
struct fizzbuzz_impl< Curr, Curr > : tmp_string<> {};

typedef fizzbuzz_impl< 1, 101 >::type fizzbuzz;

int main()
{
  std::cout << fizzbuzz::c_string;
}
This one produces a single, c-style string at compile time.
So does mine, that's what the screenshot is. It's embedded in the data section.

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

That Turkey Story posted:

I wanted to go and do it anyway:

C++ code:
#include <iostream>
#include <type_traits>

template< char... C >
struct tmp_string
{
  typedef tmp_string type;
  static char const c_string[ sizeof...( C ) + 1 ];
};

template< char... C >
char const tmp_string< C... >::c_string[ sizeof...( C ) + 1 ]
  = { C..., '\0' };

template< class... Strings > struct combine;

template< char... LC, char... RC >
struct combine< tmp_string< LC... >, tmp_string< RC... > >
  : tmp_string< LC..., RC... > {};

template< class L, class R, class... Strings >
struct combine< L, R, Strings... >
  : combine< typename combine< L, R >::type, Strings... > {};

template< unsigned Digit > struct digit_to_string
  : tmp_string< static_cast< char >( '0' + Digit ) > {};

template< unsigned Value, class CurrString >
struct value_to_string_impl
  : value_to_string_impl
    < Value / 10
    , typename combine
      < typename digit_to_string< Value % 10 >::type
      , CurrString
      >::type
    > {};

template< class CurrString >
struct value_to_string_impl< 0, CurrString > : CurrString {};

template< unsigned Value >
struct value_to_string : value_to_string_impl< Value, tmp_string<> > {};

template<> struct value_to_string< 0 > : tmp_string< '0' > {};

template< unsigned Curr, unsigned End >
struct fizzbuzz_impl;

template< unsigned Curr, unsigned End >
struct fizzbuzz_impl_fizz_or_buzz
  : combine
    < typename std::conditional
      < Curr % 3 == 0, tmp_string< 'F', 'i', 'z', 'z' >, tmp_string<> >::type
    , typename std::conditional
      < Curr % 5 == 0, tmp_string< 'B', 'u', 'z', 'z' >, tmp_string<> >::type
    , tmp_string< ' ' >
    , typename fizzbuzz_impl< Curr + 1, End >::type
    > {};

template< unsigned Curr, unsigned End >
struct fizzbuzz_impl_not_fizz_or_buzz
  : combine
    < typename value_to_string< Curr >::type
    , tmp_string< ' ' >
    , typename fizzbuzz_impl< Curr + 1, End >::type
    > {};

template< unsigned Curr, unsigned End >
struct fizzbuzz_impl
  : std::conditional< Curr % 3 == 0 || Curr % 5 == 0
                    , fizzbuzz_impl_fizz_or_buzz< Curr, End >
                    , fizzbuzz_impl_not_fizz_or_buzz< Curr, End >
                    >::type {};

template< unsigned Curr >
struct fizzbuzz_impl< Curr, Curr > : tmp_string<> {};

typedef fizzbuzz_impl< 1, 101 >::type fizzbuzz;

int main()
{
  std::cout << fizzbuzz::c_string;
}
This one produces a single, c-style string at compile time.

You're hired. When can you begin?

astr0man
Feb 21, 2007

hollyeo deuroga

Sailor_Spoon posted:

I plan on memorizing that seed for my next job interview.

:lol: This owns

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



Objective-C code:
#import <Foundation/Foundation.h>
#import <objc/runtime.h>

@interface MESwizzleBuzz : NSObject

- (void)go;

@end

@implementation MESwizzleBuzz

+ (void)load
{
    Method go = class_getInstanceMethod([self class], @selector(go));
    
    __block IMP *imps = malloc(sizeof(IMP)*100);
    IMP *p = imps;
    
    IMP orig = method_getImplementation(go);
    
    IMP fizz = imp_implementationWithBlock(^(id _s){
        NSLog(@"fizz");
        method_setImplementation(go, *++imps);
        [_s go];
    });
    
    IMP buzz = imp_implementationWithBlock(^(id _s){
        NSLog(@"buzz");
        method_setImplementation(go, *++imps);
        [_s go];
    });
    
    IMP fizzbuzz = imp_implementationWithBlock(^(id _s){
        NSLog(@"fizzbuzz");
        method_setImplementation(go, *++imps);
        [_s go];
    });
    
    IMP num = imp_implementationWithBlock(^(id _s){
        NSLog(@"%ld", labs(p-imps));
        method_setImplementation(go, *++imps);
        [_s go];
    });
    
    // welp, gently caress it...
    for (int i = 0; i < 100; i++) {
        imps[i] = num;
    }
    for (int i = 0; i < 100; i+=3) {
        imps[i] = fizz;
    }
    for (int i = 0; i < 100; i+=5) {
        imps[i] = buzz;
    }
    for (int i = 0; i < 100; i+=15) {
        imps[i] = fizzbuzz;
    }
    imps[99] = orig;
    
    method_setImplementation(go, *++imps);
}

- (void)go
{
    return;
}

@end

int main(int argc, const char * argv[])
{
    [[MESwizzleBuzz new] go];
    return 0;
}
I got bored halfway through, but this one continually replaces the go method on itself with a method that outputs the correct answer, before calling itself again.
:shepface:

qntm
Jun 17, 2009

Wheany posted:

This is not particularly clever, but I thought I'd contribute anyway.
JavaScript code:
(function () {
    var seed = ["fizzbuzz", null, null, "fizz", null, "buzz", "fizz", null, null, "fizz", "buzz", null, "fizz", null, null];
    for (var i = 1; i <= 100; i++) {
        console.log(seed[i%seed.length] || i);
    }
})()

Fits comfortably in a tweet, no conditionals:

Perl code:
print +("FizzBuzz", $_, $_, "Fizz", $_, "Buzz", "Fizz", $_, $_, "Fizz", "Buzz", $_, "Fizz", $_, $_)[$_ % 15]."\n" for 1 .. 100;

quiggy
Aug 7, 2010

[in Russian] Oof.


Sailor_Spoon posted:

code:
#!/usr/bin/env python

import random

for i in range(0, 100):
    if not i % 15:
        random.seed(1178741599)
    print [i+1, "Fizz", "Buzz", "FizzBuzz"][random.randint(0,3)]
I plan on memorizing that seed for my next job interview.

This doesn't seem to work. I just copied the code and got this output instead for the first 15:

code:
1
Buzz
FizzBuzz
4
5
Fizz
Buzz
Fizz
Fizz
Fizz
Fizz
12
FizzBuzz
FizzBuzz
Buzz
:smith:

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
The random algorithm changed in Python 2.4. That code might be dependent on the Wichmann-Hill Random Number Generator, rather than the Mersenne Twister algorithm. It seems that the Mersenne Twister in Python got updated to use the MT19337-64 variant at some time, which might have broke it.

KaneTW
Dec 2, 2011

code:
Python 2.7.3 (default, Mar  4 2013, 14:57:34)
[GCC 4.7.2] on linux2
--
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17

astr0man
Feb 21, 2007

hollyeo deuroga
It works in Python 2.6

quiggy
Aug 7, 2010

[in Russian] Oof.


Ah, the server I ran it on is running 2.2 apparently. Welp :v:

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug

quiggy posted:

Ah, the server I ran it on is running 2.2 apparently. Welp :v:

Wow. Out of curiosity, what distribution/version? 2.2 was released in 2001 and hasn't had a release since 2003.

That random seed doesn't work on 3.3, incidentally.

quiggy
Aug 7, 2010

[in Russian] Oof.


I suppose this is what I get for not checking versions on a lovely public university server :shobon:

This particular build is from Mar 27, 2002. It's running on SunOS 10.5, which itself is from Jan 2005. I swear to god I'm not the administrator of this thing.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Oh, yeah, Solaris was kinda behind on its Python version (and plenty of other stuff, too*) for a long time. I get the impression that that's just the nature of enterprisey UNIX distributions. At least they got caught up just before they became Larry Ellison's Patent Suit Data Provider :\

* I have fun writing and continually 'improving' my own find replacement in Python because the one that came with the last Solaris (10-5?) didn't even support -iname which I think is a GNU extension.

hobbesmaster
Jan 28, 2008

Volmarias posted:

You're hired. When can you begin?

I notice a distinct lack of a return statement from main. They made void legal for a reason!

ymgve
Jan 2, 2004


:dukedog:
Offensive Clock
For bonus points, write a program that finds a seed that works.

quiggy
Aug 7, 2010

[in Russian] Oof.


If I recall correctly last time this conversation came up someone wrote a genetic FizzBuzz algorithm. Now there's one to share during your job interview.

hobbesmaster
Jan 28, 2008

quiggy posted:

If I recall correctly last time this conversation came up someone wrote a genetic FizzBuzz algorithm. Now there's one to share during your job interview.

A link to a comedy fizzbuzz github would be a good gauge to see if you'd want to work somewhere.

good jovi
Dec 11, 2000

'm pro-dickgirl, and I VOTE!

ymgve posted:

For bonus points, write a program that finds a seed that works.

How do you think I found that seed?

good jovi
Dec 11, 2000

'm pro-dickgirl, and I VOTE!

Also, here's a ruby version that I think I saw on twitter originally, so I can't claim credit for the idea.

code:
puts (0..99).map {|i| srand(46308667) if (i%15).zero?; ["FizzBuzz", "Buzz", i+1, "Fizz"][rand(4)]}

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Sailor_Spoon posted:

How do you think I found that seed?

How long did it take to run? I'm assuming you just brute-forced it.

good jovi
Dec 11, 2000

'm pro-dickgirl, and I VOTE!

Munkeymon posted:

How long did it take to run? I'm assuming you just brute-forced it.

Yeah, I think I just let it run overnight. It wasn't quick, though I didn't really try to optimize.

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

hobbesmaster posted:

I notice a distinct lack of a return statement from main. They made void legal for a reason!

You're fired. Pack your things and get out.

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

Sailor_Spoon posted:

Yeah, I think I just let it run overnight. It wasn't quick, though I didn't really try to optimize.

And I was just about to ask. Hah, cool.

You know what would make a good weed-out question? Flip it, instead of asking them to solve the FizzBuzz, give them all of these solutions and tell them to pick which is most like they'd do it. Then auto-reject most of them. I bet a good number of the programmers who can't program would opt for the enterprise solution. :haw:

Alternatively, take one of the really pedantic implementations, break something, and ask them to find the bug. If they can put up with that nonsense, they're hired! Course then you have to assure them your codebase doesn't actually look that horrible.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Sailor_Spoon posted:

How do you think I found that seed?

By finding it on a site before? I found this from 6 months ago.

Dren
Jan 5, 2001

Pillbug
Am I too late for the fizzbuzz game? The random seed one inspired me.

C code:
#include <stdint.h>
#include <stdio.h>

int main()
{
    const uint32_t magic_number = 810092048;
    const char *words[] = {"%d\n", "Fizz\n", "Buzz\n", "FizzBuzz\n"};
    uint32_t tmp = 0;
    int i = 1;
    for (; i < 101; ++i)
    {
	if ( tmp == 0 ) tmp = magic_number;
	printf(words[tmp & 3], i);
	tmp >>= 2;
    }

    return 0;
}

Volte
Oct 4, 2004

woosh woosh
What have I done
C++ code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
 
int main(int argc, char *argv[]) {
    double h;
    int i, k, m, r, d, e, f, g, u=31354, q=26982;
    double rtp=0.15915494309, y=0.41887902048, z=0.62831853071;
    for (i = 1; i <= 100; i++) {
        r=0.1*i; f=0; g=0;
        h=-(i-1)*y; m=round((0.5-rtp*atan2(-sin(h),-cos(h)))*15);
        k=0x30490610>>(m<<1);
        h-=y; m=round((0.5-rtp*atan2(-sin(h),-cos(h)))*15);
        d=r&0x1|(r>>1)&0x1|(r>>2)&0x1|(r>>3)&0x1;
        e=k&0x1|(k>>1)&0x1;
        h=-i*z; m=round((0.5-rtp*atan2(-sin(h),-cos(h)))*10);
        g=(k&0x1)*u; f=(k&0x1)*q; fputs((char*)&f,stdout); fputs((char*)&g,stdout);
        g=((k>>1)&0x1)*u; f=((k>>1)&0x1)*(q+3068); fputs((char*)&f,stdout); fputs((char*)&g,stdout);
        f=r+0x30; f|=(m+0x30)<<(d<<3); f&=~-e; fputs((char*)&f,stdout); putchar(' ');
    }
    putchar('\n');
    return 0;
}
http://ideone.com/r3VOfZ

Volte fucked around with this message at 07:46 on May 21, 2013

QuarkJets
Sep 8, 2008

I'm lost, what is all of this fizz buzz poo poo?

McGlockenshire
Dec 16, 2005

GOLLOCKS!

QuarkJets posted:

I'm lost, what is all of this fizz buzz poo poo?

http://en.wikipedia.org/wiki/Fizz_buzz

quote:

Fizz buzz (also known as bizz buzz, or simply buzz) is a group word game for children to teach them about division.[1] Players take turns to count incrementally, replacing any number divisible by three with the word "fizz", and any number divisible by five with the word "buzz".

http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html

quote:

After a fair bit of trial and error I've discovered that people who struggle to code don't just struggle on big problems, or even smallish problems (i.e. write a implementation of a linked list). They struggle with tiny problems.

[...]

Most good programmers should be able to write out on paper a program which does [FizzBuzz] in a under a couple of minutes. Want to know something scary? The majority of comp sci graduates can't. I've also seen self-proclaimed senior programmers take more than 10-15 minutes to write a solution.

I was also going to say that it's one of the few worthwhile things that Jeff Atwood has ever actually contributed to software development, but it wasn't even his idea.

Also, http://rosettacode.org/wiki/FizzBuzz

Dren
Jan 5, 2001

Pillbug
Volte did you make all that up just for this or is it a transformation you adapted?

No Safe Word
Feb 26, 2005

Zaphod42 posted:

You know what would make a good weed-out question? Flip it, instead of asking them to solve the FizzBuzz, give them all of these solutions and tell them to pick which is most like they'd do it. Then auto-reject most of them. I bet a good number of the programmers who can't program would opt for the enterprise solution. :haw:
I love the enterprise solution and if I felt I could get away with it (ie, I could read that the interviewer had a sense of humor, which by presenting these they indicate they do) I'd jokingly pick it first.

Volte
Oct 4, 2004

woosh woosh

Dren posted:

Volte did you make all that up just for this or is it a transformation you adapted?
I wrote it by hand for some reason. I wanted to see if I could do it in a single loop with no conditionals, divisions, or mod operators. Plus I made it as obfuscated as I could.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Volte posted:

I wrote it by hand for some reason. I wanted to see if I could do it in a single loop with no conditionals, divisions, or mod operators. Plus I made it as obfuscated as I could.

A single loop with no conditionals or divmods is kind of easy though:

code:
int i = 1;
goto start;
while(i<100){
  printf("%d\n",i++);
  printf("Fizz\n",i++);
  printf("%d\n",i++);
  printf("%d\n",i++);
  printf("FizzBuzz\n",i++);  
start:
  printf("%d\n",i++);
  printf("%d\n",i++);
  printf("Fizz\n",i++);
  printf("%d\n",i++);
  printf("Buzz\n",i++);
  printf("Fizz\n",i++);
  printf("%d\n",i++);
  printf("%d\n",i++);
  printf("Fizz\n",i++);
  printf("Buzz\n",i++);
}

Volte
Oct 4, 2004

woosh woosh

Jabor posted:

A single loop with no conditionals or divmods is kind of easy though:

code:
int i = 1;
goto start;
while(i<100){
  printf("%d\n",i++);
  printf("Fizz\n",i++);
  printf("%d\n",i++);
  printf("%d\n",i++);
  printf("FizzBuzz\n",i++);  
start:
  printf("%d\n",i++);
  printf("%d\n",i++);
  printf("Fizz\n",i++);
  printf("%d\n",i++);
  printf("Buzz\n",i++);
  printf("Fizz\n",i++);
  printf("%d\n",i++);
  printf("%d\n",i++);
  printf("Fizz\n",i++);
  printf("Buzz\n",i++);
}
That's cheating

Volte
Oct 4, 2004

woosh woosh
Here is my solution with no conditionals or divmod. printf("1 2 fizz 4 buzz 6 ...")

Dren
Jan 5, 2001

Pillbug

Volte posted:

I wrote it by hand for some reason. I wanted to see if I could do it in a single loop with no conditionals, divisions, or mod operators. Plus I made it as obfuscated as I could.

You did quite a bit of division and multiplication by powers of 2.

good jovi
Dec 11, 2000

'm pro-dickgirl, and I VOTE!

Suspicious Dish posted:

By finding it on a site before? I found this from 6 months ago.

heh, that's actually me.

Smugdog Millionaire
Sep 14, 2002

8) Blame Icefrog
This one is my favorite fizzbuzz: http://dotnetpad.net/ViewPaste/SrmP90hoE02rYAyTIVHc3w

b0lt
Apr 29, 2005
Parallel fizzbuzz:

C++ code:
#include <stdio.h>
#include <stdlib.h>
#include <atomic>
#include <thread>
#include <vector>
 
int main(void) {
    std::atomic< bool > go(false);
    std::vector< std::thread > threads;
 
    for (int i = 1; i < 50; ++i) {
        threads.emplace_back(
            [i, &go] (void) {
                char text[33];
 
                while (!go) {}
 
                if (i % 15 == 0) {
                    sprintf(text, "FizzBuzz");
                }
                else if (i % 3 == 0) {
                    sprintf(text, "Fizz");
                }
                else if (i % 5 == 0) {
                    sprintf(text, "Buzz");
                }
                else {
                    sprintf(text, "%d", i);
                }
 
                char *ptr = text;
                while (*ptr) {
                    putchar(*ptr++);
                    std::this_thread::yield();
                }
 
                putchar('\n');
            });
    }
 
    go = true;
 
    for (auto &thread : threads) {
        thread.join();
    }
}
code:
F11FB
Fuizzzz
33iF
81
1i1zFz2B2uFzizi
F7F
7B3
2u6z14
2z8
F
1z6z

i4
Fz4iFz3zi
17zz
4Bu
zzzz

F

29
31
41
44
3
9
F
iizzzz
3zz
z
4
B
2u
Fzizz

FizzBuzz
zi
z2z
iB46u93z
izzB
4z87
zz
F



2i
uzzzzB
uzz
:rice:

Adbot
ADBOT LOVES YOU

hackbunny
Jul 22, 2007

I haven't been on SA for years but the person who gave me my previous av as a joke felt guilty for doing so and decided to get me a non-shitty av

b0lt posted:

C++ code:
                while (!go) {}

Ugggh gross, use a condition variable, you monster

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply