Maker Pro
Maker Pro

Computer programmers' habits in electronics

S

Spehro Pefhany

On Thu, 22 Dec 2005 03:17:32 GMT, the renowned Ignoramus32515

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.

Yes, default argument is a nice added feature of C++ ...
A bonus question: how to swap two integer (or char) values without
using a temporary variable? In any language? Without any function
calls?

i

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
 
I

Ignoramus32515

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.

A perfect answer! You get an A+!

i
Best regards,
Spehro Pefhany


--
 
M

Mike Young

Spehro Pefhany said:
Isn't that kinda cheating?

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);
 
S

Spehro Pefhany

I meant naked pointers in general.

As opposed to auto_ptr? In the kind of embedded stuff I do, we
generally avoid dynamically allocated memory (and most reentrancy)
entirely.


Best regards,
Spehro Pefhany
 
M

Mike Young

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.

Gak! :) Yeah, different context.
 
G

Geoff

#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?
 
P

Puckdropper

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);

Can I do it in Ada? I can do C++, but I'm not too fond of it. Note that
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;

Puckdropper
--
www.uncreativelabs.net

Old computers are getting to be a lost art. Here at Uncreative Labs, we
still enjoy using the old computers. Sometimes we want to see how far a
particular system can go, other times we use a stock system to remind
ourselves of what we once had.

To email me directly, send a message to puckdropper (at) fastmail.fm
 
I

Ignoramus32515

#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
 
I

Ignoramus32515

bool IsPowerOf2(unsigned val)
{
unsigned i=val, j=1;
while (i >>= 1) j*=2;
return (j == val);
}

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;
}
 
S

Spehro Pefhany

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);
}



Best regards,
Spehro Pefhany
 
J

John Miles

bool IsPowerOf2(unsigned val)
{
unsigned i=val, j=1;
while (i >>= 1) j*=2;
return (j == val);
}

Man, you young'uns must think CPU cycles are free or something.

bool IsPowerOf2(unsigned val)
{
return !(val & (val-1));
}

-- jm
 
M

Mike Young

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.

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?
/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;

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?
 
M

Mike Young

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;
}

Misspelling on a whiteboard can be overlooked without comment. Some are just
more unfortunate than others.

Hand optimization isn't too very important. What is important is
correctness. Does it return true for all and only integer powers of 2? Does
the loop terminate? One boundary condition to test is val = 0. (Is that a
power of 2?) What changes would you make if val is changed to int rather
than unsigned?

Also in context of optimization, what if I then mumbled something about IEEE
floating point representation? Assuming the target system is relatively
efficient with floating point values, say a modern Intel box, what thoughts
come to mind?
 
G

Geoff

#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.

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.

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."
 
P

Puckdropper

*snip*
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?

It's both... It's a major advantage in that I don't have to mess with
anything extra, it's also a disadvantage in that what happens with
negative results (say from an integer overflow) is undefined. I should
have used "Natural" instead.

You can define other types, but that caused trouble with some of the type
casting I had to do. (My solution involved using a log which Ada only
seems to have defined as float.)

Anyway, to emulate a unsigned int on my machine at least, you'd do:
type unsigned is new long_long_integer range 0..4294967295;



*snip code sement *
That returns true for even numbers. (I think. Is rem the modulus
operator? returning the remainder of division by 2?)

Not exactly... rem and mod are equal for positive numbers, but different
for negative values. (Close enough in this case.)
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?

My mistake. I misunderstood the question. I get some mathematic terms
messed up at times. Here's the code that does as you wish:

function IsPowerOf2(val: integer) return boolean is
subtotal: float;
result: integer;
result2: integer;
begin
subtotal := log(float(val), 2.0);
result := integer(subtotal);
result2 := 2**result;
return val = result2;
end IsPowerOf2;

Basically what I'm doing is taking the log of a number, raising it to the
base again and comparing results. If there's no floating point part,
then the numbers should be equal.
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.

I like "write-fast" rather than "write-only." You can make C-based code
as readable as read-mostly languages, it just takes care.
For example, I would likely write the
above function in C++ as a one liner:

bool IsEven(unsigned val)
{
return 0 == val % 2;
}

function IsEven(val: natural) is
begin
return 0 = val rem 2;
end IsEven;
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?

As implied above, any language's code (well, except purposely obfuscated
ones such as White Space (-;) can be written so as to be very readable.
IMO, the begin and end in Ada makes reading (especially for debugging)
nested code much easier because you don't lose yourself in a fluery of
end braces - }.

I also feel that Ada demands good code while C++ is just happy to have
something to compile. In C++, assigning a float to an integer is as
simple as float = integer while in Ada you have to explicity cast it:
float := integer(integer)

Puckdropper
--
www.uncreativelabs.net

Old computers are getting to be a lost art. Here at Uncreative Labs, we
still enjoy using the old computers. Sometimes we want to see how far a
particular system can go, other times we use a stock system to remind
ourselves of what we once had.

To email me directly, send a message to puckdropper (at) fastmail.fm
 
I

Ignoramus16420

#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?


Remember that the intervieweee sits in a room with no internet access.
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?

Sounds like a good thing, assuming that they are good solutions.
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.

I am not quite sure what you are saying here.

If an intervieweee passes my test because he actually remembers a
sample from K&R, all the power to him.
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.

You see, in our practical experience, tasks somewhat similar to in
place string reversal come up frequently in real life.

This is a test to separate BSers from real programmers.

Put it more bluntly, a lot of people say that they are programmers in
resumes, whereas in fact they are not. This test is designed to filter
those people out. No one is perfect and we would accept some mistakes,
as long as it looks like it is written by a real programmer.
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.

Let me give you a poor analogy.

Suppose that you interview someone claiming to be a circuit
designer. The caldidate is charming, read AoE, read this newsgroup for
a while, and is in fact pretty good and BSing and "active listening",
and other psychology tricks. You sit there and think, what a pleasant
and knowledgeable guy, what a pleasure to talk to.

Without a little test, you (the general you) may easily become
confused and disoriented by someone who is pretty good at
bullshitting.

Same here, the test is not the whole interview, not the end all, but a
good filter.

i
Q. "Why are manhole covers round?"
A. "To keep them from falling in."

Bzzzt! "Because manholes are round."


--
 
Top