Example code - exam form

From 22116
Jump to navigation Jump to search

At this and the next lesson plus the exam, you will have to do the exercises differently. This is both an example and an explanation.
How you will achieve it using your tools - VScode, Jupyter Notebook, text editor, WSL2, command line, etc - is your problem to solve.

You get a number of assignments or exercises that you have to solve. In this example it is demonstrated with a single task.

Make a function that accepts two parameters; an input file name and and output filename. The input file contains one value per line, but the value might be anything, string, integer, float, positive or negative. The function must read the input file, discard any value that is not positive, and output the log of the non-discarded numbers to the output file.

You will as a template for your hand-in get a file that looks like this:

#!/usr/bin/env python3
# Above is the shebang line - which tells which program should interpret the rest of the file

# The libraries necessary for the exercises - use no other
import os, sys, re, math

####################################################################################
# You must fill the functions with code that does the job mentioned.               #
# All the functions must work together coherently for the exercise to be complete. #
# The main exercise function should call the other functions in it's group.        #
# You can add your own functions, but you still need to make the ones described.   #
# Any personal functions should follow the naming pattern demonstrated.            #
# You are expected the delete the "pass" statements as they are present only for   #
# syntax reasons. The template won't run without them.                             #
####################################################################################

################################
### FUNCTIONS FOR EXERCISE 1 ###
################################

def exercise_1(filenamein:str, filenameout:str) -> None:
    # Read file, discards line with non-positive numbers, output the log
    pass


############################
# Main program starts here #
# Not necessary to change  #
############################

# Below if statement allows you to execute the main code when the file runs as a script,
# but not when it is imported as a module.
if __name__ == "__main__":

    # First exercise
    exercise_1('values.txt', 'log_values.txt')

You should now fill out the code in the function exercise_1 that accomplishes the task. You can add your own helper functions to use in exercise_1, if you want - call them ex1_myhelperfunctionname. Sometimes I have decided that you need helper functions (to clarify the task) and then they are just about the main exercise function using the same naming scheme.

#!/usr/bin/env python3
# Above is the shebang line - which tells which program should interpret the rest of the file

# The libraries necessary for the exercises - use no other
import os, sys, re, math

####################################################################################
# You must fill the functions with code that does the job mentioned.               #
# All the functions must work together coherently for the exercise to be complete. #
# The main exercise function should call the other functions in it's group.        #
# You can add your own functions, but you still need to make the ones described.   #
# Any personal functions should follow the naming pattern demonstrated.            #
# You are expected the delete the "pass" statements as they are present only for   #
# syntax reasons. The template won't run without them.                             #
####################################################################################

################################
### FUNCTIONS FOR EXERCISE 1 ###
################################

def exercise_1(filenamein:str, filenameout:str) -> None:
    # Read file, discards line with non-positive numbers, output the log
    infile = open(filenamein, 'r')
    outfile = open(filenameout, 'w')
    for line in infile:
        try:
            no = float(line)
        except:
            continue
        if no > 0:
            print(math.log(no), file=outfile)
    infile.close()
    outfile.close()

############################
# Main program starts here #
# Not necessary to change  #
############################

# Below if statement allows you to execute the main code when the file runs as a script,
# but not when it is imported as a module.
if __name__ == "__main__":

    # First exercise
    exercise_1('values.txt', 'log_values.txt')

As you notice, only the exercise_1 function was changed - nothing else in the template.