Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 days ago.
Improve this question
I am learning Golang with the book "The Go Programming Language", in chapter 5 section 5.3 (Multiple Return Values) exercise 5.5 I have to implement the function countWordAndImages that receives an html Node from the (golang.org/x/net) package, and counts the number of words and images inside an html file, I implemented the following function, but for any reason I receive 0 values for each words and images returned variables.
func countWordsAndImages(n *html.Node) (words, images int) {
if n.Type == html.TextNode {
words += wordCount(n.Data)
} else if n.Type == html.ElementNode && n.Data == "img" { // if tag is img on element node
images++
}
for c := n.FirstChild; c != nil; c = n.NextSibling {
tmp_words, tmp_images := countWordsAndImages(c)
words, images = words+tmp_words, images+tmp_images
}
return words, images
}
func wordCount(s string) int {
n := 0
scan := bufio.NewScanner(strings.NewReader(s))
scan.Split(bufio.ScanWords)
for scan.Scan() {
n++
}
return n
}
I tried to avoid naming the return varibles tuple in the function ((int, int)).
Use c.NextSibling to advance to the next sibling, not n.NextSibling:
for c := n.FirstChild; c != nil; c = c.NextSibling {
⋮
https://go.dev/play/p/cm51yG8Y7Ry
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
What I'm trying to do is conditionally display a div based on user input. I'd like to parse through the input and if it contains somewhere within it (The input could be a whole paragraph) one of several keywords that are in an array, then it will return true and display the div. This is what I have so far:
jQuery(function($) {
$(".conditional-content-container").hide());
var user_input = $(":input[name=input_4]");
user_input.change(function() {
if (user_input.val().indexOf(["hello", "world", "foo"]) !== -1) {
$(".conditional-content-container").show();
} else {
$(".conditional-content-container").hide();
}
});
});
<div class="conditional-content-container">
Content to be displayed if user input contains the words "hello" or "world" or "foo" somewhere within it
</div>
You need to loop over the array and check if they exist in the string
var words = ['apple', 'foo', 'bar']
function hasAnyWords(str) {
return words.some(word => str.indexOf(word) > -1);
// return words.some(function(word){
// return str.indexOf(word) > -1;
//});
}
function hasAllWords(str) {
return words.every(word => str.indexOf(word) > -1);
// return words.every(function(word){
// return str.indexOf(word) > -1;
//});
}
console.log(hasAnyWords('I like an apple'));
console.log(hasAnyWords('I like a pear'));
console.log(hasAllWords('I like an apple'));
console.log(hasAllWords('I like a bar foo apple'));
I'm trying to create an html page out indented text.
For examle:
text file:
1. hello
- stack
- overflow
- how
- are you
Will come out as:
<ol>
<il>hello</li>
<ul>
<li>stack</li> ...
so it will render as an indented list.
I thought it would be best to create a node tree inspired by this answer for a similar problem in Python
Here's my cloned struct from the link in Go which doesn't work as intended, it gets stuck in the recursion for some reason:
func (n *node) addChildren(nodes []node) {
childLevel := nodes[0].textStart
for len(nodes) > 0 {
// pop
tempNode := nodes[0]
nodes = nodes[1:]
if tempNode.textStart == childLevel {
n.children = append(n.children, tempNode)
} else if tempNode.textStart > childLevel {
nodes = append([]node{tempNode}, nodes...)
n.children[len(n.children)-1].addChildren(nodes)
} else if tempNode.textStart <= n.textStart {
nodes = append([]node{tempNode}, nodes...)
return
}
}
}
I have found Markdown
As an optimal tool for the task!
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm learning to create a game with libGDX and uses a FreeType font.
When I try to display "déplacer", I get "dplacer" (missing "é")
here is how I get the font :
public static BitmapFont getFont(String file, int size){
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal(file));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
FreeTypeFontGenerator.setMaxTextureSize(2048);
parameter.size = size;
BitmapFont font = generator.generateFont(parameter);
generator.dispose();
return font;
}
the font file is the "arial.ttf" windows font
here is how I use the font : 1st create a label style in the skin
private void setTitleLabelStyle(){
Label.LabelStyle lbs = new Label.LabelStyle();
lbs.font = Assets.getFont("arial", 150);
lbs.fontColor = Color.WHITE;
game.uiSkin.add("title", lbs);
}
and then
setTitleLabelStyle();
Label label = new Label(title, game.uiSkin, "title");
panel.add(label);
Thanks
I'm stupid !!!
the problem came from the file in which I read the texts: it was coded in ANSI, not in UTF-8 !!!
I'm sorry for asking a useless question
A bit chaotic, but with no info about the text content, something like:
parameter.characters += "€«»ÀàÂâÆæÇçÉéÈèÊêËëÎîÏïÔôŒœÙùÛûÜüŸÿ";
Or:
byte[] b = new byte[256 - 160];
for (int i = 160; i < 256; ++i) {
b[i - 160] = i;
}
parameter.characters += new String(b, "ISO-8859-1");
Tip: the following tests a java.awt.Font for unavailable characters:
String text = "déplacer"; // Or parameter.characters.
Font font = ...;
int errorIndex = font.canDisplayUpto(text);
if (errorIndex != -1) {
System.out.println("Font cannot display " + text.charAt(errorIndex));
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
Is there any Javascript plugin to populate country codes on change of country dropdown?
Plug-ins, not that I know of... I know answers shouldn't only include external links, but I guess this might be exception, I will include a few links in case 1 breaks one day...
Since Country names and codes don't change too often nowadays might be safe with this text extract:
http://www.textfixer.com/resources/dropdowns/country-list-iso-codes.txt
then using split(':') function, easy populate text & value of select lists
options elements like this:
function populateCountriesDropDown() {
var file = "countries.txt";
var selectList = document.getElementById('selectID');
var rawlist;
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function () {
if (rawFile.readyState === 4) {
if (rawFile.status === 200 || rawFile.status == 0) {
rawlist = rawFile.responseText.split('\n');
}
}
}
rawFile.send(null);
for (var i = 0; i < rawlist.length; i++) {
var country = rawlist[i].split(':');
selectList.options[selectList.options.length] = new Option(country[1], country[0]);
}
}
OR other links with what you might be looking for:
http://www.freeformatter.com/iso-country-list-html-select.html
https://github.com/umpirsky/country-list
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a very basic question related to boolean logic.
I have two boolean flags- flagA and flagB. I need to calculate flagC based on the values of flagA and flagB.
The code/rules are:
if($flagA && $flagB) {
$flagC = true;
} else if (!$flagA || !$flagB) {
$flagC = false;
} else if(!$flagA && !$flagB) {
$flagC = true;
}
These rules match with the XNOR truth table - http://en.wikipedia.org/wiki/XNOR_gate
I want to find out different ways to re-write the above code(if possible) with:
fewer lines of code
better performance (even if it is a minute difference)
using bit shifting?
The languages I am hoping to write this in - php, ruby/ruby on rails.
Any help/pointers will be great!
Thanks!
Don't use these languages much but this might work:
$flagC = ($flagA == $flagB);
From the link posted: http://en.wikipedia.org/wiki/XNOR_gate
two-input version implements logical equality, behaving according to the truth table to the right. A HIGH output (1) results if both of the inputs to the gate are the same. If one but not both inputs are HIGH (1), a LOW output (0) results.
So flagC is true when flagA equals flagB.
if($flagA && $flagB) {
$flagC = true;
} else {
$flagC = false;
}
(Your second rule covers all other cases.)