Is there a way to separate elements of a list by order when theyre in a string format?

Question:

x=input()
x=list(x)
amtcrs=''.join(x[0:x.index(' ')])
amtst=''.join(x[x.index(' ')+1:])
a=0
b=''
while a<int(amtst):
    crsname=input()
    crsname=list(crsname)
    crs=''.join(crsname[0:crsname.index(' ')])
    st=''.join(crsname[crsname.index(' ')+1:])
    st=st.lower()
    a=a+1
    b=b+crs+st+' '
b=b.strip()
z=b.split(' ')
z.sort()
for l in range(0,int(amtcrs)):
    print(z)

This is what I’ve come up with for now. It’s incomplete but I’m getting there.

Basically what I’ve been tasked to do is to first take an input "n m" such that n is the amount of courses that will be taught, and m is the amount of students that will be applying to the courses.

Then I have to take in m lines of input so that each line is a number followed by a name, the number being which course the student wants to take (number will be 1<= x >=n) and the name of student.

for example:

if the input is 2 4, there will be 4 more inputs, lets say they are (1 david, 2 john, 2 Kevin, 1 jennifer)

What I must output is, in this case, 2 lines of the names of people enrolling in each course, with the people enrolling in course 1 being the first line of output and so on.

so the output here should be:

david jennifer
john Kevin

The tricky part is that if a person’s name is similar to another person’s name (i.e same length and at most 1 character different, mike and tike are similar, Mike and mime are similar (M and m count as the same char)) and they’re applying to the same course, then whoever’s name was inputted first would be registered for the course while the other one isn’t.

For example: input is 3 4, then the next 4 inputs are ( 1 david, 1 davin, 2 john, 2 lola)

then the output should be:

david
john lola

also notice how the third line is empty because there is a third course but no one is signing up for it.

Asked By: ineedhelp

||

Answers:

You overcomplicated it – and all code seems useless.

You should get text from input() and directly use .split(' ') (without using list())

And later you could put data in dictionary to create ie.{'1': ['david', 'jennifer'], '2': ['john', 'Kevin']} and next you should use for-loop to get lists with names and display them in line using " ".join(list).

Something like this (but not tested)

# --- get numbers ---

x = input()

n, m = x.split(' ')
#n = int(n)
m = int(m)

# --- get courses and names ---

data = {}  # dict()

for _ in range(m)
    line = input()
    course, name = line.split(' ')
    
    if course not in data:
        data[course] = []  # list()
        
    if name not in data[course]:
        data[course].append(name)
    
#print(data)

# --- display results ---

#for key, value in data.items():
#    print( " ".join(value) )
    
for key in sorted(data.keys()):
    value = data[key]
    print( " ".join(value) )
Answered By: furas
print("Enter course number,max student number ") # 3 4
course_student = str(input())

total_number_of_courses=course_student.split()[0]
max_student = course_student.split()[1]

hash_list= {str(course_id):[] for course_id in range( 1,int(total_number_of_courses))}
print("Enter course_id and name")
stu_course_ids=str(input())  #1 John, 2 Doe, 2 Johny

for stu_course_id in stu_course_ids.split(","):
    course_stu = stu_course_id.split(" ")
    course_stu = [ item for item in course_stu if item]
    if hash_list[course_stu[0]]:
        hash_list[course_stu[0]].append(course_stu[1])
    else:
       hash_list[course_stu[0]] = [course_stu[1]] 
       
for key, value in  hash_list.items():
    print( f"course_id: {key}  students: {' '.join(value)}")

my result:

course_id: 1  students: John
course_id: 2  students: Doe Johny
Answered By: Şahin Murat Oğur
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.