Tous les défis
Facile 1 résolu 100.0 % de réussite

Every Other Character

Keep the characters at even positions.

Ce que vous allez apprendre

  • Pick characters at even positions
  • Loop with a step of 2 or use slicing
  • Build a new string

Problème

Afficher la traduction

Build a new string from the characters of text at positions 0, 2, 4, … (every other character, starting with the first).

Implement every_other_character(text).

Input

text: The original text.

Output

Return the characters at even positions, in order.

Contraintes

0 ≤ length of text ≤ 10,000

Exemples

Exemple 1
Entrée :
text = "programming"

Sortie attendue :
expected = "pormig"
Entrée :
std::string text = "programming";

Sortie attendue :
std::string expected = "pormig";
Entrée :
string text = "programming";

Sortie attendue :
string expected = "pormig";

Explication

Positions 0, 2, 4, 6, 8, 10 give p, o, r, m, i, g: "pormig".

Exemple 2
Entrée :
text = "a"

Sortie attendue :
expected = "a"
Entrée :
std::string text = "a";

Sortie attendue :
std::string expected = "a";
Entrée :
string text = "a";

Sortie attendue :
string expected = "a";

Explication

Only position 0 exists: "a".

Comment l'aborder

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

Indice 1

You want characters at positions 0, 2, 4, and so on.

Indice 2

A step-by-two traversal is the simplest way to select them.

Indice 3

If the text is empty or has odd length, the pattern still works naturally.

Complexité attendue

O(n) time, O(n) 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.

# Codeur Langage Temps Mémoire Note de l'IA Solution
1 LMLaura Mahlow Python 818 ms - 4/5 Voir le code

Apprenez des autres

Solutions de la communauté

Attention spoiler : 1 solution complète ci-dessous Essayez d'abord de le résoudre vous-même. Vous pouvez toujours jeter un œil au code des autres développeurs et aux avis IA. Connectez-vous et résolvez le défi pour rejoindre la discussion.