I
Ignoramus32515
<snip>
P.S., I don't think you want those 'const' thingies up there..
I did the same thing, rotflmao!
i
Best regards,
Spehro Pefhany
--
<snip>
P.S., I don't think you want those 'const' thingies up there..
Best regards,
Spehro Pefhany
the nice thing about it is that one does not have to supply
length, start or fin for initial invocation. Not that I think that
recursion is appropriate for this problem.
In your example, the user would need to supply strlen( l ) - 1 for
fin, for the initial call. And in my example, the user would only
supply str.
A bonus question: how to swap two integer (or char) values without
using a temporary variable? In any language? Without any function
calls?
i
On Thu, 22 Dec 2005 03:17:32 GMT, the renowned Ignoramus32515
Yes, default argument is a nice added feature of C++ ...
ummm... well, I think in reality I'd probably use some inline
assembler or something ;-) , but I think this is what you're looking
for:
int x, y;
x ^= y; y ^= x; x ^= y; // swap x and y
or, alternatively, you could use
y ^= x; x ^= y; y ^= x; // swap y and x
I don't think I've seen this before, but ex-or seemed the way to go.
Best regards,
Spehro Pefhany
Ignoramus32515 said:I did the same thing, rotflmao!
Nasty things, those non-const pointers.
when they point to const, yes. ;-)
Spehro Pefhany said:Isn't that kinda cheating?
I meant naked pointers in general.
Spehro Pefhany said:As opposed to auto_ptr? In the kind of embedded stuff I do, we
generally avoid dynamically allocated memory (and most reentrancy)
entirely.
I suppose you should also ding me for <string.h>.
It's a valid answer, and meaningful of itself. The follow up questions
would bring it back to where we started.
If you're not tired of the game yet, write:
// return true if val is an integer power of 2; false otherwise.
bool IsPowerOf2(unsigned val);
#include <string.h>
void reverse(char string[])
{
char c;
int i,j;
for (i=0, j=strlen(string)-1; i < j; i++,j--)
c = string, string = string[j], string[j]=c;
}
What does it pay? When do I start?
bool IsPowerOf2(unsigned val)
{
unsigned i=val, j=1;
while (i >>= 1) j*=2;
return (j == val);
}
I suppose you should also ding me for <string.h>.
It's a valid answer, and meaningful of itself. The follow up questions would
bring it back to where we started.
If you're not tired of the game yet, write:
// return true if val is an integer power of 2; false otherwise.
bool IsPowerOf2(unsigned val);
bool IsPowerOf2(unsigned val)
{
unsigned i=val, j=1;
while (i >>= 1) j*=2;
return (j == val);
}
Puckdropper said:Can I do it in Ada? I can do C++, but I'm not too fond of it. Note that
Cool!
Ada doesn't have an unsigned type, so I'm using an integer instead. The
code would be the same regardless.
/http://www.adahome.com/Ammo/cpp2ada.html
function IsPowerOf2(val: integer) return boolean is
result: integer;
begin
result := val rem 2;
if result = 0 then
return true;
else
return false;
end if;
end IsPowerOf2;
Ignoramus32515 said:here's my try, could be a little faster, untested
bool IsPowerOf2(unsigned int val) {
int count = 0;
while( val ) {
if( val & 0x1 ) {
count++;
if( count > 1 )
return false;
}
val = val >> 1;
}
return **** == 1;
}
#include <string.h>
void reverse(char string[])
{
char c;
int i,j;
for (i=0, j=strlen(string)-1; i < j; i++,j--)
c = string, string = string[j], string[j]=c;
}
What does it pay? When do I start?
great job!
I wish that people who we see could do as good job as you did.
i
Ada doesn't have an unsigned type, so I'm using an integer instead.
The code would be the same regardless.
I recall vaguely that Ada lets you define ranges? Something like type
uint32 is integer[0..4billionsumthing]? In any case, integer would be
less restrictive. Is this an advantage or disadvantage in the
application?
That returns true for even numbers. (I think. Is rem the modulus
operator? returning the remainder of division by 2?)
I was looking for
true if val is 2 raised to some integer power, 2^n, where n is a
positive integer. How would you write that in Ada?
Also, this seems a good time to discuss the read-mostly intent of
Ada's more verbose coding style, versus the (alleged) "write-only"
brevity of some other languages.
For example, I would likely write the
above function in C++ as a one liner:
bool IsEven(unsigned val)
{
return 0 == val % 2;
}
Bit-operation equivalence aside, what are your thoughts on the
readability of both versions, in context of both a large application
and just this one simple function in isolation?
#include <string.h>
void reverse(char string[])
{
char c;
int i,j;
for (i=0, j=strlen(string)-1; i < j; i++,j--)
c = string, string = string[j], string[j]=c;
}
What does it pay? When do I start?
great job!
I wish that people who we see could do as good job as you did.
i
Stolen and revised from "The C Programming Language" Second Edition,
Brian W. Kernighan & Dennis M. Ritchie, Prentice Hall Software Series,
Pg 63.
Naughty!
In the text, the c variable is an integer and string[] is s[]. They
can be forgiven, I think. The legacy of C is its occasional terseness.
Yes.
Every time I see interview questions like these I am amused. First,
because a solution to a question like these is as much a test of the
interviewer as the interviewee, since no one can spot plagiarism in
code very easily. Can code be plagiarized?
What happens when a programmer uses techniques or styles or
generalized solutions to the same problems from one project to the
next or from one job to the next?
I suppose the real test would be for the interviewee to be able to
come up with this code from memory. I know I couldn't do it anymore,
but I am an old fart and not a young whipper-snapper with a fresh
degree. I remembered it from reading the book but I had to dust it
off and crack it open to find it.
Solutions to programming problems ought to be the equivalent of
putting tinker toys together by now. It is, after all, the 21st
century and software design is almost 50 years old. Libraries exist
that can solve these problems far more reliably than reinvention every
time a project needs to be done. These questions only serve to break
the ice or simulate a development environment where the give and take
between the team members can advance the project or set it back. It's
up to the team members to determine those qualities that are desirable
in their candidates.
A truly valuable programmer is one who has a whole tool chest full of
source libraries that can be applied to projects with little or no
modification. The equivalent of LSI chips, if you will, connected to
the other functions (chips) with a little glue logic to create a
system.
Q. "Why are manhole covers round?"
A. "To keep them from falling in."
Bzzzt! "Because manholes are round."