Regular expression to form words with exactly 4 a's or 5 b's - regular-language

In formal language theory (with Kleene Star) if I wanted to create a regualr expression that defines the language containing all words over the Set {a ,b}, with exactly 4 a's or 5 b's.
(note the book I have uses + for OR, and * for 0 or many)
So far I have:
[a(b*)aaa + aa(b*)aa + aaa(b*)a + aaaa(b*) + abababab + babababa + (b*)aaaa]
+
[b(a*)bbbb + bb(a*)bbb + bbb(a*)bb + bbbb(a*)b + bbbbb(a*) + (a*)bbbbb + bababababa + ababababab]
Is there a shorten way to do this? It does not seem that theory allows the use of min and max for letters.

Your expression doesn't suit all possibilities (e.g. babababababbbb). The searched expression should be much easier:
[ b*ab*ab*ab*ab* + a*ba*ba*ba*ba*ba* ]

Related

Why i = 1, and i = i + 1 would become 2?

i = 1
i = i + 1
print(i)
I am pretty confused about the code's logic. Why would i eventually become 2?
Lets begin with the first assignment:
i = 1
This creates the variable i and initialize it to the integer value 1.
Then we get to what you seem to have problem understanding:
i = i + 1
This statement can be split into two parts:
The addition
The assignment
The addition i + 1 will take the current values of the variable i, which is 1, and add the value 1 to that. In essence the expression i + 1 is the same as 1 + 1.
The result of the addition will be 2. And this result is then assigned to the variable i, making the value of i be equal to 2.
You then print the (new) current value of i:
print(i)
This will of course print the value 2.
The difference is that one modifies the data-structure itself (in-place operation) b += 1 while the other just reassigns the variable a = a + 1.
Just for completeness:
x += y is not always doing an in-place operation, there are (at least) three exceptions:
If x doesn't implement an __iadd__ method then the x += y statement is just a shorthand for x = x + y. This would be the case if x was something like an int
If __iadd__ returns NotImplemented, Python falls back to x = x + y.
The __iadd__ method could theoretically be implemented to not work in place. It'd be really weird to do that, though.
As it happens your bs are numpy.ndarrays which implements __iadd__ and return itself so your second loop modifies the original array in-place.
You can read more on this in the Python documentation of "Emulating Numeric Types".
'i' is a variable which stored 1 if We add 1 again in 'i' that means
i=1;
i+1 means 1+1=2
i=1
i=i+1// i has already 1 and here we are adding 1 again so result will be 2.
hope you understood.
Let's start from i = 1. So you are assigning i to 1. Now your situation is:
i = i + 1
So if i is 1, then the abovementioned code would be "translated" to:
i = 1 + 1
That's why i = i + 1 is equal to 2.

Calculate a serie in python

n is a knwon integer.
I need to print the serie:
Sn = 1/n + 2/(n-1) + 3/(n-2) + ... + n.
I tried:
n =2
soma =1
for i in range(1,n+1):
if n>i:
soma+=i/n + i/(n-1)
What is wrong?
You are doing it like this for now:
(1/2 + 1/1) + (2/2 + 2/1) .....
When actually what you want is to accumulate only one of them each iteration.
Therefore: soma+=i/(n-(i-1))
Also the condition n>i is always true in that case so you can try and omit it.

Sympy substitute in fractions won't work as expected

Simpy is not detecting basic substitions with subs.
I have the following fraction:
d₂⋅n₁⋅n₂
──────── + m₂⋅n₁ + mm₂⋅n₂
nc
─────────────────────────
nc
Written as
cm2 = (d2*n1*n2/nc + m2*n1 + mm2*n2)/nc
Now I want to replace n1/nc = np1 and n2/nc = np2. So I've written:
cm2.subs({n1/nc : symbols("np1"), n2/nc: symbols("np2")})
The result is:
d₂⋅n1p⋅n₂ + m₂⋅n₁ + mm₂⋅n₂
──────────────────────────
nc
Instead I expected that sympy would figure out the other substitutions and output:
d2⋅n1p⋅n2p + m2⋅n1p + mm2⋅n2p
What I'm missing here?
subs is mostly literal, so after the first substitution, say n1/nc->n1p, then n2/nc no longer appears and so it cannot be replaced. But it's not necessary to do the re-arrangement of expressions to get them in the form needed to make the substitution, you can use solve to resolve everything for you:
>>> eqs
(Eq(cm2, (d2*n1*n2/nc + m2*n1 + mm2*n2)/nc), Eq(n1/nc, np1), Eq(n2/nc, np2))
>>> solve(eqs,cm2,n1,n2, dict=True)
[{cm2: d2*np1*np2 + m2*np1 + mm2*np2, n1: nc*np1, n2: nc*np2}]
There is also an unimplemented feature described here that offers, perhaps, a more intuitive way of doing this.
Sympy's subs can have troubles converting an expression by another expression, especially if they don't appear literally in the source expression. It also can be quite ambiguous.
When the goal is that all n1 and n2 disappear from cm2, the substitution can be written differently:
from sympy import symbols
d2, n1, n2, nc, m2, mm2, n = symbols("d2 n1 n2 nc m2 mm2 n")
np1, np2 = symbols("np1 np2")
cm2 = (d2 * n1 * n2 / nc + m2 * n1 + mm2 * n2) / nc
cm2.subs({n1: np1 * nc, n2: np2 * nc}).simplify()
Result: d2*np1*np2 + m2*np1 + mm2*np2

Python: is there a way to force code to ignore a variable if user input is only whitespace?

this is my first time posting and I haven't been learning to code for very long. This is the first script I've tried to build on my own, please be nice.
The program is linked to a CSV of elements and their atomic mass. The user inputs a chemical formula and the return is the molecular mass and a breakdown of the molecular mass percentage. It runs fine but as it stands it requires 6 values, eg H 2 S 1 O 4 for H2SO4. Obviously I want to have the option for long formulae but for shorter ones the script returns an error saying it expected 6 variables but only had 4 to unpack eg: H2O1.
Is there a straightforward way to make the script skip/ignore variables if there is no user input/the input is only white-space? I've been Googling it but either it's more difficult than I imagine or I'm not using the search engine well enough. I messed around with for loops but couldn't get anything to play ball.
Attached is the code, I'm aware that it could probably be twice as simple/slick if I had a little more knowledge. Thanks in advance.
def formula():
element_1, element_1_size, element_2, element_2_size, element_3, element_3_size = input("Enter your formula: ").split()
element_1_mass = float(elements_data_symbols.loc[element_1, "Atomic Mass"])
element_2_mass = float(elements_data_symbols.loc[element_2, "Atomic Mass"])
element_3_mass = float(elements_data_symbols.loc[element_3, "Atomic Mass"])
element_1_molecular_mass = element_1_mass * int(element_1_size)
element_2_molecular_mass = element_2_mass * int(element_2_size)
element_3_molecular_mass = element_3_mass * int(element_3_size)
molecular_mass = element_1_molecular_mass + element_2_molecular_mass + element_3_molecular_mass
molecule_name = str(element_1 + element_1_size + element_2 + element_2_size + element_3 + element_3_size)
print("Molecular mass of " + molecule_name + ": " + str(molecular_mass) + " g/mol")
element_1_percentage = element_1_molecular_mass / molecular_mass * 100
element_2_percentage = element_2_molecular_mass / molecular_mass * 100
element_3_percentage = element_3_molecular_mass / molecular_mass * 100
print(element_1 + ": " + str(element_1_percentage) + " %")
print(element_2 + ": " + str(element_2_percentage) + " %")
print(element_3 + ": " + str(element_3_percentage) + " %")
print(" ")
You're right a more efficient way of doing this would be using a loop to go through each pair of elements within the array, but keeping it simple you could amend your code to conditionally set elements only if the input array is at a certain length:
# Receive user input as an array
elements = input("Enter your formula").split()
# First element should always exist (but could also do this conditionally)
element_1 = elements[0]
element_1_size = elements[1]
# Check if array has at least 4 elements
if len(elements) >= 4:
element_2 = elements[2]
element_2_size = elements[3]
# Check if array has at leat 6 elements
if len(elements) >= 6:
element_3 = elements[4]
element_3_size = elements[5]

Pattern matching on deque

I'm trying to do some pattern matching on deque in haskell, but have some problems.
Here is some explanation for what i'm trying to do.
I'm trying to implement a compiler from Refal to Haskell. Refal is a language that uses pattern matching alot. So a typical refal function looks like this:
RefalFunc {
e.1 s.1 (e.2) 's' t.1 t.2 t.1 e.2 = e.1 e.2
e.1 ' ' e.2 = 'foo' e.1 'foo' e.2
e.1 = 'anyexpression'
}
So this function takes an refal-expression (which consists of terms) and try to match it with 1 of 3 patterns, and if it succeeded then transform refal-expression into another according to rules on the right side.
e.1, e.2 .. - means some refal-expressions
t.1 t.2 - means some terms , for example "(e.1 t.2 e.2)" is a term, "e.1 t.2 e.2" is not
s.A s.1 - means symbols
's' - means symbol 's'. there are some others type of symbols, but it doesn't matter.
In my compiler refal-expressions represent as deques, so for each refal function i need to generate a function, which takes some deque and transform it, according to some pattern.
So i have something like this
myFunc1:: Deque -> Deque
myFunc1 deq = if matchPattern1 deq then doSomeWork1
else if matchPattern2 deq then doSomeWork2
...
where matchPattern1 = ...
matchPattern2 = ...
....
So, the main problem is, that I'm realy don't know how to do that matchPattern functions.
So, for given deque I can easily find first(or last) term or symbol, but i'm don't know what to do, when i meet e.1, which means part of the deque with uknown size.
So here's example.
Deque: ( 5 + 6 ) * 7 * ( 5 + ( 4 + 1 ) ) * ( 5 + 6 ) * ( 5 + 6 ) //every symbol here is an element of the deque, every non-Bracket symbol is a term, every (/some symbols/) is a term
I need to check is this deque match the following patterns (and find an appropriate partition)
pattern1: t.1 e.1 t.1
pattern2: t.1 '*' e.1 (t.2 e.2) s.1 t.1 e.3
answer for pattern1 :
t.1 = ( 5 + 6 )
e.1 = * 7 * ( 5 + ( 4 + 1 ) ) * ( 5 + 6 ) *
answer for pattern2 :
t.1 = ( 5 + 6 )
e.1 = 7 *
t.2 = 5
s.1 = *
e.2 = + ( 4 + 1 )
e.3 = * ( 5 + 6 )
I'm using an implementation of Okasaki CatenableDeque.

Resources