How to implement bitset binary exclusion used to find code? - c++

I am new to C++, and I need to write a code that can find a binary code by checking a bitset against inputs and excluding unnecessary options.
Example:
input-000000000 = 6 correct
This implies that there must be 3 ones present.
I need the code to only use strings that contain 6 zeros and three ones and print one attempting to narrow down the code.
Example:
000000111 = 5 correct
This must mean that two of those ones are correct and one must be a zero with a one somewhere in the first six digits.
How do I approach this problem?
I have so far the function:
string index2code (int i)
{
return bitset <9>(i).to_string();
}
But I am still struggling to think of the logic, and how to use it.

Related

Get values of nested json with regex

I'm trying to select all of the lowest tier(in the "nest") possible values that are numbers, and not strings, in a nested json. This will include arrays and objects. Currently, im using this code
(?<=:)\s*([+-]?\d+(?:\.\d*(?:E-?\d+)?)?)\b\s*(?=(?:\{|,)\s*\"[^\"]*\":)
(which is mostly from this question)
and it works, if it isn't the last value of an object, or in a list. My main problem is, that it can be fooled. for example, if i have these two "a":":1,", ":":akey-value pairs next to eachother, it sees the number 1 as a value, except that the actual value is ":1,". How can i make a foolproof system for this?
Im excpecting it to not select anything, that couldn't be an integer or a float. I would like to keep the middle part of my original regex, beacuse it also needs to work with scientific notations.

Write a function that crosses out up to n-1 signs of a n-letter word, and returns the amount of unique strings created that way

Let's say we have the string* abcd, which I will refer to as "word". The program should return the amount of unique strings* that are made by crossing out zero or more letters from "abcd". In this particular example, these unique strings* are "abcd", "abc", "abd", "acd", "bcd", "ab", "ac", "ad", "bc", "bd", "cd", "a", "b", "c", "d". Therefore, in this case, the program should return 15. Using vectors and strings in this assignment is forbidden, so I will have to use char[] arrays instead. The use of the word string* above is to avoid complicating the already complicated task. By string I mean char[].
So far my idea is to create arrays that store the strings with the same length. The number of such strings I find using the binomial formula. (For example the number of 3-letter strings from a 4-letter word is C(3,4). So in a for-loop I create the required arrays to accomodate the strings and add only those strings that aren't already in that array. Then I return the number of elements in the array.
//size is the size of the word, i is the number of crossed out letters
int total=0; //stores number of all possible little strings
for(int i=1; i<size; i++){
int sizeOfSubstring=binomial(size-i, size);
char substrings[sizeOfSubstring][size-i];
//populate the substrings array and return the number of char[] arrays added to it.
//Then add that number to total.
}
However, as you can clearly see, this problem is already complicated. I bypass the C++ requirement for constant array sizes by using GCC. But it gets even worse, when you have to populate the array of strings. For example, we need to add "abc", "abd", "acd", "bcd" to substrings[4][3]. And then the same procedure for substrings[6][2] etc. This will require a function like
void addSubstring(char crossedOutIndexes[], char word[], char substr[][]){
//I haven't implemented that yet
}
I am asking this question because I already have great difficulties with this problem and I don't know how to implement the addSubstring() function. Is this even the right idea to solve the problem?
I recommend first solving the problem without the complicated cases. For instance, assume your input will never have repeated letters. Then solve it manually on paper for a string of 2 characters, then 3, then 4, until you see a pattern develop as to an algorithm you manually use when doing it by hand. Then code that, and get it working. Once that's working. Solve the next problem, duplicates. Well there's a couple options there. Don't add a word if it's already in your list or remove all duplicates from the list.
As to variable length arrays, your initial solution of using a compiler that supports variable length arrays is fine for the first version. Another possibility is simply over allocating an array of strings. For instance an array of 10000 filled with empty strings. While memory inefficient, it's fine for learning. Once you have a working solution you can always move to a standard container like std::vector.
One thing that happens here is that learners often get great advice. But they're still learning so the advice just overwhelms them. There's nothing wrong with using a compiler that supports variable length arrays, it just locks you in to that tool.
I do recommend finding a development environment with a great debugger. One that will let you step through the code line by line and see what's happening. Visual Studio Community is the free one, that I'm familiar with. But I know there are others. I just don't know what they are.

How to create a program that identifies longest substring palindrome regardless of letter casing

I've attempted at googling algorithms for a program that outputs the result indicated in the question. Mostly, all what I've found was algorithms that satisfied the first constraint, but did not take into account the second part (ignoring the casing of letters). Conventional functions, such as strcmpi (I'm using c++) requires constant characters which make it impossible to incorporate within the algorithms alluded to above. In essence, I just need an idea on how I can go about creating such a program.
First create a program that identifies longest substring palindrome using your own compare function. And in that compare function if two characters are same then return true else if the difference between ASCII values of two characters is 32 then also return true. And rest as it is.

C++: finding a specific digit in a number

Someone please guide me how to check whether a specific digit exists in an integer or not. for the sake of code optimization I am trying to avoid the use of strings or any kind of loops to iterate over all the digits of the integer.
If I need to find out whether 4 exists the in an integer or not, input and the output samples are given below:
Sample Input:
154
Sample Output:
true
Desired Code:
bool ifExists(int digit, int number)
{
if()
{
return true;
}
else
{
return false;
}
}
Possible Logic:
I believe there must be a mathematical approach which will do the job in the if condition, however I am unable to find such method in cmath library.
Convert integer to string, do a string search for digit.
The "mathematical" method would have to do the same, compute the digit sequence by division/remainder by 10 and compare with the given digit.
A mathematical approach would be possible i think. By using log(x) in some crazy way.
But ones for surey you should use string or a loop by dividing by 10. log(x) requires far more resources than the way you want to use.
Also string or dividing by 10 is far more easy to read than a mathimatical solution.
For the mathimatical solution.
I suggest you try to transform the decimal number in a binary representation of it. Then it could be possible to create a filter for the digit you look for. By joining the filter and the transformed value through a bitwise & you could get what you seek. Im not sure if that realy is possible but that would be my first idea for an approach.
But as i said before. This method will be very expensive to the cpu and will be hard to read.

quickly find a short string that isn't a substring in a given string

I've been trying to serialize some data with a delimiter and ran into issues.
I'd like to be able to quickly find a string that isn't a substring of given string if it contains a delimiter, so that I can use that for a delimiter.
If I didn't care about size the quickest way to find it would be to check a character in the given string, and pick a different character, make a string of the given string's length of only that character.
There may be a way to do some sort of check, testing first the middle characters, then the middle of the first and last segment... but I didn't see a clear algorithm there.
My current idea, which is fairly quick but non optimal is
initialize a hash with all characters as keys and 0 as a count
Read string characters as bytes using the hash to count.
walk the keys finding the smallest number of characters. stopping immediately if I find one that has zero characters.
Use that number of characters plus one as the delimiter.
I believe that is O(n), though obviously non the shortest. But the delimiter will always be no more than n/256 + 1 characters.
I could also try some sort of trie based construction, but I'm not quite sure how to implement that and thats 0(n^2) right?
https://cs.stackexchange.com/questions/21896/algorithm-request-shortest-non-existing-substring-over-given-alphabet
may be helpful.
Your counting of characters method isn't sufficient because you're only talking about the current string. The whole point of a delimiter is that in theory you're separating multiple strings, and therefore you'd need to count all of them.
I see two potential alternative solutions
Pick a delimiter and escape that delimiter in the strings.
Can use URI::Escape to escape a specific character, say &, and use that as the delimiter.
Specify the size of your string before you send it. That way you know exactly how many characters to pull. Essentially pack and unpack
And because I'm already on the train of alternative solutions, might as well propose all of the other serialization modules out there: Comparison of Perl serialization modules
I like the theory behind a task like this, but rings too much like an XY Problem
I agree with #Miller that your best bet is to pick a character and escape that in the text.
However, this is not what you asked, so I'll attempt to answer the question.
I take it these strings are long, so finding the delimiter is time-sensitive.
In straight Perl, the hash idea may well be as fast as you can get. As a native C extension, you can do better. I say this because my experience is that Perl array access is pretty slow for some reason, and this algorithm uses arrays to good effect:
int n_used_chars = 0;
int chars[256], loc_of_char[256];
for (int i = 0; i < 256; i++) used_chars[i] = loc_of_char[i] = i;
for (int i = 0; i < string_length; i++) {
char c = string[i];
int loc = loc_of_char[c];
if (loc >= n_used_chars) {
// Character c has not been used before. Swap it down to the used set.
chars[loc] = chars[n_used_chars];
loc_of_char[chars[loc]] = loc;
chars[n_used_chars] = c;
loc_of_chars[c] = n_used_chars++;
}
}
// At this point chars[0..n_used_chars - 1] contains all the used chars.
// and chars[n_used_chars..255] contains the unused ones!
This will be O(n) and very fast in practice.
What if all the characters are used? Then things get interesting... There are 64K two-byte combinations. We could use the trick above, and both arrays would be 64K. Initialization and memory would be expensive. Would it be worthwhile? Perhaps not.
If all characters are used, I would use a randomized approach: guess a delimiter and then scan the string to verify it's not contained.
How to make the guess in a prudent way?