::use_python("/opt/anaconda3/bin/python", required = TRUE) reticulate
Assignment 1
Instructions
Complete the questions by adding the code under the relevant chunks.
Please submit your assignment as html file to me via email. Please call your assignment: “yourlastname_assignment1.html”. For example, I would submit something called “popescu_assignment1.html”. Please make sure that it is all in lower case.
To obtain the same look as this template, make sure that your you assignment preamble or YAML looks like below:
---
title: "Assignment 1"
author: "Your Name"
date: "September 13, 2024"
format:
html:
toc: true
number-sections: true
colorlinks: true
smooth-scroll: true
embed-resources: true
---
Note that Headers such as “Question” need to be marked with #
(i.e # Question
will appear as header in bold and larger font after you render the document. It will thus appear in the Table of Contents). Regular text can just be written one line underneath.
1 Question
Write Python code to evaluate the following expressions:
\(5+ 3 \times (10-2)\)
2 Question
Write Python code to evaluate the following expressions:
\((4 \times 5 + 3) // 2\)
3 Question
Write Python code to evaluate the following expressions:
\(3+Python\)
4 Question
Write Python code to evaluate the following expressions:
\(Hello \times 3 + !\)
5 Question
Store the string “Programming is fun!” in a variable called message.
6 Question
Work with the message variable and use slicing to extract: The first word.
Your output should look like the following:
Programming
7 Question
Work with the message variable and use slicing to extract: The last word.
Your output should look like below:
fun
8 Question
Work with the message variable and use slicing to extract: Every second character in the string.
Your output should look like below:
Pormigi u!
9 Question
Write a Python program to check if the word “Python” exists in the string “I am learning Python programming.”
Python
# Original string
= "I am learning Python programming." sentence
Your output should look like below:
True
10 Question
Replace “Python” with “JavaScript” in the same string and print the result.
Your output should look like below:
I am learning JavaScript programming.
11 Question
Write a program to assign letter grades based on the following rules:
90+
= ‘A’80-89
= ‘B’70-79
= ‘C’60-69
= ‘D’- Below
60
= ‘F’
Test the program with these grades: 92, 76, 85, 59, 68.
Note: Don’t use lists or functions.
Your output should look like below:
Score: 92, Grade: A
Score: 76, Grade: C
Score: 85, Grade: B
Score: 59, Grade: F
Score: 68, Grade: D
12 Question
Write a program using if
, elif
, and else
to print advice based on the weather:
- If it’s “sunny”, print “Wear sunglasses.”
- If it’s “rainy”, print “Take an umbrella.”
- If it’s “windy”, print “Wear a jacket.”
- For any other weather, print “Check the forecast.”
Test your program with at least three different weather conditions.
Note: Don’t use lists or functions.
Your output should look like below:
Wear sunglasses.
Take an umbrella.
Check the forecast.
13 Question
Write a program to determine if a student qualifies for an honors list:
- The student must have a GPA of at least 3.5 and have completed at least 30 credits.
- If the student meets these criteria, print “Qualified for Honors.”
- If the GPA is less than 3.5, print “Improve your GPA.”
- If credits are less than 30, print “Complete more credits.”
Test the program with these scenarios:
- GPA = 3.8, Credits = 32
- GPA = 3.2, Credits = 40
- GPA = 3.6, Credits = 25
Note: Don’t use lists or functions.
Your output should look like below:
Qualified for Honors.
Improve your GPA.
Complete more credits.
14 Question
Write a program to assign letter grades based on the following rules:
90+
= ‘A’80-89
= ‘B’70-79
= ‘C’60-69
= ‘D’- Below
60
= ‘F’
Test the program with these grades: 92, 76, 85, 59, 68.
Note: Use a list.
Your output should look like below:
Score: 92, Grade: A
Score: 76, Grade: C
Score: 85, Grade: B
Score: 59, Grade: F
Score: 68, Grade: D
15 Question
Write a program using if
, elif
, and else
to print advice based on the weather:
- If it’s “sunny”, print “Wear sunglasses.”
- If it’s “rainy”, print “Take an umbrella.”
- If it’s “windy”, print “Wear a jacket.”
- For any other weather, print “Check the forecast.”
Test your program with at least three different weather conditions.
Note: Don’t use functions.
Your output should look like below:
Wear sunglasses.
Take an umbrella.
Check the forecast.
16 Question
Write a program to determine if a student qualifies for an honors list:
- The student must have a GPA of at least 3.5 and have completed at least 30 credits.
- If the student meets these criteria, print “Qualified for Honors.”
- If the GPA is less than 3.5, print “Improve your GPA.”
- If credits are less than 30, print “Complete more credits.”
Test the program with these scenarios:
- GPA = 3.8, Credits = 32
- GPA = 3.2, Credits = 40
- GPA = 3.6, Credits = 25
Note: Use lists and for loop.
Your output should look like below:
Qualified for Honors.
Improve your GPA.
Complete more credits.
17 Question
Write a program that categorizes a list of numbers into three categories:
- “Positive” if the number is greater than 0.
- “Negative” if the number is less than 0.
- “Zero” if the number equals 0.
Use the following list of numbers for testing: [-3, 0, 7, -1, 4, -6, 0]
.
Your output should look like below:
-3 is Negative.
0 is Zero.
7 is Positive.
-1 is Negative.
4 is Positive.
-6 is Negative.
0 is Zero.
18 Question
Write a program that checks if a list of ages qualifies for voting. A person is eligible to vote if they are 18 or older.
Use the following list of ages: [16, 21, 17, 18, 20, 15]
.
For each age, print:
- “Age 16: Not eligible to vote.”
- “Age 21: Eligible to vote.”
Your output should look like below:
Age 16: Not eligible to vote.
Age 21: Eligible to vote.
Age 17: Not eligible to vote.
Age 18: Eligible to vote.
Age 20: Eligible to vote.
Age 15: Not eligible to vote.