Code Lab

Modo entrevista. Cronômetro de 10 min, sem dicas e sem autocompletar

Find the version number

MédiaStrings10 min

Write a function extractVersion(text) that finds the first version number in a string and returns it. For 'oracle release 12.2c' the answer is '12.2'.

  • A version is one or more digits, then zero or more groups of a dot and one or more digits: 19, 12.2, 3.10.4.
  • Letters and other characters around the version do not matter: in 'v2.0', '19c' and '4.5-beta1' the versions are '2.0', '19' and '4.5'.
  • A dot with no digit right after it is not part of the version: for 'version 12.' return '12'.
  • If there are several versions, return the first one.
  • Return the version as a string, exactly as it is written in the text ('3.10' stays '3.10', not a number).
  • If the text has no digits (or is empty), return null.

A regular expression with match finds the version in one step. Remember that match returns null when nothing is found.

Exemplos

extractVersion('oracle release 12.2c')

Retorna '12.2'

extractVersion('Python 3.10.4 released')

Retorna '3.10.4'

extractVersion('no version')

Retorna null

Cobrada em Oracle · Fonte

Carregando o editor...
  1. EntradaextractVersion('oracle release 12.2c')
    Esperado'12.2'
  2. EntradaextractVersion('Python 3.10.4 released')
    Esperado'3.10.4'
  3. EntradaextractVersion('no version')
    Esperadonull

Os testes ocultos rodam no envio