Page 1 of 1
QUIZ: Write a script outputting a multiplication table. No e
Posted: Thu Dec 18, 2008 3:50 am
by FractalizeR
Hello.
One respected member of russian PHP forum
phpclub.ru DiMA posted a good PHP brain-quiz there. I really liked it so much (after I solved it myself, not before

) that I would like to share it with you here.
Write a script, that is printing a multiplication table (example below). The following limitations are implied:
- No PHP functions used at all (both built-in and extensions including shell calls etc.)
- No language constructs for outputting data used at all (like echo, print, var_dump, print_r etc (even inside variable functions). Consider they don't exist
- No <?= ?> tags used
- No backticks allowed (ex. `ls`).
- No HTML, no JavaScript. Consider script is run from console
- No file includes used (require, include, readfile etc.)
- Simple stupid output of the whole example at once is cosidered stupid and is not considered as solution

You can:
- Write your own functions if you like
- Use PHP operators and control structures you like
Actually, the basic purpose of this quiz is to show how can one output the result of calculations without using echo-like functions. One additional condition can be added if you like - to output a multiplication table of $_GET['x'] by values from $_GET['y'] to $_GET['z'].
Output example:
Code: Select all
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
3*2=6
3*3=9
3*4=12
3*5=15
3*6=18
3*7=21
3*8=24
3*9=27
4*2=8
4*3=12
4*4=16
4*5=20
4*6=24
4*7=28
4*8=32
4*9=36
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
6*2=12
6*3=18
6*4=24
6*5=30
6*6=36
6*7=42
6*8=48
6*9=54
7*2=14
7*3=21
7*4=28
7*5=35
7*6=42
7*7=49
7*8=56
7*9=63
8*2=16
8*3=24
8*4=32
8*5=40
8*6=48
8*7=56
8*8=64
8*9=72
9*2=18
9*3=27
9*4=36
9*5=45
9*6=54
9*7=63
9*8=72
9*9=81
As this forum does not have any HIDE or SPOILER tag, if you solved this puzzle,
please, don't post a solution here. Allow others to enjoy beating their brains out
If you just like to check, if you solved correctly, send me PM with a source.
Re: QUIZ: Write a script outputting a multiplication table. No e
Posted: Thu Dec 18, 2008 4:14 am
by Mark Baker
are we allowed to use shell, exec, etc?
Re: QUIZ: Write a script outputting a multiplication table. No e
Posted: Thu Dec 18, 2008 4:43 am
by FractalizeR
Mark Baker wrote:are we allowed to use shell, exec, etc?
No

They are functions of PHP anyway.
Re: QUIZ: Write a script outputting a multiplication table. No e
Posted: Thu Dec 18, 2008 5:42 am
by papa
Fun but tough one. I'm stuck atm.

Re: QUIZ: Write a script outputting a multiplication table. No e
Posted: Thu Dec 18, 2008 5:58 am
by FractalizeR
papa wrote:Fun but tough one. I'm stuck atm.

I solved it at the second day of thinking and searching through PHP manual (didn't find there anything though....)

Re: QUIZ: Write a script outputting a multiplication table. No e
Posted: Thu Dec 18, 2008 6:04 am
by papa
FractalizeR wrote:papa wrote:Fun but tough one. I'm stuck atm.

I solved it at the second day of thinking and searching through PHP manual (didn't find there anything though....)

Hehe yeah googling my ass of, it's that damn echo statement that ruins it.

Re: QUIZ: Write a script outputting a multiplication table. No e
Posted: Thu Dec 18, 2008 6:34 am
by Mark Baker
papa wrote:it's that damn echo statement that ruins it.

Being unable to use echo doesn't preclude creating a string that contains the text '<?php echo... ?>'.
It's what to do with that?
no PHP functions means no eval.
no include means you can't write a php file and then chain it via an include
Can't use bcompiler
Re: QUIZ: Write a script outputting a multiplication table. No e
Posted: Thu Dec 18, 2008 6:45 am
by papa
Well it's still an echo statement that ruins it.

Re: QUIZ: Write a script outputting a multiplication table. No e
Posted: Mon Dec 29, 2008 7:32 am
by FractalizeR
For those who are interested in solution - I posted mine into my blog:
http://phpquiz.wordpress.com
Re: QUIZ: Write a script outputting a multiplication table. No e
Posted: Mon Dec 29, 2008 9:10 am
by onion2k
I hate problems like this: "Try to write some exceptionally bad code...". No.
Plus, I would say that if switch() is allowed then echo should be too. They're both statements rather than functions. It even says on the manual page for echo "Because this is a language construct and not a function, it cannot be called using variable functions" (emphasis mine).
EDIT: Reading your solution again I've just noticed you're still using short tags. Epic fail.
Re: QUIZ: Write a script outputting a multiplication table. No e
Posted: Mon Dec 29, 2008 9:34 am
by FractalizeR
onion2k wrote:Plus, I would say that if switch() is allowed then echo should be too
Regarding echo:
FractalizeR wrote:No language constructs for outputting data used at all
So, echo is not enabled. "should" doesn't count here.
onion2k wrote:Reading your solution again I've just noticed you're still using short tags
Regarding short tags:
FractalizeR wrote:No <?= ?> tags used
I don't use <?= tag. Only <?. There is a difference.
onion2k wrote:I hate problems like this: "Try to write some exceptionally bad code...". No.
Regarding "bad code":
This wasn't an example of well-written code. I read "Code Complete" and I respect that book very much. But this is definitely not the case where it matters. It was a brain teaser, not a code styling example.
And, actually, I didn't press you to come here, to read this and to solve this. Please leave it to people, that like such things. Skip it if you don't.
Re: QUIZ: Write a script outputting a multiplication table. No e
Posted: Mon Dec 29, 2008 9:49 am
by onion2k
FractalizeR wrote:onion2k wrote:Reading your solution again I've just noticed you're still using short tags
Regarding short tags:
FractalizeR wrote:No <?= ?> tags used
I don't use <?= tag. Only <?. There is a difference.
<? is a short tag. <?= is shorthand. There's a difference. <? is depreciated (as is <?= ). It has been since PHP 4. Using it is bad practise. It's second only to suppressing errors with @ in my list of PHP sins.
FractalizeR wrote:onion2k wrote:I hate problems like this: "Try to write some exceptionally bad code...". No.
Regarding "bad code":
This wasn't an example of well-written code. I read "Code Complete" and I respect that book very much. But this is definitely not the case where it matters. It was a brain teaser, not a code styling example.
Everything you write should be an example of well-written code. There's no good reason to write anything else. Puzzles that force you to write bad code are stupid puzzles that, in the hands of newbie, will actually make you worse at coding. How is that a good thing?
FractalizeR wrote:And, actually, I didn't press you to come here, to read this and to solve this. Please leave it to people, that like such things. Skip it if you don't.
Are you trying to tell me that my input to this thread is invalid in some way?
Re: QUIZ: Write a script outputting a multiplication table. No e
Posted: Mon Dec 29, 2008 10:31 am
by FractalizeR
I am not going to tell a mod what is right and what is wrong on this forum. I just feel a little offended by your words. Why you try to punch me? What bad have I done to you? I hope this isn't because I am Russian? For some reason we are not in favour all over the world....
Regarding your comments:
There was nothing in start conditions about "short tags". There was a rule about <?= tag if you please scroll up there. If even there was something, all <? in the solution can be replaced by <?php. It will change nothing.
Puzzles that force you to write bad code are stupid puzzles that, in the hands of newbie, will actually make you worse at coding. How is that a good thing?
This is just your opinion. Nothing else. This quiz wasn't supposed for newbies if even you wasn't able to figure out the correct answer. Anyway, I am sorry if this quiz forced any newbie into writting bad code.
Re: QUIZ: Write a script outputting a multiplication table. No e
Posted: Mon Dec 29, 2008 10:53 am
by PM2008
A cute one. Here is my script (It is written in biterscripting, but you can use any other language of your choice.)
#START SCRIPT
var int i
while ($i <= 65) # Since your example shows only up to 65
do
echo $i "*" $i "=" ($i*$i)
set $i=$i+1
done
#END SCRIPT
What grade did I get ?
Patrick

Re: QUIZ: Write a script outputting a multiplication table. No e
Posted: Mon Dec 29, 2008 10:58 am
by onion2k
FractalizeR wrote:I am not going to tell a mod what is right and what is wrong on this forum. I just feel a little offended by your words. Why you try to punch me? What bad have I done to you? I hope this isn't because I am Russian? For some reason we are not in favour all over the world....
I don't give a damn where you're from. All I care about is the code posted (and linked to) here. If people post badly crafted code I'm going to say so regardless of whether or not it's part of the puzzle. I'm not "punching you", I'm pointing out a problem with what you posted. If you'd posted something I thought was good I'd be just as quick to compliment it... It's all about the code.
FractalizeR wrote:Regarding your comments:
There was nothing in start conditions about "short tags". There was a rule about <?= tag if you please scroll up there. If even there was something, all <? in the solution can be replaced by <?php. It will change nothing.
Short tags are a problem with all PHP scripts that use them. It's nothing to do with the rules of your quiz. They're old, depreciated, and make scripts that generate XML break (hence they were depreciated). Noone should be posting examples of code that use them.
FractalizeR wrote:Puzzles that force you to write bad code are stupid puzzles that, in the hands of newbie, will actually make you worse at coding. How is that a good thing?
This is just your opinion. Nothing else. This quiz wasn't supposed for newbies if even you wasn't able to figure out the correct answer. Anyway, I am sorry if this quiz forced any newbie into writting bad code.
Yep, it's just my opinion. I have been writing PHP code professionally for a little over 8 years, and developing websites for over 11 years though, so I reckon my opinion counts for something. Maybe not though.
You've nothing to apologise for mind you. We don't agree on something. That's fine. Good even. There'll be plenty of things people here don't agree about, especially when it comes to what's right and wrong in coding practises. It's good to discuss these things. Less experienced coders can read both sides of the argument and formulate their own opinions. Everyone's a winner!