Not sure how to do parse tree when a ()'s is involved? - grammar

I have the following grammar :
E -> E+T | E-T | T
T -> T*F | T/F | F
F -> i | (E)
And this string :
(i+i)*i
What's messing me up is how I would incorporate the ()'s?
Attempt:
E -> T
Left sub-branch of T = T -> F -> i
Mid-branch = *
Right sub-branch of T = F -> (E) -> E + T ..... (E -> i, T -> i)
I'm not sure if that is understandable but I'm not sure how else to write parse tree here. Any help will be much appreciated! Thanks!

A common way to present the parse tree is as a tree diagram (usually with the root at the top), where each node in the tree is a symbol, and the tree's 'child' relation reflects the steps in the derivation.
E.g., for i*(i+i), the parse tree might look something like this:
E
|
T
|
+---+---+
| | |
T * F
| |
F +---+---+
| | | |
i ( E )
|
+---+---+
| | |
E + T
| |
T F
| |
F i
|
i
or like this:
E
|
T
|
+---+-----------+
| | |
T | F
| | |
F | +-------+-------+
| | | | |
| | | E |
| | | | |
| | | +---+---+ |
| | | | | | |
| | | E | T |
| | | | | | |
| | | T | F |
| | | | | | |
| | | F | | |
| | | | | | |
i * ( i + i )
As you can see, parentheses don't pose a special problem.

Related

Pandas Pivot-Table Containing List

I'd like to create a pivot table with the counts of values in a list, filtered by another column but am not sure how to use pandas pivot table (or function) with a list.
Here's an example what I'd like to do:
| Col1 | Col2 |
| --- | ----------- |
| A | ["e", "f"] |
| B | ["g", "f"] |
| C | ["g", "h"] |
| A | ["e", "g"] |
| B | ["g", "f"] |
| C | ["g", "e"] |
Ideal Pivot Table
| 1 | 2 |count|
| A | e | 2 |
| | f | 1 |
| | g | 1 |
| B | g | 2 |
| | f | 2 |
| C | g | 2 |
| | h | 1 |
| | e | 1 |
I cannot use a list to make a pivot table and am struggling to figure out how to modify the data or find a different method. Any help would be much appreciated!
Try this:
cols = ['Col1','Col2']
df.explode('Col2').groupby(cols).size()

get X number of non null columns as single string

I am trying to find the SQL command to do something but I don't know how to explain it so I'll use an example. I have a table like so:
| one | two | three | four |
|-----|-----|-------|------|
| a | h | i | j |
| b | k | l | |
| c | m | n | o |
| d | p | | |
| e | q | | |
| f | r | s | |
| g | t | | |
I need to create new columns that take the first non-null column from the right and kind of reverse it going up and joining/concatenating the fields.
| one | 1-up | 2-up | 3-up |
|-----|------|------|---------|
| a | j | j, i | j, i, h |
| b | l | l, k | |
| c | o | o, n | o, n, m |
| d | p | | |
| e | q | | |
| f | s | s, r | |
| g | t | | |
For b, since column four doesn't have data it uses three as the first value. Same for the other rows.
I hope this makes sense. I'm not sure how else to explain this.
You can use COALESCE like this :
select one, COALESCE(four,three,two,'') as '1-up',
COALESCE(four+','+three,three+','+two,'') as '2-up',
COALESCE(four+','+three+','+two,'') as '3-up'
from Table1
SQL Fiddle link Here

VBA Copy & Paste Loop ( Generate Field Number)

Right now im working to generate a label based on quantity in excel. I managed to get it copy & paste based on value from cell. But, i didnt know how to make some cell change according to the loop.
Below is as example :
Current result :
| A | B | C | D | E |
|------------------------------- |----- |-------------------- |----- |----- |
| NMB IN DIA | | MADE IN THAILAND | | |
| INVOICE NO | : | MM035639 | | |
| C/NO | : | 1 | / | 2 |
| SHIP TO | : | A | | |
| QTY | : | 100 | | |
| NMB PARTS NO | : | SFASDF234 | | |
| | | *SFASDF234* | | |
| CUST PARTS NO | : | SFASDF234 | | |
| CUST ORDER NO | : | | | |
| ----------------------------- | --- | ------------------ | --- | --- |
| NMB IN DIA | | MADE IN THAILAND | | |
| INVOICE NO | : | MM035639 | | |
| C/NO | : | 1 | / | 2 |
| SHIP TO | : | A | | |
| QTY | : | 100 | | |
| NMB PARTS NO | : | SFASDF234 | | |
| | | *SFASDF234* | | |
| CUST PARTS NO | : | | | |
| CUST ORDES NO | : | | | |
Expected result :
| A | B | C | D | E |
|------------------------------- |----- |-------------------- |----- |----- |
| NMB IN DIA | | MADE IN THAILAND | | |
| INVOICE NO | : | MM035639 | | |
| C/NO | : | 1 | / | 2 |
| SHIP TO | : | A | | |
| QTY | : | 100 | | |
| NMB PARTS NO | : | SFASDF234 | | |
| | | *SFASDF234* | | |
| CUST PARTS NO | : | SFASDF234 | | |
| CUST ORDER NO | : | | | |
| ----------------------------- | --- | ------------------ | --- | --- |
| NMB IN DIA | | MADE IN THAILAND | | |
| INVOICE NO | : | MM035639 | | |
| C/NO | : | 2 | / | 2 |
| SHIP TO | : | A | | |
| QTY | : | 100 | | |
| NMB PARTS NO | : | SFASDF234 | | |
| | | *SFASDF234* | | |
| CUST PARTS NO | : | | | |
| CUST ORDES NO | : | | | |
As you can see on the expected result, the C/No is loop based on quantity. Not just copy paste. Is there anything I can add?
Below is my current code :
Private Sub CommandButton1_Click()
Dim i As Long
For i = 2 To Worksheets("Sheet3").Range("E3").Value
Range("A1:A9", Range("E9")).Copy Sheet3.Range("A65536").End(xlUp)(2)
Next i
End Sub
Just set the value of the relevant cell to i:
Private Sub CommandButton1_Click()
Dim i As Long
Dim NewLoc As Range
For i = 2 To Worksheets("Sheet3").Range("E3").Value
'Decide where to copy the output to
Set NewLoc = Sheet3.Cells(Sheet3.Rows.Count, "A").End(xlUp).OffSet(1, 0)
'Copy the range
Range("A1:E9").Copy NewLoc
'Change the value of the cell 2 rows down and 2 rows to the right
NewLoc.Offset(2, 2).Value = i
Next i
End Sub

What is the best way to apply SQL Pivot to a table?

I'm trying to use the pivot function but not sure how to replace the Letters in the bi-weekly table to a persons name instead of the query result. Here is my query.
SELECT a.OCD, a.[f_name],a.[l_name],a.[ads_phone], b.wk1m, b.wk1t, b.wk1w, b.wk1r, b.wk1f, b.wk2m, b.wk2t, b.wk2w, b.wk2r, b.wk2f
FROM [dbo].[tbl_teleworkers] as a
inner join [dbo].[tbl_scheduled] as b ON a.pin = b.pin
where ocd = '022'
here is my result:
OCD|f_name|l_name|ads_phone|wk1m|wk1t|wk1w|wk1r|wk1f|wk2m|wk2t|wk2w|wk2r|wk2f
022|John |smith |111-1111 |M | | | R | | | | | |
022|Jane |smith |222-2222 | | | | | | | | W | |
022|Joe |smith |333-3333 |M | | | | F | | | | |
022|Jim |smith |444-4444 | | T | | | | M | | | |
022|Jill |smith |555-5555 |M | | W | | | | | | R |
Here is what I'm looking to get: So instead of the letters of the week. I'm trying to display the person's name and phone number.
wk1m |wk1t|wk1w|wk1r |wk1f|wk2m|wk2t|wk2w |wk2r|wk2f
John | | |John | | | | | |
phone| | |phone| | | | | |
| | | | | | |Jane S| |
| | | | | | |phone | |
Joe | |
phone| |
Any help would be greatly appreciated.
Thanks!
I'm going to ignore the question in your subject line because your sample data and desired output do not require a PIVOT at all.
You can get your desired output by making each column a CASE expression. In each wk** column, if the value is not empty, return the value of the first name, last initial (if desired) and phone number concatenated together.

Determine/obtain/find management chain (hierarchy) for employees using SQL

I have a table (in an MS Access DB) with every employee at a company. The table also has a column to indicate each employee's manager. There are columns (with no data) to indicate that employee's full management chain to the CEO. I need to determine/obtain/find and fill this data using SQL (and/or VBA).
I know what needs to be done but I'm drawing a complete blank on how to do it efficiently.
I know I could go row by row but that seems so inefficient. There has to be a better way.
For example, take this table below:
+------------+-----------+----------+----------+----------+
| employeeID | managerID | manager1 | manager2 | manager3 |
+------------+-----------+----------+----------+----------+
| a | | | | |
| b | a | | | |
| c | a | | | |
| d | a | | | |
| e | b | | | |
| f | b | | | |
| g | c | | | |
| h | c | | | |
| i | d | | | |
| j | e | | | |
| k | f | | | |
| l | g | | | |
+------------+-----------+----------+----------+----------+
a has no manager (CEO)
b's manager is a
l's manager is g whose manager is c whose manager is a
etc...
So this would result in the table:
+------------+-----------+----------+----------+----------+
| employeeID | managerID | manager1 | manager2 | manager3 |
+------------+-----------+----------+----------+----------+
| a | | | | |
| b | a | a | | |
| c | a | a | | |
| d | a | a | | |
| e | b | a | b | |
| f | b | a | b | |
| g | c | a | c | |
| h | c | a | c | |
| i | d | a | d | |
| j | e | a | b | e |
| k | f | a | b | f |
| l | g | a | c | g |
+------------+-----------+----------+----------+----------+