-
Notifications
You must be signed in to change notification settings - Fork 19
Sharma_Exercise10 #9
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
Open
psharma-gif
wants to merge
2
commits into
qtran4:main
Choose a base branch
from
psharma-gif:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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 hidden or 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,65 @@ | ||
| #Using the score-by-score information from this game summarized in | ||
| #“UWvMSU_1-22-13.txt” generate a graph similar to the one I show above. | ||
| #Don’t worry about how pretty your graph is. Focus more on the control structures | ||
| #required in your script used to generate the plot. | ||
|
|
||
| score <- read.table("UWvMSU_1-22-13.txt", header = TRUE) | ||
| score_MSU <- integer(nrow(score)) | ||
| score_UW <- integer(nrow(score)) | ||
| time <- 1:nrow(score) | ||
| for(i in 1:nrow(score)){ | ||
| time[i] <- score$time[i] | ||
| } | ||
| score_MSU <- 0 | ||
| score_UW <- 3 | ||
| for(i in 2:nrow(score)){ | ||
| if(score$team[i]=="UW"){ | ||
| score_UW[i] <- score_UW[i-1]+score$score[i] | ||
| score_MSU[i] <- score_MSU[i-1] | ||
| }else if(score$team[i]=="MSU"){ | ||
| score_MSU[i] <- score_MSU[i-1]+score$score[i] | ||
| score_UW[i] <- score_UW[i-1] | ||
| } | ||
| } | ||
| #create dataframe for time, MSU score and UW score. | ||
| score_dataframe <- data.frame(time, score_MSU, score_UW) | ||
| #loading ggplot2 package | ||
| library(ggplot2) | ||
| ggplot(score_dataframe, aes(time))+ | ||
| geom_line(aes(y=score_MSU), color=1)+ | ||
| geom_line(aes(y=score_UW), color=2)+ | ||
| xlab("Time")+ | ||
| ylab("Score")+ | ||
| ggtitle("Cummulative score for MSU and UW") | ||
|
|
||
|
|
||
| #2. Write a game called “guess my number”. The computer will generate a random | ||
| #number between 1 and 100. The user types in a number and the computer replies | ||
| #“lower” if the random number is lower than the guess, “higher” if the random | ||
| #number is higher, and “correct!” if the guess is correct. The player can continue | ||
| #guessing up to 10 times. | ||
|
|
||
| count <- 1 | ||
| #pick a random number from 1 to 100 | ||
| Guess <- sample(1:100,1) | ||
| Guess <- as.numeric(Guess) | ||
| while (count <= 10) { | ||
| reply <- readline(prompt = "Enter a number between 1 and 100:") | ||
| reply <- as.numeric(reply) | ||
| if(Guess < reply && count < 10){ | ||
| print("Guess a lower number, please!") | ||
| count <- count+1 | ||
| }else if(Guess > reply && count < 10){ | ||
| print("Guess a higher number, please!") | ||
| count <- count + 1 | ||
| }else if (Guess == reply){ | ||
| print("You win") | ||
| break | ||
| } | ||
| else if(count == 10){ | ||
| print("Sorry :(, you cant make guess for more than 10 times") | ||
| break | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
more efficient way to do this
load table of scoring
scoring=read.table("UWvMSU_1-22-13.txt",header=TRUE,sep="\t",stringsAsFactors=FALSE)
look at data
dim(scoring)
head(scoring)
preallocating matrix to store cumulative scores
cum_scores=matrix(NA,nrow(scoring)+1,3)
cum_scores[,1]=c(0,scoring[,1])
cum_scores[1,2:3]=0
colnames(cum_scores)=c("time","UW","MSU")
looping through individual scoring events
for(i in 1:nrow(scoring)){
if(scoring[i,2]=="UW"){
cum_scores[(i+1),2]=cum_scores[i,2]+scoring[i,3]
cum_scores[(i+1),3]=cum_scores[i,3]
}else{
cum_scores[(i+1),2]=cum_scores[i,2]
cum_scores[(i+1),3]=cum_scores[i,3]+scoring[i,3]
}
}