코드잇
[코드잇] 대학교 강의실 배정하기2
오월&절미
2021. 2. 3. 21:24
In [13]:
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:1200px !important; }</style>"))
아래 세 가지 조건을 만족하도록 코드를 작성하세요.
- 같은 크기의 강의실이 필요한 과목에 대해 알파벳 순서대로 방 번호를 배정하세요.
- 예를 들어 Auditorium이 필요한 과목으로 “arts”, “commerce”, “science” 세 과목이 있다면, “arts”는 “Auditorium-1”, “commerce”는 “Auditorium-2”, “science”는 “Auditorium-3” 순서로 방 배정이 되어야 합니다.
- "status” column이 “not allowed”인 수강생은 “room assignment” column을 그대로 “not assigned”로 남겨둡니다.
- room assignment” column의 이름을 “room number”로 바꿔주세요.
In [1]:
import pandas as pd
df = pd.read_csv('./enrolment_3.csv')
df.head(10)
Out[1]:
id | year | course name | status | room assignment | |
---|---|---|---|---|---|
0 | 2777729 | 1 | science | allowed | Auditorium |
1 | 2777730 | 2 | science | allowed | Auditorium |
2 | 2777765 | 1 | arts | allowed | Auditorium |
3 | 2777766 | 2 | arts | allowed | Auditorium |
4 | 2777785 | 1 | mba | allowed | Small room |
5 | 2777786 | 2 | mba | allowed | Small room |
6 | 2777793 | 1 | mba 2nd shift | not allowed | not assigned |
7 | 2777794 | 2 | mba 2nd shift | not allowed | not assigned |
8 | 2777795 | 1 | mca 2nd shift | not allowed | not assigned |
9 | 2777796 | 2 | mca 2nd shift | not allowed | not assigned |
In [2]:
df['room assignment'].value_counts()
Out[2]:
not assigned 552 Medium room 531 Small room 437 Auditorium 383 Large room 97 Name: room assignment, dtype: int64
In [3]:
# aud
a = df['room assignment'] == 'Auditorium'
aud = df.loc[a,'course name'].value_counts()
aud_name= list(aud.index)
aud_name_sort = sorted(aud_name)
aud_idx = df.loc[a,'room assignment'].index
aud_idx
Out[3]:
Int64Index([ 0, 1, 2, 3, 35, 36, 37, 42, 43, 44, ... 1857, 1858, 1859, 1860, 1861, 1862, 1863, 1914, 1915, 1916], dtype='int64', length=383)
In [4]:
cnt = 0
for name in aud_name_sort:
cnt += 1
df.loc[a & (df['course name'] == name),'room assignment'] = 'Auditorium-' + str(cnt)
df.head()
Out[4]:
id | year | course name | status | room assignment | |
---|---|---|---|---|---|
0 | 2777729 | 1 | science | allowed | Auditorium-3 |
1 | 2777730 | 2 | science | allowed | Auditorium-3 |
2 | 2777765 | 1 | arts | allowed | Auditorium-1 |
3 | 2777766 | 2 | arts | allowed | Auditorium-1 |
4 | 2777785 | 1 | mba | allowed | Small room |
In [5]:
# large
l = df['room assignment'] == 'Large room'
lar = df.loc[l,'course name'].value_counts()
lar_name= list(lar.index)
lar_name_sort = sorted(lar_name)
lar_idx = df.loc[l,'room assignment'].index
cnt = 0
for name in lar_name_sort:
cnt += 1
df.loc[l & (df['course name'] == name),'room assignment'] = 'Large room-' + str(cnt)
df.head(15)
Out[5]:
id | year | course name | status | room assignment | |
---|---|---|---|---|---|
0 | 2777729 | 1 | science | allowed | Auditorium-3 |
1 | 2777730 | 2 | science | allowed | Auditorium-3 |
2 | 2777765 | 1 | arts | allowed | Auditorium-1 |
3 | 2777766 | 2 | arts | allowed | Auditorium-1 |
4 | 2777785 | 1 | mba | allowed | Small room |
5 | 2777786 | 2 | mba | allowed | Small room |
6 | 2777793 | 1 | mba 2nd shift | not allowed | not assigned |
7 | 2777794 | 2 | mba 2nd shift | not allowed | not assigned |
8 | 2777795 | 1 | mca 2nd shift | not allowed | not assigned |
9 | 2777796 | 2 | mca 2nd shift | not allowed | not assigned |
10 | 2777797 | 3 | mca 2nd shift | not allowed | not assigned |
11 | 2777798 | 1 | bjmc | not allowed | not assigned |
12 | 2777799 | 2 | bjmc | not allowed | not assigned |
13 | 2777800 | 3 | bjmc | not allowed | not assigned |
14 | 2777814 | 1 | history | allowed | Medium room |
In [6]:
# Medium room
m = df['room assignment'] == 'Medium room'
mi = df.loc[m,'course name'].value_counts()
mi_name= list(mi.index)
mi_name_sort = sorted(mi_name)
mi_idx = df.loc[m,'room assignment'].index
cnt = 0
for name in mi_name_sort:
cnt += 1
df.loc[m & (df['course name'] == name),'room assignment'] = 'Medium room-' + str(cnt)
df.head(15)
Out[6]:
id | year | course name | status | room assignment | |
---|---|---|---|---|---|
0 | 2777729 | 1 | science | allowed | Auditorium-3 |
1 | 2777730 | 2 | science | allowed | Auditorium-3 |
2 | 2777765 | 1 | arts | allowed | Auditorium-1 |
3 | 2777766 | 2 | arts | allowed | Auditorium-1 |
4 | 2777785 | 1 | mba | allowed | Small room |
5 | 2777786 | 2 | mba | allowed | Small room |
6 | 2777793 | 1 | mba 2nd shift | not allowed | not assigned |
7 | 2777794 | 2 | mba 2nd shift | not allowed | not assigned |
8 | 2777795 | 1 | mca 2nd shift | not allowed | not assigned |
9 | 2777796 | 2 | mca 2nd shift | not allowed | not assigned |
10 | 2777797 | 3 | mca 2nd shift | not allowed | not assigned |
11 | 2777798 | 1 | bjmc | not allowed | not assigned |
12 | 2777799 | 2 | bjmc | not allowed | not assigned |
13 | 2777800 | 3 | bjmc | not allowed | not assigned |
14 | 2777814 | 1 | history | allowed | Medium room-15 |
In [7]:
# Small room
s = df['room assignment'] == 'Small room'
sm = df.loc[s,'course name'].value_counts()
sm_name= list(sm.index)
sm_name_sort = sorted(sm_name)
sm_idx = df.loc[s,'room assignment'].index
cnt = 0
for name in sm_name_sort:
cnt += 1
df.loc[s & (df['course name'] == name),'room assignment'] = 'Small room-' + str(cnt)
df.head(15)
Out[7]:
id | year | course name | status | room assignment | |
---|---|---|---|---|---|
0 | 2777729 | 1 | science | allowed | Auditorium-3 |
1 | 2777730 | 2 | science | allowed | Auditorium-3 |
2 | 2777765 | 1 | arts | allowed | Auditorium-1 |
3 | 2777766 | 2 | arts | allowed | Auditorium-1 |
4 | 2777785 | 1 | mba | allowed | Small room-34 |
5 | 2777786 | 2 | mba | allowed | Small room-34 |
6 | 2777793 | 1 | mba 2nd shift | not allowed | not assigned |
7 | 2777794 | 2 | mba 2nd shift | not allowed | not assigned |
8 | 2777795 | 1 | mca 2nd shift | not allowed | not assigned |
9 | 2777796 | 2 | mca 2nd shift | not allowed | not assigned |
10 | 2777797 | 3 | mca 2nd shift | not allowed | not assigned |
11 | 2777798 | 1 | bjmc | not allowed | not assigned |
12 | 2777799 | 2 | bjmc | not allowed | not assigned |
13 | 2777800 | 3 | bjmc | not allowed | not assigned |
14 | 2777814 | 1 | history | allowed | Medium room-15 |
In [8]:
df.rename(columns={'room assignment':'room number'}, inplace=True)
df.head(5)
Out[8]:
id | year | course name | status | room number | |
---|---|---|---|---|---|
0 | 2777729 | 1 | science | allowed | Auditorium-3 |
1 | 2777730 | 2 | science | allowed | Auditorium-3 |
2 | 2777765 | 1 | arts | allowed | Auditorium-1 |
3 | 2777766 | 2 | arts | allowed | Auditorium-1 |
4 | 2777785 | 1 | mba | allowed | Small room-34 |
코드잇 답안
In [9]:
import pandas as pd
df = pd.read_csv('./enrolment_3.csv')
# allowed
allowed = df['status'] == 'allowed'
# aud
a = df['room assignment'] == 'Auditorium'
aud = df.loc[a,'course name'].value_counts()
aud_name= list(aud.index)
aud_name_sort = sorted(aud_name)
aud_idx = df.loc[a,'room assignment'].index
cnt = 0
for name in aud_name_sort:
cnt += 1
df.loc[a & (df['course name'] == name) & allowed,'room assignment'] = 'Auditorium-' + str(cnt)
# large
l = df['room assignment'] == 'Large room'
lar = df.loc[l,'course name'].value_counts()
lar_name= list(lar.index)
lar_name_sort = sorted(lar_name)
lar_idx = df.loc[l,'room assignment'].index
cnt = 0
for name in lar_name_sort:
cnt += 1
df.loc[l & (df['course name'] == name) & allowed,'room assignment'] = 'Large room-' + str(cnt)
# Medium room
m = df['room assignment'] == 'Medium room'
mi = df.loc[m,'course name'].value_counts()
mi_name= list(mi.index)
mi_name_sort = sorted(mi_name)
mi_idx = df.loc[m,'room assignment'].index
cnt = 0
for name in mi_name_sort:
cnt += 1
df.loc[m & (df['course name'] == name) & allowed,'room assignment'] = 'Medium room-' + str(cnt)
# Small room
s = df['room assignment'] == 'Small room'
sm = df.loc[s,'course name'].value_counts()
sm_name= list(sm.index)
sm_name_sort = sorted(sm_name)
sm_idx = df.loc[s,'room assignment'].index
cnt = 0
for name in sm_name_sort:
cnt += 1
df.loc[s & (df['course name'] == name) & allowed,'room assignment'] = 'Small room-' + str(cnt)
# not allowed 과목에 대해 값 지정해주기
not_allowed = df['status'] == 'not_allowed'
df.loc[not_allowed, "room assignment"] = "not assigned"
df.rename(columns={'room assignment':'room number'}, inplace=True)
df
Out[9]:
id | year | course name | status | room number | |
---|---|---|---|---|---|
0 | 2777729 | 1 | science | allowed | Auditorium-3 |
1 | 2777730 | 2 | science | allowed | Auditorium-3 |
2 | 2777765 | 1 | arts | allowed | Auditorium-1 |
3 | 2777766 | 2 | arts | allowed | Auditorium-1 |
4 | 2777785 | 1 | mba | allowed | Small room-34 |
... | ... | ... | ... | ... | ... |
1995 | 2796805 | 3 | computer application | allowed | Medium room-7 |
1996 | 2796812 | 1 | nursing | allowed | Medium room-22 |
1997 | 2796813 | 2 | nursing | allowed | Medium room-22 |
1998 | 2796814 | 3 | nursing | allowed | Medium room-22 |
1999 | 2796815 | 4 | nursing | allowed | Medium room-22 |
2000 rows × 5 columns
In [10]:
df.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 2000 entries, 0 to 1999 Data columns (total 5 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 id 2000 non-null int64 1 year 2000 non-null int64 2 course name 2000 non-null object 3 status 2000 non-null object 4 room number 2000 non-null object dtypes: int64(2), object(3) memory usage: 78.2+ KB
In [11]:
df['room number'].value_counts()
Out[11]:
not assigned 552 Auditorium-1 158 Auditorium-3 124 Auditorium-2 101 Large room-2 56 ... Small room-30 5 Small room-35 5 Small room-41 5 Small room-15 5 Small room-6 5 Name: room number, Length: 83, dtype: int64
In [12]:
import pandas as pd
df = pd.read_csv('./enrolment_3.csv')
# 과목별 인원 가져오기
allowed = df["status"] == "allowed"
course_counts = df.loc[allowed, "course name"].value_counts()
# 각 강의실 규모에 해당되는 과목 리스트 만들기
auditorium_list = list(course_counts[course_counts >= 80].index)
large_room_list = list(course_counts[(80 > course_counts) & (course_counts >= 40)].index)
medium_room_list = list(course_counts[(40 > course_counts) & (course_counts >= 15)].index)
small_room_list = list(course_counts[(15 > course_counts) & (course_counts > 4)].index)
# 강의실 이름 붙이기
for i in range(len(auditorium_list)):
df.loc[(df["course name"] == sorted(auditorium_list)[i]) & allowed, "room assignment"] = "Auditorium-" + str(i + 1)
for i in range(len(large_room_list)):
df.loc[(df["course name"] == sorted(large_room_list)[i]) & allowed, "room assignment"] = "Large-" + str(i + 1)
for i in range(len(medium_room_list)):
df.loc[(df["course name"] == sorted(medium_room_list)[i]) & allowed, "room assignment"] = "Medium-" + str(i + 1)
for i in range(len(small_room_list)):
df.loc[(df["course name"] == sorted(small_room_list)[i]) & allowed, "room assignment"] = "Small-" + str(i + 1)
# column 이름 바꾸기
df.rename(columns={"room assignment": "room number"}, inplace = True)
# 정답 출력
df['room number'].value_counts()
Out[12]:
not assigned 552 Auditorium-1 158 Auditorium-3 124 Auditorium-2 101 Large-2 56 ... Small-30 5 Small-15 5 Small-35 5 Small-41 5 Small-42 5 Name: room number, Length: 83, dtype: int64
In [ ]: