An interview test that i face


write the algorithm to draw a sphere (Circle) , the function should have paramters
	1. diameter (how many line)
	2. character (use which character to draw)
	
	eg, draw_sphere(10, '8') will produce:
	
			          88888
			      8888888888888	
			   8888888888888888888
			  888888888888888888888
			 88888888888888888888888
			 88888888888888888888888
			  888888888888888888888
			   8888888888888888888
			      8888888888888	
			          88888
				
	the above is a sphere of 10 lines, which is drawn using the character "8"

could anyone help on this? im newbie in php, thanks in advance…

EDIT: I tried but vBulletin messes with the tabbing from post to render. Hopefully you can guess what it is intended to look like :wink: Mittineague

could u help me on this? they know im newbie too:confused:

It may seem cruel, but really it’s the kindest cut.

Well, there’s two variables for each line: the whitespace and the 8.

How can you figure out how much whitespace there should be and how many characters to draw?

Start out with what you should know: it should be as wide as it is tall. The width and the height are the same as the diameter. The length of a chord is the product of a function: http://www.mathopenref.com/chord.html and the whitespace is related to the diameter and the chord length.

But you should be able to do this. This is a a problem that high-school sophomores solve.

+1!

I’ll second that.

I’ve been learning php for years… and even now I wouldn’t put myself forward for a php job despite a several people in the field suggesting my understanding of it is better than some of the pro’s.

You can’t just jump in as someone who knows very little and grab a job… can you? - If so where do I sign? :stuck_out_tongue:

That’s not a php problem but an algorithm one. So even if you’re not proficient with php, just resolve it with pseudo-code.

If you can not, you’ve got bigger problems than not knowing php.

Indeed.

And if they hire you because someone here helped you with the answer… are you going to post all your work here too? If so, what’s our cut? :stuck_out_tongue:

And how often in the real world are we faced with a problem such as drawing a sphere full of the digit “8”?

The original poster was asking this same question on other boards. The one where I found an explanation, if it is correct, is not as easy as some of you seem to think it is.

When I first got interested in computer programming, I talked to one of the programmers at the company I worked for. The most important tool a programmer can have, he said, is documentation. Nobody knows the answer to every problem. But someone with a brain and access to documentation can arrive at an answer (eventually). I’ll bet they don’t test for that.

I thought it would be as easy as using the COS function to determine how many spaces, and SIN to determine how many characters to write.

It’s not?

one thing that this has done is flag up a site that’s stealing our threads :mad:

In fact, even COS isn’t needed. The core of it could be as simple as:


var arc = TAU / 2 / size;
for (radians = arc / 2; radians < TAU / 2; radians += arc) {
    chars = Math.round(Math.sin(radians) * size);
    ...

That is JavaScript code of course, but the idea should be easily implementable.

Well, I spend a big chunk of my time trying to figure out how to programatically generate one thing from another, so I will go with the answer “almost every single day”.

Spam :rolleyes:

Then perhaps you would like to provide us a solution to the problem which we can verify is correct?