-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
notebook 04: move part of exercise 4.2 to Additional Exercises
- Loading branch information
1 parent
b0c5d7c
commit d25eacb
Showing
6 changed files
with
230 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,40 @@ | ||
# Exercise 4.3 | ||
|
||
# 1. What does is_part_of_set do? | ||
# ******************************* | ||
from exercise_43_module import is_part_of_set | ||
|
||
# help(is_part_of_set) | ||
# "is_part_of_set()" returns True if its argument is a prime number | ||
# and False otherwise. | ||
|
||
for n in range(10): | ||
print(n, "->", is_part_of_set(n)) | ||
|
||
|
||
# 2 and 3. Use is_part_of_set to get all primes numbers between 2 and 50000 | ||
# ************************************************************************* | ||
from exercise_43_module import is_part_of_set | ||
from time import time | ||
|
||
primes = [] | ||
|
||
# Get the current time, so we can compute elapsed time at the end. | ||
t0 = time() | ||
for i in range(50000): | ||
if is_part_of_set(i): | ||
primes.append(i) | ||
|
||
time_first_algo = time() - t0 | ||
print("it took", time_first_algo, "seconds") | ||
print("found", len(primes), "prime numbers") | ||
|
||
|
||
# Optional: devise a more time efficient way of getting the prime numbers | ||
# *********************************************************************** | ||
# | ||
# Principle: rather than testing each number separately, we test the whole | ||
# set of numbers at once by going over all number and "eliminating" all | ||
# multiples of that number. | ||
from time import time | ||
|
||
t0 = time() | ||
|
||
# Phase1 : initialization | ||
upperLimit = 50000 | ||
|
||
# Create a list that contains True for all numbers we want to test. | ||
# During the algorithm we will set all non-prime numbers to False. | ||
arePrime = [True] * (upperLimit + 1) | ||
arePrime[0] = False # 0 is not prime | ||
arePrime[1] = False # 1 is not prime | ||
|
||
|
||
# Phase2 : go through all numbers | ||
primes2 = [] | ||
|
||
for i in range(2, upperLimit + 1): # for each candidate number | ||
|
||
# only do something if that number has not been set as a non-prime before | ||
if arePrime[i]: | ||
primes2.append(i) | ||
|
||
# then we want to set all multiples of that number as non-prime | ||
mult = 2 * i | ||
|
||
# all multiples until the upper limit is reached | ||
while mult <= upperLimit: | ||
arePrime[mult] = False # set the multiple to non-prime | ||
mult += i # nest multiple | ||
|
||
time_second_algo = time() - t0 | ||
|
||
print("it took", time() - t0, "seconds") | ||
print("speedup compared to first algorithm:", time_first_algo / time_second_algo) | ||
print("found", len(primes2), "prime numbers") | ||
print("is this list the same as with the first algorithm ?", primes == primes2) | ||
import os # Import the os module into the global namespace. | ||
|
||
# We re-use the function from exercise 4.2, and add an optional argument | ||
# "ignore_hidden" that, when set to "True", will ignore hidden files (i.e. | ||
# files whose name is starting with a dot, e.g. ".DS_Store") | ||
|
||
def count_files(dir_name, ignore_hidden=False): | ||
"""Counts files present in the input directory. | ||
Only files are counted, directories are ignored. | ||
Arguments: | ||
dir_name: path of directory in which to count files. | ||
ignore_hidden: Optional. If set to True, hidden files (files that | ||
start with a '.') are ignored from the count. | ||
""" | ||
|
||
# Initialize file counter. | ||
file_count = 0 | ||
|
||
# Loop through all files and directories present in the input directory. | ||
for f in os.listdir(path=dir_name): | ||
|
||
# Get the absolute path of the file/directory. | ||
full_path = os.path.join(dir_name, f) | ||
|
||
# Verify the path corresponds to a file, not a directory. | ||
if os.path.isfile(full_path) and not (ignore_hidden and f.startswith(".")): | ||
file_count += 1 | ||
|
||
return file_count | ||
|
||
parent_dir = os.path.dirname(os.getcwd()) | ||
print("File count in [", parent_dir, "]: ", count_files(parent_dir), sep="") | ||
print( | ||
"File count (excluding hidden files) in [", parent_dir, "]: ", | ||
count_files(parent_dir, ignore_hidden=True), | ||
sep="" | ||
) |
Oops, something went wrong.