Coding puzzle :D

SLH

Newbie
Joined
Sep 5, 2003
Messages
1,563
Reaction score
0
Code:
int i, n = 20;
for (i = 0; i < n; i--)
printf("-");
By adding or removing or replacing only
a single character from the code, make it print '-' 20 times.
There are 3 possible solutions, can you get them all?
 
for (i = 0; -i < n; i--)

or

for (i = 0; i < n; n--)

or

hmm .. I need to use my brain a bit more .. :sleep:
 
and so the idea of coding competitions entered the mind of onions! ta :D
 
Onions said:
and so the idea of coding competitions entered the mind of onions! ta :D
http://icpc.baylor.edu/icpc/ - ACM's international programming competition (done in teams of 3).
http://icpc.baylor.edu/Past/PastProblems.html - past problem sets for world finals.
http://www.acm.inf.ethz.ch/ProblemSetArchive.html - problem set archive.

SLH said:
By adding or removing or replacing only
a single character from the code, make it print '-' 20 times.
There are 3 possible solutions, can you get them all?
With the first 2 answeres from hasan, last one is :

for(i = 0; i + n; i--)
 
int i, n = 20;
for (i = 0; i < n; i--)
printf("-");;

int i, n = 20;;
for (i = 0; i < n; i--)
printf("-");

int i, n = 20;
for (i = 0; i < n; i--)
printf("-");
;

happy?
You should really word the problem better, cause I found the easiest way out.
 
also, since '\n' could be considered a single character, you could easily simply remove the line breaks in the code... yet another easy way around the problem...

you could also eliminate spaces..


programmers always find the easiest and most efficient way to solve a problem...
 
Synthos said:
int i, n = 20;
for (i = 0; i < n; i--)
printf("-");;

int i, n = 20;;
for (i = 0; i < n; i--)
printf("-");

int i, n = 20;
for (i = 0; i < n; i--)
printf("-");
;

happy?
You should really word the problem better, cause I found the easiest way out.
Errr, that doesn't work. Stop trying to be a smart-ass, do you really think the problem would be solved that easily, and that you'd be the one to get it first? :D
 
Synthos said:
programmers always find the easiest and most efficient way to solve a problem...
That's 100% correct, but you've not sloved the problem. :D
 
he just doesn't understand the question IMO.

P.S. Wow Akrin, I would've never thought of that .. gj
 
hasan said:
he just doesn't understand the question IMO.

P.S. Wow Akrin, I would've never thought of that .. gj
I guess he can answer for himself, but i can see what's not to understand.
I mean what's confusing about this:
"By adding or removing or replacing only
a single character from the code, make it print '-' 20 times."

It looks like he thought the code i gave already worked or something....
 
err
yah, I didn't actually read the whole question...
but still
if originally you had had the usual
int i, n = 20;
for (i = 0; i < n; i++)
printf("-");
and the same spiel, my solution would have technically worked...
I was just lazy i suppose.

hey wait, you said "print '-' 20 times"
but that doesn't mean you can't print it more than 20 times. You set no limiter in the question, therefore you are allowed to print it more than 20 times. If it print it 40 times, it still printed it 20 times, so it validates that section of the problem.
so
my solution would yet again technically work. So stop bashing me for pointing out the problems in the question -_-
 
Synthos said:
hey wait, you said "print '-' 20 times"
but that doesn't mean you can't print it more than 20 times. You set no limiter in the question, therefore you are allowed to print it more than 20 times. If it print it 40 times, it still printed it 20 times, so it validates that section of the problem.
so
my solution would yet again technically work. So stop bashing me for pointing out the problems in the question -_-
Ok, sorry i didn't see the fault in that question, if we're gonna post 'different' solutions, then i propose identical code (adding a null charactor) :rolleyes: :cheese:

I think you knew what i ment though (and i think the limiter was implicit)..... :)
 
Synthos said:
So stop bashing me for pointing out the problems in the question -_-
The question is ok, it's the instructions that are a bit dodgy. :D

p.s. i was bashing you for trying to be a smart-ass, not for pointing out problems in my instructions/question.
 
oh ok,
That's fine ;) My ass is very intelligent, I'm quite proud of it :p

and yes, i did know what the question ment, It had already been solved so I thought I'd post the technically correct solution for fun
 
Synthos said:
hey wait, you said "print '-' 20 times"
but that doesn't mean you can't print it more than 20 times. You set no limiter in the question, therefore you are allowed to print it more than 20 times. If it print it 40 times, it still printed it 20 times, so it validates that section of the problem.

If a recipe said to cook something for 20 minutes, would you treat that as being equal with cooking it for 4000 years (on par with what your solution does)?
 
if the instruction tells you to cook it for 20 minutes, and doesn't specifically say to take it out *at* 20 minutes, then yes if you so pleased..
 
just curious. i'm a bit sketchy with c/cpp. if i had a compiler i'd try it.

could
for (i = 0; i < n; i--1)
be interpreted as 'i minus negative 1'

i'm assuming that's a syntax error (without parenthesis, at least).
 
Never afraid to kick a dead horse (or whatever) I thought I could clarify the rules for Synthos a bit.


Please reference the attatched definintions page.

[size=+1]Instructions:[/size]

By adding or removing or replacing only
a single character from the code, make it print '-' 20 times.
There are 3 possible solutions, can you get them all?

[size=+1]Definitions:[/size]

adding -
To join or unite, as one thing to another, or as several particulars, so as to increase the number, augment the quantity, enlarge the magnitude, or so as to form into one aggregate.
removing
To cause to leave a thing; to cause to cease to be; to take away; to delete or abolish.
replacing
To take the place or move into the position of; to put in the place of another; or to switch seemingly equivalent items (as characters);
single
One only, as distinguished from more than one; consisting of one alone; individual; separate.
character
For the purposes of this document, and ASCII symbol as a valid representation of an alphanumeric constant, variable, or operator within the C / C++ programming language.
print
For the purposes of this document, to display to the monitor, printer, or output device.
-
For the purposes of this document, ASCII character as internally represented by the C / C++ programming language by hexidecimal number 0x2D.
20
For the purposes of this document, a number interpreted a radix 10, with zero in the first order position (ones), and two in the second order position (tens). Hereafter known as "twenty."
times
A product of singular events multiplied through a process of repetition.
possible solutions
For the purposes of this document, unique and independant products of creative endeavor pursuent to the goals established in the Instructions [sect.1; para.1; pg.1].
get
To procure; to obtain; to gain possession of; to acquire; to earn; to obtain as a price or reward; to come by; to win, by almost any means.
them
For the purposes of this document, aforementioned possible solutions.
all
The whole quantity, extent, duration, amount, quality, or degree of; the whole; the whole number of; any whatever; every.
 
Phisionary said:
just curious. i'm a bit sketchy with c/cpp. if i had a compiler i'd try it.

could
for (i = 0; i < n; i--1)
be interpreted as 'i minus negative 1'

i'm assuming that's a syntax error (without parenthesis, at least).

No it couldn't. The postdecerment operator takes precedence over the unary negation, predecrement, and subtraction operators. Meaning that i--1 would be interpreted as (i--)1 NOT i(--1) or i-(-1). This ofcoarse does lead to a syntax error, so your assumption is correct.

On the other hand i---1 IS valid. It would be interpreted as (i *postdecrement operator*) *subtraction operator* 1.
 
even if i--1 would be interpreted as (i) - (-1) it won't do a thing. but the expression would be evaluated to "true" I think.
ok well, I'm not sure .. I'm just trying to be a smart-ass nerd.
 
hasan said:
even if i--1 would be interpreted as (i) - (-1) it won't do a thing. but the expression would be evaluated to "true" I think.
ok well, I'm not sure .. I'm just trying to be a smart-ass nerd.
I would get incremented by 1 each time the loop ran, so had the syntax been like that, it'd be another solution.
 
hmmmm :/

If I'm not wrong .. (i) - (-1) just means (i + 1) which doesn't do anything. it's just an expression that would evaluate to "true" except when i == -1 is true.
 
hasan said:
hmmmm :/

If I'm not wrong .. (i) - (-1) just means (i + 1) which doesn't do anything. it's just an expression that would evaluate to "true" except when i == -1 is true.
Oops, yes, you're absolutly correct, i was having a funny 5 minutes. :rolleyes:
 
Neither, I just posted a busted "4th answer" and editted my post to hide my blunder.
 
hehe. sorry. an overreaction to harmless nitpicking. :dozey:

if it makes you feel better, it was a product of boredom mostly. 13 definitions for two sentances. i mean, really. don't i have anything better to do. :p *sigh*
 
Back
Top