Current Contest
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP ContestsCurrent Contest

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old May 27th, 2006, 02:08 AM
rllqph rllqph is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 2 rllqph User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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)

Reply With Quote
  #2  
Old May 31st, 2006, 02:05 AM
instigator instigator is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Maryland
Posts: 1,158 instigator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 20 m 13 sec
Reputation Power: 4
RE: challenge: number tree

problem #1 with recursion:
php Code:
Original - php Code
  1. <?php
  2.  
  3. row(1, 1, 5);
  4.  
  5. function row($row, $column, $max_rows){
  6.     if($row <= $max_rows){
  7.         column($row, $column, $max_rows);
  8.     }
  9. }
  10.  
  11. function column($row, $column, $max_rows){
  12.     if($column <= $row){
  13.         echo $column;
  14.         column($row, ++$column, $max_rows);
  15.     }
  16.     else{
  17.         echo "<br>";
  18.         row(++$row, 1, $max_rows);
  19.     }
  20. }
  21.  
  22. ?>

does recursion break any rules? if it does, i really don't see how this is possible, lol...



http://www.idevlabs.com

Reply With Quote
  #3  
Old May 31st, 2006, 08:54 AM
jerryrika jerryrika is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 1 jerryrika User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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++;
    }
?>

Reply With Quote
  #4  
Old May 31st, 2006, 10:53 AM
instigator instigator is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Maryland
Posts: 1,158 instigator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 20 m 13 sec
Reputation Power: 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

Reply With Quote
  #5  
Old July 12th, 2006, 08:36 PM
dschreck dschreck is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 44 dschreck User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 53 sec
Reputation Power: 3
RE: challenge: number tree

5 lines, (6 if you count actually starting the damn thing).

php Code:
Original - php Code
  1.  
  2. <?php
  3. function start($i = 1,$r=20,$inc=1,$old='') {
  4.         $old .= "&nbsp;".$i;
  5.         echo $old,"<br />";
  6.         $i+$inc <= $r ? start($i+$inc,$r,$inc,$old) : die("<br />done.");
  7. }
  8. start(1,20,1);
  9. ?>


It's a recursion excersice. Just like the infamous "Towers of Hanoi" puzzle, only way to do it is with recursion.

Reply With Quote
  #6  
Old July 12th, 2006, 10:23 PM
dschreck dschreck is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 44 dschreck User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 53 sec
Reputation Power: 3
RE: challenge: number tree

or maybe....

php Code:
Original - php Code
  1.  
  2. <?php
  3. function start($i = 1,$r=20,$inc=1,$old='') {
  4.         echo ($old .= "&nbsp;".$i) ? $old."<br />" : '';
  5.         $i+$inc <= $r ? start($i+$inc,$r,$inc,$old) : die("<br />done.");
  6. }
  7. start(1,20,1);
  8. ?>


Reply With Quote
  #7  
Old August 16th, 2006, 06:29 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 25
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?

Reply With Quote
  #8  
Old September 29th, 2006, 08:58 PM
[JoE] [JoE] is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Szeged, Hungary
Posts: 12 [JoE] User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 9 m 7 sec
Reputation Power: 0
RE: challenge: number tree

#1 hardcore version

php Code:
Original - php Code
  1.  
  2. <?
  3. function tree($s,$q) {
  4.   echo ($s .= strlen($s));
  5.   if (--$q) tree($s,$q);
  6. }
  7. tree("n",5);
  8. ?>


Reply With Quote
  #9  
Old September 29th, 2006, 09:23 PM
[JoE] [JoE] is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Szeged, Hungary
Posts: 12 [JoE] User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 9 m 7 sec
Reputation Power: 0
RE: challenge: number tree

problem #3 solution

php Code:
Original - php Code
  1.  
  2. <?
  3. function tree($q,$s) {
  4.   $p = '0x';
  5.   echo ($s .= $p[$q % 2]);
  6.   if (--$q) tree($q,$s);
  7. }
  8. tree(5,"n");
  9. ?>


Reply With Quote
  #10  
Old September 29th, 2006, 09:33 PM
[JoE] [JoE] is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Szeged, Hungary
Posts: 12 [JoE] User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 9 m 7 sec
Reputation Power: 0
RE: challenge: number tree

problem #2

php Code:
Original - php Code
  1.  
  2. <?
  3. function tree($s,$q) {
  4.   echo ($s .= (int)(strlen($s) / 2));
  5.   if (--$q) tree($s,$q);
  6. }
  7. tree("rn",5 * 2);
  8. ?>


Reply With Quote
  #11  
Old October 31st, 2006, 03:29 AM
doni49 doni49 is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 30 doni49 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
RE: RE: challenge: number tree

Quote:
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.

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.

Reply With Quote
  #12  
Old April 23rd, 2007, 02:24 PM
[JoE] [JoE] is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Szeged, Hungary
Posts: 12 [JoE] User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 9 m 7 sec
Reputation Power: 0
lost backslashes

Forum conversion losts the backslashes in php snippets... (eg. "\n" => "n", "\r\n" => "rn")
__________________
JoE

Reply With Quote
  #13  
Old June 13th, 2007, 09:32 PM
dubhunter dubhunter is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 1 dubhunter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 32 sec
Reputation Power: 0
Had to try the 'x0' series...only one param to start recursion

PHP Code:
<?
function tree($rows$col 1$row 1)
{
    echo 
$col == "x" "0";
    if (
$col $row)
    {
        
tree($rows$col 1$row);
    }
    else
    {
        echo 
"<br />";
        if (
$rows == $row) return;
        
tree($rows1$row 1);
    }
}

tree(6);
?>

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP ContestsCurrent Contest > challenge: number tree


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek