|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
challenge: number tree
try to display the following output using conditional statements only. best code wins. the price? self-contentment. ^^
just pick one problem 1 12 123 1234 12345 (flowing tree) --------------- 1 112 1122 11223 112233 1122334 11223344 112233445 1122334455 (duplicate tree) --------------- x x0 x0x x0x0x (x0 series) --------------- x xx xx0 xx0x xx0xx xx0xx0 xx0xx00 xx0xx00x xx0xx00x0 xx0xx00x00 (x0 double series) |
|
#2
|
|||
|
|||
|
RE: challenge: number tree
problem #1 with recursion:
does recursion break any rules? if it does, i really don't see how this is possible, lol... http://www.idevlabs.com |
|
#3
|
|||
|
|||
|
RE: challenge: number tree
Flowing Tree: 3 different ways, but like instigator said, I don't see an efficient way to do this with a conditional statement. A loop works much better.
Code:
<?php
$col = 1;
$row = 1;
$value = 5;
// 1 conditional statement, but problem is values are hard coded...not very
// upgradeable
echo "1. Conditional Statement<br>";
if($row <= $value)
{
echo "1<br>12<br>123<br>1234<br>12345<br>";
}
echo "<br>";
$col = 1;
$row = 1;
// Same problem as above...to much work to add more rows.
echo "2. Many Conditional Statements<br>";
if($row <= $value)
{
if($row == 1)
{
echo "$col<br>";
$row++;
}
if($row == 2)
{
echo "$col".($col+1)."<br>";
$row++;
}
if($row == 3)
{
echo "$col".($col+1).($col+2)."<br>";
$row++;
}
if($row == 4)
{
echo "$col".($col+1).($col+2).($col+3)."<br>";
$row++;
}
if($row == 5)
{
echo "$col".($col+1).($col+2).($col+3).($col+4)."<br>";
$row++;
}
}
echo "<br>";
$col = 1;
$row = 1;
// Wouldn't a loop be a much simpler way to do this than with a conditional
// statement?
echo "3. Loop<br>";
while($row <= $value)
{
while($col <= $row)
{
echo "$col";
$col++;
}
$col = 1;
echo "<br>";
$row++;
}
?>
|
|
#4
|
|||
|
|||
|
RE: challenge: number tree
not being able to use loops is the challange, and it obviously shouldn't be hardcoded. you should be able to change one variable that represents the number of rows, and that alone changes the output.
i didn't use any loops in mine, but i'm not sure if we're allowed to use functions. this is really similar to a class assignment i had once, but i can't remember if recursion was how we did it. oh well, not like there's a prize anyway, i was just bored. http://www.idevlabs.com |
|
#5
|
|||
|
|||
|
RE: challenge: number tree
|
|
#6
|
|||
|
|||
|
RE: challenge: number tree
|
|
#7
|
|||
|
|||
|
RE: challenge: number tree
wow. thanks for the answer guys. i actually test your codes and it worked on my part. nice job.
so... anymore answers? |
|
#8
|
|||
|
|||
|
RE: challenge: number tree
|
|
#9
|
|||
|
|||
|
RE: challenge: number tree
|
|
#10
|
|||
|
|||
|
RE: challenge: number tree
|
|
#11
|
|||
|
|||
|
RE: RE: challenge: number tree
Quote:
Well actually I think I could make an argument that a WHILE loop IS technically a conditional similar to an IF statement. If Statement: If a condition is TRUE, then do something While Loop: If a condition TRUE, then do someting and come back and try again. If the condition is STILL TRUE, then do it again......... Just like a square is also a rectangle, but not all rectangles are squares. A loop is a conditional, but not all conditionals are loops. |
|
#12
|
|||
|
|||
|
lost backslashes
Forum conversion losts the backslashes in php snippets... (eg. "\n" => "n", "\r\n" => "rn")
__________________
JoE |
|
#13
|
|||
|
|||
|
Had to try the 'x0' series...only one param to start recursion
PHP Code:
|
![]() |
| Viewing: Codewalkers Forums > PHP Contests > Current Contest > challenge: number tree |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|