generated from jhudsl/OTTR_Template
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Coursera formatting updates #108
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
|
||
```{r, include = FALSE} | ||
ottrpal::set_knitr_image_path() | ||
``` | ||
|
||
# Writing Code: Hands-On Exercise | ||
|
||
Now it’s your turn to try! | ||
|
||
## Code | ||
|
||
Here's the code you need for question #2. | ||
|
||
``` | ||
def calculate_average(numbers): | ||
total = 0 | ||
for number in numbers: | ||
total += number | ||
average = total / len(numbers) | ||
return average | ||
|
||
numbers = [1, 2, 3, 4, "5"] | ||
average = calculate_average(numbers) | ||
print("The average of the numbers is:", average) | ||
``` | ||
|
||
## Questions | ||
|
||
1. Write a prompt to create a function that replaces all instances of the string "three" with the number 3, then create a prompt that allows you to modify the function so that "three", "Three", and "THREE" are all replaced with the number 3. | ||
|
||
1. Write a query that helps you debug the code snippet from above. | ||
|
||
1. Create a query that explains how to retrieve data from an online database. Don't forget to find out what the source of the code is. | ||
|
||
1. Write a prompt to plan out a program that creates and plays a game of Rock, Paper, Scissors. You should create a multi-step plan as well as figure out what your first coding task should be. | ||
|
||
1. Create a prompt that identifies the best coding language for the above game. | ||
|
||
|
||
|
||
```{r} | ||
devtools::session_info() | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
|
||
```{r, include = FALSE} | ||
# enable python code previews; must use python 3 | ||
library(reticulate) | ||
use_python("/usr/bin/python3") | ||
|
||
ottrpal::set_knitr_image_path() | ||
``` | ||
|
||
|
||
# Refactoring Code: Hands-On Exercise | ||
|
||
Now it's your turn to try. | ||
|
||
## The Code | ||
|
||
Let's say you are dusting off some code from your past (no judgment here). You were investigating tweets about [Mr. Trash Wheel](https://www.mrtrashwheel.com/), a beloved Baltimore-based contraption that filters trash out of the waterways. | ||
|
||
**Note**: This code is just an example and was written strictly for educational purposes. | ||
|
||
```{python eval = FALSE, python.reticulate = FALSE} | ||
import tweepy | ||
import pandas | ||
|
||
# Enter your API keys and access tokens here | ||
consumer_key = 'your_consumer_key' | ||
consumer_secret = "your_consumer_secret" | ||
access_token = 'your_access_token' | ||
access_token_secret = 'your_access_token_secret' | ||
|
||
# Authenticate with Twitter API | ||
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | ||
auth.set_access_token(access_token, access_token_secret) | ||
|
||
# Search for tweets containing the search term | ||
tweets = tweepy.Cursor(tweepy.API(auth).search_tweets, q="Mr. Trash Wheel", tweet_mode='extended').items(100) | ||
|
||
# Create empty list to store tweet data | ||
date_data = [] | ||
location_data = [] | ||
text_data = [] | ||
|
||
def get_tweet_length(tweet): | ||
# Return the length of the tweet text | ||
return len(tweet.full_text) | ||
|
||
# Loop through each tweet and extract desired data | ||
for tweet in tweets: | ||
date_info = { | ||
'date': tweet.created_at | ||
} | ||
date_data.append(date_info) | ||
|
||
# Search for tweets containing the search term | ||
tweets = tweepy.Cursor(tweepy.API(auth).search_tweets, q="Mr. Trash Wheel", tweet_mode='extended').items(100) | ||
|
||
# Loop through each tweet and extract desired data | ||
for tweet in tweets: | ||
location_info = { | ||
'location': tweet.user.location | ||
} | ||
location_data.append(location_info) | ||
|
||
# Search for tweets containing the search term | ||
tweets = tweepy.Cursor(tweepy.API(auth).search_tweets, q='Mr. Trash Wheel', tweet_mode='extended').items(100) | ||
|
||
# Loop through each tweet and extract desired data | ||
for tweet in tweets: | ||
text_info = { | ||
'text': tweet.full_text | ||
} | ||
text_data.append(text_info) | ||
|
||
# Combine lists into a dictionary | ||
data = {'date': date_data, | ||
'location': location_data, 'text': text_data} | ||
|
||
# Store results in pandas dataframe | ||
df = pandas.DataFrame(data) | ||
|
||
# Print dataframe | ||
print(df) | ||
``` | ||
|
||
## Questions | ||
|
||
1. Create an AI prompt that fixes any formatting issues with the code that would cause it not to run. | ||
|
||
1. Devise an AI prompt that removes any dead code from your sample above. What gets removed? | ||
|
||
1. Create a prompt that makes the code less repetitive, adhering to the DRY principle. What aspect of the code was repetitive? | ||
|
||
1. Construct a prompt that makes the code more concise. What are some trade-offs that appear in this code between readability and brevity? | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could have been even lazier and just changed the H2's to H1's and that would have made a new chapter in bookdown and for the resulting OTTR stuff. But this is probably better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha, I didn't even think about that