I have a column of comments called MMR_notes. In the MMR_notes there should be a the word good, okay or bad. For each row that has a comment that includes the word/pattern "bad", "okay" or "good" I would like a "1", "2" and "3" to be entered into another column called quality respectively. Here is the code I've been trying to use so far.
The image shows a table of what I'm hoping the result will be
if (grep(DATA$MMR_notes=="*bad*")){
print(1,DATA$quality)
} else if (grep( DATA$MMR_notes=="*good*")) {
print(3,DATA$quality)
} else if (grep( DATA$MMR_notes=="*okay*")) {
print(2,DATA$quality)
} else {
print("Na")
}
Related
here is input and output format for the problem.
[] and {} are only acceptable braces in this case.
Assume for this problem that space characters can be done away with.
Your solution should return a list of strings, where each entry corresponds to a single line. The strings should not have ā\nā character in them.
Input : ["foo", {"bar":["baz",null,1.0,2]}]
Output :
[
"foo",
{
"bar":
[
"baz",
null,
1.0,
2
]
}
]
here is my code i am getting segmentation error, i am new to c++ and unable to find the error in the program please help
vector<string> Solution::prettyJSON(string A) {
int tabcount=0,j=0;
vector<string> res;
cout<<"0st check \n";
for(int i=0;A[i];i++){
if(A[i]=='{' || A[i]=='['){
if(i==0)
res[j].push_back(A[i]);
else{
j++;
res[j].insert(0,tabcount,'\t');
res[j].push_back(A[i]);
}
j++;
tabcount++;
}
else {
if(A[i]=='}' || A[i]==']'){
tabcount--;
j++;
res[j].insert(0,tabcount,'\t');
res[j].push_back(A[i]);
j++;
}
else{
if(A[i]==','){
res[j].push_back(A[i]);
j++;
res[j].insert(0,tabcount,'\t');
}
else{
res[j].push_back(A[i]);
}
}
}
}
return res;
}
Firstly, please be aware that there are many C++ JSON parsing and manipulation and prettification libraries, some of which are quite compact and easy to use and only take up a single header. Consider reusing one of those, which will help you avoid the many pitfalls that you'll find when trying to write your own.
Now, I'm assuming you haven't debugged this. Running it though visual studio, for example, shows the exact line the crash arises on:
res[j].push_back(A[i]);
i is zero, so this is the very first character. j is also zero, but more importantly the size of res is also zero. This means you're trying to reference a nonexistent entry in res, which will, of course, cause your application to crash.
If j is equal to or greater than res.size(), you'll get this problem, and because you never push any entries into res there's no way this code would work. This is probably an indication that you're unfamiliar with using vectors, but this isn't really the place to teach you how to do that.
Anyway, as a minimal fix, if I add res.push_back(""); before the for loop and after every instance of j++, I get this result:
[
"foo",
{
"bar":
[
"baz",
null,
1.0,
2
]
}
]
Is there any way to shorten this statement:
if(string.Equals("Hello") || string.Equals("Hi") || string.Equals("Hey")) { }
To something like:
if(string.Equals("Hello" || "Hi" || "Hey")) { }
It's not necessary, but can be handy.
Thanks to #thelaws who suggested using an array of the possible values and flipping the statement, which I got to work with:
if(new string[]{"Hello", "Hi", "Hey"}.Contains(value)) { }
if ((new List<string> { "Hello", "Hi", "Hey" }).Contains(yourValue))
{
//your code here
}
Here I created a list of strings with values Hello, Hi and Hey. Then I am just searching whether the value of variable yourValue is present in the created list.
My code is:
#include<stdio.h>
void main(void)
{
float timeLeavingTP;
int transitNumber;
float transitTime;
printf("Please enter the time leaving TP.\n");
scanf_s("%f",&timeLeavingTP);
printf("Please enter bus number.\n");
scanf_s("%d",&transitNumber);
if(timeLeavingTP==1.00)
{
if(transitNumber==27)
{
transitTime=1.56;
}
else if(transitNumber==8);
{
transitTime=1.39;
}
}
if(timeLeavingTP==6.30)
{
if(transitNumber==27)
{
transitTime=7.32;
}
else if(transitNumber==8)
{
transitTime=7.29;
}
printf("The time reached home is %f\n",transitTime);
}
}
After debugging i got
Please enter the time leaving TP
1.00
Please enter bus number
27
Please enter to continue...
My question is How do i adjust the program to make it look like the one below instead. What kind of error did i commit?
Please enter the time leaving TP
1.00
Please enter bus number
27
The time reached home is 1.56
Thanks for the help in advance!
Hi guys after including == i still got the same for my debugging? Is there something else that i did wrong?
Part 1: = vs ==
Note that:
if(timeLeavingTP=1.00)
Does not do what you expect. It assigns timeLeavingTP with 1.00.
You probably want:
if(timeLeavingTP==1.00)
Additionally, note that this error occurs 6 times in your program.
Part 2: comparing floating point numbers
Your code might work in this case, but I'm not 100% sure if it will or not. It's often difficult to directly compare 2 floating point numbers, because of the inaccuracy of storing them (for example, 0.1 is usually not representable in floating point).
Most people solve this problem in one of a few ways:
Test a range around the number.
Convert to some fixes width format. Perhaps you could store the number as an integer, knowing that it's representation is actually 0.01 * the stored number.
In this case, you could actually just store the information as strings, and compare those.
Part 3: conditionals
To write a proper conditional, it should look like:
if (condition) {
...
} else if (condition) {
...
} else if (condition) {
...
} else {
...
}
You can certainly nest conditionals as well:
if (condition) {
if (condition) {
...
} else {
...
}
} else if (condition) {
...
}
Your code, for example, messes this up when you do:
}
else(transitNumber=8);
{
transitTime=1.39;
}
Note that the else statement does not accept a conditional after it.
Part 4: excessive semicolons
Additionally, note that after the else and if statements there are no semicolons. The semicolons only appear within the braces. So this statement:
if(timeLeavingTP=6.30);
While semantically valid, does not do what you expect. You actually want to remove that semicolon.
if(timeLeavingTP == 1.00)
{
if(transitNumber == 27)
{
transitTime=1.56;
}
else if(transitNumber == 8)
{
transitTime=1.39;
}
}
else if(timeLeavingTP == 6.30)
{
if(transitNumber == 27)
{
transitTime == 7.32;
}
if(transitNumber ==8)
{
transitTime=7.29;
}
}
printf("The time reached home is %f\n",transitTime);
}
if(transitNumber=27)
{
transitTime=1.56;
}
else(transitNumber=8);
{
transitTime=1.39; //this line is executed all the time
}
This code is completly invalid!
First, you do not compare anything... transitNumber = 27 is an assignment.
Second else(transitNumber=8); again this is an assignment and it should be else if(...). Also ; at the and means that transitTime = 1.39(inside bracket) will always happen, even if transitNumber != 8
Change
if(timeLeavingTP=1.00)
to
if(timeLeavingTP==1.00)
so that you can compare timeLeavingTP correctly.
if (dog.equalsIgnoreCase("yes")) {
drink.don.setCost(8.75);
drink.don.getType();
drin.l.add(drink.don.getType());
drink.c.add((double) coke.don.getCost());
cokeprice = coke + fanta.don.getCost();
else if (dog.equalsIgnoreCase("no"))
else catch(IllegalArgumentException iae) {
System.out.println("requires yes or no");
}
}
Ignore the stupid naming conventions had to change them, incase any class mates decided to steal anything ;p
I'm trying to get my if statement to allow the user input yes and do a condition, then if "no" has been entered then nothing happens just moves onto the next statement, then anything else is illegal and the program crashes.
I don't like throwing exceptions, especially if I expect that the user might type in something that I don't want. I'd rather do something like
if (userInput.equalsIgnoreCase("Yes")) {
// do yes
}
else if (userInput.equalsIgnoreCase("No")) {
// do no
}
else {
// Sorry, invalid input
}
I don't know what language you are using, nor do I know what any of the methods you are using do, but here is an example of a similar statement in C#.
First, use a method to convert the user input to a true or false (boolean) value:
public static bool IsYes (string userInput)
{
if (userInput == "yes')
{
return true;
}
else if (userInput == "no")
{
return false;
}
else
{
throw new CustomException();
}
}
Next, you can take the result of IsYes() and use it for the if else statement:
if (IsYes(userInput))
{
// code you want to execute if "yes"
}
else
{
// code you want to execute if "no"
}
Hopefully this code will give you an idea of how to use if-else statements, but in the future please explain your question more clearely. Remember, this is C#, so although if statements are similar in almost all languages some of the other code will differ. Also, this is just an example, it won't do anything on its own.
My program simulates a video store. In my list there are multiple copies of some videos. If I try to rent a video and the first copy of that video in the list is already rented, my program fails to continue checking to see if the other copies are available (a film is available if custId is '0000'). Take a look at the text file from where the list gets its members for a better understanding of what i'm describing:
Could anyone take a look and let me know if they spot an issue? Any help is appreciated, thanks.
Code from main
try
{
int index = 0;
bool found = false;
while (!found)
{
if (strncmp(filmId,filmList.getAt(index).number,6) == 0 && strncmp("0000",filmList.getAt(index).rent_id,5) == 0)//If that film is rented by NO customer
{
found = true;//customer can rent it
strcpy(newItem.number,filmId);//copy filmId into newItem
filmList.retrieve(newItem);//copy the struct in our orderedList with the same filmId/copy into newItem
filmList.remove(newItem);//delete the struct with same filmId/copy as newItem from the orderedList
strcpy(newItem.rent_id,custId);//update info in
strcpy(newItem.rent_date,rentDate);// newItem to show
strcpy(newItem.return_date,dueDate);// that it has been rented
filmList.insert(newItem);//put NewItem into list, effectivily replacing the removed item.
cout << "Rent confirmed!" << endl;
}
else
{
if (strncmp(filmId,filmList.getAt(index).number,6) > 0 || strncmp("0000",filmList.getAt(index).rent_id,5) > 0)
{
++ index;
}
else
{
throw string ("Not in list");
}
}
}
}
catch (string s)
{
cout << "\n***Failure*** " << s << endl;
}
Let me know if more code is required from any other parts of the program.
Here's my best guess with the code provided.
Let's say we are looking up 101001Casablanca, therefore I'm assuming filmId = "101001Casablanca". Also, assume the 101001Casablanca is checked out to customer 0001. We are comparing the first 6 characters of filmId to filmList.getAt(index).number, which I'm going to assume is at the very least "101001". This passes, but since it is checked out the second condition fails.
In the else we check the same strings in the first condition and still get 0 returned from strncmp which is false. The second condition is also false since strncmp("0000", "0001", 5) is -1. Therefore we go to the final else which throws.
If you are only checking string equality with strncmp, remember that it can return -1, therefore check if equal or not equal to 0.