Difference between two functions that reverse a linked list...Why does one work and the other doesn't? - reverse

Below the linked list comment description are two functions that are supposed to reverse a singly linked list. The first one is correct and the 2nd one is wrong. My question is what is the difference between the two? In my eyes I feel like they are doing the exact same thing. I know the difference is in one line, I just don't know what is different in that line. Any help would be greatly appreciated.
Thank you!
/*
Reverse a linked list and return pointer to the head
The input list will have at least one element
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
CORRECT IMPLEMENTATION:
Node* Reverse(Node *head)
{
if (head->next == NULL) {
return head;
}
else {
Node* blah = Reverse(head->next);
head->next->next = head;
head->next = NULL;
return blah;
}
}
INCORRECT IMPLEMENTATION:
Node* Reverse(Node *head)
{
if (head->next == NULL) {
return head;
}
else {
Node* blah = Reverse(head->next);
blah->next = head;
head->next = NULL;
return blah;
}
}

Related

XPath text/replace to find text which may contain soft-hyphen

Searching for a text: Bescheinigungen my XPath should also return elements which include a soft-hyphen like: Beschei\u00ADnigungen.
I tried this:
//*[text()[replace(., "\u00AD", "")="Bescheinigungen"]]
Doesn't work. Need some help, please.
Okay, had to use some 'helper' code to make it work:
public static WebElement findByText(WebDriver driver, String text) {
List<WebElement> elements = driver.findElements(By.xpath("//*[text()]"));
return elements.stream().filter(element -> {
String elementText = element.getText();
if (elementText != null && !elementText.isEmpty()) {
return text.equals(elementText.replace("\u00AD", ""));
}
return false;
}).findFirst().orElseThrow(NotFoundException::new);
}

Can we search or filter " data-tag='to-do' " in onenote API ? If yes then how we can do this?

How can we use OneNote tags (like data-tag='to-do') with search or filter in OneNote API. I tried using provide operators but found no success.
I tried in this way --
$url = "https://www.onenote.com/api/v1.0/me/notes";
//$url .= "/pages?search=hello";
$url .= "/pages?filter=data-tag eq 'to-do'";
I want to search data-tag and then extract the data from OneNote pages which contains the data-tag='to-do'.
Any help is appreciated and thanks in advance.
You'll have to run through all your pages.
For each pages, you can retrieve its content with a GET call to https://www.onenote.com/api/v1.0/me/notes/pages/%s/content?includeIds=true
From there you get a string that you can parse.
I'll advise you to use jsoup.
With jsoup you can then write (assuming contentcontains your page's content):
Document doc = Jsoup.parse(content);
Elements todos=doc.select("[data-tag^=\"to-do\"]");
for(Element todo:todos) {
System.out.println(todo.ownText());
}
Sadly OneNote API doesn't support it yet, so I've written my custom parser which extracts notes with data-tags from page content. Here it is:
public class OneNoteParser
{
static public List<Note> ExtractTaggedNotes(string pageContent, string tag = "*")
{
List<Note> allNotes = new List<Note>();
string[] dataTagString = { "data-tag=\""};
string[] dirtyNotes = pageContent.Split(dataTagString, StringSplitOptions.RemoveEmptyEntries);
//First one in this array can be dropped as it doesn't contain todo
for (int i = 1; i < dirtyNotes.Length; i )
{
string curStr = dirtyNotes[i];
Note curNote = new Note();
// Firstly we need to extract all the tags from it (sample html: data-tag="to-do:completed,important" ....)
string allTags = curStr.Substring(0,curStr.IndexOf("\""));
curNote.Tags = new List<string>(allTags.Split(','));
// Now we have to jump to the next ">" symbol and start finding the text after it
curStr = curStr.Substring(curStr.IndexOf(">"));
int depth = 1;
bool addAllowed = false;
for (int j = 0; j < curStr.Length - 1; j )
{
// Finding next tag opener "<" symbol
if (curStr[j] == '<')
{
addAllowed = false;
// Checking if it is not "</" closer
if (curStr[j 1] == '/')
{
// Means this is a tag closer. Decreasing depth
depth--;
}
else
{
// Means this is an tag opener. Increasing depth
depth ;
}
}
else if (curStr[j] == '>')
{
addAllowed = true;
if (j > 0 && curStr[j - 1] == '/')
{
// Means this is a tag closer. Decreasing depth
depth--;
}
}
else
{
if (depth < 1)
{
// Found end of the tag. Saving index and exiting for loop
break;
}
if (addAllowed)
curNote.Text = curStr[j]; // Appending letter to string
}
}
// Filtering by tag and adding to final list
if (tag == "*" || curNote.Tags.Any(str => str.Contains(tag)))//curNote.Tags.Contains(tag, StringComparer.CurrentCultureIgnoreCase))
allNotes.Add(curNote);
}
return allNotes;
}
}
And here is the class Note
public class Note
{
public string Text;
public List<string> Tags;
public Note()
{
Tags = new List<string>();
}
}
To extract todo-s simply call this function:
OneNoteParser.ExtractTaggedNotes(pageContent, "to-do");
Also you can extract other tags like this:
OneNoteParser.ExtractTaggedNotes(pageContent, "important");
OneNoteParser.ExtractTaggedNotes(pageContent, "highlight");
//...

How do I dynamically create an html table given only a table name in entity framework?

My code is more like creating an engine that when passed a table name variable, will construct the table dynamically. This is the code I'm already working on. In my controller,
public ActionResult ViewTable(string tablename)
{
ScsContext context = new ScsContext();
List<string> columnNames = new List<string>();
var html = "<table><thead><tr>";
switch(tablename)
{
case "table1":
columnNames = typeof(table1).GetProperties().Select(a => a.Name).ToList();
foreach(var c in columnNames)
{
html += "<td>" + c.ToString() + "</td>";
}
html += "</tr></thead><tbody>";
var rows = from c in context.table1.ToList()
select c;
foreach(var c in rows)
{
foreach(var p in c.GetType().GetProperties())
{
foreach(var s in columnNames)
{
if(p.Name == s.ToString())
{
// How do I get the value here?
}
}
}
}
break;
// more case statements here based on table names
default:
break;
}
// return empty for now
return new EmptyResult();
}
I was able to loop the names of my columns in my entities, as above, and now constructing my html table based on those values.. but when I need to get the value now, I'm stucked. How do I get the value from my properties above to construct my html table based on table name variable?
And since I have more hundred tables the user want rendered, is there a simpler and more efficient way to do this?
Thanks very much,

Golang, build error inside function

So I have a really frustrating build error I have been staring at for the past hour or two. It involves one of my functions in my linked list program. It thinks I have statements outside the function when they are clearly inside, and thinks the { : } ratio is off. Am I missing something really simple?
// Index returns the location of element e. If e is not present,
// return 0 and false; otherwise return the location and true.
func (list *linkedList) Index(e AnyType) (int, bool) {
var index int = 0
var contain bool = false
if list.Contains(e) == false {
return 0, false
}
for int i := 0; i < list.count; i++ { \\175
list.setCursor(i)
if list.cursorPtr.item == e {
index = list.cursorIdx
contain = true
}
}
return index, contain \\182
} \\183
Build errors
./lists.go:175: syntax error: unexpected name, expecting {
./lists.go:182: non-declaration statement outside function body
./lists.go:183: syntax error: unexpected }
I appreciate any help. Thank you.
Looks like it's all line 175's fault, should be
for i := 0; i < list.count; i++ {
note I removed int

Jinja2 automatic creation of prefix whitespace

In StringTemplate - which I've used in some projects to emit C code - whitespace prefixes are automatically added in the output lines:
PrintCFunction(linesGlobal, linesLocal) ::= <<
void foo() {
if (someRuntimeFlag) {
<linesGlobal>
if (anotherRuntimeFlag) {
<linesLocal>
}
}
}
>>
When this template is rendered in StringTemplate, the whitespace
prefixing the multilined linesGlobal and linesLocal strings,
is copied for all the lines emitted. The generated C code is
e.g.:
void foo() {
if (someRuntimeFlag) {
int i;
i=1; // <=== whitespace prefix copied in 2nd
i++; // <=== and 3rd line
if (anotherRuntimeFlag) {
int j=i;
j++; // <=== ditto
}
}
}
I am new to Jinja2 - and tried to replicate this, to see if I can use Python/Jinja2 to do the same thing:
#!/usr/bin/env python
from jinja2 import Template
linesGlobal='\n'.join(['int i;', 'i=1;'])
linesLocal='\n'.join(['int j=i;', 'j++;'])
tmpl = Template(u'''\
void foo() {
if (someRuntimeFlag) {
{{linesGlobal}}
if (anotherRuntimeFlag) {
{{linesLocal}}
}
}
}
''')
print tmpl.render(
linesGlobal=linesGlobal,
linesLocal=linesLocal)
...but saw it produce this:
void foo() {
if (someRuntimeFlag) {
int i;
i=1;
if (anotherRuntimeFlag) {
int j=i;
j++;
}
}
}
...which is not what I want.
I managed to make the output emit proper whitespace prefixes with this:
...
if (someRuntimeFlag) {
{{linesGlobal|indent(8)}}
if (anotherRuntimeFlag) {
{{linesLocal|indent(12)}}
}
}
...but this is arguably bad, since I need to manually count whitespace
for every string I emit...
Surely Jinja2 offers a better way that I am missing?
There's no answer (yet), because quite simply, Jinja2 doesn't support this functionality.
There is, however, an open ticket for this feature - if you care about it, join the discussion there.