Do my homework, I'm lazy [submitted without subject -mod] 
Author Message
 Do my homework, I'm lazy [submitted without subject -mod]

hello,
I had a programming problem which i am unable to do.Please could you solve
it
Problem:
A server keeps a log file containing the information about users logging in
and out of the system. The file is organized in the following way: each line
contains a word IN or OUT (indicating whether the user logs in or out)
followed by a space and a user ID. For example the file log.txt contains:

IN sj
OUT sj
IN tom
IN roger
IN sj
OUT tom

Your task is to write a program loggedin that will read a file in the format
described above from standard input, and print on standard output the list
of users currently looged in the system. For the example above your program
would print

% loggedin < log.txt
roger
sj

Assume the following:
The users can be printed in any order.
User ID is a string of up to 30 characters, case is important.
Before the first line of the log file was written no users were logged on to
the system.
Each user can be logged in the system only once, for example you should
print an error message in a case like this
% loggedin
IN tom
IN tom
OUT tom
^D
Error: user tom already logged in

You DO HAVE to print an error message when a user who is not logged in logs
out, e.g.
% loggedin
IN sj
OUT SJ
^D
Error: user SJ not logged in

You DO HAVE to assume that the log file can be of any length, and there can
be any number of users (your data strucutres have to grow dynamically)
Suggested solution
What is given below is just a suggestion, feel free to choose your own
implementation.

A possible solution is to create a linked list, each node corresponding to a
user logged in to the system. When an IN line is read, your program would
check if the user is already on the list and if so print an error message.
Otherwise the user would be added to the list. When an OUT line is read,
your program would check if the user is on the list and if not print an
error message. Otherwise the node corresponding to the user would be deleted
from the list. After all of the log file is read you program would simply
print the contents of every node of the list.

Hoping for a prompt and positive reply.
Bye
Jerry
--



Mon, 21 Jul 2003 11:16:08 GMT  
 Do my homework, I'm lazy [submitted without subject -mod]

Quote:

> I had a programming problem which i am unable to do.Please could you solve
> it

> Hoping for a prompt and positive reply.

I am positive that you'd learn a lot more if you had the same attitude
towards other people doing your homework as our esteemed moderator has.

Prompt enough?

Richard
--



Tue, 22 Jul 2003 01:54:06 GMT  
 Do my homework, I'm lazy [submitted without subject -mod]

Quote:

>hello,
>I had a programming problem which i am unable to do.Please could you solve
>it
>Problem:
>A server keeps a log file containing the information about users logging in

     No, no, no!  That's not the problem.  The problem is that you
aren't doing your own homework.  Do your own homework!

     I'm sorry, but short of you presenting yourself to my dungeon
where I keep my torture instruments, I don't think I'll be able to
convince you.

[snip]

Quote:
>Hoping for a prompt and positive reply.

     Promptness: I replied as soon as I read your message.
     Positiveness: I'm positive you should do your own homework.

Quote:
>Bye
>Jerry

     Bye, Jerry.  Don't forget to write[ your own programs].

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
     I have preferences.
     You have biases.
     He/She has prejudices.
--



Thu, 24 Jul 2003 22:56:50 GMT  
 Do my homework, I'm lazy [submitted without subject -mod]


writes

Quote:

>> I had a programming problem which i am unable to do.Please could you solve
>> it

>> Hoping for a prompt and positive reply.

>I am positive that you'd learn a lot more if you had the same attitude
>towards other people doing your homework as our esteemed moderator has.

>Prompt enough?

>Richard

Our moderator is too kind. Can I suggest that homework assignment
submissions which show no evidence of thought be rejected summarily?
[well, yeah, but they're sorta fun. -mod]
--
Walter Briscoe
--



Sat, 26 Jul 2003 05:03:18 GMT  
 Do my homework, I'm lazy [submitted without subject -mod]


Quote:
>     I wouldn't mind it so much if I knew that the "students" were
>actually sweating over our "answers".  Until you can guarantee their
>torture, I have to agree with Walter.

Well, I find such threads amusing and they also remind readers that we
really do not do other people's homework. Perhaps that helps to reduce
Peter's load by deterring a few students from asking:)

--
Francis Glassborow
See http://www.accu.org for details of The ACCU Spring Conference, 2001
(includes many regular participants to C & C++ newsgroups)
--



Mon, 28 Jul 2003 03:59:44 GMT  
 Do my homework, I'm lazy [submitted without subject -mod]

Quote:



> >     I wouldn't mind it so much if I knew that the "students" were
> >actually sweating over our "answers".  Until you can guarantee their
> >torture, I have to agree with Walter.

> Well, I find such threads amusing and they also remind readers that we
> really do not do other people's homework. Perhaps that helps to reduce
> Peter's load by deterring a few students from asking:)

I would have to agree here - I have derived no small amount of
enjoyment from reading these threads =-)

Micah
--



Mon, 28 Jul 2003 09:42:52 GMT  
 Do my homework, I'm lazy [submitted without subject -mod]
This is an interesting assignment. As I had half an hour on my hands, I
decided to do it. After all, many people here can learn from good UNIX
code. And I could use this as a test message to see if mozilla is able
to cope with news.

As you can see, the standard case is nicely handled

20:07:05|260 [ladybug:/tmp] fred% cat people.txt
IN sj
OUT sj
IN tom
IN roger
IN sj
OUT tom
20:07:06|261 [ladybug:/tmp] fred% ./loggedin < people.txt
sj
roger

The error case is also quite easy to treat (You may want to tailor the
error message to your needs)

20:07:11|262 [ladybug:/tmp] fred% cat people2.txt
IN sj
OUT SJ
20:07:16|263 [ladybug:/tmp] fred% ./loggedin < people2.txt
/tmp/lusers.h:6: user SJ not  logged in

As the code is quite straightforward, the opposite error is also
handled, so you can get one or two extra points here

20:07:22|264 [ladybug:/tmp] fred% cat people3.txt
IN tom
IN tom
OUT tom
20:07:27|265 [ladybug:/tmp] fred% ./loggedin < people3.txt
/tmp/lusers.h:6: user tom already logged in
20:07:36|266 [ladybug:/tmp] fred%

The code is probably a little UNIX oriented, but the use of ^D to
interrupt the entry in the assigment example let me think that it would
not be a problem. Please, note how carefully the limits given in the
assignement have been respected. I am afraid that the code is a little
tied to ASCII charset, but this could be removed. The security of it
could also be improved.

Please, post more homework assignements. Or could we have a
comp.lang.c.assignment ?

Cheers,


/* loggedin.c */

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main()
{
char a['"'];
        FILE *f = fopen( "/tmp/lusers.c", "w" );
        FILE *h = fopen( "/tmp/lusers.h", "w" );

        fprintf( f, "#include <stdio.h>\n#include "
                "\"lusers.h\"\nmain(){\n" );
        while ((int)fabs(scanf( "%s %s", a, a+3 ))&2)
                if (!(*a&2))
                {       fprintf( h, "#ifdef %s\n#error user %s already "
                                "logged in\n#endif\n#define %s\n",
                                a+3, a+3, a+3 );
                        fprintf( f, "#ifdef %s\nprintf(\"%s\\n\");\n"
                                "#undef %s\n#endif\n",
                                a+3, a+3, a+3 );
                }
                else
                {       fprintf( h, "#ifndef %s\n#error user %s not "
                                "logged in\n#endif\n#undef %s\n",
                                a+3, a+3, a+3 );
                }
        fprintf( f, "return 0;}\n" );
        fclose( f );
        fclose( h );
        system( "cc /tmp/lusers.c -o /tmp/lusers && "
                        "/tmp/lusers && rm /tmp/lusers" );
        return 0;

Quote:
}

--



Sat, 02 Aug 2003 04:23:21 GMT  
 Do my homework, I'm lazy [submitted without subject -mod]


Quote:
>int main()
>{
>char a['"'];

That is an amazing construct.  It took me a while to realize that it
is tic-quote-tic and not quote-quote.  What happens if the program is
used on a machine with a weird character set such that the double
quote character has a very low integer value?  Your array will be a
little short for your scanf.

<<Remove the del for email>>
--



Sat, 02 Aug 2003 14:42:54 GMT  
 Do my homework, I'm lazy [submitted without subject -mod]

Quote:



>> int main()
>> {
>> char a['"'];

> That is an amazing construct.  It took me a while to realize that it
> is tic-quote-tic and not quote-quote.  What happens if the program is
> used on a machine with a weird character set such that the double
> quote character has a very low integer value?  Your array will be a
> little short for your scanf.

Sure. That's why I said:

Quote:
> Please, note how carefully the limits given in the assignement have been respected.

Because 3 (length of "OUT") plus 30 (max user name length as specified in the assignment) + 1 (final \0) is equal to 34.

Quote:
> I am afraid that the code is a little tied to ASCII charset, but this could be removed.

Because '"' == 34 in ASCII.
(There is one other reason why the code is tied to ASCII)

Note that the code supposes that the two strings will be scanned
left-to-right, as the trailing NUL of "OUT" will be overriten by first
char of user name...

Btw, if you like weird constructs, there are the famous:

'-'-'-'
and
'/'/'/'

that can be used as shorthands(!) for 0 and 1 respectively. :-)

Cheers,


--



Sun, 03 Aug 2003 05:05:14 GMT  
 Do my homework, I'm lazy [submitted without subject -mod]

[snip]

Quote:
>Sure. That's why I said:

>> Please, note how carefully the limits given in the assignement have been respected.

>Because 3 (length of "OUT") plus 30 (max user name length as specified in the assignment)
>+ 1 (final \0) is equal to 34.

>> I am afraid that the code is a little tied to ASCII charset, but this could be removed.

>Because '"' == 34 in ASCII.
>(There is one other reason why the code is tied to ASCII)

      But that would be telling!

Quote:
>Note that the code supposes that the two strings will be scanned
>left-to-right, as the trailing NUL of "OUT" will be overriten by first
>char of user name...

>Btw, if you like weird constructs, there are the famous:

>'-'-'-'
>and
>'/'/'/'

>that can be used as shorthands(!) for 0 and 1 respectively. :-)

                     ^^^^^^^^^^
     "underhands"?  (as in "underhanded")

     You are evil.  I like that in a homework thread.

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
     I have preferences.
     You have biases.
     He/She has prejudices.
--



Mon, 04 Aug 2003 03:23:15 GMT  
 Do my homework, I'm lazy [submitted without subject -mod]

[ obfuscated homework answer ]

Quote:
> /* loggedin.c */

> #include <stdio.h>
> #include <math.h>
> #include <stdlib.h>

> int main()

> char a['"'];
> FILE *f = fopen( "/tmp/lusers.c", "w" );
> FILE *h = fopen( "/tmp/lusers.h", "w" );

Not multiuser safe.  Prefer:
  char n[something_for_L_tmpnam_plus_2];
  FILE *f = fopen(strcat(strcpy(n,tmpnam("lusers")),".c"),"w");
  FILE *h = fopen((strcpy(n+strlen(n)-2,".h"),n),"w"); /* order matters */
  fprintf(f, ..."#include \"%s\"\n" ..., n);

....

Quote:
> fprintf( f, "#ifdef %s\nprintf(\"%s\\n\");\n"
> "#undef %s\n#endif\n",
> a+3, a+3, a+3 );

Might prefer
 ..."puts(X(%s))"... a+3
and nearer top output "#define X(X)#X\n"

....

Quote:
> system( "cc /tmp/lusers.c -o /tmp/lusers && "
> "/tmp/lusers && rm /tmp/lusers" );
> return 0;

Adjust filenames as needed due to above,
and might as well return system(...) & 0xFF.
After all you did call this "good Unix code" ;-)

--
- David.Thompson 1 now at worldnet.att.net
--



Mon, 18 Aug 2003 14:44:04 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. <sent to mod without subject>

2. Doing mod without %

3. Some one 's done his homework

4. matrix determinant code [submitted with no subject]

5. Newbie Question [came in with no subject line -mod]

6. Anybody interested in doing my homework

7. Doing YOUR homework: Fraud or Plagurism

8. Sorry if this is a FAQ, I'm too lazy to read it

9. Deadline for submitting to POPL '95 approaches

10. Can't submit a form to ISAPI!!

11. MOD's in C

12. My friend's homework(Tic Tac Toe Algorithm)

 

 
Powered by phpBB® Forum Software