Author Topic: We want numbers that lie between 1 and 10 inclusive  (Read 1297 times)

0 Members and 1 Guest are viewing this topic.

Offline shivajikobardanTopic starter

  • Contributor
  • Posts: 23
  • Country: np
We want numbers that lie between 1 and 10 inclusive
« on: November 26, 2021, 03:11:37 pm »
I know the answer, I need to use an OR gate because that is how the code works. I want to learn the problem solving part of this.



The code that works is:



 

Code: (python) [Select]
  if(x<1 or x>10):

         print("Error")

    else:

         print("Ok")





Also and gate works if used differently. but I am unable to figure out the problem solving part of it. i.e figuring out how to decide what to use. that is what is confusing me. and this is very common thing in programming. very important as well.



I know the truth tables of OR and AND gate as well:

https://imgur.com/a/vRp29yx




What are we expecting in output condition in this case?



I will explain it with a figure:
(as show in above figure)






What is the condition we are expecting there? What is needed to be either true or false in output condition ? Can you clarify that much?





  This is such a basic question but it is affecting my programming skills too much vividly. I am unable to figure out what condition are we trying to find in output?? I asked this question here as well, but didn't get my answer that I was trying to get.



[URL unfurl="true"]https://electronics.stackexchange.com/questions/596563/we-want-numbers-that-lie-between-1-and-10-inclusive-why-should-i-use-or-gate-in/596643#596643[/URL]
« Last Edit: November 26, 2021, 04:04:04 pm by shivajikobardan »
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: We want numbers that lie between 1 and 10 inclusive
« Reply #1 on: November 26, 2021, 04:44:23 pm »
I don't think it matters which way you do it but sometimes there is no 'else' clause like:

if (some test condition)
  do something exceptional
end if

In this case you want to wind up with a true test falling into 'do something'.  If there is an 'else' clause then it could go either way.  It would still make sense to use a positive test that is easy to understand.

FWIW, I would always create my truth tables in binary ascending order

F F
F T
T F
T T

I guess an argument could be made for ordering the table by output values but I have never done it.  I wait until I get to the Karnaugh Map before I worry about adjacencies.
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6260
  • Country: fi
    • My home page and email address
Re: We want numbers that lie between 1 and 10 inclusive
« Reply #2 on: November 28, 2021, 09:38:24 pm »
In the arithmetic sense,
    out = (in ≥ 1 ∧ in ≤ 10)
and
    out = ¬(in < 1 ∨ in > 10)
are exactly the same.  Here, I'm usingfor OR,for AND, and ¬ for NOT, as is common.

Is one of the above more correct than the other?  No, both of them are equally exactly correct.

But, if you can ONLY use AND () and OR (), and cannot use NOT (¬), then you'd be limited to the first one, using OR.

I personally don't like it when I'm given such artificial limitations; there should always be a stated reason why, because often there is an even better overall solution that makes the artificial limitation completely irrelevant.



Let's examine the truth table by splitting the input into four binary inputs:
$$\begin{array}{cc|c}
\text{Input} & D C B A & \text{Output} \\
\hline
0 & 0 0 0 0 & 0 \\
1 & 0 0 0 1 & 1 \\
2 & 0 0 1 0 & 1 \\
3 & 0 0 1 1 & 1 \\
4 & 0 1 0 0 & 1 \\
5 & 0 1 0 1 & 1 \\
6 & 0 1 1 0 & 1 \\
7 & 0 1 1 1 & 1 \\
8 & 1 0 0 0 & 1 \\
9 & 1 0 0 1 & 1 \\
10 & 1 0 1 0 & 1 \\
11 & 1 0 1 1 & 0 \\
12 & 1 1 0 0 & 0 \\
13 & 1 1 0 1 & 0 \\
14 & 1 1 1 0 & 0 \\
15 & 1 1 1 1 & 0 \\
\end{array}$$
If there are more significant binary digit inputs than the above four, any one of them being one means the output must be zero.

The corresponding Karnaugh map would be 4×4.  For example,
$$\begin{array}{c|cccc}
~ \diagdown & BA & ~ & ~ & ~ \\
DC & 01 & 11 & 10 & 00 \\
\hline
00 & 1 & 1 & 1 & 0 \\
01 & 1 & 1 & 1 & 1 \\
11 & 0 & 0 & 0 & 0 \\
10 & 1 & 0 & 1 & 1 \\
\end{array}$$
One way to write this would be
   out = ¬( (D ∧ C) ∨ (D ∧ B ∧ A) ∨ (¬(D ∨ C ∨ B ∨ A))
which uses both AND () and OR () operations, as well as NOT (¬).  The (D ∧ C) refers to the entire third row, the (D ∧ B ∧ A) refers to the second column in the third and fourth rows, and the ¬(D ∨ C ∨ B ∨ A) refers to the fourth column on the first row.  These three cover all zeros in the K-map, so OR'ing them together and inverting the result, yields the output.

(There are different methods for solving K-maps, and perhaps this one isn't the one you arrive at using what you have in your proverbial "toolbox".  That's okay; I pulled this one way out of my proverbial backside, using my experience and observations in minimizing binary expressions in programming as my "toolkit".)

So, as you can see, the underlying question, "how to decide what to use" is more of an optimization problem: you decide what to use, based on what gives you the expression you prefer.  How you compare different valid expressions, is up to you.
« Last Edit: November 28, 2021, 09:40:23 pm by Nominal Animal »
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: We want numbers that lie between 1 and 10 inclusive
« Reply #3 on: November 28, 2021, 10:14:36 pm »
If you've got a windows PC, you may find Logic Friday useful for wrangling combinatorial logic problems.  Its a GUI frontend for the Espresso logic minimizer  The original website's gone to domain parking, but it can still be accessed, including downloading the installer, [here] at the Internet Archive.

You can enter logic problems by truth table, logic equations, or as a gate diagram and convert from one representation to the others.  It also does minimization, and can output C source for functions to generate each output.
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 11887
  • Country: us
Re: We want numbers that lie between 1 and 10 inclusive
« Reply #4 on: November 28, 2021, 10:14:59 pm »
I know the answer, I need to use an OR gate because that is how the code works. I want to learn the problem solving part of this.

I think you are confusing yourself by thinking about OR gates or AND gates.

If you are programming then you want to be thinking in a natural language like English, so you and other people can read the program like a book. If you read a book, you don't have to solve a problem, you just read it.

Similarly, with your problem.

"I want the number to be between 1 and 10"

"I want the number to be at least 1, and I want the number to be no more than 10"

Let the number be N. Then:

"I want N >= 1 and I want N <= 10"

"I want N >= 1 and N <= 10"

Code: [Select]
if (N >= 1 and N <= 10):
    print("OK")
else:
    print("Error")

Also note that in Python only, you could write this:

Code: [Select]
if (1 <= N <= 10):
    print("OK")
else:
    print("Error")

But be careful, because this won't work the same way in other languages.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf