programming

프로그래밍 퀴즈 화학 실험 모니터링 파이썬 풀이


by Kitle · 2020. 07. 12.



오늘은 프로그래밍 퀴즈를 풀어 보겠습니다.

edX의 C Programming: Language Foundations 코스에 나오는 문제라고 합니다.

University chemists have developed a new process for the manufacturing of a drug that heals wounds extremely quickly. The manufacturing process is very lengthy and requires monitoring the chemicals at all times, sometimes for hours! Entrusting this task to a student is not possible; students tend to fall asleep or not pay close attention after a while. Therefore you need to program an automatic device to monitor the manufacturing of the drug. The device measures the temperature every 15 seconds and provides these measurement to your program.


Your program should first read two integers representing the minimum and maximum safe temperatures. Next, your program should continuously read temperatures (integers) that are being provided by the device. Once the chemical reaction is complete the device will send a value of -999, indicating to you that temperature readins are done. For each recorded temperature that is in the correct range (it could also be equal to the min or max values), your program should display the text "Nothing to report". But as soon as a temperature reaches an unsafe level your program must display the text "Alert!" and stop reading temperatures (although the device may continue sending temperature values).

영어 해석이 필요하신 분은 여기를 참고해주세요 : https://wikidocs.net/85725


Examples

Input:

10 20

15 10 20 0 15 -999


Output:

Nothing to report

Nothing to report

Nothing to report

Alert!


Input:

0 100

15 50 75 -999


Output:

Nothing to report

Nothing to report

Nothing to report


결국 최대 최소를 입력(A)받고 그다음 데이터 리스트를 입력(B) 받아 B의 각 아이템들이 A의 범위에 포함하는지에 따라 분기처리 하면 되겠습니다. 그리고 종료 조건에 맞으면 종료시키고 종료하면 되겠죠?

총 출력 조건으로 2가지 케이스가 있고, Nothing to report / Alert! 종료처리 케이스 Done이 있습니다.  Done은 따로 출력문 없이 종료입니다.

풀이 보겠습니다.

user_input_min_max = input('input min max: ')
safe_min, safe_max = user_input_min_max.split(' ')
user_input_data_list = input('input data list: ')
input_data_list = user_input_data_list.split(' ')
print(input_data_list)

for data in input_data_list:
if data == '-999':
break
elif int(safe_min) <= int(data) <= int(safe_max):
print('Nothing to report')
else:
print('Alert!')
break


user_input_min_max = input('input min max: ')

safe_min, safe_max = user_input_min_max.split(' ')

1-2번째 라인은 안전 온도의 최소, 최대값을 입력 받습니다. 입력이 한번에 '10 20' 이렇게 들어오므로

각각 다른 변수로 분리해줄 필요가 있습니다. 리스트로 받아서 list[0], list[1] 등으로 처리할 수 있고 for 문으로 각 아이템을 지정할수도있지만  갯수만 맞는다면 여러개의 변수이름으로 한번에 받을 수도 있어 여기서는 바로 넣습니다. 물론 갯수가 안맞으면 오류가 납니다. 입력이 올바르다고 가정하겠습니다. 데이터는 .sprit(' ') 사이 공백 기준으로 나눕니다.

user_input_data_list = input('input data list: ')

input_data_list = user_input_data_list.split(' ')

두번째는 데이터들을 받는 것입니다. 주루루룩 순서대로 검토 하므로 list 자료구조가 맞습니다. 역시 데이터는 .sprit(' ') 사이 공백 기준으로 나눕니다. 나눈것들은 자동으로 리스트가 되며 원하는 이름을 붙여 줍니다.

for data in input_data_list:
#for 문을 통해 데이터 리스트의 데이터들을 하나씩 조건에 맞는지 체크해보겠습니다.
    if data == '-999':
      # split 한 결과 아이템들은 str 입니다. 그래서 원문 그대로 '-999' 로 비교했습니다.
        break
    elif int(safe_min) <= int(data) <= int(safe_max):
      #파이썬의 경우   0 < var < 10 이런식의 연산이 가능합니다. 하지만 int로 형변환이 필요합니다. 형변환하여 범위 비교를 해줍니다 
        print('Nothing to report')
    else:
        print('Alert!')
        break
# 범위에 맞지 않으면 당연히 경고해주고 끝내면 되겠죠?

파이썬은 이렇게 사용자 입력을 쪼개고 문자열 처리에 굉장히 편하게 되어있습니다. 큰 어려움 없이 잘 돌아가네요.
더 좋고 효율적인 방법도 많을 겁니다. 이것이 정답은 아니니 스터디 위주로만 활용하세요. :)