JustAnswer > Programming
Ask A Question|Register|Login|Help
JustAnswer

Programming

Ask a Programming Question, Get an Answer ASAP!

Have your own Programming question?

4 Programmers are Online Now
characters left:
Not a Programming Question?
Bookmark and Share

Question

I have to write a program in python that will allocate students to tutorial sessions according to student preferences. Tutorials can only have a certain number of students. If the session is full I have to move the student to their next preference. I have files containing student preferences and tutorial information. I am completely new to programming having only managed to grasp a few basics so far. This to me is completely overwhelming, how do I even start!!!

Submitted: 254 days and 9 hours ago.
Category: Programming
Value: $56
Status: AWAITING CUSTOMER ACTION
+
Read More

Optional Information

OS: Windows XP; Browser: IE

Already Tried:
Tried to break down problem into what I need to do. But really just a start with some sample code would be great and just a general idea on what is entailed in writing such a program.

Posted by Mike B 253 days and 18 hours ago.

Info Request

XXXXXX,

I'm not Python expert, but I have seen your question just sitting here without anyone taking it...

Would you like to try and learn together?

What version of Python do you need to write this in? Or does it matter?

Posted by Mike B 253 days and 17 hours ago.

Info Request

Experts - If you're a Python expert, feel free to PM me and I will gladly let you take over.

XXXXXX - If you don't feel comfortable moving on with me, just let me know and I can opt-out and let someone else handle this.

253 days and 14 hours ago.

Reply

Hi Mike, thank you for looking at my problem. I'm fine with you helping me. The version of Python is 2.6. I will give you an example of what the final product should look like.

 

>>> allocate()

Enter the filename of the student preferences: tuts_eg1

Enter the filename of the tutorial info: tuts_eg1

T1 5 3

T2 10 3

T3 5 1

T4 10 2

T5 5 8

Command : m 3 2 T5 T3

2 moved

T1 5 3

T2 10 3

T3 5 3

T4 10 2

T5 5 6

Command : m 2 2 T5 T4

2 moved

 

and so on until all students have been allocated to their preferences and according to tutorial size.

 

As I said previously there are files with information about tutorials and one about student preferences. The first file has

 

T1 5

T2 10

T3 5

T4 10

T5 5

on it

 

and the second file has

 

Student 99999 T5 T4 T1

Student 99998 T5

Student 99997 T3

Student 99996 T2 T3 T5

etc. on it

This means that student 99999 preferences are to go to T5 then T4 then lastly T1 and so on with the other students

 

Any help greatly appreciated.

Posted by Mike B 253 days and 13 hours ago.

Info Request

Alright... First we need to setup a development environment.

I've downloaded Python 2.6.1 from here: http://www.python.org/download/
Also, to make the Python editing files a bit easier and nicer I've downloaded Notepad++ from here (this is not really req'd): http://notepad-plus.sourceforge.net/uk/site.htm

Now, let me play around with Python for a bit, and I will get back to you with some code that you could try...

Posted by Mike B 253 days and 12 hours ago.

Info Request

This is what I have so far... It just prompts for the 2 files and prints them out. Try to look through the code and see if you understand it.

Questions:
(1) Can you explain what each item in each line is in the following example:

T1 5 3

T2 10 3

T3 5 1

T4 10 2

T5 5 8

For instance, the T1, T2, T3, etc is for the Tutorial, the 5, 10, 5, 10, 5 is the maximum number of students allowed, and the 3, 3, 1, 2, 8 is the current number of students... Is this right?

(2) Can you explain the "Command : m 3 2 T5 T3" ?

m = move (are there any other commands?)

3 = ??

2 = ??

T5 = Tutorial 5

T3 = Tutorial 3

(3) What happens when a student only wants to attend a single tutorial, but that tutorial is already full?



---------------------------------------------------------------------
#!/usr/bin/python

def GetFile(fileName):
f = open(fileName, 'r')
try:
return f.readlines()
finally:
f.close()

def allocate():
StudentPrefsFileName=raw_input('Enter the filename of the student preferences: ')
StudentPrefs = GetFile(StudentPrefsFileName)
TutorialInfoFileName=raw_input('Enter the filename of the tutorial info: ')
TutorialInfo = GetFile(TutorialInfoFileName)

for fileLine in StudentPrefs:
print fileLine

for fileLine in TutorialInfo:
print fileLine

Edited by Mike B on 3/14/2009 at 1:34 AM

Accepted Answer

XXXXXX,

Good news! I got most of your program all done... The only part that you're going to have to finish is the writing of the command function. I think if you look through my code, you'll be able to figure out how to add that part. That part should be very small anyways.

Thanks... Please click the Accept button so that I can credited for this work.

--------------------------------------------------------

#!/usr/bin/python

class tutorial:
def __init__(self,name,capacity):
self.name = name
self.capacity = capacity
self.cur_studs = 0

def add_stud(self):
self.cur_studs += 1
return self.cur_studs

class student:
def __init__(self,name,number):
self.name = name
self.number = number
self.tutorials = []


def GetFile(fileName):
f = open(fileName, 'r')
try:
return f.readlines()
finally:
f.close()

def allocate():
StudentPrefsFileName=raw_input('Enter the filename of the student preferences: ')
StudentPrefs = GetFile(StudentPrefsFileName)
TutorialInfoFileName=raw_input('Enter the filename of the tutorial info: ')
TutorialInfo = GetFile(TutorialInfoFileName)

students = []
for fileLine in StudentPrefs:
cur_student = fileLine.split(' ')
students.append(student(cur_student[0].strip(),cur_student[1].strip()))
for n in range(2, len(cur_student)):
cur_tut_desired = cur_student[n].strip()
students[len(students)-1].tutorials.append(cur_tut_desired)

tutorials = []
for fileLine in TutorialInfo:
cur_tutorial = fileLine.split(' ')
tut_name = cur_tutorial[0].strip()
tut_cap = cur_tutorial[1].strip()
tutorials.append(tutorial(tut_name,tut_cap))

for s in students:
for t in s.tutorials:
if t == tut_name:
tutorials[len(tutorials)-1].add_stud()
print tut_name, tut_cap, tutorials[len(tutorials)-1].cur_studs

command=raw_input('Command : ')


Picture
Expert: Mike B
Pos. Feedback: 100.0 %
Accepts: 
Answered: 3/14/2009

Informati&on Analyst

10+ years experience programming in a variety of languages

253 days and 6 hours ago.

Reply

Thanks very much. I don't know if you still want answers to the questions you asked but I will try to answer them anyway.

 

The initial first preference allocation shows too many students in T5 and too few in T3 and T4. The first command attempts to move three students from T5 to T3 whose second preferences were for T3. There were only two students in this category and they were moved. The second command tries to move two students from T5 to T4 whose second preference is for T4. Two students were moved.

 

It is assumed that all students can come to at least one tutorial.

 

After reading in the files, the program makes an initial allocation using first preferences and then enters a phase where the interface prompts for a command and the user enters a command to be processed by the program.

 

Commands are

 

e exit the program

s display the current allocations

m num pref_level from to

Posted by Mike B 253 days and 1 hours ago.

Answer

XXXXXX,

Thanks for the info. I have updated the code parse the commands. The code lacks error checking, and I did leave you the part where the moving of the students needs to be done. I think this is fair. Here is the updated code (the indents need to be fixed, since this site removes the tabs):

-------------------------------------------------------

#!/usr/bin/python

class tutorial:
def __init__(self,name,capacity):
self.name = name
self.capacity = capacity
self.cur_studs = 0

def add_stud(self):
self.cur_studs += 1
return self.cur_studs

class student:
def __init__(self,name,number):
self.name = name
self.number = number
self.tutorials = []

def display_alloc(tutorials):
for t in tutorials:
print t.name, t.capacity, t.cur_studs

def GetFile(fileName):
f = open(fileName, 'r')
try:
return f.readlines()
finally:
f.close()

def allocate():
StudentPrefsFileName=raw_input('Enter the filename of the student preferences: ')
StudentPrefs = GetFile(StudentPrefsFileName)
TutorialInfoFileName=raw_input('Enter the filename of the tutorial info: ')
TutorialInfo = GetFile(TutorialInfoFileName)

students = []
for fileLine in StudentPrefs:
cur_student = fileLine.split(' ')
students.append(student(cur_student[0].strip(),cur_student[1].strip()))
for n in range(2, len(cur_student)):
cur_tut_desired = cur_student[n].strip()
students[len(students)-1].tutorials.append(cur_tut_desired)

tutorials = []
for fileLine in TutorialInfo:
cur_tutorial = fileLine.split(' ')
tut_name = cur_tutorial[0].strip()
tut_cap = cur_tutorial[1].strip()
tutorials.append(tutorial(tut_name,tut_cap))

for s in students:
for t in s.tutorials:
if t == tut_name:
tutorials[len(tutorials)-1].add_stud()
print tut_name, tut_cap, tutorials[len(tutorials)-1].cur_studs

command=''
while command != 'e':
command=raw_input('Command : ')

if command == 's':
display_alloc(tutorials)
else:
split_command = command.split(' ')
if split_command[0] == 'm'
cnum = split_command[1]
cpref_level = split_command[2]
cfrom = split_command[3]
cto = split_command[4]
#this is where you have to put the code for moving the students around

253 days ago.

Reply

Again, thank you very much. You have have been a great help. Couldn't have done it without youSmile

Accepted Answer

Excellet!

Please click the ACCEPT button, so that I can credited for this work.

Thank you.

Picture
Expert: Mike B
Pos. Feedback: 100.0 %
Accepts: 
Answered: 3/14/2009

Informati&on Analyst

10+ years experience programming in a variety of languages

Posted by Mike B 253 days ago.

Info Request

XXXXXX,

It looks like I received credit twice for doing this project. I'm not sure why this happened. Maybe you accepted my answer once already, and then I asked you to accept again and you did... I appologize, if this is the case (I'm kinda new on JA).

Can you double-check to see if you got billed twice? If so, I can try to get a hold of someone at JA to resolve this.

Thanks,
Mike

252 days and 16 hours ago.

Reply

Yes you did ask me to accept twice (see your last post). If you can resolve this i would be grateful.

Posted by Mike B 252 days and 15 hours ago.

Answer

XXXXXX,

You will have to request a refund for the second payment. You can request the refund at this website: http://www.justanswer.com/support.aspx I don't think that I can submit it for you, since I don't have your personal email (and it is not allowed for you to share that with me).

Actually, you can copy and paste this Reply into the message you wish to send.

XXXXXX has paid twice for the same Answer by mistake. Please refund the second ACCEPT that XXXXXX has submitted for this question: http://www.justanswer.com/questions/1wgcl-write-program-python-allocate

The initital ACCEPT was valid @ "March 13 2009 at 11:33 PM". However, the second ACCEPT @ "March 14 2009 at 8:11 AM".needs to be refunded to XXXXXX.

Thanks
Mike B


Edited by Mike B on 3/14/2009 at 10:14 PM

252 days and 12 hours ago.

Reply

Thank you Mike, I have sent the request off pasting in part of the message you wrote. I appreciate you making me aware of this discrepancy. A lot of less scrupulous people would have just kept the money.

243 days and 16 hours ago.

Reply

Mike, are you still able to answer questions for me regarding this problem? I would just like to know how to take the name of a text file containing student preferences and return a dictionary that associates each student with their preferences as a list using 'extract_preferences(filename)'.

Posted by Mike B 243 days and 16 hours ago.

Info Request

Sure thing... I'm just in the middle of another issue right now... Can I get back to you in a couple of hours?

243 days and 16 hours ago.

Reply

No worries, Mike. Thank you so much

Posted by Mike B 243 days and 14 hours ago.

Info Request


Does this need to be part of the program I've sent you previously?

Also, if you've made changes to the program I sent you, can you send me your updated version? Just upload it to http://wikisend.com and Reply with the link.

Edited by Mike B on 3/23/2009 at 11:53 PM

Posted by Mike B 243 days and 12 hours ago.

Info Request

I'm not positive, but I believe I figured out what you were asking for... I didn't add it into the existing program, but I created a new one. The existing program already uses classes, which is a much better choice for this project.

Anyways, this script will accept a file name as a parameter, and it will return the student dictionary object which contains a list object for each student.

I've uploaded it here: http://wikisend.com/download/459832/eirel5_2.py

There are lots of comments in this file, so you should be able to learn from it and figure out how it works.

Let me know if you have any questions.

Edited by Mike B on 3/24/2009 at 1:53 AM

241 days and 16 hours ago.

Reply

Hi Mike, I finally have a copy of the task I have to do. I have uploaded it as well as what I have done so far, thanks to your help. The comments you provided on your last script were great. The links are below.

 

http://wikisend.com/download/492878/Tutorial Allocator.doc

 

http://wikisend.com/download/494948/Program so far.doc

 

If you could help me with tasks 1.2.5 and 1.2.6 that would be great

 

Thanks

Posted by Mike B 241 days and 10 hours ago.

Answer

XXXXXX,

I've uploaded the file to: http://wikisend.com/download/949030/tut_alloc_126.py

I'm not a hundred precent clear on 1.2.6, but I coded it how I understood it... Hope it is what you wanted.

In 1.2.2 I found a mistake, and fixed it... you'll see my comments in the file

I have clicked the Answer button... If you feel that I deserve to get paid for this work (using the original amount), then click the Accept button.

Thanks,
XXXX X.

Edited by Mike B on 3/26/2009 at 3:10 AM

239 days and 16 hours ago.

Reply

Hi Mike,

 

Thank you so much again. I will accept but just want to ask a couple more things first if that's OK. Please bear with me and forgive my ignorance. Now that all the functions are been written (extract_preferences, allocate_first_prefs etc.) how do you put it all together so that it will run as a program?

 

For example - I type in allocate () and then python automatically returns "Enter the filename of the student preferences:" and I input prefs_eg1 then python returns with "Enter the filename of the tutorial info" and I input tuts_eg1. Python then prints the tutorial info and so on continuing through the program until you get down to command : e (to exit the program). Basically I suppose how do you write a module to lauch or start the program. Do you think that is what is meant by 1.2.6 - write a function allocate () that will start the entire program?

 

 

Posted by Mike B 239 days and 15 hours ago.

Answer

XXXXXX,

Let me take a look and get back to you in a bit

Thanks,
XXXX X.

Edited by Mike B on 3/27/2009 at 10:33 PM

Posted by Mike B 239 days and 14 hours ago.

Answer

Hi XXXXXX,

I've finished the remaining tasks in the program and uploaded it here: http://wikisend.com/download/606484/tut_alloc_127.py

Again, I put a bunch of comments in there so it'll help you understand what's happening on each line.

Let me know if you have any questions.

Thanks,
XXXX X.

+
Read More

Related Programming Questions

  • I need to describ what office automation and group collabora...
  • =IF(OR(L2>=10000,5),IF(L2<10000,1)) Want to the formula to
  • how do i monitor a custom apps in delphi?
  • Hi, I'm building up a website with dreamweaver cs4 and a db
  • I downloaded a trial Microsoft Office 2007 software which wr...
  • Hi! I'm trying to calculate an average in Excel, but it seem...
  • Write a program in C which develops a database of measured d...
  • example: if I want to ..... find a charge as in this exam...



Disclaimer: Information in questions, answers, and other posts on this site ("Posts") comes from individual users, not JustAnswer; JustAnswer is not responsible for Posts. Posts are for general information, are not intended to substitute for informed professional advice (medical, legal, veterinary, financial, etc.), or to establish a professional-client relationship. The site and services are provided "as is" with no warranty or representations by JustAnswer regarding the qualifications of Experts. To see what credentials have been verified by a third-party service, please click on the "Verified" symbol in some Experts' profiles. JustAnswer is not intended or designed for EMERGENCY questions which should be directed immediately by telephone or in-person to qualified professionals.
Question List | Become an Expert | Terms of Service | Security & Privacy | About Us
© 2003-2009 JustAnswer Corp.