Out of boredom I've decided to timely post programming challenges/puzzles, probably mainly minimalist, to promote programming activity in LG.
There's already a few posted here by Artificial, but I've yet to allocate the time to have a crack at them. I plan on eventually trying my hand at it, though.
Alright, here's the challenge criteria:
-Define a procedure that [1]strictly takes as input an even [2]natural number (n) and from 1 prints all numbers that n consists of. (e.g. Supplying 4 in the procedure should sequentially print all numbers from 1 to 4.)
Subjective difficulty: Extremely easy
Post your code in this thread (preferably enclosed in [spoiler] tags by convention).
No deadline. Feel free to do this anytime.
Any programming language of your choice can be used.
I implore anyone to try to solve these given problems whenever you can.
Now, rev up those engines!
Superscript explanations:
[1]By strictly I mean submissions have to follow every specification made. The main processing has to begin with an even natural number. If the input is not an even natural number, you have to be the one to programmatically determine such and act on that.
[2]A natural number by definition is 1 or any repetition of it (¹ - ∞). That means pretty much any number above 1.
My solution in Python:
Spoiler:PHP Code:
def print_numbers(n):
if n > 0:
if not int(n) % 2 == 0: return
i = 1
for i in range(i, n + 1): print(i)
Note: Plagiarism is not tolerated. I also do not recommend looking at the code in the spoiler (or at least until you're sure you've completed the challenge), as it could contain all kinds of hidden factories (including an invalid solution and/or watermarks). The purpose of a challenge is for an intellectually stimulating challenge, not a false sense of self-gratification.
Edit: This is sort of the wrong section, so if a staff member would be so kind as to move it to the appropriate section, it would be greatly appreciated.
Results 1 to 11 of 11
- 27 Jul. 2012 07:24pm #1
[Programming Challenge] #1 - Print numbers
Last edited by The Unintelligible; 28 Jul. 2012 at 03:35am.
- 28 Jul. 2012 02:40am #2
Without looking at your solution, I wasn't sure how you wanted to deal with negative numbers. Strictly speaking, they fit your specification. I worked under the assumption that if you pass through a negative integer, rather than work from 1 up to the integer, it simply works backwards.
Spoiler:PHP Code:def number_consists_of(num):
if isinstance(num, int) == False or num % 2 != 0:
raise Exception("You must pass through an even whole number.")
start = 1 if num >= 1 else num
end = num + 1 if start == 1 else 2
for x in range(start, end):
print x if start == 1 else num - x + 1
PHP Code:try:
number_consists_of(4)
print "\n"
number_consists_of(-2)
print "\n"
number_consists_of(-10)
print "\n"
number_consists_of(3)
except Exception as err:
print "Unexpected error:", err
pass
try:
print "\n"
number_consists_of("pie")
except Exception as err:
print "Unexpected error:", err
pass
PHP Code:1
2
3
4
1
0
-1
-2
1
0
-1
-2
-3
-4
-5
-6
-7
-8
-9
-10
Unexpected error: You must pass through an even whole number.
Unexpected error: You must pass through an even whole number.
Also moved to the correct section.
- 28 Jul. 2012 03:06am #3
Yeah, I don't think I made it too clear. The challenge was taken from some other source. You aren't supposed to process anything below 1 at all. Your solution meets the default criteria, but it also goes beyond that. It's also done far more properly than mine was.
I hastily put mine together and I don't really use Python primarily. I just used it because at the time it was one of the few languages I had access to.
Nice job.
Edit: I also just realized I was using the term "whole number" ambiguously. I took the challenge from another site, it passed all test cases, so I thought I'd re-share it here. The term I was looking for was natural number or counting number.
I apologize for that, I'll revise the original post immediately.Last edited by The Unintelligible; 28 Jul. 2012 at 03:23am.
- 28 Jul. 2012 03:22am #4
Fixed .
- 28 Jul. 2012 03:24am #5
In that case:
Spoiler:PHP Code:def number_consists_of(num):
if isinstance(num, int) == False or num < 1 or num % 2 != 0:
raise Exception("You must pass through an even whole number.")
for x in range(1, num + 1):
print x
- 28 Jul. 2012 03:26am #6
Looks pretty similar to mine aside from isinstance being used for type-checking (but then again, it is a basic algorithm). That and the side-effect of the function being a raised exception instead of a halted procedure.
I still like yours better.
- 28 Jul. 2012 03:39am #7
They're all going to look similar. :p There's only two elements to it, check the integrity of the data passed through and then loop from 1 to n + 1.
As for raising the exception, you did say procedure. If you'd asked for a function I would have returned null instead.
- 28 Jul. 2012 03:46am #8(but then again, it is a basic algorithm).
And @ raising the exception: pretty much. Unambiguously speaking, when dealing with procedures you generally don't return any data as that's the purpose of a standard function (I learned this from Pascal some years ago).
Instead, you would invoke some type of side effect (whether that be a dialog, exception, or any other means). I thought the easiest way would be to end the procedure with return signifying that it wasn't valid data passed, if that qualifies.
- 29 Jul. 2012 05:07pm #9
- 29 Jul. 2012 11:36pm #10
- 30 Jul. 2012 12:48am #11
It's not that I particularly care, it's the fact that it's a specification or technicality of the challenge. You provided the correct functionality, yes. But sometimes being just correct doesn't always cut it.
If I used terminology that might have confused you, I apologize. But you have to get that kind of vocabulary down if you plan on expanding your programming skill further.