dimanche 15 octobre 2023

find file universe-formula

 You don’t remember where you saved the file universe-formula . 

The only thing you remember about this file is you put it somewhere in a sub-folder of /tmp/documents . 

Implement the method string LocateUniverseFormula() to locate universe-formula and then return the absolute path of the file (from the file system root). 

/tmp/documents can contain nested sub-folders and universe-formula can belong to any one of them. 

If universe-formula cannot be found, then your program should return null . 

Example : LocateUniverseFormula() returns /tmp/documents/a/b/c/universe-formula if universeformula is found into the folder /tmp/documents/a/b/c .



using System; 

using System.IO; 

using System.Collections.Generic; 

using System.Linq;

public class Answer {

    public static string LocateUniverseFormula() 

 { 

 List toReturns = new List(); 

 List files = Directory.GetFiles("documents", "*",SearchOption.AllDirectories).ToList(); 

 foreach (var item in files)

 var toReturn = Path.Combine("/tmp", item); 

 if(item.Contains("universe-formula")){ 

 toReturns.Add(toReturn); 

 } 

 } 

 if(toReturns.Count() == 0)

{ return null; } 

 return toReturns.FirstOrDefault(); 

 } 

}

Aucun commentaire:

Enregistrer un commentaire