Skip to content
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 4 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 01-intro.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ This is not a comprehensive list!

- [Phind](https://www.phind.com/) is a AI-powered search engine designed specifically for developers asking technical questions. In addition to answering your coding question, it will provide links to the internet sources used to find the answer.

- [Poe](https://poe.com/) is Quora's platform that gives you access to multiple AI chatbots, including Sage, based on OpenAI's GPT-3.5 architecture; Claude, an AI natural language bot created by [Anthropic](https://www.anthropic.com); and ChatGPT.
- [Poe](https://poe.com/) is Quora's platform that gives you access to multiple AI chatbots, including Sage, based on OpenAI's GPT-3.5 architecture; Claude, an AI natural language bot created by [Anthropic](https://www.anthropic.com); Llama 2, created by [Meta](https://ai.meta.com/llama/), and ChatGPT.

**IDE-based chatbots**

Expand Down Expand Up @@ -158,4 +158,4 @@ Code in these boxes was written by AI.

## About This Book

We used AI bots (including ChatGPT, Bard, Sage, and Claude) to develop and write much of this course. We firmly believe this new technology can be used in ethical ways and we put into practice all the rules and suggestions offered over the following modules while we created this book.
We used AI bots (including ChatGPT, Bard, Sage, and Claude) as part of the process of creating this book. The words are all original content, but we used AI bots to edit for grammar and tone. We firmly believe this new technology can be used in ethical ways and we put into practice all the rules and suggestions offered over the following modules while we created this book.
34 changes: 0 additions & 34 deletions 03-writing_code.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -509,40 +509,6 @@ ottrpal::include_slide("https://docs.google.com/presentation/d/1MCNeSO4aOm1iESWD
```



## 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.

## Summary

* Coding has become an essential skill in today's world. AI can increase the efficiency of planning, writing, and debugging code, as well as make coding more accessible for beginners.
Expand Down
46 changes: 46 additions & 0 deletions 03-writing_code_activity.Rmd
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()
```


83 changes: 0 additions & 83 deletions 04-refactoring.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -519,89 +519,6 @@ Because AI models are created by humans, they can be biased. This means they may

When using AI to refactor code, the code itself is often sent to an external service or platform for analysis and transformation. This can raise concerns about the security of the code, especially if it contains sensitive information such as trade secrets, proprietary algorithms, or personal data. If your code is sensitive, it's important to carefully vet any third-party AI tools or services used in the refactoring process.

## 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?

## Summary

Expand Down
94 changes: 94 additions & 0 deletions 04-refactoring_activity.Rmd
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
Copy link
Contributor

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.

Copy link
Collaborator Author

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


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?

92 changes: 0 additions & 92 deletions 06-understanding_other_ppl_code.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -293,98 +293,6 @@ Although we can't truly understand some of this information, it is however possi
Chatbot tools are also limited in terms of how up-to-date their training data is to know about current possible issues with code.



## Hands-On Exercise

Now it's your turn to try. Let's say you were handed this bit of code to work with (and eventually modify), but first you need to figure out what's going on with it. Unfortunately, the original programmer left very little in the way of notes or annotation.


### The Code
**Note**: This code is just an example and was written strictly for educational purposes.

```
use ode_solvers::{Euler, OdeMethod};
use plotters::prelude::*;
use std::fs::File;


const N: f64 = 1000000.0;
const I0: f64 = 10.0;
const R0: f64 = 0.0;
const S0: f64 = N - I0 - R0;
const BETA: f64 = 0.3;
const GAMMA: f64 = 0.1;

fn sir_model(t: f64, y: &[f64], v: f64) -> Vec<f64> {
let s = y[0];
let i = y[1];
let r = y[2];
let dsdt = -BETA * s * i / N - v * s;
let didt = BETA * s * i / N - GAMMA * i;
let drdt = GAMMA * i + v * s;
vec![dsdt, didt, drdt]
}

fn main() {

let mut solver = Euler::new(sir_model);
solver.set_initial_condition(&[S0, I0, R0]);


let root = BitMapBackend::new("sir_vaccination.png", (800, 600)).into_drawing_area();
root.fill(&WHITE).unwrap();
let mut chart = ChartBuilder::on(&root)
.caption("SIR model with vaccination", ("sans-serif", 40))
.set_label_area_size(LabelAreaPosition::Left, 60)
.set_label_area_size(LabelAreaPosition::Bottom, 40)
.build_cartesian_2d(0.0..100.0, 0.0..N)
.unwrap();

let mut data1 = vec![];
for _ in 0..1000 {
let y = solver.integrate(0.1, 0.0);
data1.push((solver.time(), y[0], y[1], y[2]));
}


let vac_rate = 0.1;
let vac_coverage = 0.5;
let vac_num = vac_coverage * N;
let mut data2 = vec![];
for i in 0..1000 {
let t = i as f64 * 0.1;
let v = if t >= 50.0 && t < 150.0 {vac_num * vac_rate} else {0.0};
let y = solver.integrate(0.1, v);
data2.push((solver.time(), y[0], y[1], y[2]));
}


chart
.configure_mesh()
.x_labels(10)
.y_labels(10)
.disable_x_mesh()
.disable_y_mesh()
.draw()
.unwrap();
chart
.draw_series(LineSeries::new(
data1.iter().map(|d|
```

### Questions

1. Write a query that identifies the features that can be used to determine the coding language, as well as the version of the language. What language and version is it written in? What features give away the coding language?

1. Create a prompt that determines what this code does, as well as important features of the program. What does this code do?

1. Write a prompt that determines the purpose of this specific code snippet:

```
let root = BitMapBackend::new("sir_vaccination.png", (800, 600)).into_drawing_area();
```
What is the purpose of this line of code?

## Summary

* Understanding code written by someone else can be made easier by using AI
Expand Down
Loading
Loading