migrate to git.charlotte.sh
This commit is contained in:
commit
fbd588721e
412 changed files with 13750 additions and 0 deletions
7
python-csi160/week06/week6practiceproblems/part1.py
Normal file
7
python-csi160/week06/week6practiceproblems/part1.py
Normal file
|
@ -0,0 +1,7 @@
|
|||
#1 initialize the loop
|
||||
x = int(input('What number do you want to start at? '))
|
||||
|
||||
# Print countdown using while loop with one number per line
|
||||
while x > 0:
|
||||
print(x)
|
||||
x -= 1
|
8
python-csi160/week06/week6practiceproblems/part2.py
Normal file
8
python-csi160/week06/week6practiceproblems/part2.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
a = int(input('What number do you want to start at? '))
|
||||
b = int(input('What number do you want to end at? '))
|
||||
|
||||
# Print all numbers from a to b inclusively. Assume (a ≤ b)
|
||||
|
||||
while a <= b:
|
||||
print(a)
|
||||
a += 1
|
18
python-csi160/week06/week6practiceproblems/part3.py
Normal file
18
python-csi160/week06/week6practiceproblems/part3.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
def all_squares(n):
|
||||
"""Returns a list that contains all the squares of positive integers
|
||||
where the square is less than or equal to N, in ascending order.
|
||||
|
||||
:param n: (int) Upper bound
|
||||
:return: (list) List of squares
|
||||
"""
|
||||
squares = []
|
||||
i = 1
|
||||
while i * i <= n:
|
||||
squares.append(i * i)
|
||||
i += 1
|
||||
return squares
|
||||
|
||||
|
||||
# Leave this part for easily testing your function
|
||||
print('all_squares(50) returns:', all_squares(50))
|
||||
print('all_squares(9) returns:', all_squares(9))
|
21
python-csi160/week06/week6practiceproblems/part4.py
Normal file
21
python-csi160/week06/week6practiceproblems/part4.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
def introductions(names):
|
||||
"""Prints introductions.
|
||||
|
||||
An introduction is 'Hello' followed by a space and a name.
|
||||
Include a newline after each introduction.
|
||||
Example:
|
||||
'Hello Josh'
|
||||
|
||||
Important: Your function should use print() and not have a return statement
|
||||
|
||||
:param names: (list) First Names as strings
|
||||
:return: None
|
||||
"""
|
||||
for name in names:
|
||||
print("Hello", name)
|
||||
|
||||
|
||||
# Leave this part for easily testing your function
|
||||
sample_names = input('Enter the names separated by a comma and a space: ').split(', ')
|
||||
introductions(sample_names)
|
||||
|
25
python-csi160/week06/week6practiceproblems/part5.py
Normal file
25
python-csi160/week06/week6practiceproblems/part5.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
def num_distinct_elements(numbers):
|
||||
"""Determine the number of distinct elements in the list
|
||||
|
||||
:param numbers: (list) A list of numbers (int or float)
|
||||
:return: (int) Number of distinct elements
|
||||
"""
|
||||
# Use the function sorted() to temporarily sort the numbers
|
||||
# into ascending order or use the method .sort() to permanently
|
||||
# sort the list in ascending order.
|
||||
numbers.sort()
|
||||
unique_numbers = 1
|
||||
for i in range(1, len(numbers)):
|
||||
if numbers[i] != numbers[i - 1]:
|
||||
unique_numbers += 1
|
||||
return unique_numbers
|
||||
|
||||
|
||||
|
||||
|
||||
# Leave this part for easily testing your function
|
||||
test1 = [2, 5, 5, 7,2, 9.5, 2, 4]
|
||||
print(f'num_distinct_elements({test1}) returns:', num_distinct_elements(test1))
|
||||
test2 = [2, 1, 1, 7, 1, 9.5, 2, 1]
|
||||
print(f'num_distinct_elements({test2}) returns:', num_distinct_elements(test2))
|
||||
|
15
python-csi160/week06/week6practiceproblems/part6.py
Normal file
15
python-csi160/week06/week6practiceproblems/part6.py
Normal file
|
@ -0,0 +1,15 @@
|
|||
def fibonacci(n):
|
||||
"""Return the nth Fibonacci number.
|
||||
|
||||
param n: (int) position to determine fibonacci of
|
||||
return: (int) value at position n
|
||||
"""
|
||||
|
||||
fib_numbers = [0, 1]
|
||||
for i in range(2, n + 1):
|
||||
fib_numbers.append(fib_numbers[i - 1] + fib_numbers[i - 2])
|
||||
return fib_numbers[n]
|
||||
|
||||
# Leave this part for easily testing your function
|
||||
print('fibonacci(6) returns:', fibonacci(6), 'expected 8')
|
||||
print('fibonacci(3) returns:', fibonacci(3), 'expected 2')
|
Loading…
Add table
Add a link
Reference in a new issue