A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

[Programming Challenge] #1 - Print numbers

1.3k views · started by The Unintelligible ·
#1
[Programming Challenge] #1 - Print numbers
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][/1]strictly takes as input an even [2][/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][/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][/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
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.
#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
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


Sample cases:
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


Output:
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.
#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.
#4
Fixed .
#5
In that case:

Spoiler
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
#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.
#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.
#8
Artificial wrote:
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.


(but then again, it is a basic algorithm).


Heh, yeah.

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.
#9
rosewill wrote:
In python:
Spoiler
n = 4

if n % 2 == 0 and n >= 1:
for i in range(n):
print(i + 1)


While you did provide the correct functionality, you forgot to roll up that code in a procedure that takes user input or arguments. That's probably the most important part of the challenge.
#10
rosewill wrote:
That wasn't in the specifications, but if you actually care:
Spoiler
n = int(raw_input())

if n % 2 == 0 and n >= 1:
for i in range(n):
print(i + 1)


He did say define a procedure. A procedure can be thought of as a function which often doesn't return a value.
#11
rosewill wrote:
That wasn't in the specifications, but if you actually care:
Spoiler
n = int(raw_input())

if n % 2 == 0 and n >= 1:
for i in range(n):
print(i + 1)


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.