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

	# 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))
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
		# First: Take 9 digits, randomize the order, & create a number from it
		denominator_list = ["1","2","3","4","5","6","7","8","9"]

		# 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

		# 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):

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

		# 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])
The numerator is: 613987452
The denominator is: 195438276
The approximation of PI is: 3.1415926530174674