Weird pattern - drawing algorithm - c++

Hi how can I draw something like this:
https://pastebin.com/6TWZwPzq
from this input:
1. 3
2. 4
3. 1
4. 2
5. 4
where 1 is a first column ,2 is a second.. etc
where

Case 1:
perform a spread process.
Case 2:
Store into as many string as there are deep, then append into it. Check stringsteam from sstream package

Related

Using awk, how do I match pattern and variants?

I've been struggling with this for a while in regex testers but what came up as a correct regex pattern actually failed. I've got a large file, tab delimited, with numerous types of data. I want to print a specific column, with the characters XYZ, and it's subsequent values.
In the specific column I'm interested in I have values like:
XYZ
ABCDE
XYZ/WORDS
XYZ/ABCDE
ABFE
XYZ
regex tester that was successful was something like:
XYZ(.....)*
It obviously fails when implemented as:
awk '{if ($1=="XYZ(......)*") print$0}'
What regex character do I use to denote that I want everything after the backslash(/), including the original pattern (XYZ)?
Specifically, I want to be able to capture all instances of XYZ, and print the other columns that go along with them (hence the print$0). Specifically, capture these values:
XYZ
XYZ/WORDS
XYZ/ABCDE
Thank you
Setup: (assuming actual data file does not include blank lines)
$ cat x
XYZ 1 2 3 4
ABCDE 1 2 3 4
XYZ/WORDS 1 2 3 4
XYZ/ABCDE 1 2 3 4
ABFE 1 2 3 4
XYZ 1 2 3 4
If you merely want to print all rows where the first field starts with XYZ:
$ awk '$1 ~ /^XYZ/' x
XYZ 1 2 3 4
XYZ/WORDS 1 2 3 4
XYZ/ABCDE 1 2 3 4
XYZ 1 2 3 4
If this doesn't provide the expected results then please update the question with more details (to include a more representative set of input data and the expected output).

Convert letter to numeric number formatting in Excel using RegExp

I have an Excel sheet with given data in a cell. Want to convert letter numbering (e.g. a, c) or b)) to numeric number format(e.g. 1, 3) or 2))
Input Cell Data
Introduction and Basic wording
More lines here
a,c) A steps 1 and 3
- A sub-step) 1 of 1.
- A sub-step 2 of 1
...
b) A step 2.
- A sub-step 1 of 2
d-g) A step 4 and 6
h) A step 5
Note:
Inside cell there are few character in given format 'p)'
Output Cell Data
Introduction and Basic wording
More lines here
1,3) A steps 1 and 3
- A sub-step) 1 of 1.
- A sub-step 2 of 1
...
2) A step 2.
- A sub-step 1 of 2
4-7) A step 4 and 6
8) A step 5
I used below RegExp to get the proper matching of letter numbering
(^|\n)((\w(?=\)|\,|\-)(\,|\-)){0,3}\w?\))
To replace it to numeric numbering format, I am using below code
Set colMataches = RE.Execute(strRegExp)
For Each match In colMataches
WScript.Echo "Found:"&match.Value
Dim mt : mt = match.Value 'Output result: a,c), b)etc.
Select Case mt
...
Case "b)"
sProc = RE.Replace(strRegExp, 2)
...
Case Else
WScript.Echo "Not Found"
End Select
As per my understanding:Replace(expression , find , replacewith[ , start[ , count[ , compare]]])
where find is the substring you want to replace & replacewith is the substring you want to replace with.
This usually work good when we are trying to find global matches for a specific value.e.g. want to replace a character like 'a' or non character like '-' globally with a single & specific value like alphabet('b') or Numeric('1') or anything other than alphanumeric('#').
But i am failing to replace(a->1, b->2, c->3, d->4 etc) by use of Select Case statement. Is there a better way to do proper replacement?

Bring all rows to one row

In Excel, I have rows like below:
1 2 3 4 5
6 7 8 9 0
9 8 7 6 5
...
I need to bring all of them to the first row:
1 2 3 4 5 6 7 8 9 0 9 8 7 6 5 ...
The numbers of rows and columns are fixed.
What is the fastest way I can achieve this?
Alternatively, can I solve this on a Textpad or Notepad++ using some REGEX grouping?
If you wanted to do it with an Excel formula, pasting the following, starting in column F, would produce it across the top row:
=INDIRECT("r"&CEILING(COLUMN()/5,1)&"c"&IF(MOD(COLUMN(),5)=0,5,MOD(COLUMN(),5)),FALSE)
If your table started from A2 and your row values are to be copied from A1 onwards, following should work:
=OFFSET($A$2, (COLUMN()-COLUMN($A$1))/5, MOD(COLUMN()-COLUMN($A$1), 5))
However, I think for just a small table of size 5X10, using '=' sign manually would be the fastest.
I just select and drag them up there. When there was a pattern of projects I wrote a VBA function to do the job, but for most small unique projects select and drag worked for me.
In Notepad++, for Find what : \r\n, Replace with : 'space' with Search Mode Extended, Replace All, then copy result into Excel.
With images :) here:
Replace Carriage Return and Line Feed in Notepad++.

Divide a large image into two non overlapping images whose union is the large image

Given a large image composed of smaller images stored as a matrix. I need to find out a boundary dividing the large image into two parts(not necessarily equal but preferably nearly equal) without cutting past a smaller image.
Each small image is represented by a single integer in the larger image matrix.
Ex:
1 1 2 2 2
1 1 2 2 2
3 3 3 4 4
3 3 3 4 4
is the large image matrix composed of 4 small images.
I need to find one such boundary to separate it into two smaller images such that their sizes don't differ by a very large amount.
This is my solution:
1. Start from considering the 1st row.
2. Using binary search find the start of a boundary. In above example it will be like
1 1 | 2 2 2
1 1 2 2 2
3 3 3 4 4
3 3 3 4 4
3.Proceed down until the dividing line doesn't intersect an image. If end of large image is reached then stop.
1 1 | 2 2 2
1 1 | 2 2 2
3 3 3 4 4
3 3 3 4 4
4.Again do step 1,2,3 considering the remaining rows and make horizontal line from old line to new division line.
1 1 | 2 2 2
1 1 | 2 2 2
--
3 3 3 4 4
3 3 3 4 4
1 1 | 2 2 2
1 1 | 2 2 2
-----
3 3 3 | 4 4
3 3 3 | 4 4
End of large image...Stop.
Of-course if no vertical line can be found in step 2. We can look for a horizontal line first in a similar way like in the case of:
1 1 1 1 1
1 1 1 1 1
--
3 3 3 2 2
3 3 3 2 2
and then proceed.
How can I improve on this solution?
Are there better solutions and will my algorithm fail anytime?
I will be coding in C++. A heuristic/ greedy solution will be nice as well.
If the image is somehow big enough to make sense then you could get local differences to guide your boundaries selection.
Here is an example implemented in MATLAB for simplicity but you will get the picture:
suppose we create an image similar to the one you defined:
img = [ ones(20,20), 2*ones(20,30); ones(10,20), 2*ones(10,30); 3*ones(20,30), 4*ones(20,20)]
This command creates an image 50x50, having a 20x30 sub-image 1, a 30x30 sub-image 2, a 30x20 sub-image 3 and a 20x20 sub-image 4, as depicted graphically bellow:
Ideally you would like to get the boundaries between these "trays" representing the values 1 to 4. One way to do so is to shift the image one pixel left/right and one pixel top/bottom and subtract it with the original. This will produce another image with values only in the boundary positions.
See for example in MATLAB:
mask=((img-shift(img,1) + img-shift(img',1)')~=0);
This will create a mask by adding the difference of the right-shifted image and the original with the difference of the bottom-shifted image and the original, and, finally, by comparing the result with zero (zero values will be all pixel values except in boundaries). Function shift just shifts values of a matrix right or left. There is no need to put the code here since I just want to show the concept.
So you will end-up with the following mask image:
This mask has been cropped one pixel at the right and bottom since the previous subtractions produces a border that is not needed.
In this image, true values (white pixels) are on the last pixel of the previous image, i.e. image 1 ends at the 1st boundary and image 2 begins at the next pixel, so image 1 is bounded by x=20 and y=30, and so on for the other sub-images.

J, the unfindable verb

1 0 0 1 verb 1 2 3 4
result:1 4
The verb drops the items from the list on the right that have a 0 in the list on the left. I can remember seeing this verb in the Vocabulary but I can't find it again. Does anybody know this verb?
It's #.
Explanation: Such verbs (1 or 2 symbols, rarely 3) are called primitives. The # primitive is called Tally as a monad (effectively tallies the items, returning the count on the first dimension), and Copy as a dyad, where it copies the right arguments as many times as indicated on the left argument. Of course, in this case, your right and left elements must be the same length (or that one of them is scalar if the other is not).
Example:
1 0 0 1 # 1 2 3 4
1 4