Menu
CoddyTech

영수증 상태 복습

Coddy의 Ruby 개발자를 위한 Rust 코스 레슨. 48개 중 35번째.

challenge icon

챌린지

쉬움

공개 label: String, unit: i32, count: i32 필드를 가진 line::Line을 구현하세요. receipt::Receipt::new() -> Receipt, add(&mut self, line: Line) -> bool, total(&self) -> i32, labels(self) -> Vec<String>를 구현하세요. 두 숫자 필드가 모두 음수가 아닐 때만 항목을 받아들이세요. 거부된 경우 기존 상태는 변경되지 않아야 합니다. 받아들인 항목은 unit * count만큼 합계에 기여하며, 레이블은 삽입 순서와 중복을 유지합니다. labels는 영수증을 소비합니다. solve는 공백으로 구분된 label unit count 세 항목으로 이루어진 입력을 전부 읽습니다. 정수는 -100부터 100까지이며, 세 항목 묶음은 최대 100개입니다. 받아들인 항목 수와 합계를 accepted:n total:n 형식으로 반환하고, 그 뒤에 줄바꿈과 쉼표로 구분된 받아들인 레이블을 반환하거나, 해당 레이블이 없으면 empty를 반환하세요.

필요한 공개 API를 구현하고 exercise::solve(input: &str) -> String의 결과를 마지막 줄바꿈 없이 반환하세요. 편집 가능한 코드에서 출력하지 마세요. 잠금 처리된 드라이버는 필요한 API를 각각 확인하고, 표준 입력 전체를 읽은 다음 반환된 텍스트를 마지막 줄바꿈 하나와 함께 출력합니다.

코드는 exercise.rs, line.rs, receipt.rs에 작성하세요. main.rs에는 제공된 입력/출력 코드가 있으며 편집할 수 없습니다.

직접 해보기

#![allow(dead_code, unused_imports, unused_variables, unused_mut)]
mod exercise;
mod line;
mod receipt;
use std::io::{self, Read};
fn main() {
    let mut input=String::new();
    io::stdin().read_to_string(&mut input).unwrap();
    // PUBLIC API 검증 시작
let mut r = receipt::Receipt::new(); assert_eq!(r.total(), 0);
assert!(r.add(line::Line { label: "ink".to_string(), unit: 13, count: 7 }));
assert!(!r.add(line::Line { label: "bad".to_string(), unit: -1, count: 3 }));
assert!(!r.add(line::Line { label: "bad2".to_string(), unit: 2, count: -3 }));
assert_eq!(r.total(), 91);
assert!(r.add(line::Line { label: "ink".to_string(), unit: 0, count: 5 }));
assert_eq!(r.total(), 91); assert_eq!(r.labels(), vec!["ink".to_string(), "ink".to_string()]);
assert!(receipt::Receipt::new().labels().is_empty());
    // PUBLIC API 검증 종료
    println!("{}", exercise::solve(&input));
}

Ruby 개발자를 위한 Rust의 모든 레슨

직접 연습해 보세요: 온라인 Rust 컴파일러