포트: 정책 성적표
Coddy의 Luau 개발자를 위한 Swift 코스 레슨. 48개 중 48번째.
챌린지
중급Luau 레퍼런스를 이식하세요. accepts(_ score: Int) -> Bool을 요구하는 ScoreRule과, 점수가 minimum 이상인 경우를 수락하는 PassRule(minimum:)을 구현하세요. mutating record(_ name: String, score: Int) -> Bool 및 report(using rule: any ScoreRule) -> String을 가진 값 타입 GradeBook()을 구현하세요. 0부터 100까지의 점수를 수락하고, 동일한 이름에 대한 이전 점수를 덮어씁니다. 다른 점수는 변경 없이 거부합니다. 전달된 규칙에 의해 수락된 항목만 이름순으로 정렬하여 공백으로 구분된 name:score 형식으로 보고하세요. 사용자 지정 규칙과 독립적인 복사본이 제대로 작동해야 합니다. 입력은 0부터 100까지의 minimum 뒤에 0부터 30개의 이름/점수 쌍이 따릅니다. 이름은 최대 10자의 소문자 ASCII 단어이고, 점수는 -10부터 110까지입니다. 수락된 record 연산 횟수를 출력하고, 보고서가 비어 있지 않은 경우에만 공백 뒤에 보고서를 출력하세요.
필요한 API와 solve(_ input: String) -> String을 구현하세요. 마지막 줄 바꿈 없이 출력 텍스트를 반환하며, 편집 가능한 코드에서 출력을 수행하지 마세요. 잠긴 드라이버 코드가 추가적인 유효한 입력으로 API를 독립적으로 검사한 후, 반환된 텍스트를 정확히 하나의 줄 바꿈과 함께 출력합니다. 모든 숫자 연산은 Int 범위 내에 유지됩니다.
Luau 레퍼런스는 범위가 제한된 정수 값과 ASCII 이름을 사용합니다. 이와 협력하는 객체들은 호출 가능한 메서드를 가진 테이블입니다. Swift 이식에서는 프로토콜 준수를 명시적으로 선언하고 요구되는 파일 레이아웃을 유지하세요. Luau 테이블 할당은 성적표를 참조 공유(alias)하며, 명시적인 copy 메서드는 해당 점수 테이블을 복사합니다. 반면에 요구되는 Swift 구조체는 일반적인 독립적 값 복사를 지원해야 합니다.
동일한 입력 및 출력 계약에 대한 전체 Luau 레퍼런스:
local function pass_rule(minimum)
return {accepts = function(_, score) return score >= minimum end}
end
local function new_grade_book()
local scores, book = {}, {}
function book:record(name, score)
if score < 0 or score > 100 then return false end
scores[name] = score
return true
end
function book:copy()
local other = new_grade_book()
for name, score in pairs(scores) do other:record(name, score) end
return other
end
function book:report(rule)
local names, parts = {}, {}
for name in pairs(scores) do names[#names + 1] = name end
table.sort(names)
for _, name in ipairs(names) do
if rule:accepts(scores[name]) then
parts[#parts + 1] = name .. ":" .. scores[name]
end
end
return table.concat(parts, " ")
end
return book
end
local function solve(input)
local tokens = {}
for token in input:gmatch("%S+") do tokens[#tokens + 1] = token end
local rule, book = pass_rule(tonumber(tokens[1])), new_grade_book()
local accepted = 0
for i = 2, #tokens, 2 do
if book:record(tokens[i], tonumber(tokens[i + 1])) then accepted = accepted + 1 end
end
local report = book:report(rule)
return tostring(accepted) .. (report == "" and "" or " " .. report)
end
print(solve(io.read("*a")))작성한 코드는 ScoreRule.swift, PassRule.swift, GradeBook.swift, Functions.swift에 들어갑니다. main.swift에는 제공된 입력/출력 코드가 포함되어 있으며 편집할 수 없습니다.
직접 해보기
import Foundation
func apiChecks(_ input: String) -> Bool {
struct ProbeRule: ScoreRule { func accepts(_ score: Int) -> Bool { score % 2 == 0 } }
var b = GradeBook(); guard b.record("a", score: 40) && b.record("b", score: 71) && !b.record("a", score: 101) else { return false }; var copy = b; _ = copy.record("a", score: 88); return b.report(using: ProbeRule()) == "a:40" && copy.report(using: ProbeRule()) == "a:88" && b.report(using: PassRule(minimum: 40)) == "a:40 b:71" && PassRule(minimum: 50).accepts(50)
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// 공개 API 프로브 시작
let apiOK = apiChecks(input)
// 공개 API 프로브 종료
if apiOK { print(solve(input)) } else { print("API mismatch") }
Luau 개발자를 위한 Swift의 모든 레슨
직접 연습해 보세요: Swift 플레이그라운드