migrate to git.charlotte.sh
This commit is contained in:
commit
fbd588721e
412 changed files with 13750 additions and 0 deletions
8
python-csi160/week04/Week-4Practice-Problems/part1.py
Normal file
8
python-csi160/week04/Week-4Practice-Problems/part1.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
number = int(input('Enter an integer: '))
|
||||
|
||||
# Determine odd or even
|
||||
|
||||
if number % 2 == 0:
|
||||
print("even")
|
||||
else:
|
||||
print("odd")
|
26
python-csi160/week04/Week-4Practice-Problems/part10.py
Normal file
26
python-csi160/week04/Week-4Practice-Problems/part10.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
def is_legal_bishop_move(initial_x, initial_y, final_x, final_y):
|
||||
"""Returns True / False whether a given move is a legal move for the Bishop.
|
||||
|
||||
This function assumes that there are no other pieces on the board.
|
||||
|
||||
:param initial_x: (int) Horizontal 1-8
|
||||
:param initial_y: (int) Vertical 1-8
|
||||
:param final_x: (int) Horizontal 1-8
|
||||
:param final_y: (int) Vertical 1-8
|
||||
:return: (bool) True / False if it is a legal move
|
||||
"""
|
||||
# Complete this function
|
||||
if initial_x == final_x and initial_y == final_y:
|
||||
return False
|
||||
return abs(initial_x - final_x) == abs(initial_y - final_y)
|
||||
|
||||
|
||||
# Leave this part for easily testing your function
|
||||
print('(4, 4, 3, 5) It is', is_legal_bishop_move(4, 4, 3, 5), 'that this is a legal move')
|
||||
print('(1, 4, 4, 7) It is', is_legal_bishop_move(1, 4, 4, 7), 'that this is a legal move')
|
||||
print('(5, 4, 2, 1) It is', is_legal_bishop_move(5, 4, 2, 1), 'that this is a legal move')
|
||||
print('(5, 4, 1, 1) It is', is_legal_bishop_move(5, 4, 1, 1), 'that this is a legal move')
|
||||
print('(5, 4, 6, 4) It is', is_legal_bishop_move(5, 4, 6, 4), 'that this is a legal move')
|
||||
print('(1, 1, 1, 1) It is', is_legal_bishop_move(1, 1, 1, 1), 'that this is a legal move')
|
||||
|
||||
|
12
python-csi160/week04/Week-4Practice-Problems/part2.py
Normal file
12
python-csi160/week04/Week-4Practice-Problems/part2.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
|
||||
number1 = int(input('Enter an integer: '))
|
||||
number2 = int(input('Enter an integer: '))
|
||||
|
||||
|
||||
# Print the smaller number
|
||||
|
||||
|
||||
if number1 < number2:
|
||||
print(number1)
|
||||
else:
|
||||
print(number2)
|
9
python-csi160/week04/Week-4Practice-Problems/part3.py
Normal file
9
python-csi160/week04/Week-4Practice-Problems/part3.py
Normal file
|
@ -0,0 +1,9 @@
|
|||
num = int(input('Enter a number: '))
|
||||
|
||||
# Print out result
|
||||
if num > 0:
|
||||
print("1")
|
||||
elif num < 0:
|
||||
print("-1")
|
||||
else:
|
||||
print("0")
|
7
python-csi160/week04/Week-4Practice-Problems/part4.py
Normal file
7
python-csi160/week04/Week-4Practice-Problems/part4.py
Normal file
|
@ -0,0 +1,7 @@
|
|||
year = int(input('Enter the year: '))
|
||||
|
||||
# Prints Yes or No based on if year is in the 21st century
|
||||
if year >= 2001 and year <= 2100:
|
||||
print("YES")
|
||||
else:
|
||||
print("NO")
|
16
python-csi160/week04/Week-4Practice-Problems/part5.py
Normal file
16
python-csi160/week04/Week-4Practice-Problems/part5.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
def one_positive(num1, num2):
|
||||
"""Given two non-zero integers, returns True if exactly one of
|
||||
them is positive and returns False otherwise.
|
||||
|
||||
:param num1: non-zero integer
|
||||
:param num2: non-zero integer
|
||||
:return: True / False
|
||||
"""
|
||||
# Complete this function
|
||||
return (num1 > 0) ^ (num2 > 0)
|
||||
|
||||
|
||||
# Leave this part for easily testing your function
|
||||
print('(5, 7) It is', one_positive(5, 7), 'that only one of these numbers is positive')
|
||||
print('(-5, -7) It is', one_positive(-5, -7), 'that only one of these numbers is positive')
|
||||
print('(5, -7) It is', one_positive(5, -7), 'that only one of these numbers is positive')
|
27
python-csi160/week04/Week-4Practice-Problems/part6.py
Normal file
27
python-csi160/week04/Week-4Practice-Problems/part6.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
def ascending_digits(num):
|
||||
"""Given a three-digit integer X consisting of three different digits,
|
||||
returns True if its three digits are going in an ascending
|
||||
order from left to right and returns False otherwise.
|
||||
|
||||
:param num: three digit integer
|
||||
:return: True / False
|
||||
"""
|
||||
# Complete this function
|
||||
# Hint: assign variables for each digit first
|
||||
# then build the comparison
|
||||
|
||||
a = num // 100
|
||||
b = num % 100 // 10
|
||||
c = num % 10
|
||||
|
||||
ascending=False
|
||||
if a < b and b < c:
|
||||
ascending = True
|
||||
|
||||
return ascending
|
||||
|
||||
|
||||
# Leave this part for easily testing your function
|
||||
print('(136) It is', ascending_digits(136), 'that the digits are ascending')
|
||||
print('(462) It is', ascending_digits(462), 'that the digits are ascending')
|
||||
print('(823) It is', ascending_digits(823), 'that the digits are ascending')
|
20
python-csi160/week04/Week-4Practice-Problems/part7.py
Normal file
20
python-csi160/week04/Week-4Practice-Problems/part7.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
month = int(input('Enter the month: '))
|
||||
|
||||
|
||||
if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
|
||||
print("31")
|
||||
elif month == 4 or month == 6 or month == 9 or month == 11:
|
||||
print("30")
|
||||
else:
|
||||
print("28")
|
||||
|
||||
'''
|
||||
month = month.lower()
|
||||
|
||||
if month == "january" or month == "march" or month == "may" or month == "july" or month == "august" or month == "october" or month == "december":
|
||||
print("31")
|
||||
elif month == "april" or month == "june" or month == "september" or month == "november":
|
||||
print("30")
|
||||
else:
|
||||
print("28")
|
||||
'''
|
18
python-csi160/week04/Week-4Practice-Problems/part8.py
Normal file
18
python-csi160/week04/Week-4Practice-Problems/part8.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
def is_legal_rook_move(initial_x, initial_y, final_x, final_y):
|
||||
"""Returns True / False whether a given move is a legal move for the Rook.
|
||||
|
||||
This function assumes that there are no other pieces on the board.
|
||||
|
||||
:param initial_x: (int) Horizontal 1-8
|
||||
:param initial_y: (int) Vertical 1-8
|
||||
:param final_x: (int) Horizontal 1-8
|
||||
:param final_y: (int) Vertical 1-8
|
||||
:return: (bool) True / False if it is a legal move
|
||||
"""
|
||||
# Complete this function
|
||||
return (initial_x != final_x) ^ (initial_y != final_y)
|
||||
|
||||
# Leave this part for easily testing your function
|
||||
print('(4, 4, 5, 5) It is', is_legal_rook_move(4, 4, 5, 5), 'that this is a legal move')
|
||||
print('(1, 4, 6, 4) It is', is_legal_rook_move(1, 4, 6, 4), 'that this is a legal move')
|
||||
print('(1, 4, 1, 7) It is', is_legal_rook_move(1, 4, 1, 7), 'that this is a legal move')
|
24
python-csi160/week04/Week-4Practice-Problems/part9.py
Normal file
24
python-csi160/week04/Week-4Practice-Problems/part9.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
def is_legal_king_move(initial_x, initial_y, final_x, final_y):
|
||||
"""Returns True / False whether a given move is a legal move for the King.
|
||||
|
||||
This function assumes that there are no other pieces on the board.
|
||||
|
||||
:param initial_x: (int) Horizontal 1-8
|
||||
:param initial_y: (int) Vertical 1-8
|
||||
:param final_x: (int) Horizontal 1-8
|
||||
:param final_y: (int) Vertical 1-8
|
||||
:return: (bool) True / False if it is a legal move
|
||||
"""
|
||||
# Complete this function
|
||||
|
||||
if initial_x == final_x and initial_y == final_y:
|
||||
return False
|
||||
return abs(initial_x - final_x) <= 1 and abs(initial_y - final_y) <= 1
|
||||
|
||||
|
||||
|
||||
|
||||
# Leave this part for easily testing your function
|
||||
print('(4, 4, 5, 5) It is', is_legal_king_move(4, 4, 5, 5), 'that this is a legal move')
|
||||
print('(1, 4, 6, 4) It is', is_legal_king_move(1, 4, 6, 4), 'that this is a legal move')
|
||||
print('(1, 4, 1, 3) It is', is_legal_king_move(1, 4, 1, 3), 'that this is a legal move')
|
14
python-csi160/week04/week04_labs/croce_week04_lab_01.py
Normal file
14
python-csi160/week04/week04_labs/croce_week04_lab_01.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
|
||||
#1. Implement a voting test. The user enters their age and then the program prints either,
|
||||
# “You must be 18 to vote” or “You are of voting age”.
|
||||
|
||||
def voting_test():
|
||||
age = int(input("age: "))
|
||||
if age >= 18:
|
||||
print("You are of voting age")
|
||||
else:
|
||||
print("You must be 18 to vote")
|
||||
|
||||
|
||||
voting_test()
|
||||
|
25
python-csi160/week04/week04_labs/croce_week04_lab_02.py
Normal file
25
python-csi160/week04/week04_labs/croce_week04_lab_02.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
|
||||
#2. Ask the user to enter a grade percentage. Convert the grade into a letter
|
||||
# grade using the official Champlain College grading scale. For instance, if the user
|
||||
# types 99 then print A+.
|
||||
|
||||
def grade_to_letter():
|
||||
grade = int(input("GPA (100pt scale): "))
|
||||
if grade >= 97: letter = "A+"
|
||||
elif grade >= 93: letter = "A"
|
||||
elif grade >= 90: letter = "A-"
|
||||
elif grade >= 87: letter = "B+"
|
||||
elif grade >= 83: letter = "B"
|
||||
elif grade >= 80: letter = "B-"
|
||||
elif grade >= 77: letter = "C+"
|
||||
elif grade >= 73: letter = "C"
|
||||
elif grade >= 70: letter = "C-"
|
||||
elif grade >= 67: letter = "C+"
|
||||
elif grade >= 63: letter = "C"
|
||||
elif grade >= 60: letter = "C-"
|
||||
else: letter = "F"
|
||||
print(letter)
|
||||
|
||||
|
||||
grade_to_letter()
|
||||
|
16
python-csi160/week04/week04_labs/croce_week04_lab_03.py
Normal file
16
python-csi160/week04/week04_labs/croce_week04_lab_03.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
|
||||
# 3. Write a program that asks for two numbers. If the sum of the numbers is greater than
|
||||
# 100, print "They add up to a big number" if it is less than/equal to 100 than print "They add up to ____".
|
||||
|
||||
def add_to_big_number():
|
||||
num1 = int(input("num1: "))
|
||||
num2 = int(input("num2: "))
|
||||
sum = num1 + num2
|
||||
if sum > 100:
|
||||
print("They add up to a big number")
|
||||
else:
|
||||
print("They add up to " + str(sum))
|
||||
|
||||
|
||||
add_to_big_number()
|
||||
|
17
python-csi160/week04/week04_labs/croce_week04_lab_04.py
Normal file
17
python-csi160/week04/week04_labs/croce_week04_lab_04.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
|
||||
#4. Implement a random number guessing game. The computer will pick a number at random from
|
||||
# 0-9, the user will be asked to guess the number. Inform the user if they get the answer correct.
|
||||
|
||||
from random import randint
|
||||
|
||||
def random_number_game():
|
||||
num = randint(0,9)
|
||||
guess = int(input("random number guess(0-9): "))
|
||||
if guess == num:
|
||||
print("correct! you win")
|
||||
else:
|
||||
print("incorrect, the anser was", num)
|
||||
|
||||
|
||||
random_number_game()
|
||||
|
13
python-csi160/week04/week04_labs/croce_week04_lab_05.py
Normal file
13
python-csi160/week04/week04_labs/croce_week04_lab_05.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
|
||||
# 5. Write a function to compute whether a given year is a leap year in the Hebrew calendar.
|
||||
|
||||
def is_hebrew_leap_year(year):
|
||||
year_of_cycle = year % 19
|
||||
if year_of_cycle in (3, 6, 8, 11, 14, 17, 19):
|
||||
print(str(year) + " is year number " + str(year_of_cycle) + " of the cycle and is therefore a leap year.")
|
||||
else:
|
||||
print(str(year) + " is year number " + str(year_of_cycle) + " of the cycle and is therefore NOT a leap year.")
|
||||
|
||||
|
||||
is_hebrew_leap_year(5779)
|
||||
is_hebrew_leap_year(5780)
|
85
python-csi160/week04/week04_labs/croce_week04_labs_ALL.py
Normal file
85
python-csi160/week04/week04_labs/croce_week04_labs_ALL.py
Normal file
|
@ -0,0 +1,85 @@
|
|||
|
||||
#1. Implement a voting test. The user enters their age and then the program prints either,
|
||||
# “You must be 18 to vote” or “You are of voting age”.
|
||||
|
||||
def voting_test():
|
||||
age = int(input("age: "))
|
||||
if age >= 18:
|
||||
print("You are of voting age")
|
||||
else:
|
||||
print("You must be 18 to vote")
|
||||
|
||||
|
||||
voting_test()
|
||||
|
||||
|
||||
#2. Ask the user to enter a grade percentage. Convert the grade into a letter
|
||||
# grade using the official Champlain College grading scale. For instance, if the user
|
||||
# types 99 then print A+.
|
||||
|
||||
def grade_to_letter():
|
||||
grade = int(input("GPA (100pt scale): "))
|
||||
if grade >= 97: letter = "A+"
|
||||
elif grade >= 93: letter = "A"
|
||||
elif grade >= 90: letter = "A-"
|
||||
elif grade >= 87: letter = "B+"
|
||||
elif grade >= 83: letter = "B"
|
||||
elif grade >= 80: letter = "B-"
|
||||
elif grade >= 77: letter = "C+"
|
||||
elif grade >= 73: letter = "C"
|
||||
elif grade >= 70: letter = "C-"
|
||||
elif grade >= 67: letter = "C+"
|
||||
elif grade >= 63: letter = "C"
|
||||
elif grade >= 60: letter = "C-"
|
||||
else: letter = "F"
|
||||
print(letter)
|
||||
|
||||
|
||||
grade_to_letter()
|
||||
|
||||
|
||||
# 3. Write a program that asks for two numbers. If the sum of the numbers is greater than
|
||||
# 100, print "They add up to a big number" if it is less than/equal to 100 than print "They add up to ____".
|
||||
|
||||
def add_to_big_number():
|
||||
num1 = int(input("num1: "))
|
||||
num2 = int(input("num2: "))
|
||||
sum = num1 + num2
|
||||
if sum > 100:
|
||||
print("They add up to a big number")
|
||||
else:
|
||||
print("They add up to " + str(sum))
|
||||
|
||||
|
||||
add_to_big_number()
|
||||
|
||||
|
||||
#4. Implement a random number guessing game. The computer will pick a number at random from
|
||||
# 0-9, the user will be asked to guess the number. Inform the user if they get the answer correct.
|
||||
|
||||
from random import randint
|
||||
|
||||
def random_number_game():
|
||||
num = randint(0,9)
|
||||
guess = int(input("random number guess(0-9): "))
|
||||
if guess == num:
|
||||
print("correct! you win")
|
||||
else:
|
||||
print("incorrect, the anser was", num)
|
||||
|
||||
|
||||
random_number_game()
|
||||
|
||||
|
||||
# 5. Write a function to compute whether a given year is a leap year in the Hebrew calendar.
|
||||
|
||||
def is_hebrew_leap_year(year):
|
||||
year_of_cycle = year % 19
|
||||
if year_of_cycle in (3, 6, 8, 11, 14, 17, 19):
|
||||
print(str(year) + " is year number " + str(year_of_cycle) + " of the cycle and is therefore a leap year.")
|
||||
else:
|
||||
print(str(year) + " is year number " + str(year_of_cycle) + " of the cycle and is therefore NOT a leap year.")
|
||||
|
||||
|
||||
is_hebrew_leap_year(5779)
|
||||
is_hebrew_leap_year(5780)
|
Loading…
Add table
Add a link
Reference in a new issue