generated from jhudsl/OTTR_Template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/fhdsl/AI_for_software into …
…main
- Loading branch information
Showing
64 changed files
with
9,552 additions
and
3,668 deletions.
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,115 @@ | ||
|
||
|
||
|
||
|
||
# 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() | ||
``` | ||
|
||
``` | ||
## ─ Session info ─────────────────────────────────────────────────────────────── | ||
## setting value | ||
## version R version 4.0.2 (2020-06-22) | ||
## os Ubuntu 20.04.5 LTS | ||
## system x86_64, linux-gnu | ||
## ui X11 | ||
## language (EN) | ||
## collate en_US.UTF-8 | ||
## ctype en_US.UTF-8 | ||
## tz Etc/UTC | ||
## date 2023-11-01 | ||
## | ||
## ─ Packages ─────────────────────────────────────────────────────────────────── | ||
## package * version date lib source | ||
## assertthat 0.2.1 2019-03-21 [1] RSPM (R 4.0.5) | ||
## bookdown 0.24 2023-03-28 [1] Github (rstudio/bookdown@88bc4ea) | ||
## bslib 0.4.2 2022-12-16 [1] CRAN (R 4.0.2) | ||
## cachem 1.0.7 2023-02-24 [1] CRAN (R 4.0.2) | ||
## callr 3.5.0 2020-10-08 [1] RSPM (R 4.0.2) | ||
## cli 3.6.1 2023-03-23 [1] CRAN (R 4.0.2) | ||
## crayon 1.3.4 2017-09-16 [1] RSPM (R 4.0.0) | ||
## desc 1.2.0 2018-05-01 [1] RSPM (R 4.0.3) | ||
## devtools 2.3.2 2020-09-18 [1] RSPM (R 4.0.3) | ||
## digest 0.6.25 2020-02-23 [1] RSPM (R 4.0.0) | ||
## ellipsis 0.3.1 2020-05-15 [1] RSPM (R 4.0.3) | ||
## evaluate 0.20 2023-01-17 [1] CRAN (R 4.0.2) | ||
## fansi 0.4.1 2020-01-08 [1] RSPM (R 4.0.0) | ||
## fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.0.2) | ||
## fs 1.5.0 2020-07-31 [1] RSPM (R 4.0.3) | ||
## glue 1.4.2 2020-08-27 [1] RSPM (R 4.0.5) | ||
## hms 0.5.3 2020-01-08 [1] RSPM (R 4.0.0) | ||
## htmltools 0.5.5 2023-03-23 [1] CRAN (R 4.0.2) | ||
## jquerylib 0.1.4 2021-04-26 [1] CRAN (R 4.0.2) | ||
## jsonlite 1.7.1 2020-09-07 [1] RSPM (R 4.0.2) | ||
## knitr 1.33 2023-03-28 [1] Github (yihui/knitr@a1052d1) | ||
## lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.0.2) | ||
## magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.0.2) | ||
## memoise 2.0.1 2021-11-26 [1] CRAN (R 4.0.2) | ||
## ottrpal 1.0.1 2023-03-28 [1] Github (jhudsl/ottrpal@151e412) | ||
## pillar 1.9.0 2023-03-22 [1] CRAN (R 4.0.2) | ||
## pkgbuild 1.1.0 2020-07-13 [1] RSPM (R 4.0.2) | ||
## pkgconfig 2.0.3 2019-09-22 [1] RSPM (R 4.0.3) | ||
## pkgload 1.1.0 2020-05-29 [1] RSPM (R 4.0.3) | ||
## prettyunits 1.1.1 2020-01-24 [1] RSPM (R 4.0.3) | ||
## processx 3.4.4 2020-09-03 [1] RSPM (R 4.0.2) | ||
## ps 1.4.0 2020-10-07 [1] RSPM (R 4.0.2) | ||
## R6 2.4.1 2019-11-12 [1] RSPM (R 4.0.0) | ||
## readr 1.4.0 2020-10-05 [1] RSPM (R 4.0.2) | ||
## remotes 2.2.0 2020-07-21 [1] RSPM (R 4.0.3) | ||
## rlang 1.1.0 2023-03-14 [1] CRAN (R 4.0.2) | ||
## rmarkdown 2.10 2023-03-28 [1] Github (rstudio/rmarkdown@02d3c25) | ||
## rprojroot 2.0.3 2022-04-02 [1] CRAN (R 4.0.2) | ||
## sass 0.4.5 2023-01-24 [1] CRAN (R 4.0.2) | ||
## sessioninfo 1.1.1 2018-11-05 [1] RSPM (R 4.0.3) | ||
## stringi 1.5.3 2020-09-09 [1] RSPM (R 4.0.3) | ||
## stringr 1.4.0 2019-02-10 [1] RSPM (R 4.0.3) | ||
## testthat 3.0.1 2023-03-28 [1] Github (R-lib/testthat@e99155a) | ||
## tibble 3.2.1 2023-03-20 [1] CRAN (R 4.0.2) | ||
## usethis 1.6.3 2020-09-17 [1] RSPM (R 4.0.2) | ||
## utf8 1.1.4 2018-05-24 [1] RSPM (R 4.0.3) | ||
## vctrs 0.6.1 2023-03-22 [1] CRAN (R 4.0.2) | ||
## withr 2.3.0 2020-09-22 [1] RSPM (R 4.0.2) | ||
## xfun 0.26 2023-03-28 [1] Github (yihui/xfun@74c2a66) | ||
## yaml 2.2.1 2020-02-01 [1] RSPM (R 4.0.3) | ||
## | ||
## [1] /usr/local/lib/R/site-library | ||
## [2] /usr/local/lib/R/library | ||
``` | ||
|
||
|
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,89 @@ | ||
|
||
|
||
|
||
|
||
# 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 | ||
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.