71A. Way Too Long Words – CodeForces Solution

Solution in__

Python:


n = int(input())
for _ in range(n):
    word = input()
    if len(word) > 10:
        print(f"{word[0]}{len(word) - 2}{word[-1]}")
    else:
        print(word)

Python

C#:


using System;

class Program
{
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        for (int i = 0; i < n; i++)
        {
            string word = Console.ReadLine();
            if (word.Length > 10)
            {
                Console.WriteLine($"{word[0]}{word.Length - 2}{word[^1]}");
            }
            else
            {
                Console.WriteLine(word);
            }
        }
    }
}

C#

Go:


package main

import (
    "fmt"
)

func main() {
    var n int
    fmt.Scan(&n)

    for i := 0; i < n; i++ {
        var word string
        fmt.Scan(&word)
        if len(word) > 10 {
            fmt.Printf("%c%d%c\n", word[0], len(word)-2, word[len(word)-1])
        } else {
            fmt.Println(word)
        }
    }
}

Go

Ruby:


n = gets.to_i
n.times do
  word = gets.chomp
  if word.length > 10
    puts "#{word[0]}#{word.length - 2}#{word[-1]}"
  else
    puts word
  end
end

Ruby

Rust:


use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    let lines: Vec<String> = stdin.lock().lines().map(|line| line.unwrap()).collect();
    
    let n: usize = lines[0].parse().unwrap();
    
    for i in 1..=n {
        let word = &lines[i];
        if word.len() > 10 {
            println!("{}{}{}", &word[0..1], word.len() - 2, &word[word.len()-1..]);
        } else {
            println!("{}", word);
        }
    }
}

Rust

Kotlin:


fun main() {
    val n = readLine()!!.toInt()
    repeat(n) {
        val word = readLine()!!
        if (word.length > 10) {
            println("${word.first()}${word.length - 2}${word.last()}")
        } else {
            println(word)
        }
    }
}
Kotlin

Explanation

  • Each solution starts by reading the number of words n from the input.
  • For each word, if its length is greater than 10, the word is abbreviated by printing the first character, the number of characters between the first and the last, and the last character. Otherwise, the word is printed as is.

April 7, 2024