{ }codelper
else-if chainP0315

Kite-flying notice board

A kite-flying club looks at each day's wind speed w, chance of rain p and temperature t, and writes one word on its notice board. The first line gives the number of days n, and each of the next n lines gives w, p and t for one day in this order, separated by single spaces. Check the rules below from the top in order and write only the word of the first rule that matches. Rule 1: if p is at least 70, the word is "rain". Rule 2: if w is at least 12, the word is "storm". Rule 3: if w is below 3, the word is "calm". Rule 4: if t is below 0, the word is "cold". Rule 5: if w is at least 8, the word is "stunt". Rule 6: if none of the above matches, the word is "kite". For each day, in the given order, print the word for the notice board on its own line.

Input
1 ≤ n ≤ 1000, 0 ≤ w ≤ 40, 0 ≤ p ≤ 100, −30 ≤ t ≤ 45 (all integers)
Output
n lines.
Each line is one of rain, storm, calm, cold, stunt, kite
Example 1 · stdin
7
3 80 10
15 80 20
15 10 20
2 10 -5
9 10 -5
9 10 5
5 69 0
stdout
rain
rain
storm
calm
cold
stunt
kite
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

A chain that picks exactly one of several branches is written if … elif … else in Python, if … else if … else in C, C++, Java, C#, JS, TS and Craykon.
Only Python has the separate word elif; the others simply put if after else.
A chain asks from the top, runs only the first branch that matches and skips the rest.
The first trap is the order of the questions.
In this problem the conditions overlap, so the order is the rule.
Asking about wind first turns 15 80 20 into storm instead of rain.
Asking stunt before cold, to keep the wind conditions together, turns 9 10 -5 into stunt instead of cold.
The second trap is writing separate ifs without else.
Every matching condition prints, so 15 80 20 gives three lines, rain, storm and stunt, and the final else belongs only to the if just before it, so 3 80 10 prints rain and then kite.
Writing just one of the middle ones as if instead of else if cuts the chain in two there.
Do that to the second condition and 15 80 20 prints two lines, rain and storm.
If you overwrite a variable instead of printing, the last match wins and 2 10 -5 becomes cold instead of calm.
The third trap is the boundaries.
'At least' is >= and 'below' is <.
p > 70 gets a day with rain chance exactly 70 wrong, w <= 3 a day with wind exactly 3, and t <= 0 a day with temperature exactly 0.
5 69 0 in the example is kite, but with t <= 0 it becomes cold.
Earlier branches have already filtered, so later conditions need not repeat them as in w >= 3 && w < 12.

Pythonmain.py · 16 lines
n = int(input())for _ in range(n):    w, p, t = map(int, input().split())    # 위에서부터 차례로 묻는다. 먼저 맞은 하나만 고른다    if p >= 70:        print("rain")    elif w >= 12:        print("storm")    elif w < 3:        print("calm")    elif t < 0:        print("cold")    elif w >= 8:        print("stunt")    else:        print("kite")
JavaScriptmain.js · 22 lines
const [n, ...v] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const out = [];for (let i = 0; i < n; i++) {  const w = v[3 * i];  const p = v[3 * i + 1];  const t = v[3 * i + 2];  // 위에서부터 차례로 묻는다. 먼저 맞은 하나만 고른다  if (p >= 70) {    out.push("rain");  } else if (w >= 12) {    out.push("storm");  } else if (w < 3) {    out.push("calm");  } else if (t < 0) {    out.push("cold");  } else if (w >= 8) {    out.push("stunt");  } else {    out.push("kite");  }}console.log(out.join("\n"));
TypeScriptmain.ts · 23 lines
import { readFileSync } from "node:fs";const [n, ...v]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const out: string[] = [];for (let i = 0; i < n; i++) {  const w: number = v[3 * i];  const p: number = v[3 * i + 1];  const t: number = v[3 * i + 2];  // 위에서부터 차례로 묻는다. 먼저 맞은 하나만 고른다  if (p >= 70) {    out.push("rain");  } else if (w >= 12) {    out.push("storm");  } else if (w < 3) {    out.push("calm");  } else if (t < 0) {    out.push("cold");  } else if (w >= 8) {    out.push("stunt");  } else {    out.push("kite");  }}console.log(out.join("\n"));
JavaMain.java · 30 lines
import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int n = sc.nextInt();        StringBuilder out = new StringBuilder();        for (int i = 0; i < n; i++) {            int w = sc.nextInt();            int p = sc.nextInt();            int t = sc.nextInt();            // 위에서부터 차례로 묻는다. 먼저 맞은 하나만 고른다            if (p >= 70) {                out.append("rain");            } else if (w >= 12) {                out.append("storm");            } else if (w < 3) {                out.append("calm");            } else if (t < 0) {                out.append("cold");            } else if (w >= 8) {                out.append("stunt");            } else {                out.append("kite");            }            out.append('\n');        }        System.out.print(out);    }}
C#Main.cs · 45 lines
using System;using System.Text; class Program{    static void Main()    {        int n = int.Parse(Console.ReadLine());        var sb = new StringBuilder();        for (int i = 0; i < n; i++)        {            string[] s = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);            int w = int.Parse(s[0]);            int p = int.Parse(s[1]);            int t = int.Parse(s[2]);            // 위에서부터 차례로 묻는다. 먼저 맞은 하나만 고른다            if (p >= 70)            {                sb.Append("rain");            }            else if (w >= 12)            {                sb.Append("storm");            }            else if (w < 3)            {                sb.Append("calm");            }            else if (t < 0)            {                sb.Append("cold");            }            else if (w >= 8)            {                sb.Append("stunt");            }            else            {                sb.Append("kite");            }            sb.Append('\n');        }        Console.Write(sb);    }}
C++main.cpp · 24 lines
#include <iostream> int main() {    int n;    std::cin >> n;    for (int i = 0; i < n; i++) {        int w, p, t;        std::cin >> w >> p >> t;        // 위에서부터 차례로 묻는다. 먼저 맞은 하나만 고른다        if (p >= 70) {            std::cout << "rain" << "\n";        } else if (w >= 12) {            std::cout << "storm" << "\n";        } else if (w < 3) {            std::cout << "calm" << "\n";        } else if (t < 0) {            std::cout << "cold" << "\n";        } else if (w >= 8) {            std::cout << "stunt" << "\n";        } else {            std::cout << "kite" << "\n";        }    }}
Cmain.c · 25 lines
#include <stdio.h> int main(void) {    int n;    scanf("%d", &n);    for (int i = 0; i < n; i++) {        int w, p, t;        scanf("%d %d %d", &w, &p, &t);        /* 위에서부터 차례로 묻는다. 먼저 맞은 하나만 고른다 */        if (p >= 70) {            puts("rain");        } else if (w >= 12) {            puts("storm");        } else if (w < 3) {            puts("calm");        } else if (t < 0) {            puts("cold");        } else if (w >= 8) {            puts("stunt");        } else {            puts("kite");        }    }    return 0;}
Craykonmain.cray · 33 lines
n = Console.ReadLine().ToNumber();for (i = 0; i < n; i++){    날 = Console.ReadNumbers();    w = 날[0];    p = 날[1];    t = 날[2];    // 위에서부터 차례로 묻는다. 먼저 맞은 하나만 고른다    if (p >= 70)    {        Console.WriteLine("rain");    }    else if (w >= 12)    {        Console.WriteLine("storm");    }    else if (w < 3)    {        Console.WriteLine("calm");    }    else if (t < 0)    {        Console.WriteLine("cold");    }    else if (w >= 8)    {        Console.WriteLine("stunt");    }    else    {        Console.WriteLine("kite");    }}