Solutions to Beginner Programming Projectsļƒ

Factorial Solutionļƒ

 1# ---------------------------------------------------------
 2#                          NOTES
 3# ---------------------------------------------------------
 4
 5# ---------------------------------------------------------
 6#                         INCLUDES
 7# ---------------------------------------------------------
 8
 9# ---------------------------------------------------------
10#                         METHODS
11# ---------------------------------------------------------
12
13def factorial(number):
14    """
15    Calculates the factorial of a given number.
16
17    :param number: integer that the user wants the factorial of.
18    :type number: int
19    :return: integer of the resulting factorial.
20    :rtype: int
21    """
22
23    return_val = 1
24    if number > 1:
25        return_val = number * factorial(number - 1)
26
27    return return_val
28
29# ---------------------------------------------------------
30#                          MAIN
31# ---------------------------------------------------------
32
33
34if __name__ == "__main__":
35    num_user = input("Enter a number to compute a factorial")
36    result = factorial(int(num_user))
37    print(result)
38
39# ---------------------------------------------------------
40#                       END OF FILE
41# ---------------------------------------------------------

File IO Solutionļƒ

  1# ---------------------------------------------------------
  2#                          NOTES
  3# ---------------------------------------------------------
  4
  5# ---------------------------------------------------------
  6#                         INCLUDES
  7# ---------------------------------------------------------
  8
  9import os
 10
 11# ---------------------------------------------------------
 12#                         SETUP
 13# ---------------------------------------------------------
 14
 15poem = """Two roads diverged in a yellow wood,
 16And sorry I could not travel both
 17And be one traveler, long I stood
 18And looked down one as far as I could
 19To where it bent in the undergrowth;
 20
 21Then took the other, as just as fair,
 22And having perhaps the better claim,
 23Because it was grassy and wanted wear;
 24Though as for that the passing there
 25Had worn them really about the same,
 26
 27And both that morning equally lay
 28In leaves no step had trodden black.
 29Oh, I kept the first for another day!
 30Yet knowing how way leads on to way,
 31I doubted if I should ever come back.
 32
 33I shall be telling this with a sigh
 34Somewhere ages and ages hence:
 35Two roads diverged in a wood, and Iā€”
 36I took the one less traveled by,
 37And that has made all the difference."""
 38
 39local_dir = os.path.abspath(os.path.dirname(__file__))
 40bin_file = os.path.join(local_dir, 'binary_file.txt')
 41readable_file = os.path.join(local_dir, 'readable_file.txt')
 42
 43
 44# encode data for the binary file
 45hex_list = []
 46for letter in poem:
 47    hex_list.append(hex(ord(letter)))
 48
 49# write to the binary file
 50with open(bin_file, 'w') as f:
 51    for value in hex_list:
 52        f.write(value)
 53
 54
 55# ---------------------------------------------------------
 56#                         ASSIGNMENT
 57# ---------------------------------------------------------
 58
 59local_dir = os.path.abspath(os.path.dirname(__file__))  # path to local directory
 60bin_file = os.path.join(local_dir, 'binary_file.txt')   # binary file in local directory
 61readable_file = os.path.join(local_dir, 'readable_file.txt')  # path to local directory
 62
 63# Method 1: More lines but easier to understand for beginners.
 64
 65# read the binary file
 66with open(bin_file, 'r') as f:
 67    read_data = f.read()
 68
 69# parse byte data as letters
 70hex_values = read_data.split('0x')  # separate every value by their hex delimiter.
 71hex_values.remove('')  # remove the weird empty element at the beginning of the list.
 72
 73# decode each letter in the list
 74final_string = ''
 75for value in hex_values:
 76
 77    # This will cast the string hex number to an integer, reading the value in base 16 (hex)
 78    temp = int(value, 16)
 79
 80    # interpret the hex value as an ascii/UTF-8 letter
 81    letter = chr(temp)
 82
 83    # add/append the letter to the complete string.
 84    final_string = final_string + letter
 85
 86# write to the binary file
 87with open(readable_file, 'w', encoding='UTF-8') as f:
 88    f.writelines(final_string)
 89
 90# ---------------------------------------------------------
 91
 92# Method 2: Less lines but more pythonic and efficient.
 93
 94# read the binary file
 95with open(bin_file, 'r') as f:
 96    read_data = f.read()
 97
 98# parse byte data as letters
 99hex_values = read_data.split('0x')
100decoded_letters = [chr(int(val, 16)) for val in hex_values[1:]]
101
102# compose a stirng
103final_string = ''.join(decoded_letters)
104print(final_string)
105
106# write to the binary file
107with open(readable_file, 'w', encoding='UTF-8') as f:
108    f.writelines(final_string)
109
110# ---------------------------------------------------------