Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
21366bead3 | |||
54a46068fb | |||
fd9a28c43e | |||
4e1d357998 | |||
b7133f3362 | |||
5100753526 | |||
2fc3e17d0c | |||
11f0b2e045 | |||
7311c6644d | |||
fd1b456a08 | |||
9a4c854521 | |||
7c7857a462 | |||
87f0577c1d | |||
da33ca3269 | |||
fa190af269 | |||
078760393a | |||
ea632d1c9f | |||
4f7408d40d |
@ -1,7 +1,16 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
def titlecase( string, exceptions ):
|
||||
pass
|
||||
def titlecase( sentence, exceptions ):
|
||||
sentence = sentence.lower().split(' ')
|
||||
out = [sentence[0].title()]
|
||||
|
||||
for word in sentence[1:]:
|
||||
if word in exceptions:
|
||||
out.append(word)
|
||||
else:
|
||||
out.append(word.title())
|
||||
|
||||
return ' '.join(out)
|
||||
|
||||
assert( titlecase( 'the quick brown fox jumps over the lazy dog', ['jumps', 'the', 'over'] ) == 'The Quick Brown Fox jumps over the Lazy Dog' )
|
||||
assert( titlecase( 'THE vitamins ARE IN my fresh CALIFORNIA raisins', ['are', 'is', 'in', 'your', 'my'] ) == 'The Vitamins are in my Fresh California Raisins' )
|
||||
|
@ -1,13 +1,11 @@
|
||||
Float to Currency
|
||||
==================
|
||||
|
||||
Write a method that gives the number of currency coins and bills needed to
|
||||
represent that number (rounded to the nearest 1/100, i.e. the nearest coin).
|
||||
Write a method that gives the number of American coins and bills needed to represent that number (rounded to the nearest 1/100, i.e. the nearest penny).
|
||||
|
||||
As an exmaple, if the float is 12.33, the result would be 1 $10, 2 $1 bills,
|
||||
1 quarter, 1 nickel and 3 pennies.
|
||||
As an exmaple, if the float is 12.33, the result would be 1 $10, 2 $1 bills, 1 quarter, 1 nickel and 3 pennies.
|
||||
|
||||
Here is the the list USD banknotes. Feel free to use USD or you local currency.
|
||||
Use the following as denominations for your currencies:
|
||||
|
||||
Penny: 1 cent
|
||||
Nickel: 5 cent
|
||||
@ -16,25 +14,5 @@ Here is the the list USD banknotes. Feel free to use USD or you local currency.
|
||||
One-dollar bill
|
||||
Five-dollar bill
|
||||
Ten-dollar bill
|
||||
Twenty-dollar bill
|
||||
Fifty-dollar bill
|
||||
Hundred-dollar bill
|
||||
|
||||
|
||||
## Bonus
|
||||
|
||||
Write a second function to make the output look nice, something like this:
|
||||
|
||||
```
|
||||
currency_converter_formater(12.23)
|
||||
|
||||
Penny: 3
|
||||
Dime: 2
|
||||
One-dollar: 2
|
||||
Ten-dollar: 1
|
||||
```
|
||||
|
||||
# Bonus II
|
||||
|
||||
Abjust your function so it takes a second argument of `denominations` so a
|
||||
single currency is not hard coded.
|
||||
|
@ -1,2 +1,24 @@
|
||||
def currency_converter():
|
||||
pass
|
||||
units_USD = [
|
||||
{'name': 'Penny', 'value': 1, 'count': 0},
|
||||
{'name': 'Nickel', 'value': 5, 'count': 0},
|
||||
{'name': 'Dime', 'value': 10, 'count': 0},
|
||||
{'name': 'Quarter', 'value': 25, 'count': 0},
|
||||
{'name': 'One-dollar', 'value': 100, 'count': 0},
|
||||
{'name': 'Five-dollar', 'value': 500, 'count': 0},
|
||||
{'name': 'Ten-dollar', 'value': 1000, 'count': 0},
|
||||
{'name': 'Fifty-dollar', 'value': 5000, 'count': 0},
|
||||
{'name': 'Hundred-dollar', 'value': 10000, 'count': 0},
|
||||
]
|
||||
|
||||
def currency_converter(amount, units=None):
|
||||
units = (units or units_USD).copy()
|
||||
amount = 100*amount
|
||||
|
||||
for current in units[::-1]:
|
||||
while amount >= current['value']:
|
||||
amount -= current['value']
|
||||
current['count'] += 1
|
||||
|
||||
return [i for i in units if i['count']]
|
||||
|
||||
print(currency_converter(12.23))
|
@ -1,2 +1,16 @@
|
||||
def remove_duplicate(string):
|
||||
pass
|
||||
def remove_duplicate(sentence):
|
||||
out = sentence[0]
|
||||
dupes = ''
|
||||
last = sentence[0]
|
||||
|
||||
for letter in sentence[1:]:
|
||||
if letter == last:
|
||||
dupes += letter
|
||||
else:
|
||||
out += letter
|
||||
last = letter
|
||||
return out
|
||||
|
||||
assert(remove_duplicate('hello') == 'helo')
|
||||
assert(remove_duplicate('hhhhheeeeelllllooooo') == 'helo')
|
||||
|
||||
|
@ -1,5 +1,26 @@
|
||||
def age_to_time(age):
|
||||
pass
|
||||
from datetime import datetime
|
||||
|
||||
def birthday_to_time(birthday):
|
||||
pass
|
||||
def to_time( delta ):
|
||||
months = int( delta.days/30 )
|
||||
days = delta.days
|
||||
hours = delta.days*24
|
||||
minutes = delta.days*24*60
|
||||
|
||||
return 'months : {}, days : {}, hours : {}, and minutes : {}'.format( months, days, hours, minutes )
|
||||
|
||||
def age_to_time( age ):
|
||||
delta = datetime.now() - datetime.now().replace( year=datetime.now().year-age )
|
||||
|
||||
return to_time( delta )
|
||||
|
||||
def birthday_to_time( birthday ):
|
||||
birthday = datetime.strptime( birthday, "%Y-%m-%d" )
|
||||
delta = datetime.now() - birthday
|
||||
|
||||
return to_time( delta )
|
||||
|
||||
age = int( input('How old are you? ') )
|
||||
print( age_to_time(age) )
|
||||
|
||||
birthday = input( 'What is your birth date?(YYYY-MM-DD) ')
|
||||
print( birthday_to_time(birthday) )
|
@ -1,18 +1,25 @@
|
||||
# Use this function to get and return all the prime numbers below the input number
|
||||
class int( int ):
|
||||
|
||||
def to_roman(num):
|
||||
pass
|
||||
def roman( self ):
|
||||
romanNum = []
|
||||
symbols = ( ( 'M', 1000 ), ( 'C', 100 ), ( 'XC', 90 ), ( 'L', 50 ), ( 'X', 10 ),
|
||||
( 'IX', 9 ), ('V', 5 ) , ( 'IV', 4 ), ( 'I', 1 ) )
|
||||
|
||||
for symbol, value in symbols:
|
||||
while self >= value:
|
||||
self -= value
|
||||
romanNum.append( symbol )
|
||||
|
||||
return ''.join( romanNum )
|
||||
|
||||
n = int( 56 )
|
||||
print( n, n.roman() )
|
||||
|
||||
# alias int.roman() not to change assert lines
|
||||
roman = int.roman
|
||||
|
||||
|
||||
|
||||
assert to_roman(11) == "XI", "11 should return XI"
|
||||
assert to_roman(60) == "LX", "60 should return LX"
|
||||
assert to_roman(78) == "LXXVIII", "78 should return LXXVIII"
|
||||
assert to_roman(4) == "IV", "4 should return IV"
|
||||
assert to_roman(99) == "XCIX", "99 should return XCIX"
|
||||
|
||||
# Add your own assert tests below
|
||||
assert roman( 11 ) == "XI", "11 should return XI"
|
||||
assert roman( 60 ) == "LX", "60 should return LX"
|
||||
assert roman( 78 ) == "LXXVIII", "78 should return LXXVIII"
|
||||
assert roman( 4 ) == "IV", "4 should return IV"
|
||||
assert roman( 99 ) == "XCIX", "99 should return XCIX"
|
@ -1,47 +0,0 @@
|
||||
24 58 36 91 57 23 83 34 56 70 84 92 65 78 11 16 92 28 42 10 37 92 75 63 14 68 71 58 41 49 87 4 99 85 5 91 54 79 69 98
|
||||
83 56 20 46 28 66 71 44 85 87 6 80 13 43 40 56 66 34 72 79 80 35 53 1 31 31 90 32 13 7 91 23 86 8 42 94 65 46 53 63
|
||||
75 11 76 10 39 90 33 51 44 44 61 17 59 26 70 84 32 42 24 59 85 75 80 81 23 83 25 14 16 28 59 61 99 97 97 79 23 33 13 9
|
||||
54 50 86 49 33 12 22 35 96 87 41 38 42 58 77 21 16 67 9 89 16 24 67 77 91 54 53 87 45 59 93 55 33 57 86 23 13 75 2 37
|
||||
14 18 97 75 40 53 84 57 83 12 85 87 20 31 86 84 85 100 29 45 14 48 23 7 59 54 42 7 13 70 78 94 82 29 45 2 12 79 66 86
|
||||
31 61 24 51 25 11 45 12 12 77 71 31 20 63 16 45 25 25 12 96 23 99 59 54 10 25 87 8 64 49 6 20 3 45 37 27 51 62 38 20
|
||||
69 73 56 50 89 66 68 31 49 95 5 86 87 28 28 62 36 72 94 38 48 40 59 61 37 34 92 77 69 43 57 32 33 27 98 81 86 37 91 72
|
||||
29 58 100 13 22 66 97 16 100 26 3 71 66 70 50 7 33 80 96 60 71 39 13 76 25 11 42 74 71 65 94 71 6 36 24 96 85 20 24 63
|
||||
23 19 80 96 22 15 68 26 5 42 81 22 27 1 33 95 88 25 21 9 89 100 11 19 65 93 57 10 60 94 64 92 1 10 69 16 90 77 69 48
|
||||
23 25 90 26 67 32 17 11 61 18 8 7 90 16 12 81 62 11 84 40 11 72 34 11 71 5 33 29 76 2 45 59 38 86 60 22 32 39 30 91
|
||||
17 96 86 25 37 86 3 9 16 16 97 1 86 28 3 29 18 4 63 74 83 88 35 90 62 37 100 59 84 2 35 78 68 8 84 65 40 31 26 42
|
||||
82 72 36 37 29 23 83 56 41 4 65 98 91 66 89 66 46 50 91 50 61 97 87 46 46 56 85 49 20 87 21 61 7 38 79 94 91 38 31 5
|
||||
25 97 78 7 66 69 80 18 19 41 21 81 29 43 89 24 67 23 68 97 24 43 35 77 4 39 33 53 1 92 64 96 24 37 17 44 4 91 50 41
|
||||
31 6 58 25 34 58 51 72 25 69 75 100 88 14 21 62 90 2 61 88 46 47 16 92 100 100 96 14 29 77 5 24 20 54 74 11 56 98 3 38
|
||||
11 46 85 77 74 59 6 23 97 29 68 2 87 49 9 18 85 24 97 78 75 1 27 28 32 31 45 33 15 77 34 74 66 79 20 23 33 100 99 88
|
||||
34 48 81 40 85 17 1 70 44 84 69 28 99 59 76 36 51 42 23 76 83 6 5 93 50 22 56 67 18 81 55 31 1 72 1 31 66 83 50 80
|
||||
83 8 46 18 35 47 9 28 50 83 54 68 97 87 34 76 63 26 75 39 15 9 2 84 37 57 14 52 55 8 25 25 31 92 44 20 90 49 75 38
|
||||
80 99 93 15 78 89 10 85 3 87 23 2 24 4 66 36 2 57 78 85 5 55 74 94 28 31 31 91 23 33 70 52 69 84 82 59 95 78 30 97
|
||||
70 49 57 47 78 33 93 82 40 22 2 36 33 97 37 99 34 79 78 33 64 75 76 19 37 82 48 67 7 99 91 12 87 5 73 85 19 64 7 15
|
||||
66 33 89 1 50 93 80 65 40 18 15 52 11 27 99 78 78 26 60 65 78 41 98 93 8 69 76 10 41 23 48 27 34 73 99 55 95 88 1 99
|
||||
1 3 61 44 42 92 13 42 36 20 14 82 62 83 31 67 92 72 41 10 85 92 12 78 8 16 85 48 45 50 62 54 6 82 26 28 44 87 45 70
|
||||
48 12 73 9 73 9 6 95 61 5 27 4 61 35 92 53 25 59 22 89 89 63 84 74 53 6 60 14 21 20 6 87 60 32 85 11 26 48 95 52
|
||||
8 16 13 68 11 40 58 44 95 57 26 12 98 78 82 56 59 57 10 46 11 62 20 41 37 17 35 73 29 60 57 24 94 40 76 94 61 45 87 29
|
||||
59 68 52 84 44 47 66 74 90 13 73 27 16 65 5 11 44 47 14 95 68 21 94 63 87 82 35 92 32 35 78 97 71 60 48 82 6 34 59 19
|
||||
63 2 62 50 26 50 79 100 93 87 43 98 62 76 70 35 97 97 19 41 30 55 45 13 82 31 44 91 90 87 71 99 57 52 3 27 37 88 9 28
|
||||
12 17 29 52 79 55 21 47 39 56 67 60 81 43 37 41 17 14 8 31 59 25 62 20 33 48 35 92 58 57 44 75 26 68 27 47 90 45 35 43
|
||||
71 2 97 68 34 17 17 12 87 12 94 17 56 14 18 3 16 81 78 11 79 14 75 41 95 72 20 63 5 79 44 28 83 54 50 48 51 63 43 32
|
||||
56 44 31 27 5 85 7 68 70 11 87 29 48 32 13 59 80 41 60 46 31 82 72 21 43 81 5 47 48 29 33 29 94 28 80 72 25 90 14 23
|
||||
61 37 94 15 83 39 95 82 38 31 96 71 26 30 42 99 69 75 86 99 92 84 49 47 3 63 44 47 48 2 12 9 53 63 73 96 92 49 100 40
|
||||
19 91 56 37 48 20 98 38 70 19 86 58 100 21 24 35 41 46 87 7 34 11 18 13 4 43 40 4 33 39 29 17 59 18 54 92 27 20 17 10
|
||||
19 86 64 90 66 86 61 80 69 68 79 65 49 80 15 59 6 60 53 28 26 89 87 89 98 8 83 73 1 67 22 24 20 16 65 63 20 26 20 49
|
||||
90 87 51 84 52 92 89 55 50 67 30 25 47 43 48 9 73 89 39 60 67 60 78 58 30 43 37 28 17 67 92 39 60 91 99 14 87 16 7 99
|
||||
87 4 8 30 100 53 32 39 38 86 68 61 36 28 86 25 84 31 99 87 28 74 29 88 91 52 98 44 70 24 36 88 62 21 91 41 88 26 52 6
|
||||
47 23 36 40 87 26 7 65 50 62 83 23 80 96 41 63 41 22 85 25 36 88 76 42 12 68 65 94 29 45 88 2 84 70 8 89 73 5 40 51
|
||||
8 83 54 82 91 15 35 87 58 60 2 24 40 58 59 91 31 23 78 70 91 75 41 64 57 87 12 52 28 40 77 87 55 10 13 81 97 69 50 4
|
||||
67 54 59 18 36 84 72 37 50 14 77 89 31 27 62 83 36 64 28 86 35 59 52 56 78 90 97 8 5 45 23 80 97 6 23 49 51 97 11 89
|
||||
34 9 21 15 60 83 20 47 90 56 22 98 82 44 45 98 100 89 52 13 26 9 34 29 1 74 50 100 20 13 16 37 97 65 80 17 48 92 2 6
|
||||
51 82 98 67 58 64 18 67 43 74 8 4 54 68 23 24 56 19 69 75 78 55 24 11 58 39 65 4 20 53 49 97 57 85 81 53 68 30 28 47
|
||||
43 37 35 64 40 57 48 67 30 60 54 64 94 38 95 63 69 64 3 50 24 39 98 15 54 67 55 31 13 10 48 40 53 57 63 74 25 60 33 80
|
||||
33 79 47 22 62 42 74 82 40 73 77 27 55 34 14 54 33 94 7 16 38 35 94 75 26 100 33 26 85 56 15 56 37 4 10 50 22 98 82 2
|
||||
18 73 97 58 100 60 44 45 7 1 80 80 38 30 34 14 56 74 97 75 98 26 97 57 79 24 63 64 31 94 30 64 53 23 37 40 79 6 1 5
|
||||
77 90 2 17 52 39 59 53 30 57 98 77 26 60 62 89 11 5 8 62 65 64 43 48 87 2 84 62 69 94 11 43 21 53 44 90 33 19 23 77
|
||||
64 61 77 59 88 18 64 52 70 39 65 55 93 61 18 80 77 5 69 6 8 36 61 37 84 40 5 61 52 70 65 82 46 34 99 94 6 51 34 14
|
||||
5 45 72 76 77 94 41 97 82 25 48 5 41 55 43 39 95 37 93 72 97 32 15 77 46 80 37 99 30 97 14 9 2 27 60 100 77 41 17 1
|
||||
8 68 24 61 54 19 6 66 43 80 92 10 19 2 13 15 47 69 81 41 23 95 62 39 52 91 28 59 6 71 12 12 74 67 90 86 29 26 66 60
|
||||
46 17 45 82 59 3 98 41 19 76 100 83 58 80 81 4 6 50 24 75 97 23 7 100 72 74 84 49 10 62 29 38 8 69 13 71 7 63 1 73
|
||||
85 72 57 81 15 48 60 51 92 66 46 93 87 83 67 61 74 64 64 64 93 93 65 77 77 79 73 42 80 23 50 36 72 2 24 76 28 44 65 61
|
@ -0,0 +1,72 @@
|
||||
def matrix_parse(matrix):
|
||||
out = []
|
||||
for row in matrix.split('\n'):
|
||||
out.append([int(col) for col in row.split()])
|
||||
|
||||
return out
|
||||
|
||||
def matrix_from_file(file):
|
||||
with open(file, 'r') as file:
|
||||
|
||||
return matrix_parse(file.read())
|
||||
|
||||
def matrix_sum(matrix):
|
||||
row_sum = []
|
||||
col_sum = [0 for col in range(len(matrix)+1)]
|
||||
|
||||
for row in matrix:
|
||||
row_sum.append(sum(row))
|
||||
for idx, col in enumerate(row):
|
||||
col_sum[idx] += col
|
||||
|
||||
return row_sum, col_sum
|
||||
|
||||
def matrix_sort_row(matrix):
|
||||
row_sum, col_sum = matrix_sum(matrix)
|
||||
out = []
|
||||
|
||||
for index, row in sorted( [i for i in enumerate(row_sum)], key=lambda x:x[1] ):
|
||||
out.append(matrix[index])
|
||||
|
||||
return out;
|
||||
|
||||
def matrix_sort_col(matrix):
|
||||
row_sum, col_sum = matrix_sum(matrix)
|
||||
out = []
|
||||
places = sorted( [i for i in enumerate(col_sum)], key=lambda x:x[1])
|
||||
|
||||
for index, row in enumerate(matrix):
|
||||
out.append([row[i[0]] for i in places])
|
||||
|
||||
return out
|
||||
|
||||
|
||||
def matrix_get_element_width(matrix):
|
||||
max = 0
|
||||
for row in matrix:
|
||||
for element in row:
|
||||
if max < len(str(element)):
|
||||
max = len(str(element))
|
||||
|
||||
return max
|
||||
|
||||
def matrix_print(matrix, width=None):
|
||||
if not width:
|
||||
return matrix_print(matrix, matrix_get_element_width(matrix))
|
||||
|
||||
for row in matrix:
|
||||
print('', *['{:{}d}'.format(el, width) for el in row], '', sep=' | ')
|
||||
|
||||
|
||||
m1 ='''10 5 4 20
|
||||
9 33 27 16
|
||||
11 6 55 3'''
|
||||
|
||||
# m1 = matrix_parse(m1)
|
||||
# print(m1)
|
||||
# print(matrix_sum(m1))
|
||||
# print(matrix_get_element_width(m1))
|
||||
# matrix_print(m1)
|
||||
|
||||
m2 = matrix_from_file('./matrix.txt')
|
||||
print(matrix_sum(m2))
|
||||
|
80
04-matrix-sort/row_col_sort_class.py
Normal file
80
04-matrix-sort/row_col_sort_class.py
Normal file
@ -0,0 +1,80 @@
|
||||
class Matrix:
|
||||
def __init__(self, **kwargs):
|
||||
if kwargs.get('file'):
|
||||
self.matrix = self.__from_file(kwargs['file'])
|
||||
elif kwargs.get('text'):
|
||||
self.matrix = self.__parse(kwargs['text'])
|
||||
elif kwargs.get('matrix'):
|
||||
self.matrix = kwargs['matrix']
|
||||
|
||||
def __from_file(self, file):
|
||||
with open(file, 'r') as file:
|
||||
|
||||
return self.parse(file.read())
|
||||
|
||||
def __parse(self, matrix):
|
||||
out = []
|
||||
for row in matrix.split('\n'):
|
||||
out.append([int(col) for col in row.split()])
|
||||
|
||||
return out
|
||||
|
||||
@property
|
||||
def sum(self):
|
||||
row_sum = []
|
||||
col_sum = [0 for col in range(len(self.matrix)+1)]
|
||||
|
||||
for row in self.matrix:
|
||||
row_sum.append(sum(row))
|
||||
for idx, col in enumerate(row):
|
||||
col_sum[idx] += col
|
||||
|
||||
return row_sum, col_sum
|
||||
|
||||
@property
|
||||
def sort_row(self):
|
||||
row_sum, col_sum = self.sum
|
||||
out = []
|
||||
for index, row in sorted( [i for i in enumerate(row_sum)], key=lambda x:x[1]):
|
||||
out.append(self.matrix[index])
|
||||
|
||||
return Matrix(matrix=out);
|
||||
|
||||
@property
|
||||
def sort_col(self):
|
||||
row_sum, col_sum = self.sum
|
||||
out = []
|
||||
places = sorted( [i for i in enumerate(col_sum)], key=lambda x:x[1])
|
||||
|
||||
for index, row in enumerate(self.matrix):
|
||||
out.append([row[i[0]] for i in places])
|
||||
|
||||
return Matrix(matrix=out)
|
||||
|
||||
def __get_element_width(self):
|
||||
max = 0
|
||||
for row in self.matrix:
|
||||
for element in row:
|
||||
if max < len(str(element)):
|
||||
max = len(str(element))
|
||||
|
||||
return max
|
||||
|
||||
def __str__(self, width=None):
|
||||
width = self.__get_element_width()
|
||||
out = ''
|
||||
|
||||
for row in self.matrix:
|
||||
out +=' | '.join(['{:{}d}'.format(el, width) for el in row])
|
||||
out += '\n'
|
||||
|
||||
return out
|
||||
|
||||
m1 ='''10 5 4 20
|
||||
9 33 27 16
|
||||
11 6 55 3'''
|
||||
|
||||
m1 = Matrix(text=m1)
|
||||
# print(m1.sum)
|
||||
print(m1.sort_row)
|
||||
print(m1.sort_col)
|
@ -1,9 +1,27 @@
|
||||
def char_shift(char, shift):
|
||||
start = ord('a') if char.isupper() else ord('A')
|
||||
char = ord(char) - start
|
||||
shift = (char + shift) % 26
|
||||
return chr(shift + start)
|
||||
|
||||
def caesar(message, shift):
|
||||
pass
|
||||
out = ''
|
||||
for letter in message:
|
||||
if letter.isalpha():
|
||||
letter = char_shift(letter, shift)
|
||||
out += letter
|
||||
|
||||
# or in one line
|
||||
# out += char_shift(letter, shift) if letter.isalpha() else letter
|
||||
|
||||
return out
|
||||
|
||||
def decrypt_caesar(message, shift):
|
||||
return caesar(message, shift - (shift * 2))
|
||||
|
||||
# Add your own assert statements to test your code.
|
||||
|
||||
sentence = 'But the Caesar Cipher is [still used](http://en.wikipedia.org/wiki/ROT13)'
|
||||
shift = 300
|
||||
|
||||
# Add your own assert statements to test your code.
|
||||
assert( decrypt_caesar(caesar(sentence, shift), 300) == sentence )
|
||||
|
@ -1,14 +1,40 @@
|
||||
import fractions
|
||||
|
||||
def compute_divisors(num):
|
||||
pass
|
||||
divisors = [1]
|
||||
for i in range( 2, num//2+1 ): # if num is even, one needs to be added
|
||||
if num % i == 0:
|
||||
divisors.append(i)
|
||||
|
||||
divisors.append( num )
|
||||
|
||||
return divisors
|
||||
|
||||
def sum_of_divisors(num):
|
||||
pass
|
||||
sumOf = 0
|
||||
for i in compute_divisors( num ):
|
||||
sumOf += i
|
||||
|
||||
return sumOf
|
||||
|
||||
def divisor_count(num):
|
||||
pass
|
||||
return len( compute_divisors(num) )
|
||||
|
||||
def get_totatives(num):
|
||||
pass
|
||||
totatives = []
|
||||
for i in range(0, num):
|
||||
gcd = fractions.gcd( i, num )
|
||||
if gcd == 1:
|
||||
totatives.append( i )
|
||||
|
||||
return totatives
|
||||
|
||||
def totient(num):
|
||||
pass
|
||||
return len( get_totatives(num) )
|
||||
|
||||
|
||||
assert( compute_divisors(60) == [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60] )
|
||||
assert( sum_of_divisors(60) == 168 )
|
||||
assert( divisor_count(60) == 12 )
|
||||
assert( get_totatives(30) == [1, 7, 11, 13, 17, 19, 23, 29] )
|
||||
assert( totient(30) == 8 )
|
@ -1,16 +1,23 @@
|
||||
def binary_to_decimal(num):
|
||||
pass
|
||||
if num == 0: return 0
|
||||
decimal = 0
|
||||
|
||||
t = [decimal += 2**i for i, char in enumerate(str(num)[:: -1]) if char != '0']
|
||||
|
||||
|
||||
return int(decimal)
|
||||
|
||||
def decimal_to_binary(num):
|
||||
pass
|
||||
|
||||
print(binary_to_decimal(11100101),binary_to_decimal(10001101),binary_to_decimal(10010000))
|
||||
|
||||
assert binary_to_decimal(0) == 0, "0 to decimal should return 0"
|
||||
assert binary_to_decimal(1011) == 11, "1011 to decimal should return 11"
|
||||
assert binary_to_decimal(101011) == 43, "101011 to decimal should return 43"
|
||||
assert binary_to_decimal(101011101) == 349, "101011101 to decimal should return 349"
|
||||
|
||||
assert decimal_to_binary(0) == 0, "0 to binary should return 0"
|
||||
assert decimal_to_binary(5) == 101, "5 to binary should return 101"
|
||||
assert decimal_to_binary(10) == 1010, "10 to binary should return 1010"
|
||||
assert decimal_to_binary(113) == 1110001, "113 to binary should return 1110001"
|
||||
# assert decimal_to_binary(0) == 0, "0 to binary should return 0"
|
||||
# assert decimal_to_binary(5) == 101, "5 to binary should return 101"
|
||||
# assert decimal_to_binary(10) == 1010, "10 to binary should return 1010"
|
||||
# assert decimal_to_binary(113) == 1110001, "113 to binary should return 1110001"
|
@ -1,60 +1,98 @@
|
||||
class CreditCard:
|
||||
card_types = [
|
||||
# (starts with digits, card type, card length)
|
||||
('4', 'VISA', 16),
|
||||
('34', 'AMEX', 15),
|
||||
('37', 'AMEX', 15),
|
||||
('51', 'MASTERCARD', 16),
|
||||
('52', 'MASTERCARD', 16),
|
||||
('53', 'MASTERCARD', 16),
|
||||
('54', 'MASTERCARD', 16),
|
||||
('55', 'MASTERCARD', 16),
|
||||
('6011', 'DISCOVER', 16),
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def luna_check(cls, card_number):
|
||||
digits = []
|
||||
for index, digit in enumerate(card_number[::-1]):
|
||||
if index % 2:
|
||||
digits += [int(i) for i in str(int(digit)*2)]
|
||||
else:
|
||||
digits.append(int(digit))
|
||||
|
||||
return not bool(sum(digits) % 10)
|
||||
|
||||
def __init__(self, card_number):
|
||||
self.card_number = "the card number"
|
||||
self.card_type = "a string"
|
||||
self.valid = "a boolean"
|
||||
self.card_number = card_number
|
||||
self.is_valid, self.card_type = self.__is_valid() or (False, 'INVALID')
|
||||
|
||||
#Create and add your method called `determine_card_type` to the CreditCard class here:
|
||||
def determine_card_type(self):
|
||||
pass
|
||||
def __card_type(self):
|
||||
for start, type, length in self.card_types:
|
||||
if self.card_number.startswith(start):
|
||||
|
||||
return type, length
|
||||
|
||||
def __is_valid(self):
|
||||
card_type = self.__card_type()
|
||||
if card_type:
|
||||
length = len(self.card_number) == card_type[1]
|
||||
luna = self.luna_check(self.card_number)
|
||||
if length and luna:
|
||||
|
||||
return True, card_type[0]
|
||||
|
||||
#Create and add your method called `check_length` to the CreditCard class here:
|
||||
def check_length(self):
|
||||
pass
|
||||
|
||||
#Create and add your method called 'validate' to the CreditCard class here:
|
||||
def validate(self):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# # print(CreditCard.luna_check('4485040993287616'))
|
||||
# cc = CreditCard('4485040993287616')
|
||||
|
||||
# print(cc.is_valid)
|
||||
# exit(0)
|
||||
|
||||
#do not modify assert statements
|
||||
|
||||
cc = CreditCard('9999999999999999')
|
||||
|
||||
assert cc.valid == False, "Credit Card number cannot start with 9"
|
||||
assert cc.card_type == "INVALID", "99... card type is INVALID"
|
||||
assert(cc.is_valid == False)
|
||||
assert(cc.card_type == "INVALID")
|
||||
|
||||
cc = CreditCard('4440')
|
||||
|
||||
assert cc.valid == False, "4440 is too short to be valid"
|
||||
assert cc.is_valid == False, "4440 is too short to be valid"
|
||||
assert cc.card_type == "INVALID", "4440 card type is INVALID"
|
||||
|
||||
cc = CreditCard('5515460934365316')
|
||||
|
||||
assert cc.valid == True, "Mastercard is Valid"
|
||||
assert cc.is_valid == True, "Mastercard is Valid"
|
||||
assert cc.card_type == "MASTERCARD", "card_type is MASTERCARD"
|
||||
|
||||
cc = CreditCard('6011053711075799')
|
||||
|
||||
assert cc.valid == True, "Discover Card is Valid"
|
||||
assert cc.is_valid == True, "Discover Card is Valid"
|
||||
assert cc.card_type == "DISCOVER", "card_type is DISCOVER"
|
||||
|
||||
cc = CreditCard('379179199857686')
|
||||
|
||||
assert cc.valid == True, "AMEX is Valid"
|
||||
assert cc.is_valid == True, "AMEX is Valid"
|
||||
assert cc.card_type == "AMEX", "card_type is AMEX"
|
||||
|
||||
cc = CreditCard('4929896355493470')
|
||||
|
||||
assert cc.valid == True, "Visa Card is Valid"
|
||||
assert cc.is_valid == True, "Visa Card is Valid"
|
||||
assert cc.card_type == "VISA", "card_type is VISA"
|
||||
|
||||
cc = CreditCard('4329876355493470')
|
||||
|
||||
assert cc.valid == False, "This card does not meet mod10"
|
||||
assert cc.is_valid == False, "This card does not meet mod10"
|
||||
assert cc.card_type == "INVALID", "card_type is INVALID"
|
||||
|
||||
cc = CreditCard('339179199857685')
|
||||
|
||||
assert cc.valid == False, "Validates mod10, but invalid starting numbers for AMEX"
|
||||
assert cc.is_valid == False, "Validates mod10, but invalid starting numbers for AMEX"
|
||||
assert cc.card_type == "INVALID", "card_type is INVALID"
|
216
README.md
216
README.md
@ -1,216 +0,0 @@
|
||||
# Python Fundamentals
|
||||
|
||||
### Learning Objectives
|
||||
|
||||
***Students Will Be Able To...***
|
||||
|
||||
* Create a variable in python
|
||||
* Assign the different data structures to variables
|
||||
* Write python statements using control flow
|
||||
* Write python statements using loops and iteration
|
||||
|
||||
---
|
||||
### Context
|
||||
|
||||
* The fundamentals of programming translate throughout every language
|
||||
* Like learning any new language we're going to start with the basics and build up
|
||||
* If you wanted to learn English you wouldn't start by reading a novel, but with the alphabet
|
||||
|
||||
### Variables
|
||||
|
||||
* Variables are a way to store and save data for use
|
||||
* This is called `assignment`. You are assigning a value to a variable
|
||||
* Declaring Variables
|
||||
* Cannot start with a number
|
||||
* Cannot declare with special characters
|
||||
* Written in snake case
|
||||
* Open up Python in the terminal
|
||||
|
||||
```python
|
||||
name = "Jason"
|
||||
fav_num = 8
|
||||
turtles = ["Raph", "Leo", "Mickey", "Donny"]
|
||||
```
|
||||
|
||||
### Data Types
|
||||
|
||||
* Now you may have noticed that variables can hold different `types` of values
|
||||
* These are called `Data Types` and python3 has [many built-in types](https://docs.python.org/3/library/stdtypes.htm)
|
||||
* [Strings](https://docs.python.org/3/library/stdtypes.html#str)
|
||||
* Sequence of 0 or more characters(a-z, A-Z, 0-9, !,@,#, ect).
|
||||
* python type `str()` or with the literal `''` or `""`
|
||||
* [methods to know](https://docs.python.org/3/library/stdtypes.html#string-methods)
|
||||
* `.format()` [https://pyformat.info/](More info)
|
||||
* `.isdigit()`, `islower()`, `isupper()`, check to see if the string is a digit and so on. There are many more like these
|
||||
* `.lower()`, `.upper()` changed the string to lower and up case
|
||||
* `.split()` changes the string to a list based around the character[s] given
|
||||
* [Numbers](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex)
|
||||
* [Floats](https://docs.python.org/3/library/functions.html#float)
|
||||
* decimals
|
||||
* python type `float()` or with the literal `23.3`
|
||||
* methods to know
|
||||
* `is_integer()` Return `True` if the float a whole number, and `False` otherwise
|
||||
* [Integers](https://docs.python.org/3/library/functions.html#int)
|
||||
* whole number
|
||||
* python type `int()` or with the literal '4'
|
||||
* [Lists](https://docs.python.org/3/library/stdtypes.html#lists)
|
||||
* Ordered sequence of items
|
||||
* python type `list()` or the literal `['a', 'b', 'c']`
|
||||
* methods to know
|
||||
* `.append()` adds a item to a list
|
||||
* `.pop()` removes and returns the last item from the list
|
||||
* [Dictionary](https://docs.python.org/3/library/stdtypes.html#dict)
|
||||
* Collections of key, value pairs
|
||||
* python type `dict()` or the literal `{'key':'value'}`
|
||||
* methods to know
|
||||
* `.get()` return the value of a give key, or a default value if its not found
|
||||
* `.values()` returns a `list` of values in the Dictionary
|
||||
* `.keys()` returns a `list` of keys in the Dictionary
|
||||
* [Booleans](https://docs.python.org/3/library/stdtypes.html#boolean-values)
|
||||
* Represents something that is `True` or `False`
|
||||
* python type `bool()` or the literal `True` or `False`
|
||||
* [Range](https://docs.python.org/3/library/stdtypes.html#range)
|
||||
* `Range()` is a special type that represents range of numbers
|
||||
* [None](https://docs.python.org/3/library/stdtypes.html#the-null-object)
|
||||
* nothing, nothing at all
|
||||
* python type ... there is one way, the literal way `None`
|
||||
|
||||
### Notable built in functions
|
||||
* `len()` return the length of the given *sequence*
|
||||
* `help()` shows help documentation of the given object
|
||||
* `dir()` show the available methods of the give object
|
||||
|
||||
#### Math Operators
|
||||
All math operators can be done on both floats and ints
|
||||
Python comes with the following symbols for mathematical operators.
|
||||
|
||||
* `+` add
|
||||
* `-` subtract
|
||||
* `*` multiplication
|
||||
* `\` division
|
||||
* `\\` floor divided, always returns a whole number
|
||||
* `%` modulo: finds the remainder after division
|
||||
* The language also supports PEMDAS
|
||||
* `5+(50+5)`
|
||||
|
||||
#### Comparison Operators
|
||||
|
||||
* `==` equality
|
||||
* `!=` not equal
|
||||
* `<` less then
|
||||
* `<=` less then or equal to
|
||||
* `>` greater then
|
||||
* `>=` greater then or equal to
|
||||
|
||||
|
||||
#### Control Flow
|
||||
|
||||
* Now we have reached `if/else` statements
|
||||
* If an expression you passed in is `True` do something
|
||||
* `else` do something `else`
|
||||
|
||||
```python
|
||||
if expression == true:
|
||||
run code
|
||||
|
||||
if name == "Jason":
|
||||
print("That is an awesome name")
|
||||
else:
|
||||
print("You should get a different name")
|
||||
|
||||
if number > 100:
|
||||
print("That's a big number")
|
||||
elif number > 50 && number < 100:
|
||||
print("That's a medium number")
|
||||
else:
|
||||
print("Your number is puny")
|
||||
```
|
||||
* Things to note
|
||||
* Put a colon after the expression you want to evaluate to start the `if` body
|
||||
* `if` to `elif` to `else`
|
||||
* indents show what code is part of the body of the statement and where it ends
|
||||
|
||||
|
||||
#### Lists and Indexing
|
||||
|
||||
* What if you wanted to store more data.
|
||||
* Can be assigned to variables
|
||||
* Can hold different data types at once
|
||||
* The values are indexed for us starting at zero
|
||||
|
||||
```python
|
||||
my_list = ["Jason", "Anna Kendrick", 2015, True]
|
||||
|
||||
my_list[0] == "Jason" # True
|
||||
|
||||
my_list[2] == 2016 # False
|
||||
```
|
||||
* Just a heads up indexing through a list is similar to indexing with strings.
|
||||
* the value at index zero will be the first element in the list, or the first letter in a string
|
||||
|
||||
#### Functions and Statements
|
||||
|
||||
* We declare our functions with the word `def` for define
|
||||
* Functions follow the same naming principles as declaring variables
|
||||
* Snake case
|
||||
* Do not start with numbers or special characters
|
||||
* Remember how we used white space to organize our code with if/else statements. Well that idea holds true everywhere in Python
|
||||
|
||||
```python
|
||||
def my_name():
|
||||
return "My name is Jason"
|
||||
```
|
||||
* Functions allow us to build code that is reusable
|
||||
* This follows the concept of **DRY - Don't Repeat Yourself**
|
||||
* Functions can also take arguments. These allow our functions to be more dynamic
|
||||
|
||||
```python
|
||||
def my_name(name):
|
||||
return "My name is " + name
|
||||
```
|
||||
|
||||
* When there is no `return` statement, the function *implicitly* returns `None`
|
||||
|
||||
### `for` loops
|
||||
|
||||
`for` loops *iterate* over a *sequence*. There are 2 parts to a `for` loop, the *statement* and the *body*. The *statement* tells the loop what to *iterate* over and *assigns* the loop variable. The body tells python what to do in each iteration. Before each iteration the loop variable is assigned to the next value in the *sequence*, in oder from the zero index to the last item.
|
||||
|
||||
```python
|
||||
teachers = ['billy', 'tom', 'jason', 'jeff']
|
||||
for teacher in teachers:
|
||||
print( teacher.capitalize() )
|
||||
```
|
||||
|
||||
In this loop we *iterate* over each teacher in the loop and print there name capitalized.
|
||||
|
||||
### `while` loops
|
||||
|
||||
Unlike `for` loops, `while` are not bound to a sequence and can continue for ever.
|
||||
like a `for` loop, `while` loops have 2 parts, an *statement* and a body.
|
||||
|
||||
```python
|
||||
num = input('Please enter a whole number: ')
|
||||
while not num.isdigit():
|
||||
print('Not a whole number!')
|
||||
num = input('Please enter a whole number: ')
|
||||
```
|
||||
|
||||
|
||||
### loop control
|
||||
There are 2 ways can control what loops. They both with `for` and `while` loops in the same way.
|
||||
|
||||
* [`break`](https://docs.python.org/3/reference/simple_stmts.html#break)
|
||||
The `break` *statement* stops loop and allows python to move on to the rest of the script. If you are using a `while` loop, you should have a break *statement* to stop the loop.
|
||||
```python
|
||||
count = 1
|
||||
while True:
|
||||
count *= count
|
||||
if count > 100: break
|
||||
```
|
||||
* [`continue`](https://docs.python.org/3/reference/simple_stmts.html#continue)
|
||||
The `continue` *statement* skips to the next iteration of the loop. Generally, `continue` statement are at the top of the loop body.
|
||||
```python
|
||||
for num in range(100):
|
||||
if num%10 != 0: continue
|
||||
print(num)
|
||||
```
|
Reference in New Issue
Block a user