Jump to content

Computer related puzzles and questions


Sip

Recommended Posts

  • Replies 129
  • Created
  • Last Reply

Top Posters In This Topic

  • 3 months later...

quote:
Originally posted by Harut:

any more problems?


Ok try this one ... my coworker just spent 1 hour trying to debug this thing!!!

 

code:
int c=99;

int n=100;

for (int i=0; i<2; i++) {

c = (c++) % n;

}

cout << c << endl;


What is the output of this thing? Of course you can "cheat" and actually run it ... but see if you can figure it out! I can tell you I was WRONG! ... we tried it with 3 compilers and they all got the same thing. Not what I was expecting at all.
Link to comment
Share on other sites

ha ha ha, it was a good one.

 

the answer is 1.

 

here is what is happening in c = (c++) % 100 expresion.

0. c=99

1. c%100

2. c=99

3. c++

4. c=100

 

and one more time

0. c=100

1. c%100

2. c=0

3. c++

4. c=1

 

i know where you got confused.

99%110 gives 99 not 0.

and you tried to confuse me with and () around c++?

even with the () it doesn't the effect of ++c.

 

any more problems?

Link to comment
Share on other sites

The place where I made my mistake was:

 

c=99

 

c++ -> c=100 (but returns 99)

 

so: c=99%100 which makes c=99.

 

But the way these compilers seem to work, is they do:

 

c=99%100 then

c=c+1 (for the c++)

 

For one thing, I would NEVER write any program that looks like c=c++ !!! But I have not seen any book or any documentation which actually explains what would happen if you have something like that!

Link to comment
Share on other sites

quote:
Originally posted by Aghtchik:

[QB]1 goes left, 2 goes right, 3 left, 4 right, etc.


Harut jan, you ha ...

 

ooops, Sorry, it was a force of habit!!!!

 

Ok so as far as I understand, now you have all odd numbers on the "left" and all even ones on the "right". What now?

 

---

Update: Actually, after thinking about it for a while, I think that problem is much harder than I thought at first. So here is the updated version of the problem! But feel free to answer either one

 

You have 1 million objects with integer names (between 0 and 1 million) and you want to put them in order. What is the fastest way?

Link to comment
Share on other sites

quote:
Originally posted by Azat:

depends what language you are working in. If in vb, you creaet a collection and run Sort on it.

 

I know I am a WiseASS. But this was the computers section.


Good point ... you got me! I meant fastest "run-time" ... not fastest programming/debugging time.

 

Otherwise, I can also import them in Excel and choose "SORT" from the data menu

Link to comment
Share on other sites

  • 3 weeks later...

quote:
Originally posted by Sip:

Next problem?

 

Let's say you have 1000 integers between 0 and one million and you want to put them in order. What is the fastest way to do it?


SOLUTION:

 

You make one million + 1 slots numbered from 0 to 1000000. You mark them all emtpy. Then for each number, you put it in it's corresponding slot by marking the slot FULL. So basically with one pass through the 1000 integers, you have them in sorted order! You may need one more pass to "print them out" but that's about it. Fastest way to sort 1000 integers (or integer-numbered objects)

Link to comment
Share on other sites

  • 3 months later...

here is one for you guys. actually it's not a callenge, just a regular, easy task. i recently took a test-like thing for an internship possision. and one of the question was this. i just want to see how you guys would do it and compare it with mine. i just want to see if there are any hopes, should i wait for a call or not.

 

anyways, here it is.

 

quote:
1) Using any programming language you are familiar with, write a program

which does the following:

 

A) parse the following string based on the spaces between words, and output

each word on its own line to the console.

"The lazy brown fox jumped over the cat"

 

B) The following string represents a method call on an object instance named

'myObject'. Parse the string and output the name of the object, the name of

the method, and each method parameter, with each value on its own line.

Strip out any unnecessary characters, such as '.', '(', ')', and literal

quotes.

"myObject.myMethod(1, 72, "test1", 82.5);"


i'd prefer you use C++. that's what i used.
Link to comment
Share on other sites

A)

 

code:
char c;

while(!cin.eof()) cout << (((c=cin.get())==' ') ? 'n' : c);


B)

 

code:
int i=0;

char c;

cout << "object: "; while(!cin.eof() && (c=cin.get())!='.') cout << c; cout << endl;

cout << "method: "; while(!cin.eof() && (c=cin.get())!='(') cout << c; cout << endl;

while (!cin.eof() && cin.peek()!=')') {

cout << "paramater " << ++i << ": ";

while (!cin.eof() && cin.peek()!=')' && (c=cin.get())!=',') if (c!='"' && c!=' ') cout << c;

cout << endl;

}


In both cases I assumed there is no syntax checking required. In both cases I assumed EOF (end of file) terminates the input.

 

Both those work exactly as specified by using the input stream redirection (i.e. compile the program and run it using a redirected input text file from the console). I assumed the " " around the input are only there in the problem statement to specify things as input.

Link to comment
Share on other sites

Nice MESS you created here HARUT

 

Part B doesn't look like it would work. Seems to me it would just print some characters, one character one a line, and won't really "parse" things the way it's supposed to.

 

Did you try to run it?

Link to comment
Share on other sites

quote:
Originally posted by Sip:

Nice MESS you created here HARUT smilies/lol.gif

 

Part B doesn't look like it would work. Seems to me it would just print some characters, one character one a line, and won't really "parse" things the way it's supposed to.

 

Did you try to run it?


it does work. i ran it and it gives me this output.

code:
The

lazy

brown

fox

jumped

over

the

cat

 

myObject

myMethod

1

72

test1

82.5


can you please write a little what "parse" really means?
Link to comment
Share on other sites

oh, just to note that

 

i missed the function declaration for the second one. so there is a first line to the third part.

 

code:
void PartB ()

and one more thing. like i mentioned already, i "or" should be logical or.

logical or is the cause of the mess in the forum.

Link to comment
Share on other sites

...

code:
void PartA ()

{

char sentence[] = "The lazy brown fox jumped over the cat";

for ( int i = 0; i < sizeof ( sentence ); i++ )

cout << ( sentence [ i ] == ' ' ? 'n' : sentence [ i ] );

cout << endl;

}


continued...
Link to comment
Share on other sites

i see what's the problem. i changed the logical or to "or".

 

...

code:
{

char methodCall [] = "myObject.myMethod(1, 72, "test1", 82.5);";

bool newLine = true;

cout << endl;

for ( int i = 0; i < sizeof ( methodCall ); i++ )

{

if (( methodCall [ i ] >= 48 && methodCall [ i ] <= 57 ) or

( methodCall [ i ] >= 65 && methodCall [ i ] <= 90 ) or

( methodCall [ i ] >= 97 && methodCall [ i ] <= 122 ) or

( methodCall [ i ] == '.' && methodCall [ i + 1 ] >= 48 &&

methodCall [ i + 1 ] <= 57 ) )

{

cout << methodCall [ i ];

newLine = true;

}

else if ( newLine )

{

cout << 'n';

newLine = false;

}

}

}


the end
Link to comment
Share on other sites

quote:
Originally posted by Harut:

quote:
Originally posted by Sip:

Oooooh ... and another thing... take a close look at where you say "sizeof(methodCall)"


can't figure it out. what do you mean?


My bad on BOTH things. The sizeof the way you are using is correct. However, one often uses the strlen function to check for the length of a string as sizeof() doesn't work for dynamic arrays.

 

Also, you are right ... your code works I didn't compile it correctly in my head

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...