questions for programmers interview
category: code [glöplog]
Code:
<?php
$fizz = file_get_contents('http://www.pouet.net/topic.php?which=8881&page=6');
echo str_replace('"<br /> "', '', substr($fizz, strpos($fizz, 'puts(<br') + 23, 554));
@tomaes: It's under the WTFPL.
Tried to do something interesting with your version, but ended up with an unwieldy mess :P
I also updated fizzbuzz.c with 2 new versions, one based on yours.
I also updated fizzbuzz.c with 2 new versions, one based on yours.
Earx, the LINQ pretend-SQL is confusing. You can write the same in more standard C-pound syntax (which I prefer):
Code:
Enumerable.Range(1, 100).GroupJoin(
Enumerable.Range(1, 100).SelectMany(
j=>new[] {
new { k = j, v = j.ToString() },
new { k = j * 3, v = "Fizz"},
new { k = j * 5, v = "Buzz"},
new { k = j * 15, v = "FizzBuzz"},
}
), i=>i, e=>e.k, (i, l)=>l.First().v);
216 sees right through me. in fact, i started with roughly that :-)
#ponce, nice. what language is that?
#ponce, nice. what language is that?
skrebbel: Looks like D.
Here's an approach that I've not seen yet. :)
Code:
#define T .999
#define F float
char n[8] = {0};
char *s[] = { "fizz","buzz","fizzbuzz" };
for( i = 0; i++ < range; ){
itoa(i,n,10);
printf("%s:",cos((i/15.-(F)(i/15)))>T?s[2]:cos((i/5.-(F)(i/5)))>T?s[1]:cos((i/3.-(F)(i/3)))>T?*s:n);
}
Code:
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
void main()
{
char* words[] = { "Buzz", "Fizz", "Fizz", "FizzBuzz" };
for(int i = 1; i <= 100; i++)
{
int k = (int)floor((2*cos(i*M_PI/1.5) + cos(i*M_PI/2.5))+0.5);
if(k >= 0) printf("%s\n", words[k]);
else printf("%d\n", i);
}
}
wow. eh. great minds think alike, i guess :-)
skrebbel: Since timing. :D ("fizz" twice? Mhmm? :P)
Also, I'm done. Not gonna do a whitespace, piet or brainfuck version any time soon. :)
Also, I'm done. Not gonna do a whitespace, piet or brainfuck version any time soon. :)
Eh, Since=Nice. Brainfart.
I wonder whether the program would run faster if the modulo operations were replaced by something else, as modulo usually requires division and division is slow.
e.g.
e.g.
Code:
#include <stdio.h>
void main()
{
int i, c3, c5;
for (i = 1, c3 = 2, c5 = 4; i <= 100; i++, c3--, c5--)
{
if (!c5)
{
if (!c3)
{
printf ("Fizz");
c3 = 3;
}
printf ("Buzz\n");
c5 = 5;
}
else if (!c3)
{
printf ("Fizz\n");
c3 = 3;
}
else
printf ("%d\n", i);
}
}
Division/modulor by a constant can be optimized by a compiler which had some love behind it (like icc, gcc, vcc, clang). Also, modern desktop CPU are no 386 or Z80 :p
meh, 5 s. of googling and voila http://stackoverflow.com/questions/171301/whats-the-fastest-way-to-divide-an-integer-by-3
Oh, I've noticed BarZoule has already posted a version based on the same idea as mine.
Python 2.x, the idiomatic edition
Python 2.x, "WTF" edition
Quote:
for i in xrange(1, 101):
if i % 3 == 0:
s = ' fizz'
elif i % 5 == 0:
s = ' buzz'
else:
s = ''
print '%d%s' % (i, s)
Python 2.x, "WTF" edition
Quote:
print ''.join(['%d buzz\n' % (i+1) if (i+1) % 5 == 0 else '%s\n' % x for i, x in enumerate(['%d fizz' % x if x % 3 == 0 else x for x in xrange(1, 101)])])
Adok: Optimizing compilers turn modulo with constant values into long multiplies these days, and multiplies are just as fast as other ops. So no.
Quote:
int main(int argc, char** argv)
{
char i, j;
signed char buffer[9];
int fizz = 2054842694;
int buzz = 2054845762;
buffer[9] ^= buffer[9];
for (i = 1; i <= 100; i++)
{
buffer[4] = i;
do
{
sprintf(buffer, "%d", buffer[4]);
if (buffer[4] == i)
{
memcpy(buffer + 5, buffer, 3);
buffer[8] = 255;
}
buffer[4] = buffer[9];
for (j = 0; buffer[j] != 0; j++)
{
buffer[4] += buffer[j];
}
buffer[4] -= 3 * j << 4;
if (buffer[4] != i)
{
memcpy(buffer, buffer + 5, 3);
}
if(buffer[8] < 0) {
buffer[8] = j - 1;
}
}
while(buffer[4] >= 10);
switch(buffer[4])
{
case 0:
case 3:
case 6:
case 9:
memcpy(buffer, &fizz, 4);
buffer[8] += 5;
}
buffer[4] = buffer[9];
if (buffer[buffer[8]] == 53
|| buffer[buffer[8]] == 48
)
{
if (strlen(buffer) > 3) {
memcpy(buffer + 4, &buzz, 4);
} else {
memcpy(buffer, &buzz, 4);
buffer[4] = buffer[9];
}
}
puts(buffer);
}
return 0;
}
Guys, please use the [code] tag, not the [quote] one. Quote will eat leading tabs and spaces and you miss out on the monospaced font too.
Actually I think the combination of using proportional fonts and shunning indentation - both of which are not unheard of - suit these programs nicely.
@Optimus: your problem is communication.
1/ your sentences are too long and too complex.
Few people will take the time to understand them as we are in a rat race world.
2/ you don't use paragraphs either.
They are required to structure your thoughts correctly.
3/ If I were you, I would:
a. stop using the 'dirty minds' group name: it makes your mind dirty and so your thoughts
b. spend a lot of time using alternative OS: Linux, Mac OS X, Open Solaris
Use them, to get used to something different than Windows and its mess of interface.
Read their respective documentation.
Read well written books: short books and apply what they say so it'll not stay theory.
What you read and how it is structured, has an impact on your thinking.
Practicing clear your thoughts of doubts: books are full of mistakes,
by practicing your find them and go past them.
c. watch carefully what you eat: mens sana in corpore sano.
Read this and you can find it in Greek of course:
http://www.greekmedicine.net/whos_who/Hippocrates.html
d. challenge yourself to walk 1 hour a day at least.
Walking is the exercise which move the most blood inside your body
and
blood brings nutrients to your cells.
e. Then you'll get confident and will then be able to talk without stress.
If you do all this, and continue doing it, I guarantee you, you'll be soon very confident and will get a job.
1/ your sentences are too long and too complex.
Few people will take the time to understand them as we are in a rat race world.
2/ you don't use paragraphs either.
They are required to structure your thoughts correctly.
3/ If I were you, I would:
a. stop using the 'dirty minds' group name: it makes your mind dirty and so your thoughts
b. spend a lot of time using alternative OS: Linux, Mac OS X, Open Solaris
Use them, to get used to something different than Windows and its mess of interface.
Read their respective documentation.
Read well written books: short books and apply what they say so it'll not stay theory.
What you read and how it is structured, has an impact on your thinking.
Practicing clear your thoughts of doubts: books are full of mistakes,
by practicing your find them and go past them.
c. watch carefully what you eat: mens sana in corpore sano.
Read this and you can find it in Greek of course:
http://www.greekmedicine.net/whos_who/Hippocrates.html
d. challenge yourself to walk 1 hour a day at least.
Walking is the exercise which move the most blood inside your body
and
blood brings nutrients to your cells.
e. Then you'll get confident and will then be able to talk without stress.
If you do all this, and continue doing it, I guarantee you, you'll be soon very confident and will get a job.
My try the Python way :
Code:
print list(map((lambda x: "fizz" if x%3==0 else "buzz" if x%5==0 else "fizzbuzz" if x%15==0 else x),range(1,100)))
As usual, this question is better solved with HQ9F+ : http://cfs.maxn.jp/neta/HQ9F+.en.html
Alter-native: context?
Alter-native = EP?