Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Recursive File Search | Recursion and Lambda Functions
Python Functions Tutorial
course content

Kursusindhold

Python Functions Tutorial

Python Functions Tutorial

1. What is a Function in Python?
2. Positional and Optional Arguments
3. Arbitrary Arguments
4. Function Return Value Specification
5. Recursion and Lambda Functions

book
Challenge: Recursive File Search

Opgave

Swipe to start coding

Imagine needing to check whether a specific file exists within a nested dictionary structure representing a file system. Implement a function file_exists that recursively navigates through folders (dictionary objects) and searches for a file (represented by the string "file"). Return True if the file is found; otherwise, return False.

  1. Use a for loop to iterate through all elements of the file_system dictionary using the items() method. This retrieves the key (name) and the value (content).
  2. Check if content is a file (i.e., the string "file") and if name matches target (the name of the file being searched for).
  3. If both conditions are met, return True, indicating that the file has been found.
  4. If content is not a file, check whether it is a folder. Use the isinstance() function, passing content as the first argument and dict as the second (which checks if the element is a dictionary).
  5. If content is a folder, call file_exists recursively with the necessary parameters to continue searching inside it.
  6. If the recursive call returns True, the file has been found, so return True.
  7. If no matches are found after checking all folders and files, return False.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 5. Kapitel 2
toggle bottom row

book
Challenge: Recursive File Search

Opgave

Swipe to start coding

Imagine needing to check whether a specific file exists within a nested dictionary structure representing a file system. Implement a function file_exists that recursively navigates through folders (dictionary objects) and searches for a file (represented by the string "file"). Return True if the file is found; otherwise, return False.

  1. Use a for loop to iterate through all elements of the file_system dictionary using the items() method. This retrieves the key (name) and the value (content).
  2. Check if content is a file (i.e., the string "file") and if name matches target (the name of the file being searched for).
  3. If both conditions are met, return True, indicating that the file has been found.
  4. If content is not a file, check whether it is a folder. Use the isinstance() function, passing content as the first argument and dict as the second (which checks if the element is a dictionary).
  5. If content is a folder, call file_exists recursively with the necessary parameters to continue searching inside it.
  6. If the recursive call returns True, the file has been found, so return True.
  7. If no matches are found after checking all folders and files, return False.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 5. Kapitel 2
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt