calculate a boolean flag [closed] - boolean-logic

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.)

Related

What is the issue on my recursive function made in go? [closed]

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

Why is the result of (a+b+c')(a'b'+c) not 1? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
So I got his problem as homework in my course, when I solved it the result I came up with was 1, but everywhere I check the solution stops at line 4 is if it is the final solution, but I can't spot the error in my logic for some reason!
Line1: (a+b+c')(a'b'+c)
Line2: =aa'b'+ba'b'+c'a'b'+ac+bc+c'c
Line3: =0+0+c'a'b'+ac+bc+0
Line4: =c'a'b'+ac+bc
Line5: =c'a'b'+c(a+b)
Line6: =c'+c(a'b'+(a+b))
Line7: =1*(a'b'+(a+b))
Line8: =1
You can do a very little bit better if XOR (^) is a primitive operation in your system:
Line1: (a+b+c')(a'b'+c)
Line2: = aa'b'+ba'b'+c'a'b'+ac+bc+c'c
Line3: = 0+0+c'a'b'+ac+bc+0
Line4: = c'a'b'+ac+bc
Line5: = c'a'b'+c(a+b)
Line6: = c'a'b'+c(a'b')'
Line7: = c^(a'b')
Your error is the following:
Line5: = c'a'b'+c(a+b)
Line6: = c'+c(a'b'+(a+b))
Clearly, lines 5 and 6 do not show equivalent expressions because c=0 satisfies the second regardless of a and b whereas c=0, a=1 does not satisfy the first.

Javascript - Populate country code drop down onChange of country dropdown [closed]

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

How should you handle nested functions? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
What's the proper way to call a function in PowerShell?
Example 1:
Function Divide
{
Return ($a / $b)
}
Function GetNumbers
{
$a = Read-Host "Dividend"
$b = Read-Host "Divisor"
Divide
}
GetNumbers
Example 2:
Function Divide
{
Param
(
[Int]$Dividend,
[Int]$Divisor
)
Return ($Dividend / $Divisor)
}
Function GetNumbers
{
$a = Read-Host "Dividend"
$b = Read-Host "Divisor"
Divide -Dividend $a -Divisor $b
}
GetNumbers
I mean, why should I use the Param part when I can access the parent function's variables? Or is that just bad programming?
The problem with the first example is that your Divide function will only work if it happens to be inside the GetNumbers function. If you try to call it outside of GetNumbers, you will raise an error because $a and $b will be undefined:
PS > Divide
Attempted to divide by zero.
At line:3 char:12
+ Return ($a / $b)
+ ~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
This means the use case of your Divide function is very limited and also somewhat unintuitive. People may call the function thinking it will work (since it is at module-level) and be surprised by the results.
The Divide function in the second example however does not have this issue. It is independent of the GetNumbers function and can be used anywhere a normal function can. This has three advantages:
Divide behaves as a module-level function should, which means there are no nasty surprises for your users.
The use case of Divide has been greatly enhanced. You can now divide two numbers anywhere you need to.
Your code is more robust. Divide does not need to be inside a function which just happens to define two variables named $a and $b which just happen to be numbers (see how fragile the first example is?).
Note too that you do not need to explicitly give the parameter names in your second example. You could just do:
Divide $a $b
And if you dislike the size of the second Divide function, you could always make your Param statement less spread out:
Function Divide
{
Param ([Int]$Dividend, [Int]$Divisor)
Return ($Dividend / $Divisor)
}

Can I use the function yes () to return 1 [closed]

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
Is the following function is correct?
local function yes()
return 1
end
local function no()
return 0
end
Can I use it to set the values ​​of the variables in this way?
local May_I = yes()
if May_I ~= 0 then
-- Yes I can do that
end
I like numbers, but sometimes they are not very precise.
You can deal with the imprecision you mentioned like so:
> epsilon = 1e-2
> function yes()
>> return 1
>> end
> if math.abs( yes() - 1 ) <= epsilon then
>> print("Yes I can")
>> end
Yes I can
Or alternately, be precise and use true and false.