Code Lab

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

Files in one folder

DifícilJSON and data15 min

Write a function filesInFolder(paths, folder) that returns the names of the files that are right in one folder.

  • paths is an array of file paths of a project, like the output of git ls-files: folder names and the file name joined with /, for example 'src/utils/math.js'. A path never starts with /. The same path can occur more than once.
  • folder is a string: a folder path from the project root, such as 'src' or 'src/utils'. 'src/' (with a slash at the end) means the same as 'src'. The empty string '' means the project root.
  • Return an array of file names (the last part of a path, without folders). Take only files that are right in folder, not files in its subfolders. 'src2' is a different folder from 'src'.
  • Every name is in the result once.
  • Sort the names with the default sort(): it compares strings by character codes, so 'README.md' comes before 'app.js'.
  • If no file is right in folder (also when there is no such folder or paths is empty), return an empty array.

You can split a path by / to get its parts.

Exemplos

filesInFolder(['src/app.js', 'src/utils/math.js', 'README.md', 'src/index.html', 'src2/x.js'], 'src')

Retorna [ 'app.js', 'index.html' ]

filesInFolder(['src/app.js', 'src/utils/math.js', 'README.md'], 'src/utils')

Retorna [ 'math.js' ]

filesInFolder(['src/app.js', 'README.md'], 'docs')

Retorna []

Cobrada em ServiceNow · Fonte

Carregando o editor...
  1. EntradafilesInFolder([ 'src/app.js', 'src/utils/math.js', 'README.md', 'src/index.html', 'src2/x.js' ], 'src')
    Esperado[ 'app.js', 'index.html' ]
  2. EntradafilesInFolder([ 'docs/intro.md', 'tests/login.spec.ts', 'tests/cart.spec.ts' ], 'tests')
    Esperado[ 'cart.spec.ts', 'login.spec.ts' ]
  3. EntradafilesInFolder([ 'src/app.js', 'README.md' ], 'docs')
    Esperado[]

Os testes ocultos rodam no envio