Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

612
Vistas
Find time to write a number in one figure

write a function to calculate the number of milliseconds needed to type a number with one finger in javaScript

am try to solve this question but i don't have any idea how am solve this problem.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If I understand the comments well here's the answer

 function calcTime (digits, num ){
        const digits_arr = Array.from(digits);
        let last_index = 0, new_index, time =0;
        for (const n of num) {
            new_index =digits_arr.findIndex(x => x===n);
            time += Math.abs(new_index - last_index);
            last_index = new_index;
        }
        return time;
    }

example: Input: digits = "0123456789", num = "201" Output: 4

about 4 years ago · Juan Pablo Isaza Denunciar

0

Dude, I suppose you didn't have much time to explain what you where trying to solve. But anyway I run into this post because I was trying to understand how to start to solve the problem that I've found and thanks to @AbdelazizAlsabagh I've finally understand the trick with the indexes!. I'm posting the full problem to solve (but I haven't try the solution yet, I'll updated later):

Number Generator

A digit-only keyboard contains all 10 digits from 0 to 9. They all exist in one line. Give a string of 10 digits illustrating how the keys are positioned. To type a digit, you start from index zero to the index of the target digit. It takes |a - b| milliseconds to move from index a to index b.

Write a function to calculate the number of milliseconds needed to type a number with one finger.

Input: digits = '0123456789', num = '210

Output: 4

Input: digits = '8459761203', num = '5439'

Output: 17

Constraints:

  • digits.length == 10
  • digits contains each digit [0-9] exactly one in some order
  • 1 <= num.length <= 10e4
  • num[i] is digit
def number_gen_idx(digits: str, nums: str) -> str:
    """
    >>> number_gen_idx('0123456789', '210')
    4
    >>> number_gen_idx('8459761203', '5439')
    17
    """
    digits_idx = defaultdict(int)
    for idx, digit in enumerate(digits):
        digits_idx[digit] = idx
    curr_ptr = 0
    result = 0
    for digit in nums:
        result += abs((digits_idx[digit] - curr_ptr))
        curr_ptr = digits_idx[digit]
    return result
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda