The input is string[] like below.
"CSE111: CSE110 MATH101"
"CSE110:"
I need to order the strings based on some logic. For example my output should be a string[] like
"CSE110","MATH122","CSE111"
My question is
While scanning through the input array, if one string is picked to be the first string of the output array, then how do I skip all occurrences of that particular string, while I continue to process the string[] for the second output string etc..
Eg:
Input:
"CSE111: CSE110 MATH101"
"CSE110:"
If CSE110 is picked to be the first string in the output, then when I scan through the input string[] for the second string to be a part of output, I should not consider CSE110.
How can I achieve this? The answer I am looking forward to is something like:
Store the input in a string[]
loop through the strings one by one using strtok or stringstream >> operator.
Once the first string is found ...blah blah blah ....
Hope my question is clear enough. I will be glad to provide more details.
Edit1:More Explanation
The strings represent the order in which the classes need to taken . If a class has pre-requisite , the pre-requisite has to be taken first. ie. if Input is
"CSE111: CSE110 MATH101"
"CSE110:"
The class CSE111 has a pre-requisite of CSE110 MATH101 . So I need to consider first CSE1110(No Pre-requisite) - MATH101((No Pre-requisite) and then CSE111 . (Further Ties can broken in alphabetical order. )
I hope this helps..
I hopefully got it now: For a string of the form A: B C D, the course A has B, C, and D as prerequisites.
In that case you want a mapping from a course to its prerequisites, e.g.:
typedef std::set<std::string> CourseSet;
typedef std::map<std::string, CourseSet> Prerequisites;
Now you can fill a Prerequisites by tokenizing, using the first part as the key and a CourseSet as the value.
As it seems that you just want one of the possible orders for all courses in the input, you could then do the following:
complete the prerequisites for the courses (i.e. include the courses they indirectly depend on)
now a>b if a has b as a prerequisite
if b doesn't have a as a prerequisite use e.g. the lexicographical order
Related
I am new to C++ and I am struggling to get this string to split. Looking to create a multimap that has book as the key. Noun and the definition after the "-=>>" would be a pair and so would verb and its definition. Here is the strings I can not seem to split correctly:
book|noun -=>> A set of pages.|verb -=>> To arrange something on a particular date.
bookable|adjective -=>> Can be ordered.
This is the code I am trying. I figured out this code is not properly loading the multimap, because when I print parts[0] both names are put into the same index. It seems regex is a simpler solution, but after plugging away at this for the last couple of hours I need some help.
while (getline(myfile, line)) {
string delimiters("|-=>>.");
vector<string> parts;
boost::split(parts, line, boost::is_any_of(delimiters));
name = parts[0];
partOfSpeech = parts[1];
definition = parts[2];
dictionary.emplace(make_pair(name, make_pair(partOfSpeech, definition)));
}
Any guidance or feedback is much appreciated
First split your line by |. The first resulting part is your name. Then got through all the other resulting parts and divide them into halves by -=>>. This will give you the first (partOfSpeech) and second (definition) halves of your pairs.
I have a column with these type of names:
sp_O00168_PLM_HUMAM
sp_Q8N1D5_CA158_HUMAN
sp_Q15818_NPTX1_HUMAN
tr_Q6FGH5_Q6FGH5_HUMAN
sp_Q9UJ99_CAD22_HUMAN
I want to remove everything before, and including, the second _ and everything after, and including, the third _.
I do not which to remove based on number of characters, since this is not a fixed number.
The output should be:
PLM
CA158
NPTX1
Q6FGH5
CAD22
I have played around with these, but don't quite get it right..
library(stringer)
str_sub(x,-6,-1)
That’s not really a subset in programming terminology1, it’s a substring. In order to extract partial strings, you’d usually use regular expressions (pretty much regardless of language); in R, this is accessible via sub and other related functions:
pattern = '^.*_.*_([^_]*)_.*$'
result = sub(pattern, '\\1', strings)
1 Aside: taking a subset is, as the name says, a set operation, and sets are defined by having no duplicate elements and there’s no particular order to the elements. A string by contrast is a sequence which is a very different concept.
Another possible regular expression is this:
sub("^(?:.+_){2}(.+?)_.+", "\\1", vec)
# [1] "PLM" "CA158" "NPTX1" "Q6FGH5" "CAD22"
where vec is your vector of strings.
A visual explanation:
> gsub(".*_.*_(.*)_.*", "\\1", "sp_O00168_PLM_HUMAM")
[1] "PLM"
So I want to match every string in this list, except the ones that contain the product SKU, which is /s7892632 <---- random string of numbers. I've been trying to do this for quite some time and have been unsuccessful. Any insight would be greatly appreciated.
/account/login?returnurl=/account/forgotpassword
/account/login?returnurl=/account/orders
/account/orders
/account/updateaddress
/account/updateemail
/account/updaterewardscard
/brands/havaianas
/careers
/Category List
/checkout
/checkout/addresses
/checkout/addresses/delivery
/checkout/addresses/deliverymethod
/checkout/affilinetbasket
/checkout/anonymous
/checkout/confirmation
/checkout/express
/checkout/login
/checkout/login?returnurl=/checkout/addresses
/checkout/null
/checkout/payment
/checkout/paypal
/checkout/quickshop/
/checkout/verify
/click-and-collect
/click-and-collect/click-and-collect-overview
/corporate/about-matalan
/corporate/careers
/corporate/cookies
/corporate/history
/customer-services/accessibility
/customer-services/contact
/customer-services/customer-services-home
/customer-services/delivery
/customer-services/faq
/customer-services/fitting-room
/customer-services/here-to-help
/customer-services/size-guides
/delivery
/events/mothers-day
/events/mothers-day/s2516241/tassle-detail-slouch-bag
/events/mothers-day/s2518752/waxed-jacket
/events/mothers-day/s2519237/fabric-buckle-tote-bag
/events/mothers-day/s2521182/heart-print-nightie
/events/mothers-day/s2521184/heart-print-dressing-gown
/events/mothers-day/s2521185/heart-print-pyjama-set
/events/mothers-day/s2521679/structured-tote-bag
/events/mothers-day/s2522143/chiffon-print-dress
/events/mothers-day/s2522347/butterfly-enamel-bowl-32cm-x-8cm
/events/mothers-day/s2526013/animal-print-jersey-blazer
/events/mothers-day/s2527624/croc-tote-bag
/events/mothers-day/s2529731/shift-dress
/events/mothers-day?page=1&size=120&cols=4&sort=&id=/events/mothers-day&priceRange[min]=2&priceRange[max]=59
/events/mothers-day?page=2&size=120&cols=4&sort=&id=/events/mothers-day&priceRange[min]=2&priceRange[max]=59
/events/mothers-day?page=2&size=36&cols=4&sort=&id=/events/mothers-day&priceRange[min]=2&priceRange[max]=59
/events/mothers-day?page=3&size=36&cols=4&sort=&id=/events/mothers-day&priceRange[min]=2&priceRange[max]=59
The following should work:
^(?!.*/s\d{7}/).*
Example: http://regexr.com?343nf
This assumes you have each string as a separate element in a list. If this is actually matching one big string with multiple lines you can use the same regex, but you may need to enable global and multiline options depending on the tool you are using (and make sure dotall/singleline is disabled).
Try this:
boolean noSku = !line.matches(".*/s\\d{5,}.*");
This uses {5,} which allows for any number of digits in the SKU greater than 4 (giving you flexibility with matching). You can change the number to whatever suits.
this matches lines that don't have the code....
^((?!s\d{7}).)*$
In C++ I have a phonebook with many names, such as Sinatra, Frank, and I want the user to be able to input any length of string to scan the file for it. Once I have the user input a string of any desired length, how do I scan an entire string of "Sinatra, Frank" for just "Frank" or "Sinatra" or "atra" and see which name(s) it belongs to?
You can use the std::string::find method:
string s = "Sinatra, Frank";
string::sizetype index = s.find("Frank");
This gets you the index of the match (which in this case is 9).
A question: is your phonebook a flat file with each name on a new line (as in your example with "Sinatra, Frank" in a format like "Lastname, Firstname", etc.), or do you have some structure of this phonebook where each name-string is a node of an array, a linked list, etc?
Note that for strstr():
strstr(const char *s1, const char *s2)
locates the first occurrence of string s2 in s1, which may be sufficient for you.
For your input string, always be sure to check size limits in one way or another; if the user enters a string through some interface it should be explicitly handled to ensure it doesn't exceed your storage for it or contain malevolent characters or code.
Ken's solution produces the position of the substring in the original string (and so long as it's not null that mean's there's a 'hit') but doesn't tell you which entry of the phonebook is the hit; your code will need to track which entry/entries are hits so you can return a meaningful set of results.
You can use strstr() to locate one substring within a string.
If it's a std::string, you can use the .find() method of the "Sinatra,Frank" string
basically have two questions.
1. Is there a c++ library that would do full text boolean search just like in mysql. E.g.,
Let's say I have:
string text = "this is my phrase keywords test with boolean query.";
string booleanQuery = "\"my phrase\" boolean -test -\"keywords test\" OR ";
booleanQuery += "\"boolean search\" -mysql -sql -java -php"b
//where quotes ("") contain phrases, (-) is NOT keyword and OR is logical OR.
If answer to first is no, then;
2. Is it possible to search a phrase in text. e.g.,
string text =//same as previous
string keyword = "\"my phrase\"";
//here what's the best way to search for my phrase in the text?
TR1 has a regex class (derived from Boost::regex). It's not quite like you've used above, but reasonably close. Boost::phoenix and Boost::Spirit also provide similar capabilities, but for a first attempt the Boost/TR1 regex class is probably a better choice.
As to the 2nd point: string class does have a method find, see http://www.cppreference.com/wiki/string/find
Sure there is, try Spirit:
http://boost-spirit.com/home/