<jupyter_start><jupyter_text>1-minute introduction to Jupyter A Jupyter notebook consists of cells. Each cell contains either text or code.A text cell will not have any text to the left of the cell. A code cell has `In [ ]:` to the left of the cell.If the cell contains code, you can edit it. Press Enter to edit the selected cell. While editing the code, press Enter to create a new line, or Shift+Enter to run the code. If you are not editing the code, select a cell and press Ctrl+Enter to run the code. Before you turn this problem in, make sure everything runs as expected. First, **restart the kernel** (in the menubar, select Kernel$\rightarrow$Restart) and then **run all cells** (in the menubar, select Cell$\rightarrow$Run All).Make sure you fill in any place that says `YOUR CODE HERE` or "YOUR ANSWER HERE", as well as your name and collaborators below:<jupyter_code>NAME = ""
COLLABORATORS = ""<jupyter_output><empty_output><jupyter_text>---  Assignment 6: Standard libraries, `while` loopsIn this assignment, you should write your code in a **readable** way, and **modularise** chunks of code that would otherwise be repeated.Modularising a section of program code means to reorganise it in a way that allows it to be reused in another part of the code easily, usually in the form of a function.Your function definitions should have appropriate **docstrings**.  Part 1Write a function, `clock(n)`, that will show the time and update it once every second, for `n` number of seconds.Your clock should show the time in `HH:MM:SS` format without milliseconds. Expected output    >>> clock(3)    12:59:17<jupyter_code># Write a function to show the time and update it once every second for n seconds

### YOUR ANSWER HERE
def clock(n):


clock(3)<jupyter_output><empty_output><jupyter_text>Part 2The `os.listdir(directory)` function from the `os` module returns a `list` of files in the given `directory`. If no argument is given to `os.listdir()`, it uses `'.'` as the default value.    >>> import os    >>> os.listdir('.')    ['.ipynb_checkpoints',     'assignment_06.ipynb',     'folder1',     'words.txt']         >>> os.listdir()    ['.ipynb_checkpoints',     'assignment_06.ipynb',     'folder1',     'words.txt']         >>> os.listdir('folder1')    ['file1.txt', 'folder1-1', 'folder1-2']      Task 1This program code defines a function `list_files(filepath)` that takes in a `filepath` (string) and returns a `list` of files in the filepath.    def list_files(filepath):        import os        list_of_files = os.listdir(filepath)        return list_of_filesModify the code so that it is able to return a list of files in the current directory when no argument is given to `list_files()`. Expected output    >>> list_files()    ['assignment_06.ipynb', 'folder1', '.ipynb_checkpoints']<jupyter_code>import os
os.getcwd()
# Task 1: Modify the code so that this code cell works without raising an Exception
### YOUR ANSWER HERE


# Do not modify any code after this line
list_files() # This line should work without modification
import os
assert list_files() == os.listdir(os.getcwd()), \
'File list from list_files() does not match current directory listing'<jupyter_output><empty_output><jupyter_text>Listing subdirectories with `os.listdir()`The path of files and folders in subdirectories must be given relative to the current folder. Each level down into a subfolder is demarcated by a forward slash '/' in the `filepath`.E.g. `folder1/folder1-1` means that `folder1` is in the current directory, while `folder1-1` is a folder in `folder1`.    >>> os.listdir('folder1-1')    FileNotFoundError: [WinError 3] The system cannot find the path specified: 'folder1-1'    >>> os.listdir('folder1/folder1-1')    ['file1-1-1.txt', 'folder1-1-1']<jupyter_code># Test your code here
os.listdir('folder1-1')<jupyter_output><empty_output><jupyter_text>Task 2The function `walk_listing(filepath)` prints all the names of items in `filepath`. For each item, its type (file or directory) is indicated in `[`square brackets`]`.    def walk_listing(filepath):        list_of_files = list_files(filepath)        for this in list_of_files:            if os.path.isfile(this):                print(f'[file] {this}')            elif os.path.isdir(this):                print(f'[dir] {this}')Modify the function so that it **returns a list** all the names of items in `filepath`, **including the contents of subdirectories**. you do not need to include `[file]` or `[dir]`. Expected output    >>> walk_listing('.')    ['./assignment_06.ipynb',     './folder1',     './folder1/folder1-2',     './folder1/folder1-2/file1-2.txt',     './folder1/folder1-2/.ipynb_checkpoints',     './folder1/.ipynb_checkpoints',     './folder1/folder1-1',     './folder1/folder1-1/file1-1-1.txt',     './folder1/folder1-1/.ipynb_checkpoints',     './folder1/folder1-1/folder1-1-1',     './folder1/folder1-1/folder1-1-1/.ipynb_checkpoints',     './folder1/folder1-1/folder1-1-1/file1-1-1-1.txt',     './.ipynb_checkpoints',     './.ipynb_checkpoints/assignment_06-checkpoint.ipynb']Remember that `os.listdir()` needs the path of items in subdirectories relative to the current folder.<jupyter_code># Task 2: Write code to list all files in the current directory recursively
import os, time

def walk_listing(filepath):
    ### YOUR ANSWER HERE
    newlist = []
    initiallist = os.listdir(filepath)
    for filename in initiallist:
        fullname = f'{filepath}/{filename}'
        newlist.append(fullname)
    i = 0
    while i < len(newlist):
        time.sleep(0.1)
        filename = newlist[i]
#     for filename in os.listdir(filepath):
        fullname = f'{filepath}/{filename}'
        newlist.append(fullname)
        if os.path.isdir(fullname):
            for subfilename in os.listdir(fullname):
                subfullname = f'{fullname}/{subfilename}'
                newlist.append(subfullname)
        i += 1
        print(newlist)

walk_listing('.')
# Run this cell to test your solution
for file in ('./assignment_06.ipynb', './folder1'):
    assert file in walk_listing('.'), f'Missing file {file} in listing'
# Hidden tests, ignore this cell<jupyter_output><empty_output><jupyter_text>Part 3: Lowest common multiple (LCM) with `while` loopA simple algorithm for calculating the lowest common multiple of `a` and `b` is as follows:1. Check if a and b are equal2. If they are equal, a = b = lcm (lowest common multiple) [EXIT]3. If they are not equal, check whether a or b is smaller.4. The smaller variable is incremented by its original value.    - i.e. a += a_original, or b += b_original5. Go back to Step 1Implement the above algorithm using a `while` loop. Write your code as a function, `lcm(a,b)` that returns the lowest common multiple of `a` and `b`. Example output    >>> lcm(2,3)    6    >>> lcm(12,5)    60<jupyter_code>def lcm(a, b):
    ### YOUR ANSWER HERE
    

lcm(2,3)
# Run this cell to test your solution
testdata = [(2,3,6),
            (12,5,60),
            (11,12,132),
            (0,7,0),
           ]

for a,b,ans in testdata:
    assert lcm(a,b) == ans, f'Wrong result for lcm({a},{b}): should be {ans}'
# Hidden tests, ignore this cell<jupyter_output><empty_output><jupyter_text>Part 4: Greatest common factor (GCF) with `while` loop[Euclid's Algorithm](https://en.wikipedia.org/wiki/Euclidean_algorithm) is an algorithm for calculating the greatest common factor `g` of `a` and `b` such that both `a` and `b` are divisible by `g`:1. Check if a and b are equal2. If they are equal, a = b = gcf (greatest common factor) [EXIT]3. If they are not equal, the difference between the two variables, `d`, must also be divisible by `g`.4. Calculate the GCF of the two smallest variables among `a`, `b`, and `g` (Start from Step 1)Implement the above algorithm using a `while` loop. Write your code as a function, `gcf(a,b)` that returns the greatest common factor of `a` and `b`. Example output    >>> gcf(60,48)    12    >>> gcf(70,14)    14<jupyter_code>def gcf(a, b):
    ### YOUR ANSWER HERE
    

gcf(60,48)
# Run this cell to test your solution
testdata = [(60,48,12),
            (70,14,14),
            (99,0,99),
#             (0,3,0),
            (0,3,3),
            (58,26,2),
            (27,13,1),
            (100,100,100),
           ]

for a,b,ans in testdata:
    result = gcf(a,b)
    assert result == ans, f'Wrong result for gcf({a},{b}): got {result}, should be {ans}'
# Hidden tests, ignore this cell<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11896612769975215, 'fraction_numerical': 0.024902631889531453, 'mean_word_length': 3.5879805089334056, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 68, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '227292', 'license': 'MIT', 'n_tokens_mistral': 2746, 'n_tokens_neox': 2654, 'n_words': 981, 'path': 'Assignment 6/assignment_06.ipynb', 'repo_name': 'nyjc-computing/intro-to-python', 'size': '8473'}