Homework for Algorithms
string = input("Enter a Palindrome: ")
def palindrome(words):
for i in words:
if words[0] != words[len(words)-1]:
return "It is not a palindrome"
return "It is a palindrome"
print(palindrome(string))
names = ["Jedd", "Bob", "Dentobot", "Chris", "Kaiyu"]
sorted_names = sorted(names)
print("Names sorted in alphabetical order:")
for name in sorted_names:
print(name)
HW for Data Abstraction
#Greeting With Variables
greeting = "Hello"
name = "Chris"
print(greeting + " " + name )
#Integer Operation
int1 = 5
int2 = 10
print(int1 + int2)
#Float Operation/
float1 = 1123551.23
float2 = 57345.50
print(float1 + float2)
#Format Manipulation
message = "{0} has {1} siblings"
print(message.format("Peyton", "2"))
HW for Booleans
#Question 1
age = 16
if (age >= 18) == True:
print("You can vote")
else:
print("You cannot drive")
#Question 2
yearsInCompany = int(input("How long have you been with the company?: "))
salary = int(input("Enter your salary: "))
bonus = 0
if yearsInCompany >= 5:
bonus = (int(salary) * 1.05) - salary
print("Your bonus is " + "$" + str(bonus))
else:
print("Sorry, no bonus.")
#Question 3
grade = int(input("Enter your grade"))
if grade > 80:
letterGrade = "A"
elif grade > 60:
letterGrade = "B"
elif grade > 50:
letterGrade = "C"
elif grade > 45:
letterGrade = "D"
elif grade > 25:
letterGrade = "E"
else:
letterGrade = "F"
print(letterGrade)
HW for Developing Algorithms
test_scores = [85, 92, 78, 90, 88, 76, 84, 89, 94, 87]
sorted_scores = sorted(test_scores)
n = len(sorted_scores)
if n % 2 == 0:
median = (sorted_scores[n // 2 - 1] + sorted_scores[n // 2]) / 2
else:
median = sorted_scores[n // 2]
print("Median test grade:", median)
2.
import random
diceNumber = random.randint(1,6)
choice = int(input("What number do you think the dice will land on?: "))
if choice == diceNumber:
print("Congratulations, you were correct")
else:
print("You were incorrect, try again")
HW for Procedures
def procedural_abstraction(input_value):
result = input_value * 3 # Multiply the input by 3
return result
input_number = 7
abstraction_result = procedural_abstraction(input_number)
print(f"Result of procedural abstraction for {input_number}: {abstraction_result}")
def summing_machine(first_number, second_number):
total = first_number + second_number
return total
result = summing_machine(7, 5)
print("The sum of 7 and 5 is:", result)