Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Activity 15: Hand-coding, together

Names:

Try to solve this like real exam problem, without referring to notes or tech. This is what the hand-coding section of Friday's exam will feel like (except for the working together part). Take turns writing.

General advice: Start with the function signatures, think about inputs and outputs and the overall logic, then work on the details and syntax as you have time.

Problem 1: What is this card worth

We are scoring a card game. A card reaches us in one of three forms.

#![allow(unused)]
fn main() {
enum Card {
    Face(char),
    Number(u32),
    Joker,
}
}

Write a function value that says what a card is worth. The rules:

  • A Face card is worth 10 for 'J', 'Q', and 'K', and 11 for 'A'.
  • A Number card is worth what it says, as long as it is between 2 and 10 (inclusive).
  • A Joker is worth 0
  • Any other Face or Number card with an invalid char or u32 is worth None

*Remember!! the compiler doesn't know about your card deck, so you have to account for every possible value of Card that is conceivable.

#![allow(unused)]
fn main() {
fn value(c: Card) -> Option<u32> {














}
}

When you are done: somebody adds a fourth variant, Card::Wild. What does the compiler do to your function as is? What would you have to adjust?




Problem 2: Points per game

A player's points from each game they have played are in a Vec. Write a function that gives back their average. There is no sensible average of no games, so complain rather than guess in that case.

#![allow(unused)]
fn main() {
fn points_per_game(points: &Vec<u32>) -> Result<f64, String> {













}
}

Then, when called, print the average if there is one, and print the error message if there is not. Write that logic here in main:

fn main() {
    let points = vec![22, 31, 18, 27, 19];







}

When you are done: check your division. Are you sure you'll get a decimal like 23.4 and not 23?



Solutions

Problem 1

#![allow(unused)]
fn main() {
fn value(c: Card) -> Option<u32> {
    match c {
        Card::Face('J') | Card::Face('Q') | Card::Face('K') => Some(10),
        Card::Face('A') => Some(11),
        Card::Face(_) => None,
        Card::Number(n) if n >= 2 && n <= 10 => Some(n),
        Card::Number(_) => None,
        Card::Joker => None,
    }
}
}

or something like

#![allow(unused)]
fn main() {
fn value(c: Card) -> Option<u32> {
    match c {
        Card::Face('J') => Some(10),
        Card::Face('Q') => Some(10),
        Card::Face('K') => Some(10),
        Card::Face('A') => Some(11),
        Card::Number(n) if n >= 2 && n <= 10 => Some(n),
        _ => None,
    }
}
}

but that has the disadvantage of not noticing if a new card type was added to the enum (and you might want the compiler to fail here to alert you!)

Problem 2

fn points_per_game(points: &Vec<u32>) -> Result<f64, String> {
    if points.len() == 0 {
        return Err("no games played".to_string());
    }
    let mut total = 0;
    for p in points {
        total += p;
    }
    Ok(total as f64 / points.len() as f64)
}

fn main() {
    let points = vec![22, 31, 18, 27, 19];
    match points_per_game(&points) {
        Ok(a) => println!("{a}"),
        Err(e) => println!("{e}"),
    }
}