Lesson 1 – Challenge Solutions

Challenge 1.1
def border_text(text_var):
     # Let's create our function

     # Take the length of the text and multiply it by "=" (the border)
     border = '='*len(text_var)

     # Print the border, the test, and then the border with separating lines

     print(border+'\n'+text_var+'\n'+border)

     return 0


border_text("Hello World")

border_text("World")
Output
===========
Hello World
===========
=====
World
=====
Challenge 1.2
# First, import the math library
import math

# The math library's sin, cos, tan, etc. functions
#  deal in radians so we need to convert degrees to radians
angle_b = math.radians(54)

# Let's find side c
# cos(54 degrees)=4 inches/c inches
# c inches*cos(54 degrees)=4 inches
# c inches=4 inches/cos(54 degrees)
side_c = 4/math.cos(angle_b)

# Now let's find side b
# sin(54 degrees)=b inches/side_c
# side_c*sin(54 degrees)=b inches
side_b = side_c*math.sin(angle_b)

# Okay, let's print everything out
print("Side a: 4 inches")
print("Side b: "+str(side_b)+" inches")
print("Side c: "+str(side_c)+" inches")
Output
Side a: 4 inches
Side b: 5.505527681884694 inches
Side c: 6.805206466816319 inches
Challenge 1.3
# Used to prevent a link from the last_level and pascals_triangle
import copy

# Let's create our list and start it will 2 levels
pascals_triangle = [[1], [1, 1]]

# Let's create a while loop that will stop on the tenth level
while len(pascals_triangle) != 10:

    # We need a copy otherwise it will be linked to pascals_triangle
    last_level = copy.deepcopy(pascals_triangle[-1])

    # Okay, for our next loop, it's going to create the next level
    new_level = [1]
    while 1:
        new_level.append(last_level[0]+last_level[1])
        last_level.pop(0)
        if len(last_level) == 1:
            new_level.append(1)
            break

    pascals_triangle.append(new_level)


print(pascals_triangle[0])
print(pascals_triangle[1])
print(pascals_triangle[2])
print('...')
print(pascals_triangle[8])
print(pascals_triangle[9])
Output
[1]
[1, 1]
[1, 2, 1]
...
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

Lesson 2 Challenge Solutions

Challenge 1.1


def find_factors(number):
	# Functions because why not?

	# Initialize out list
	factors = []

	# Starting from 1, let's test each number
	for curr in range(1, number + 1):
		# Taking the modulas, if there is a reminder,
		#  It's not a factor
		if (number % curr == 0):
			# We found a factor, add it to our list
			factors.append(curr)

	# We went through every number from 1 to the user's number
	# Return what we have
	return factors

# Ask the user for an input (and take the integer of the string that comes in)
usr_inp = int(input("What number would you like to find the factors of? "))
# Print out the factors
print("The factors of", usr_inp, "are:", find_factors(usr_inp))
Output
What number would you like to find the factors of? 12
The factors of 12 are: [1, 2, 3, 4, 6, 12]
Challenge 1.2
from random import shuffle
from math import pi


def pi_finder():
	# Keeping trying until we find a suitable denominator and numerator
	while(1):
		# First: Take 9 digits, randomize the order, & create a number from it
		denominator_list = ["1","2","3","4","5","6","7","8","9"]
		shuffle(denominator_list)

		# Multiple ways to do this, this take a list of numbers, and takes an
		#  empty string and adds each element from the list - finally taking the
		#  numeric value of the string using int()
		denominator = int(''.join(denominator_list))


		# Second: Take that number times PI (denominator times pi equal numerator)
		#		    and then take the integer of the value
		numerator = denominator * pi
		numerator = int(numerator)

		# Third: Check if it's 9 digits, and check if those digits are unique
		#		   from each other
		# Much easier to deal with strings at this point
		str_numerator = str(numerator)

		if (len(str_numerator) != 9):
			# Continue means reset from the top of the loop
			continue

		# Create a pool and remove each number as it is found
		pool = ["1","2","3","4","5","6","7","8","9"]

		for curr in list(str_numerator):
			# Take each number out as it is found
			if (curr in pool):
				pool.remove(curr)

		if (pool != []):
			# If it is not empty, there are dups in the numerator
			continue

		# At this point all checks have passed, return the number
		return [numerator, denominator]


# Run the function
result = pi_finder()

# Print out the results
print("The numerator is:", result[0])
print("The denominator is:", result[1])
print("The approximation of PI is:", result[0]/result[1])
Output
The numerator is: 613987452
The denominator is: 195438276
The approximation of PI is: 3.1415926530174674

 

 

Lesson 2 – Challenge Questions

Challenge 2.1

Objective

Create a script that will take a user input, find the factors of that number, and print those factors.

Hints

  • It would be sad if you don’t know what factors are, but here you go
  • Use a list to store the factors you have found
  • Use a loop to check each possible factor
  • Use the function input() to collect a user’s input
  • That input is going to be a string, turn it into an integer

Solution

Here is the possible output of a function:

What number would you like to find the factors of? 12
The factors of 12 are: [1, 2, 3, 4, 6, 12]

Challenge 2.2

Objective

Create a script that can generate a fraction consisting of 10 unique numbers on the numerator and 10 unique numbers on the denominator that can be a close approximation of PI. So, each number (1,2,3,4,5,6,7,9,0) can only be used once in the numerator and same for the denominator.

Hints

  • You will probably use random numbers (import random) for either the denominator or the numerator
  • Use a list for creating the random unique numbers
  • Also, add the math module, it contains PI math.pi (import math)
  • You will probably use loops
  • This is simpler than you think

Solution

This is one of the many answers that could be generated through a good script:

The numerator is: 2385140697
The denominator is: 759213864
The approximation of PI is: 3.141592652739018

Challenge 2.3

Objective

We have been given some file transfer data from the NOvA experiment. These data files contains a lot of information about file transfers for this experiment. We are going to take how many jobs started in one day. We will then plot this data in a histogram with the bin size being 1 hour.

Link to the data

Here is the header which has been removed from the csv file,

JobID,JobStartTime(unix timestamp),FileID,FileSize(byte),NodeName,FileProcessingTime(second),FileTransferTime(second),NodeType,FileTransferRate(byte/s)

Hints

  • Take a look at the matplotlib histogram script you did in the lesson
  • Most of the data in the data file for what we are trying to do is irrelevant, pay attention just to how many jobs were in a particular hour
  • To find this, you are given a jobStartTime which is a unix timestamp, lookup how to get an hour of the day from this timestamp, then count how many are in each hour