코딩 트레이닝 정리

코딩 트레이닝
예) 간단한 팁 계산기를 작성하라.
1. 문제를 이해하기
1) 작성하는 문서 : 요구사항 명세서, 문제 정의서 작성
2) 요구사항 명세서 작성할 때 어떤 작업을 할 것인가?
    -> 주어진 문제에서 요구하는 것을 더 명확하게 하기 위해서 어떤 질문을 할 것인지 생각해본다.
   예) 사용할 계산식은 무엇인가? 계산할 팁이 얼마나 되는지 설명할 수 있는가?
         팁 비율은 얼마인가? 고정할 것인가 아니면 사용자가 설정할 수 있도록 할 것인가?
         프로그램을 시작하면 화면에 어떤 내용을 표시하도록 할 것인가?
         출력 값으로 화면에 무엇을 표시하도록 할 것인가?
         팁과 합계를 모두 표시할 것인가, 합계만 표시할 것인가?
         입력 값이 잘못되었을 경우, 즉 숫자가 아닌 문자가 입력이 되었을 때는 종료할 것인가?
         팁 비율의 범위가 존재하는가?
         팁 비율을 계산할 때 어디에서든 동일하게 적용할 것인가? 다르게 적용할 것인가?
         팁 비율을 입력할 때 어떻게 입력할 것인가? 백분율로 입력할 것인가? 소수점으로 입력할 것인가?
3) 문제정의서
     -> 요구 사항 명세서를 토대로 자신이 만들고자 하는 것을 정확하게 설명을 해야한다.
     예) 간단한 팁 계산기를 작성하라. 이 프로그램은 가격과 팁 비율을 입력 받아야한다. 그리고는 팁을 계산하여 팁과 전체 가격을 표시해야한다. 팁의 범위는 15%에서 20%까지 범위를 줄 것이며, 어디에서든 동일하게 입력된 값을 적용해야한다.
2. 입력, 출력, 프로세스 찾아내기
1) 어떤 프로그램이든 입력(Input), 프로세스(Process), 출력(Output)으로 구성된다.
2) 이를 명확하게 하기 위해서는 문제 정의서에서 동사와 명사를 유심히 보면된다.
     -> 명사는 입력과 출력이되고, 동사는 프로세스가 된다.
3) 문제정의서에서 작성한 문장으로 명사와 동사를 나누어 보자.
    (1) 명사 : 가격, 팁 비율, 팁, 전체 가격, 범위
    (2) 동사 : 입력 받아야한다, 계산하여, 표시해야한다, 적용해야한다
3. 테스트를 통해서 디자인 다듬기
1) 소프트웨어를 디자인하고 개발하는 가장 좋은 방법 중 하나는 시작부터 결과가 원하는 대로 나오는지 고민하는 것이다.
-> 테스트 주도 개발(TDD : Test Driven Development)
2) 테스트 계획서를 작성한다. (적어도 4가지 이상으로 하며, 복잡한 프로그램일수록 항목을 늘린다.)
3) 관심을 가질 부분은 입력과 출력에 대해서 원하는대로 나오는지 확인하는 것이다.
    예) 입력 : Bill amount : 10, tip Rate : 15
          출력 : Tip : 1.5, Total : 11.50
4) 테스트 계획서에서는 다양한 방식의 입력에 대해서 출력이 제대로 나오는지 파악한다.
    예) 입력 : Bill amount : 11.25, tip Rate : 15
          출력 : Tip : 1.6875, Total : 12.9375
          -> 출력이 다음과 같을 경우, 화폐단위가 맞지 않아서 올림, 반올림, 버림 중에 어떤 연산을 할 것인지 결정 후에 반영해야 한다.
5) 테스트 계획서와 TDD를 통해서 요구 사항 명세서, 문제정의서에서 나오지 않았던 문제점을 발견해서 반영할 수 있다.
4. 의사코드로 알고리즘 작성하기
1) 알고리즘은 어떤 일을 실행하기 위해 필요한 명령(Operation)세트를 순서대로 나열한 것을 말한다.
2) 알고리즘을 따라 코드를 작성하면, 하나의 컴퓨터 프로그램을 완성하게 된다.
예) 팁 계산기 알고리즘
Tip Calcuator
 Initialize billAmount to 0
 Initialize tip to 0
 Initialize tipRate 0
 Initialize total to 0

 Prompt for billAmount with "What is the bill amount?"
 Prompt for tipRate with "What is the tip rate?"

 convert billAmount to a number
 convert tipRate to a number

 tip = billAmount * (tipRate / 100)
 round tip up to nearest cent
 total = billAmount + tip

 Display "Tip: $" + tip
 Display "Total : $" + total
End

5. 코드 작성하기(코딩 진행)
1) 코드 작성 시에는 위에서 언급한 요구사항 명세서, 문제정의서, 입력, 출력, 프로세스, 테스트 계획서 결과, 의사코드로 작성한 알고리즘을 참고하여 구현시 제약 사항을 정의한다.
2) 제약 사항을 정의하는 것이 어렵다면, 먼저 프로그램을 작성하고 제약 사항을 적용한다.
6. 문제 해결을 위해서 언급한 주제에 대한 의사결정 코드 알고리즘
1) 인사하기
   Initialize input_name to ""
   Initialize greeting to ""
 
   Prompt for input_name with "What is your name?"
 
   Check to input_name on title type.
   Check to input_name on space character.
   Check to input_name consisting alphabet only.
 
   Display "Hello, input_name, nice to meet you!", if input_name consist only alphabet.
END

2) 글자 수 세기
  Initialize input_data to ""
  Initialize output_data to ""

  Prompt for input_data with "What is the input string?"

  Check to input_data consisting alphabet only.

  Display "Input_data(if consisting alphabet only) has input_data's length characters."
END

3) 따옴표 출력
  Initialize input_data to ""
  Initialize speaker to ""

  Prompt for input_data with "What is the quote?"
  Prompt for speaker with "Who said it?"

  Check to input_data not consisting back slash and double quote mark.

  Display "speak says, "input_data.""
END

4) Mad Libs
  Initialize noun to ""
  Initialize verb to ""
  Initialize adjective to ""
  Initialize adverb to ""

  Prompt for noun with "Enter a noun:"
  Prompt for verb with "Enter a verb:"
  Prompt for adjective with "Enter an adjective:"
  Prompt for adverb with "Enter an adverb:"

 Check to noun, verb, adjective and adverb consisting alphabet only.

 Remove to new line character if contain in noun, verb, adjective and adverb.

 Display "Do you verb your adjective noun adverb? That's hilarious!"
END

5) 간단한 수학(덧셈, 뺄셈, 곱셈, 나눗셈)
 Initialize first_num to ""
 Initialize second_num to ""

 Prompt for first_num with "What is first number?"
 Prompt for second_num with "What is second number?"

 Check to first_num and second_num consisting number only or dot between numbers.

 Change first_num and second_num to number data type.

 Display "first_num + second_num = (first_num + second_num)"
 Display "first_num - second_num = (first_num - second_num)"
 Display "first_num * second_num = (first_num * second_num)"
 Display "first_num / second_num = (first_num / second_num)"
END

6) 퇴직 계산기
 Initialize current_age to ""
 Initialize retire_age to ""
 Initialize current_year to 0
 Initialize work_age toe 0

 Prompt for current_age with "What is your current age?"
 Prompt for retire_age with "At what age would you like to retire?"

 Check to current_age and retire_age consisting number only.
 Check to current_age less than 4 lengths

 Change current_age and retire_age to number data type

 Input current_year in system

 Save work_age to (retire_age - current_age)

 Display "You have (retire_age - current_age) years left util you can retire.\n It's current_year, so you can retire in (current_year + (retire_age - current_age)).
END

7) 직사각형 방의 면적
 Initialize length_feet to ""
 Initialize width_feet to ""
 Initialize square_feet to 0
 Initialize square_meter to 0
 Initialize pyungsu to 0
 Initialize feet_to_meter_value to 0.09290304
 Initialize meter_to_pyungsu_value to 0.3025

 Prompt for length_feet with "What is the length of the room in feet?"
 Prompt for width_feet with "What is the width of the room in feet?"

 Check to length_feet and width_feet consisting number only.

 Change length_feet and width_feet to number data type

 Save square_feet to (length_feet * width_feet)
 Save square_meter to (square_feet * feet_to_meter_value)
 Save pyungsu to (square_meter * meter_to_pyungsu_value)

 Round off square_feet, square_meter and pyungsu to the nearest hundredth.

 Display "You entered dimensions of (length_feet) feet by (width_feet) feet\n The area is (square_feet) square feet\n(square_meter) square meters\n (pyungsu) pyungsu."
END

8) 피자 파티
 Initialize people to ""
 Initialize pizzas to ""
 Initialize pieces_of_pizza to ""
 Initialize people_num to 0
 Initialize pizzas_num to 0
 Initialize pieces_of_pizza_num to 0
 Initialize get_pieces_of_pizza_num to 0
 Initialize total_pieces_of_pizza_num to 0

 Prompt for people with "How many people?"
 Prompt for pizzas with "How many pizzas do you have?"
 Prompt for pieces_of_pizza with "How many pieces are in a pizza?"

 Check to people, pizzas and pieces_of_pizza consisting number only
 Check to pieces_of_pizza consisting event number

 Save to people_num to change people to number type
 Save to pizzas_num to change pizzas to number type
 Save to pieces_of_pizza_num to change pieces_of_pizza to number type

 Save to total_pieces_of_pizza_num to (pizzas_num * pieces_of_pizza_num)
 Save to get_pieces_of_pizza_num to (total_pieces_of_pizza_num / people_num) to round down

 Display "(people) people with (pizzas) pizzas.\n Each person gets (get_pieces_of_pizza_num) pieces of pizza.\n There are (total_pieces_of_pizza_num - (get_pieces_of_pizza_num * people_num)) leftover pieces."
END

9) 페인트 계산기
 Initialize length_meter to ""
 Initialize width_meter to ""
 Initialize square_meters to 0
 Initialize paint_count to 0
 Initialize paint_to_cover_square_liter to 9

 Prompt for length_meter with "What is the length of the celling in meter?"
 Prompt for width_meter with "What is the width of the celling in meter?"

 Check to length_meter and width_meter consisting number only.

 Change length_meter and width_meter to number data type

 Save square_meters to (length_meter * width_meter)
 Save paint_count to (square_meters/paint_to_cover_square_liter) to round up

 Display "You will need to purchase (paint_count) liters of paint to cover (square_meters) square meters."
END

10) 셀프 계산대
 Initialize price_list to ""
 Initialize quantity_list to ""
 Initialize total_per_tax to 0.55
 Initialize subtotal to 0
 Initialize tax to 0
 Initialize total to 0

 Prompt for price_list with "Price of item N : " (N = 1, 2, 3)
 Prompt for quantity_list with "Quantity of item N : " (N = 1, 2, 3)

 Check to price_list and quantity_list consisting integer only.

 Change price_list and quantity_list to integer data type

 Save subtotal to sum(price_list[N] * quantity_list[N]) (N = 1, 2, 3)
 Save tax to (subtotal * total_per_tax)
 Save total to (subtotal + tax)

 Display "Subtotal : (subtotal) won\n Tax : (tax) won\n Total : (total) won"
END

11) 환율 변환
 Initialize won_amount to ""
 Initialize won_rate to ""
 Initialize dollar_amount to 0.0

 Prompt for won_amount with "How many Won are you exchanging?"
 Prompt for won_rate with "What is the exchange rate?"

 Check to won_amount consisting integer only.
 Check to won_rate consisting number only.

 Change won_amount and won_rate to number data type

 Save doller_amount to ((won_amount * won_rate)/100)

 Display "(won_amount) won the exchange rate of (won_rate) is (doller_amount) dollers"
END

12) 단리계산
 Initialize Principal_Value to ""
 Initialize Rate_Value to ""
 Initialize Year_Value to ""
 Initialize Principal_and_Rate_Value to 0

 Prompt for Principal_Value with "Enter the principal :"
 Prompt for Rate_Value with "Enter the rate of interest :"
 Prompt for Year_Value with "Enter the number of years :"

 Check to Principal_Value and Year_Value consisting integer only
 Check to Rate_Value consisting number only

 Change Principal_Value, Rate_Value and Year_Value to number data type

 Save Principal_and_Rate_Value to (Principal * (1+(Rate_Value/100) * Year_Value))

 Display "After (Year_Value) years at (Rate_Value)%, the investment will be worth (Principal_and_Rate_Value) won"
 Display "------------------------"
 Display "1 year - (Principal_Value * (1+(Rate_Value/100) * 1)) won"
 Display "2 years - (Principal_Value * (1+(Rate_Value/100) * 2)) won"
 ...
 Display "(Year_Value) years - (Principal_Value * (1+(Rate_Value/100) * (Year_Value))) won"
END

13) 복리계산
 Initialize Principal_Value to ""
 Initialize Rate_Value to ""
 Initialize Year_Value t0 ""
 Initialize NumOfTimes_Per_Year_Value to ""
 Initialize Principal_and_Rate_Value to 0

 Prompt for Principal_Value with "What is the principal amount?"
 Prompt for Rate_Value with "What is the rate?"
 Prompt for Year_Value with "What is the number of years?"
 Prompt for NumOfTimes_Per_Year_Value with "What is the number of times the interest is compounded per year?"

 Check to Principal_Value, Year_Value and NumOfTime_Per_Year_Value consisting integer only
 Check to Rate_Value consisting number only

 Change Principal_Value, Rate_Value, Year_Value and NumOfTimes_Per_Year_Value to number data type

 Save Principal_and_Rate_Value to (Principal * (1+(Rate_Value/100) / NumOfTimes_Per_Year_Value))^(NumOfTimes_Per_Year_Value * Year_Value)

 Display "(Principal_Value) won invested at (Rate_Value) percent for (Year_Value) years compounded (NumOfTimes_Per_Year_Value) time per year is (Principal_and_Rate_Value) won"
 Display "------------------------"
 Display "1 year - (Principal_Value * (1+(Rate_Value/100) / NumOfTimes_Per_Year_Value))^(NumOfTimes_Per_Year_Value * 1) won"
 Display "2 years - (Principal_Value * (1+(Rate_Value/100) / NumOfTimes_Per_Year_Value))^(NumOfTimes_Per_Year_Value * 2) won"
 ...
 Display "(Year_Value) years - (Principal_Value * (1+(Rate_Value/100) / NumOfTimes_Per_Year_Value))^(NumOfTimes_Per_Year_Value * Year_Value) won"
END

14) 세금 계산기
 Initialize Order_Amount to ""
 Initialize State to ""
 Initialize Sum_Amount_Tax to 0.0
 Initialize Tax_Value to 0.055

 Prompt for Order_Amount with "What is the order amount?"
 Prompt for State with "What is the state?"

 Check to Order_Amount consisting number only
 Check to Sate excepting spacial characters

 Change Order_Amount to number data type
 Save to Sum_Amount_Tax to (Order_Amount * (1+(Tax_Value/100)))

 out_put = "The total is (Sum_Amount_Tax) won"
 IF State is "WI" than
     out_put = "The subtotal is (Order_Amount) won \n The tax is (Order_Amount*Tax_Value) won \n The total is (Sum_Amount_Tax) won"

 Display out_put
END

15) 암호 검증
 Initialize User_ID to ""
 Initialize User_PW to ""
 Initialize User_PW_CHK to ""
 Initialize User_Info to HashTable

 Prompt to User_ID with "Input your ID : "
 Prompt to User_PW with "Input your password : "
 Prompt to User_PW_CHK with "Check your password : "

 IF User_PW is equal to User_PW_CHK than
     Save to User_Info.key is User_ID and User_Info.value is User_PW
     Display "Success to register your Info"
 ELSE
    Display "Check your password"
    Try again Input Password

 Display "--------------------"
 Prompt to User_ID with "What is your ID?"
 IF User_Info.keys fall within User_ID than
    Pass
 ELSE
   Try again input User_ID

 Prompt to User_PW with "What is your password?"
 IF User_Info.key's value fall within User_PW than
     output = "Welcome!"
 ELSE
    output = "That password is incorrect."

 Display output
END

16) 합법적으로 운전 가능한 연령
 Initialize Birth_Date to ""
 Initialize Count_Age_In_Full to 0
 Initialize Possible_Drive_Age to 18

 Prompt to Biirth_Date with "What is your birth date?"

 Check to Birth_Date consisting number only
 Check to Birth_Date length equal 8

 Save Count_Age_In_Full to (Today - Birth_Date)

 IF Count_Age_In_Full >= Possible_Drive_Age than
     output = "You are old enough to legally drive."
 ELSE
    output = "You are not old enough to legally drive."

 Display output
END

17) 혈중 알코올 농도 계산기
 Initialize Weight  to ""
 Initialize Gender to ""
 Initialize Percent_Alcohol to ""
 Initialize Cup_Count to ""
 Initialize Drink_Over_Time to ""

 Prompt to Weight with "What is your weight?"
 Prompt to Gender with "What is your gender?"
 Prompt to Percent_Alcohol with "How many alcohol are there?"
 Prompt to Cup_Count with "How many cups are there?"
 Prompt to Drink_Over_Time with "How long has it been?"

 Check to WeightPercent_Alcohol, Cup_Count, Drink_Over_Time consisting number only
 Check to Gender has "M" or "W"

 Save BAC to (Percent_Alcohol * Cup_Count * 0.7984) / Weight * (0.73 or 0.6) - 0.015 * Drink_Over_Time

IF BAC >= 0.03 AND BAC < 0.08 than
    output = "You can't drive because of your suspension of license"
IF BAC >= 0.08 than
    output = "You can't drive because of revocation of your license"
ELSE
     output = "You are pass but be careful"

 Display output
END
출처 : 코딩 트레이닝 (출판사 : 프로그래밍 인사이트)