webdevRefinery Forum: Five minutes to develop an algrathim - webdevRefinery Forum

Jump to content

  • 2 Pages +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Rate Topic: ****- 1 Votes

User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 08 September 2011 - 04:32 PM (#1)

Five minutes to develop an algrathim


So this was supposed to be a challenge, right off the bat I knew what to do. So first off, don't click the link below if you want to try it, since it has my code (I'll know if you looked at it, trust me, it's obvious, just say you looked at it for help and don't claim that you did it all on your own).

So the goal? Output this in an algorithmic(mathematical) function:
Posted Image

My code: http://i.imgur.com/ixNcJ.png

So my question to you guys, was it challenging? Should I be proud that I'm the only one that got it or sad that no one else did?
Reserved.
0


User is online Lemon 

  • I have a dream...
  • Group: Members
  • Posts: 687
  • Joined: 24-February 11
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 08 September 2011 - 05:35 PM (#2)

Took a bit longer than 5 mins as Codepad died halfway through with 500 errors:

<?php

$width = 9;
$height = ceil($width / 2) - 2;

echo str_repeat('*', $width);
echo PHP_EOL;

while ($height >= - (ceil($width / 2) - 2)) {
    echo '*';
    echo str_repeat('*', abs($height));
    echo str_repeat(' ', ($width - 2) - 2 * abs($height));
    echo str_repeat('*', abs($height));
    echo '*';
    echo PHP_EOL;
    $height--;
}

echo str_repeat('*', $width);
echo PHP_EOL;


Also scales nicely to larger sizes :

**************************************************
************************  ************************
***********************    ***********************
**********************      **********************
*********************        *********************
********************          ********************
*******************            *******************
******************              ******************
*****************                *****************
****************                  ****************
***************                    ***************
**************                      **************
*************                        *************
************                          ************
***********                            ***********
**********                              **********
*********                                *********
********                                  ********
*******                                    *******
******                                      ******
*****                                        *****
****                                          ****
***                                            ***
**                                              **
*                                                *
**                                              **
***                                            ***
****                                          ****
*****                                        *****
******                                      ******
*******                                    *******
********                                  ********
*********                                *********
**********                              **********
***********                            ***********
************                          ************
*************                        *************
**************                      **************
***************                    ***************
****************                  ****************
*****************                *****************
******************              ******************
*******************            *******************
********************          ********************
*********************        *********************
**********************      **********************
***********************    ***********************
************************  ************************
**************************************************

Posted Image
In the end, it's not the years in your life that count. It's the life in your years
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 08 September 2011 - 05:37 PM (#3)

View PostLemon, on 08 September 2011 - 05:35 PM, said:


Also scales nicely to larger sizes :

**************************************************
************************  ************************
***********************    ***********************
**********************      **********************
*********************        *********************
********************          ********************
*******************            *******************
******************              ******************
*****************                *****************
****************                  ****************
***************                    ***************
**************                      **************
*************                        *************
************                          ************
***********                            ***********
**********                              **********
*********                                *********
********                                  ********
*******                                    *******
******                                      ******
*****                                        *****
****                                          ****
***                                            ***
**                                              **
*                                                *
**                                              **
***                                            ***
****                                          ****
*****                                        *****
******                                      ******
*******                                    *******
********                                  ********
*********                                *********
**********                              **********
***********                            ***********
************                          ************
*************                        *************
**************                      **************
***************                    ***************
****************                  ****************
*****************                *****************
******************              ******************
*******************            *******************
********************          ********************
*********************        *********************
**********************      **********************
***********************    ***********************
************************  ************************
**************************************************



you have 50 stars at the top and bottom, that is an even number. I have nine, an odd number.
Reserved.
0


User is offline Koen 

  • Leroy Jenkins
  • Group: Members
  • Posts: 2503
  • Joined: 10-March 10
  • Locationthe Netherlands
  • Expertise:HTML,CSS,Javascript,Graphics

Posted 08 September 2011 - 05:43 PM (#4)

Made on my iPod, 1AM.
Spoiler

Please click the + if I helped you!
Twitter: @KoenKlaren

<callumacrae> YOU DID A ROMNEY
0


User is online Lemon 

  • I have a dream...
  • Group: Members
  • Posts: 687
  • Joined: 24-February 11
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 08 September 2011 - 06:12 PM (#5)

View PostThatRailsGuy, on 08 September 2011 - 05:37 PM, said:

you have 50 stars at the top and bottom, that is an even number. I have nine, an odd number.

Set $width to 9 and it gives you your pattern, I was just demonstrating that it works for larger sizes too.
Posted Image
In the end, it's not the years in your life that count. It's the life in your years
0


User is offline Hyde 

  • Group: Members
  • Posts: 1562
  • Joined: 08-March 10

Posted 08 September 2011 - 06:31 PM (#6)

TheEmpty : 29 lines
Lemon : 18 lines
Hyde | HTML & CSS | PHP & SQL | Objective-C | Java | Basic JavaScript
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 08 September 2011 - 06:43 PM (#7)

View PostHyde, on 08 September 2011 - 06:31 PM, said:

TheEmpty : 29 lines
Lemon : 18 lines

Well over 29 lines actually and I would have more if I had the time ;)

/me points at 100 line hello world for project 1.

Also note that lemon's is not OO.

View PostLemon, on 08 September 2011 - 06:12 PM, said:

Set $width to 9 and it gives you your pattern, I was just demonstrating that it works for larger sizes too.

Ah, I don't have PHP installed so I couldn't test it. Nice though :)

View PostKoen, on 08 September 2011 - 05:43 PM, said:

Made on my iPod, 1AM.
Spoiler


You can't use conditionals in this alg (it's to be enforcive)
Reserved.
0


User is offline Hyde 

  • Group: Members
  • Posts: 1562
  • Joined: 08-March 10

Posted 08 September 2011 - 06:43 PM (#8)

View PostThatRailsGuy, on 08 September 2011 - 06:34 PM, said:

Well over 29 lines actually and I would have more if I had the time ;)

/me points at 100 line hello world for project 1.

Also note that lemon's is not OO.

OOPing Lemon's code would add a lot of lines. It would also get rid of some.
Hyde | HTML & CSS | PHP & SQL | Objective-C | Java | Basic JavaScript
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 08 September 2011 - 06:50 PM (#9)

View PostHyde, on 08 September 2011 - 06:43 PM, said:

OOPing Lemon's code would add a lot of lines. It would also get rid of some.

also don't forget he doesn't have to declare variables and other items. (main function, imports, docs, etc.) It could only get rid of a few lines, he would gain much more. But the beauty is that it's OO and OO is really nice for mat. and testing.

edit: my if statement is because it will print out "null" if I don't handle that, so it's because I don't loop over every element and set it to " "
Reserved.
0


User is offline Daniel15 

  • dan.cx
  • Group: Moderators
  • Posts: 3415
  • Joined: 17-April 10
  • LocationMelbourne, Australia
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 08 September 2011 - 07:48 PM (#10)

Quote

TheEmpty : 29 lines
Lemon : 18 lines

Characters are a better measure than lines. Otherwise you could put the whole script on a single line and say it's only one line of code :P

Quote

Also note that lemon's is not OO.

Static methods aren't really OO either. An OO design would have a "Diamond" object with a width property (or similar), used something like

Diamond test = new Diamond();
test.setWidth(9);
test.render();


:P
Daniel15! :D
Posted Image

Repeat after me: jQuery is not JavaScript. It is not the answer to every JavaScript-related question. When you have to write some JavaScript, do not instantly react with "Oh, I'll do that with jQuery!"

Spoiler
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 08 September 2011 - 08:18 PM (#11)

View PostDaniel15, on 08 September 2011 - 07:48 PM, said:

Static methods aren't really OO either. An OO design would have a "Diamond" object with a width property (or similar), used something like

Diamond test = new Diamond();
test.setWidth(9);
test.render();


:P

True. I like how his scales :( But actually, it's basiclly the same thing, just that it's static :P
Reserved.
0


User is offline Hyde 

  • Group: Members
  • Posts: 1562
  • Joined: 08-March 10

Posted 08 September 2011 - 09:50 PM (#12)

View PostDaniel15, on 08 September 2011 - 07:48 PM, said:

Characters are a better measure than lines. Otherwise you could put the whole script on a single line and say it's only one line of code :P

I know I know, it was just a quick comparison. :)
Hyde | HTML & CSS | PHP & SQL | Objective-C | Java | Basic JavaScript
0


User is offline gibbonweb 

  • 兄ヨハネス
  • Group: Members
  • Posts: 2062
  • Joined: 23-June 10
  • LocationMunich(DE)
  • Expertise:HTML,CSS,PHP,Javascript,Python,SQL,Graphics

Posted 09 September 2011 - 04:02 AM (#13)

Proud to say that mine's as short as lemon's, but more beautiful.

http://codepad.org/lgkUfDyn
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 09 September 2011 - 06:54 AM (#14)

View Postgibbonweb, on 09 September 2011 - 04:02 AM, said:

Proud to say that mine's as short as lemon's, but more beautiful.

http://codepad.org/lgkUfDyn

indeed, very nice :) how long (to program it)?
Reserved.
0


User is offline gibbonweb 

  • 兄ヨハネス
  • Group: Members
  • Posts: 2062
  • Joined: 23-June 10
  • LocationMunich(DE)
  • Expertise:HTML,CSS,PHP,Javascript,Python,SQL,Graphics

Posted 09 September 2011 - 07:32 AM (#15)

View PostThatRailsGuy, on 09 September 2011 - 06:54 AM, said:

indeed, very nice :) how long (to program it)?

Well, I read the thread and then had to leave the computer because of an appointment...
I had the chance to think about it longer than 5 minutes, I admit - but once I thought about the fact that what you're asking is basically the unit circle of the 1-norm, programming was a matter of 1-2minutes. The test case et cetera not counted in, of course, that's just there for convenience ;)

...am I hired now? :D
1


User is offline alexdavey 

  • Bringing XKCD to a post near you
  • Group: Members
  • Posts: 848
  • Joined: 05-November 10
  • LocationLondon
  • Expertise:HTML,CSS,PHP,Javascript

Posted 09 September 2011 - 04:27 PM (#16)

9 lines, am I cheating?

function Diamond(n) {
	for (var i = 2 + -n++; i < (n + 2); i += 2) {
		var spaces = new Array(n - Math.abs(i - 2)).join(' '),
			stars = new Array(n - spaces.length + 2).join('*'),
			half = stars.length / 2;
		string = stars.substring(0, half) + spaces + stars.substring(half);
		console.log(string);
	} 
}

new Diamond(10);


Posted Image
0


User is online Lemon 

  • I have a dream...
  • Group: Members
  • Posts: 687
  • Joined: 24-February 11
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 09 September 2011 - 04:33 PM (#17)

One line, do I win? :rolleyes:

<?="*********\n**** ****\n***   ***\n**     **\n*       *\n**     **\n***   ***\n**** ****\n*********";

Posted Image
In the end, it's not the years in your life that count. It's the life in your years
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 09 September 2011 - 07:29 PM (#18)

View Postalexdavey, on 09 September 2011 - 04:27 PM, said:

9 lines, am I cheating?

nah, no if's all math

View PostLemon, on 09 September 2011 - 04:33 PM, said:

One line, do I win? :rolleyes:

<?="*********\n**** ****\n***   ***\n**     **\n*       *\n**     **\n***   ***\n**** ****\n*********";


no math!
Reserved.
0


User is online Lemon 

  • I have a dream...
  • Group: Members
  • Posts: 687
  • Joined: 24-February 11
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 09 September 2011 - 07:48 PM (#19)

View PostThatRailsGuy, on 09 September 2011 - 07:29 PM, said:

no math!

<?php $piAp = 22/7; echo "*********\n**** ****\n***   ***\n**     **\n*       *\n**     **\n***   ***\n**** ****\n*********";
:D
Posted Image
In the end, it's not the years in your life that count. It's the life in your years
0


User is offline gibbonweb 

  • 兄ヨハネス
  • Group: Members
  • Posts: 2062
  • Joined: 23-June 10
  • LocationMunich(DE)
  • Expertise:HTML,CSS,PHP,Javascript,Python,SQL,Graphics

Posted 10 September 2011 - 08:18 AM (#20)

View PostLemon, on 09 September 2011 - 07:48 PM, said:

<?php $piAp = 22/7; echo "*********\n**** ****\n***   ***\n**     **\n*       *\n**     **\n***   ***\n**** ****\n*********";
:D

doesn't scale, you suck. :D

I have a scalable less-than-8 line code in my head, but I'll have to do that later. workwork...
0


Share this topic:


  • 2 Pages +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services