Tous les défis
Facile 0 résolus 0 % de réussite

Interstate Highway Numbers

Classify an interstate highway number as a primary or auxiliary route and give its direction.

Ce que vous allez apprendre

  • Use % to find the last two digits and parity
  • Separate valid from invalid inputs
  • Build result strings from parts

Problème

Afficher la traduction

US interstate numbers follow a pattern. Numbers 1 to 99 are primary routes: odd numbers run north-south and even numbers run east-west. Three-digit numbers are auxiliary routes (loops and spurs) that serve the primary route given by their last two digits, so I-405 serves I-5 and I-290 serves I-90. An auxiliary route runs in the same direction as the route it serves. A three-digit number ending in 00 does not serve any route and is not valid, and neither is 0 or anything above 999.

Implement interstate_highway_numbers(number).

Input

number: The highway number.

Output

Return one of these strings, where D is north-south or east-west and P is the served primary route:

  • "Primary route, runs D" for 1 to 99
  • "Auxiliary route of I-P, runs D" for valid numbers from 101 to 999
  • "Invalid" for everything else

Contraintes

−10,000 ≤ number ≤ 10,000

Exemples

Exemple 1
Entrée :
number = 95

Sortie attendue :
expected = "Primary route, runs north-south"
Entrée :
long long number = 95;

Sortie attendue :
std::string expected = "Primary route, runs north-south";
Entrée :
long number = 95;

Sortie attendue :
string expected = "Primary route, runs north-south";

Explication

95 is odd and below 100, so the answer is "Primary route, runs north-south".

Exemple 2
Entrée :
number = 290

Sortie attendue :
expected = "Auxiliary route of I-90, runs east-west"
Entrée :
long long number = 290;

Sortie attendue :
std::string expected = "Auxiliary route of I-90, runs east-west";
Entrée :
long number = 290;

Sortie attendue :
string expected = "Auxiliary route of I-90, runs east-west";

Explication

The last two digits are 90, an even primary route, so the answer is "Auxiliary route of I-90, runs east-west".

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

Rule out 0, negatives, numbers above 999 and multiples of 100 first.

Indice 2

number % 100 gives the primary route a three-digit number serves.

Indice 3

The direction depends on whether that primary route number is odd or even.

Complexité attendue

O(1) time, O(1) extra space

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

Aucune solution validée pour l'instant. Résolvez-le et prenez la première place.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.