Skip to content

Commit af47b75

Browse files
author
Jessica Yung
committed
test: add sample unit test and function, write test(buggy) for p2.
- chore: change file names to match Python style guide (change dashes to underscores) ISSUE: p2 tests don't run.
1 parent 42101ae commit af47b75

7 files changed

+258
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Problem 1:
3+
4+
Find the sum of all multiples of 3 or 5 below a positive integer n
5+
6+
"""
7+
8+
# Function calculates sum of multiples of k less than n
9+
def sum_multiples(n,k):
10+
sum = 0
11+
if n >= k:
12+
for i in range(1,int(n/k)):
13+
sum += k*i
14+
# It should be range(1,int(n/k)+1) for k such that n % k != 0, so
15+
if n % k != 0:
16+
sum += int(n/k)*k
17+
return sum
18+
19+
# Function calculates sum of all multiples of 3 or 5 below n
20+
def sum_multiples_3_5(n):
21+
return sum_multiples(n,3) + sum_multiples(n,5) - sum_multiples(n,15)
22+
23+
# Read input and solve
24+
t = int(input().strip())
25+
for j in range(t):
26+
n = int(input().strip())
27+
print(sum_multiples_3_5(n))
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""
2+
Problem 2:
3+
4+
Calculate the sum of all even Fibonacci numbers below positive integer n
5+
6+
"""
7+
import unittest
8+
9+
def fib(n):
10+
"""Function to calculate nth Fibonacci number, n being a non-negative integer.
11+
Def: 0th Fibonacci number is 1.
12+
"""
13+
if n < 0:
14+
return "Null"
15+
elif n == 0 or n == 1:
16+
return 1
17+
else:
18+
f_two_before = 1
19+
f = 1
20+
for i in range(2, n+1):
21+
# Save the value of f for use later
22+
f_save = f
23+
f = f + f_two_before
24+
# Additional print statement for clarity
25+
# print(i, "th Fib is ", f)
26+
f_two_before = f_save
27+
return f
28+
29+
30+
def sum_even_fibs_under_n(n):
31+
"""Calculates sum of even Fibonacci numbers under n."""
32+
if fib(2) < n:
33+
sum = 0
34+
i = 2
35+
while fib(i) < n:
36+
sum += fib(i)
37+
i += 3
38+
return sum
39+
else:
40+
return "Null"
41+
42+
# Tests
43+
44+
class Tests(unittest.TestCase):
45+
"""Tests fib(n)."""
46+
47+
def __init__(self):
48+
super(Tests, self).__init__()
49+
self.nth_fibonacci_test()
50+
self.sum_even_fibs_under_n_test()
51+
52+
53+
def nth_fibonacci_test(self):
54+
# type: () -> Error or not?
55+
self.assertEqual(fib(0), 1)
56+
self.assertEqual(fib(1), 1)
57+
self.assertEqual(fib(4), 5)
58+
59+
def sum_even_fibs_under_n_test(self):
60+
self.assertEqual(sum_even_fibs_under_n(10), 10)
61+
self.assertEqual(1, 0)
62+
63+
if __name__ == '__main__':
64+
unittest.main()
65+
# Tests.nth_fibonacci_test()
66+
# Tests.sum_even_fibs_under_n_test()
67+
68+
"""
69+
# Read input and solve
70+
t = int(input().strip())
71+
for i in range(t):
72+
n = int(input().strip())
73+
print(sum_even_fibs_under_n(n))
74+
"""
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Problem 3:
3+
4+
Find the largest prime factor of a positive integer n
5+
6+
"""
7+
import math
8+
9+
# Function to find all factors
10+
def factors(n):
11+
factors = set()
12+
for i in range(1, int(math.sqrt(n))+1):
13+
if n % i == 0:
14+
factors.add(i)
15+
factors.add(int(n/i))
16+
return factors
17+
18+
# Function that tests if integers are prime
19+
def isprime(n):
20+
isprime = True
21+
if n < 2:
22+
isprime = False
23+
elif n > 3:
24+
for i in range(2, int(math.sqrt(n))+1):
25+
if n % i == 0:
26+
isprime = False
27+
break
28+
return isprime
29+
30+
# Function that finds largest prime factor
31+
def largest_prime_factor(n):
32+
primefactors = set()
33+
for i in factors(n):
34+
if isprime(i):
35+
primefactors.add(i)
36+
if len(primefactors) == 0:
37+
return "Null"
38+
else:
39+
return max(primefactors)
40+
41+
# Read input and solve
42+
t = int(input().strip())
43+
for i in range(t):
44+
n = int(input().strip())
45+
print(largest_prime_factor(n))
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Problem 4:
3+
4+
Find the largest palindrome made from the product of two 3-digit integers which is less than positive integer n.
5+
6+
"""
7+
8+
# Function to test if a number is a palindrome
9+
def is_palindrome(n):
10+
is_palindrome = True
11+
n = str(n)
12+
for i in range(int(len(n)/2)):
13+
if n[i] != n[len(n)-1-i]:
14+
is_palindrome = False
15+
break
16+
return is_palindrome
17+
18+
# Narrow range
19+
def largest_three_digit_to_consider(n):
20+
k = 999
21+
while k*100 > n:
22+
k -= 1
23+
return k
24+
25+
"""
26+
Given the palindromic product must be a six-digit number, it must also be a multiple of 11. Thus at least one of the factors must be a multiple of 11.
27+
28+
"""
29+
30+
# Systematically go through all relevant products and find the greatest palindrome product
31+
def greatest_palindromic_product_below_n(n):
32+
k = largest_three_digit_to_consider(n)
33+
l = int(k/11)
34+
greatest = 0
35+
# Let a be the factor that must be a multiple of 11
36+
for i in range(10, l+1):
37+
a = 11*i
38+
for j in range(100,k):
39+
product = a*j
40+
if product < n:
41+
if product > greatest:
42+
if is_palindrome(product):
43+
greatest = product
44+
return greatest
45+
46+
# Read input and solve
47+
t = int(input().strip())
48+
for i in range(t):
49+
n = int(input().strip())
50+
print(greatest_palindromic_product_below_n(n))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Problem 5:
3+
4+
2520 is the smallest number that can be divided by each of the numbers from to without any remainder.
5+
What is the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to N?
6+
7+
"""
8+
9+
# Find all the prime factors and their powers in that number
10+
# Idea: Recursive
11+
12+
def blah():
13+
pass
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Problem 6:
3+
4+
Find the absolute difference between the sum of the squares of the first n natural numbers and the square of the sum.
5+
6+
"""
7+
8+
# Need abs((1^2+2^2+...+N^2) - (1+2+...+N)^2)
9+
10+
# Sum of the squares of the first n natural numbers
11+
def sum_of_squares(n):
12+
sum = 0
13+
for i in range(1,n+1):
14+
sum += i**2
15+
return sum
16+
17+
def square_of_sum(n):
18+
sum = 0
19+
for i in range(1, n+1):
20+
sum += i
21+
return sum**2
22+
23+
# Read input and solve
24+
t = int(input().strip())
25+
for i in range(t):
26+
n = int(input().strip())
27+
print(abs(sum_of_squares(n) - square_of_sum(n)))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Problem 7:
3+
4+
Find the nth prime number.
5+
n=< 10**4
6+
7+
"""
8+
9+
primes=[]
10+
def all_primes_under_n(n):
11+
primes_under_n = []
12+
for i in range(n):
13+
if isprime(i) == True:
14+
primes_under_n.append(i)
15+
return primes_under_n
16+
17+
def nth_prime(n):
18+
all_primes_under_n[n]
19+
20+
i = int(primes[n-2])
21+
while
22+

0 commit comments

Comments
 (0)