migrate to git.charlotte.sh
This commit is contained in:
commit
fbd588721e
412 changed files with 13750 additions and 0 deletions
14
python-csi160/midterm/part1.py
Normal file
14
python-csi160/midterm/part1.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
"""
|
||||
Do not use AI! You can schedule to try again if you have a bad grade!
|
||||
Basic Function Write a function named exactly "calculate_discounted_price" which receives 2 parameters: price(float)
|
||||
and discount(float) in percentage, and returns the final price after applying the discount.
|
||||
|
||||
Example:
|
||||
print(calculate_discounted_price(100, 10)) # Output: 90.0
|
||||
print(calculate_discounted_price(50, 50)) # Output: 25.0
|
||||
"""
|
||||
def calculate_discounted_price(price, discount):
|
||||
return price - (price * discount * .01)
|
||||
|
||||
print(calculate_discounted_price(100,10))
|
||||
print(calculate_discounted_price(50,50))
|
62
python-csi160/midterm/part10.py
Normal file
62
python-csi160/midterm/part10.py
Normal file
|
@ -0,0 +1,62 @@
|
|||
'''
|
||||
Do not use AI! You can schedule to try again if you have a bad grade!
|
||||
This is a bonus question, and it might hard. It is not mandatory to solve it. You can skip it if you want.
|
||||
Spiral Matrix is a matrix of size 'n x n' filled with numbers from 1 to (n*n) in a spiral order.
|
||||
Write a function 'spiral_matrix' that receives an integer 'n' as parameter and returns a list of lists representing the spiral matrix.
|
||||
Examples of Spiral Matrix:
|
||||
n = 1
|
||||
[[1]]
|
||||
n = 2
|
||||
[[1, 2],
|
||||
[4, 3]]
|
||||
n = 3
|
||||
[[1, 2, 3],
|
||||
[8, 9, 4],
|
||||
[7, 6, 5]]
|
||||
n = 4
|
||||
[[1, 2, 3, 4],
|
||||
[12, 13, 14, 5],
|
||||
[11, 16, 15, 6],
|
||||
[10, 9, 8, 7]]
|
||||
|
||||
Example:
|
||||
print(spiral_matrix(3))
|
||||
# Output:
|
||||
[[1, 2, 3], [8, 9, 4], [7, 6, 5]]
|
||||
'''
|
||||
|
||||
def spiral_matrix(n):
|
||||
# create an empty n x n matrix filled with zeros
|
||||
matrix = [[0 for i in range(n)] for j in range(n)]
|
||||
|
||||
counter = 1
|
||||
row = 0
|
||||
col = 0
|
||||
|
||||
for i in range(1, n*n):
|
||||
while col < n - i:
|
||||
matrix[row][col] = counter
|
||||
counter += 1
|
||||
col += 1
|
||||
|
||||
while row < n - i:
|
||||
matrix[row][col] = counter
|
||||
counter += 1
|
||||
row += 1
|
||||
|
||||
while col > 0 + (i - 1):
|
||||
matrix[row][col] = counter
|
||||
counter += 1
|
||||
col -= 1
|
||||
|
||||
while row > 0 + i:
|
||||
matrix[row][col] = counter
|
||||
counter += 1
|
||||
row -= 1
|
||||
|
||||
matrix[row][col] = counter
|
||||
return matrix
|
||||
|
||||
print(spiral_matrix(5))
|
||||
# Output:
|
||||
[[1, 2, 3], [8, 9, 4], [7, 6, 5]]
|
15
python-csi160/midterm/part2.py
Normal file
15
python-csi160/midterm/part2.py
Normal file
|
@ -0,0 +1,15 @@
|
|||
'''
|
||||
Do not use AI! You can schedule to try again if you have a bad grade!
|
||||
Write a function named exactly 'divide_and_remainder' and receive a parameter 'num' and a base 'b' and returns a tuple
|
||||
where: he first element is the result of integer division, and the second element is the remainder.
|
||||
|
||||
Example:
|
||||
print(divide_and_remainder(10, 3)) # Output: (3, 1)
|
||||
print(divide_and_remainder(10, 2)) # Output: (5, 0)
|
||||
'''
|
||||
|
||||
def divide_and_remainder(num, b):
|
||||
return (num // b, num % b)
|
||||
|
||||
print(divide_and_remainder(10, 3)) # Output: (3, 1)
|
||||
print(divide_and_remainder(10, 2)) # Output: (5, 0)
|
26
python-csi160/midterm/part3.py
Normal file
26
python-csi160/midterm/part3.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
'''
|
||||
Do not use AI! You can schedule to try again if you have a bad grade!
|
||||
This question can be challenging, so leave it to the end if you are running out of time.
|
||||
Lucas Sequence Write a function 'lucas' which receives an integer 'n' as parameter and returns the 'n-th' element of the
|
||||
lucas sequence. The Lucas sequence is a series of numbers in which each number is the sum of the two preceding just
|
||||
like fibonacci, but the first two numbers are 2 and 1, instead of 0 and 1. So the sequence goes like this:
|
||||
2, 1, 3, 4, 7, 11, 18, 29...
|
||||
|
||||
Do not use recursion to solve this problem.
|
||||
|
||||
Examples:
|
||||
lucas(0) # returns 2
|
||||
lucas(1) # returns 1
|
||||
lucas(2) # returns 3
|
||||
lucas(5) # returns 11
|
||||
'''
|
||||
def lucas(n):
|
||||
lucas_numbers = [2, 1]
|
||||
for i in range(2, n + 1):
|
||||
lucas_numbers.append(lucas_numbers[i - 1] + lucas_numbers[i - 2])
|
||||
return lucas_numbers[n]
|
||||
|
||||
print(lucas(0)) # returns 2
|
||||
print(lucas(1)) # returns 1
|
||||
print(lucas(2)) # returns 3
|
||||
print(lucas(5)) # returns 11
|
19
python-csi160/midterm/part4.py
Normal file
19
python-csi160/midterm/part4.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
'''
|
||||
Do not use AI! You can schedule to try again if you have a bad grade!
|
||||
Write a function named exactly "last2" which receives a list of elements as parameter and then return the second element
|
||||
from the end to the beginning of the list. If the list has less than two elements, the function should return None.
|
||||
|
||||
Example:
|
||||
print(last2(['red', 'blue', 'green', 'yellow'])) # Output: green
|
||||
print(last2([0,1])) # Output: 0
|
||||
print(last2([0])) # Output: None
|
||||
'''
|
||||
def last2(lst):
|
||||
if len(lst) < 2:
|
||||
return None
|
||||
else:
|
||||
return lst[-2]
|
||||
|
||||
print(last2(['red', 'blue', 'green', 'yellow'])) # Output: green
|
||||
print(last2([0,1])) # Output: 0
|
||||
print(last2([0])) # Output: None
|
22
python-csi160/midterm/part5.py
Normal file
22
python-csi160/midterm/part5.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
'''
|
||||
Do not use AI! You can schedule to try again if you have a bad grade!
|
||||
Write a function named exactly "median" which receives a list of element as parameter and returns the median value of
|
||||
the list. The median is the value separating the higher half from the lower half of a data sample. If the list has an
|
||||
even number of elements, the function should return the average of the two middle elements. You should not modify the
|
||||
original list, but you can sort in a copy of the list.
|
||||
|
||||
Example:
|
||||
median([1, 2, 3, 4, 5]) # returns: 3
|
||||
median([1, 2, 3, 4, 5, 6]) # returns: 3.5
|
||||
'''
|
||||
def median(numbers):
|
||||
sortedNums = sorted(numbers)
|
||||
length = len(sortedNums)
|
||||
mid = length // 2
|
||||
if length % 2 == 0: # even number-> avg of middle two
|
||||
return (sortedNums[mid - 1] + sortedNums[mid]) / 2
|
||||
else: # odd number-> middle element
|
||||
return sortedNums[mid]
|
||||
|
||||
print(median([1, 3, 2, 4, 5])) # returns: 3
|
||||
print(median([1, 2, 3, 4, 5, 6])) # returns: 3.5
|
37
python-csi160/midterm/part6.py
Normal file
37
python-csi160/midterm/part6.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
'''
|
||||
Do not use AI! You can schedule to try again if you have a bad grade!
|
||||
You are building a music playlist system. Write a function named 'manage_playlist' which receives 3 paramenters: a
|
||||
list of song names, a new song name to be added to the end of the playlist, and a song name to be searched in the
|
||||
playlist. It should return the index of the searched song in the updated playlist (or None if the song is not found).
|
||||
|
||||
Example:
|
||||
playlist = ['Dream On', 'Bohemian Rhapsody', 'Stairway to Heaven']
|
||||
print(manage_playlist(playlist, 'Hotel California', 'Bohemian Rhapsody')) # Output: 1
|
||||
print(playlist) # Output: ['Dream On', 'Bohemian Rhapsody', 'Stairway to Heaven', 'Hotel California']
|
||||
|
||||
playlist = ['Imagine', 'Confortably Numb']
|
||||
print(manage_playlist(playlist, 'Wish You Were Here', 'Wish You Were Here')) # Output: 2
|
||||
print(playlist) # Output: ['Imagine', 'Confortably Numb', 'Wish You Were Here']
|
||||
|
||||
playlist = ['Imagine', 'Confortably Numb']
|
||||
print(manage_playlist(playlist, 'Wish You Were Here', 'Smoking on the Water')) # Output: None
|
||||
print(playlist) # Output: ['Imagine', 'Confortably Numb', 'Wish You Were Here']
|
||||
'''
|
||||
def manage_playlist(playlist, new_song, search_song):
|
||||
playlist.append(new_song)
|
||||
for i in range(len(playlist)):
|
||||
if playlist[i] == search_song:
|
||||
return i
|
||||
return None
|
||||
|
||||
playlist = ['Dream On', 'Bohemian Rhapsody', 'Stairway to Heaven']
|
||||
print(manage_playlist(playlist, 'Hotel California', 'Bohemian Rhapsody')) # Output: 1
|
||||
print(playlist) # Output: ['Dream On', 'Bohemian Rhapsody', 'Stairway to Heaven', 'Hotel California']
|
||||
|
||||
playlist = ['Imagine', 'Confortably Numb']
|
||||
print(manage_playlist(playlist, 'Wish You Were Here', 'Wish You Were Here')) # Output: 2
|
||||
print(playlist) # Output: ['Imagine', 'Confortably Numb', 'Wish You Were Here']
|
||||
|
||||
playlist = ['Imagine', 'Confortably Numb']
|
||||
print(manage_playlist(playlist, 'Wish You Were Here', 'Smoking on the Water')) # Output: None
|
||||
print(playlist) # Output: ['Imagine', 'Confortably Numb', 'Wish You Were Here']
|
24
python-csi160/midterm/part7.py
Normal file
24
python-csi160/midterm/part7.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
'''
|
||||
Do not use AI! You can schedule to try again if you have a bad grade!
|
||||
Write a function named 'split_full_name' which receives a parameter 'full_name' with 2 or more surnames and returns a
|
||||
tuple with the last name and the first name. The full name will be a string with the following format:
|
||||
"first_name middle1 middle2 ... lastname" where middle1, middle2, ... are optional middle names and the last word is the
|
||||
last name. The function should return a tuple with the last name and the first name. If the full name has only one
|
||||
word, the function should return a tuple with the word as the last name and an empty string as the first name.
|
||||
|
||||
Example:
|
||||
print(split_full_name("John Doe")) # Output: ('Doe', 'John')
|
||||
print(split_full_name("John Doe Smith")) # Output: ('Smith', 'John')
|
||||
print(split_full_name("John")) # Output: ('John', '')
|
||||
print(split_full_name("John Doe Smith Lee")) # Output: ('Lee', 'John')
|
||||
'''
|
||||
def split_full_name(full_name):
|
||||
namelist = full_name.split(" ")
|
||||
if len(namelist) < 2:
|
||||
return (namelist[0], '')
|
||||
return (namelist[-1], namelist[0])
|
||||
|
||||
print(split_full_name("John Doe")) # Output: ('Doe', 'John')
|
||||
print(split_full_name("John Doe Smith")) # Output: ('Smith', 'John')
|
||||
print(split_full_name("John")) # Output: ('John', '')
|
||||
print(split_full_name("John Doe Smith Lee")) # Output: ('Lee', 'John')
|
21
python-csi160/midterm/part8.py
Normal file
21
python-csi160/midterm/part8.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
'''
|
||||
Do not use AI! You can schedule to try again if you have a bad grade!
|
||||
Write a function named 'countdown' which receives a parameter 'n' and returns a list with the countdown from 'n' to 0
|
||||
both included. If 'n' is less than 0, the function should return an empty list.
|
||||
|
||||
Example:
|
||||
print(countdown(5)) # Output: [5, 4, 3, 2, 1, 0]
|
||||
print(countdown(0)) # Output: [0]
|
||||
print(countdown(-1)) # Output: []
|
||||
print(countdown(10)) # Output: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
|
||||
'''
|
||||
def countdown(n):
|
||||
lst = []
|
||||
for i in range(n, -1, -1):
|
||||
lst.append(i)
|
||||
return lst
|
||||
|
||||
print(countdown(5)) # Output: [5, 4, 3, 2, 1, 0]
|
||||
print(countdown(0)) # Output: [0]
|
||||
print(countdown(-1)) # Output: []
|
||||
print(countdown(10)) # Output: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
|
38
python-csi160/midterm/part9.py
Normal file
38
python-csi160/midterm/part9.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
'''
|
||||
Do not use AI! You can schedule to try again if you have a bad grade!
|
||||
Write a functon named 'above_main_diagonal' which receives a square matrix as parameter and returns the sum of the
|
||||
elements above the main diagonal. The main diagonal is the one that goes from the top-left to the bottom-right of the
|
||||
matrix. A number is characterized as above the main diagonal if its row index is less than its column index. The matrix
|
||||
will always have at least one element.
|
||||
|
||||
Example:
|
||||
matrix = [[1, 1, 1],
|
||||
[1, 1, 1],
|
||||
[1, 1, 1]]
|
||||
print(above_main_diagonal(matrix)) # Output: 3. It will sum 1+1+1
|
||||
|
||||
matrix = [[1, 2, 3, 4],
|
||||
[5, 6, 7, 8],
|
||||
[9, 10, 11, 12],
|
||||
[13, 14, 15, 16]]
|
||||
print(above_main_diagonal(matrix)) # Output: 36. It will sum 2+3+4+7+8+12
|
||||
'''
|
||||
def above_main_diagonal(matrix):
|
||||
sum = 0
|
||||
for row in range(len(matrix)):
|
||||
for col in range(len(matrix[0])):
|
||||
if row < col:
|
||||
sum += matrix[row][col]
|
||||
return sum
|
||||
|
||||
|
||||
matrix = [[1, 1, 1],
|
||||
[1, 1, 1],
|
||||
[1, 1, 1]]
|
||||
print(above_main_diagonal(matrix)) # Output: 3. It will sum 1+1+1
|
||||
|
||||
matrix = [[1, 2, 3, 4],
|
||||
[5, 6, 7, 8],
|
||||
[9, 10, 11, 12],
|
||||
[13, 14, 15, 16]]
|
||||
print(above_main_diagonal(matrix)) # Output: 36. It will sum 2+3+4+7+8+12
|
Loading…
Add table
Add a link
Reference in a new issue