Yesterday, I have started to solve problems from a challenging Russian site for programming competitions. Today, I have decided to spend about 30 minutes on another problem, which seemed easy, but I am already spending hours on it, without being able to solve it for full points.

The point is, that currently I am not even able to locate what should be improved in my code, as far as any input returns the desired output. Indeed, the code is a little messy, because I have not updated it. Here is the task:

2046. The First Day at School

Time limit: 1.0 second

Memory limit: 64 MB

Vasya is a young and very promising android. Today is his first day at University. Vasya has very carefully studied the list of all courses on the wall near the Dean’s office and has chosen the ones to attend. Now he wants to write down his own week timetable. Help him do this.

Input

The first line contains an integer n that is the number of courses Vasya is going to attend (1 ≤ n ≤ 12). After that the courses are listed, each is described in two lines.

The first line of a course description contains its name. The name of the course may consist of up to five words, which are divided by exactly one space (there are no spaces before the first word and after the last one). The words consist of capital and lowercase Latin letters. The length of every word is within the range from 1 to 10.

The second line of a course description contains the day of week and the number of a lesson, when it takes place. The day of week may take one of the three values: “Tuesday”, “Thursday” и “Saturday”. The number of a lesson is an integer from 1 to 4. There are no two courses, Vasya has chosen, taking place at the same time.

Output

Output the timetable as a table of the size 4×3. The columns of the table should correspond to the three academic days: the first column — to Tuesday, the second — to Thursday, the third — to Saturday. The rows should correspond to the four classes. The width of each column should be equal to 10 characters. The height of the row of the table equals to the height of the highest of its cells. If all the cells in the row are empty then the height of the row should be equal 1 character. If some word doesn’t find room in the current line, it should be placed in the next line. The text in the cell should be aligned to top and left borders. Make the table itself using characters “-” (ASCII 45), “+” (ASCII 43) and “|” (ASCII 124).

As far as I have tried to update the whole thing quite a lot of times, at the end I managed to make it working the way I wanted. But it still passes only the first 2 tests, stopping on the 3. one. Quite fun. This is the output from some extreme tests (above the required input for words) from my code:

As you see in the example from Tuesday 4 =>”** d LALALAL LALALAL LLA LAL LAL L L L**“, I have even tried with more than 5 words and it unites them into one cell pretty neatly. So, anyone with ideas where is my program failing?

In general, the task was intersting and I am quite happy that I spent some hours on it.

Here comes the code, try to improve it and let me know if you have managed to take full points from it! 🙂

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
def strBuilder(lstLine): strLine = "|" for z in range (0,len(lstLine)): strLine += str(lstLine[z]) strLine += " " *(intMagicNumber-len(lstLine[z])) + "|" return strLine def listSplitMe(strText): listText = strText.split() strWrite = "" lastLine = "" for i in range(0,len(listText)): if len(lastLine)+ len(listText[i])>9: strWrite+=lastLine+"\n" lastLine = listText[i] if i == len(listText)-1: strWrite+=lastLine else: if i == 0: lastLine+=listText[i] else: lastLine+=" " + listText[i] if i == len(listText)-1: strWrite+=lastLine return strWrite.split(sep="\n") intSubjectsCount = int(input()) listSubjectName = [] listPosition = [] for x in range(0, intSubjectsCount): listSubjectName.append(input()) lstSecondtLine = input() lstSecondtLine = lstSecondtLine.replace("Tuesday","1") lstSecondtLine = lstSecondtLine.replace("Thursday","2") lstSecondtLine = lstSecondtLine.replace("Saturday","3") listSecondLine = lstSecondtLine.split() listSecondLine[0], listSecondLine[1] = listSecondLine[1], listSecondLine[0] listSecondLine = list(map(int,listSecondLine)) listPosition.append(listSecondLine) strLineBluePrint = "+----------+----------+----------+" intMagicNumber = 10 listLinesUsed = [] for row in range(1,5): for column in range(1,4): listTest = [] listTest.append(row) listTest.append(column) if listTest in listPosition: strSubject = listSubjectName[listPosition.index(listTest)] listLinesUsed.append(len(listSplitMe(strSubject))) else: listLinesUsed.append(1) listLinesUsedRebuild = [] for row in range (0,12,3): intMaxLines= max(listLinesUsed[row+0] , listLinesUsed[row+1] , listLinesUsed[row+2]) listLinesUsedRebuild.append(intMaxLines) #print(listLinesUsedRebuild) for row in range(1,5): print(strLineBluePrint) if listLinesUsedRebuild[row-1]<2: strLine = "|" for column in range(1,4): listTest = [] listTest.append(row) listTest.append(column) if listTest in listPosition: strSubject = listSubjectName[listPosition.index(listTest)] intLen = len(strSubject) strLine += (strSubject) strLine += " "* (intMagicNumber-intLen) +"|" else: strLine += " "*intMagicNumber +"|" print(strLine) else: listMain = [] for column in range(1,4): listTest = [] listNew = [] listTest.append(row) listTest.append(column) if listTest in listPosition: strSubject = listSubjectName[listPosition.index(listTest)] listNew.extend(listSplitMe(strSubject)) if ((int(listLinesUsedRebuild[row-1]))>len(listNew)): intDifference = int(listLinesUsedRebuild[row-1])-len(listNew) listAdditional = ["" for a in range(intDifference)] listNew.extend(listAdditional) listMain.append(listNew) #print(listMain) listNewMain = [] for a in range(0, len(listNew)): listSubList = [] for b in range(0, len(listMain)): listSubList.append(listMain[b][a]) listNewMain.append(listSubList) #print(listNewMain) for li in listNewMain: print(strBuilder(li)) print(strLineBluePrint) |

Thank you for your interst in VitoshAcademy.Com and HereComesTheCode.Com 🙂

Edit 1:

I have fixed the function ** def listSplitMe(strText):, **but it still does not let me pass 🙂