I have been trying to write a program python - algorithm

I have been trying to write a program a simple program without importing any library. I simply want to print the following strings in this array in vertical form without using any complex algorithm. I will be glad if anyone can help me please.
['San Francisco', 'Christchurch ', 'Sydney ', 'Bangkok ', 'Copenhagen ']

This can be done using some built-in functions, like max(), len() and zip():
L = ['San Francisco', 'Christchurch ', 'Sydney ', 'Bangkok ', 'Copenhagen ']
max_length = len(max(L, key = lambda x : len(x)))
new_L = []
for e in L:
new_L.append(e + ' ' * (max_length - len(e)))
for e in zip(*new_L):
for el in e:
if el != ' ':
print el,
Output:
S C S B C a h y a o n r d n p i n g e F s e k n r t y o h a c k a n h g c u e i r n s c c h o
The lines:
new_L = []
for e in L:
new_L.append(e + ' ' * (max_length - len(e)))
can be written with list comprehension like:
new_L = [e + ' ' * (max_length - len(e)) for e in L]
Edit:
L = ['San Francisco', 'Christchurch ', 'Sydney ', 'Bangkok ', 'Copenhagen ']
# Get the maximum length of a string in the list
max_length = len(max(L, key = lambda x : len(x)))
#print max(L, key = lambda x : len(x)) # get the maximum of the list based in length
#print max_length
# Iterate through indices of max_length: 0, 1, 2, 3 ...
for i in range(max_length):
# Iterate through each city in the list
for city in L:
# If the index is < than the length of the city
if i < len(city) and city[i] != ' ':
print city[i],

Related

Multi-threading issue in Julia

I'm trying parallelise some bits of a code but I do not understand why the following functions main1() and main2() give different results using Julia's multi-threading:
a = rand(4,4);b = rand(4,4);c = rand(4,4);d = rand(4,4)
function main1(a,b,c,d)
L = zeros(2,2,16)
FF = zeros(2,2,16)
FT = zeros(2,2,16)
F = Array{Float32}(undef,2,2)
# L = Array{Array{Float32, 1}, 4}
for i = 1:4
for j = 1:4
ic = i + j*(i-1)
F[1,1] = a[i,j]
F[1,2] = b[i,j]
F[2,1] = c[i,j]
F[2,2] = d[i,j]
L[:,:,ic] .= F * F'
FF[:,:,ic] .= F
FT[:,:,ic] .= F'
end
end
return L,FF,FT
end
function main2(a,b,c,d)
L = zeros(2,2,16)
FF = zeros(2,2,16)
FT = zeros(2,2,16)
F = Array{Float32}(undef,2,2)
# L = Array{Array{Float32, 1}, 4}
Threads.#threads for i = 1:4
Threads.#threads for j = 1:4
ic = i + j*(i-1)
F[1,1] = a[i,j]
F[1,2] = b[i,j]
F[2,1] = c[i,j]
F[2,2] = d[i,j]
L[:,:,ic] .= F * F'
FF[:,:,ic] .= F
FT[:,:,ic] .= F'
end
end
return L,FF,FT
end
How could the parallelisation of main1() be properly fixed?
You cannot nest #threads loops so normally you should do:
Threads.#threads for u in vec(CartesianIndices((4,4)))
i,j = u.I
# your code goes here
end
However, in your code you get the same ic value for different pair of values of (i,j). In the main1 you are overwriting the same parts of L, FF, FT many times which is an obvious bug. Multi-threading will change the order the data is overwritten so it will yields different results. In conclusion, first fix main1 and than parallelize it.

Pyspark: Parallelize a UDF

I want to loop through two lists, pass the the combos to a function, and get the following output:
ru = ['a', 'b', 'c']
ni = ['x', 'y', 'z']
def my_func(ru, ni):
print("{} + {}".format(ru, ni))
for i in ru:
for j in ni:
my_func(i,j)
# Output
a + x
a + y
a + z
b + x
b + y
b + z
c + x
c + y
c + z
Since this is Pyspark, I would like to parallelize it, since each iteration of the function can run independently.
Note: My actual function is a long complicated algorithm in pyspark. Just wanted to post an easy example to generalize.
What is the best way to do this?
​
Use cartesian:
ru = sc.parallelize(['a', 'b', 'c'])
ni = sc.parallelize(['x', 'y', 'z'])
print(ru.cartesian(ni).collect())
For your case,
ru.cartesian(ni).map(some_func)
Or:
def my_func(ru, ni):
print("{} + {}".format(ru, ni))
ru.cartesian(ni).foreach(lambda t: my_func(t[0], t[1]))
a + z
a + y
a + x
b + y
b + x
b + z
c + y
c + x
c + z

VBScript: How to select text after 6th occurence of char?

I have this string:
0|1|2|3|4|5|6|7|8|9
I need to return the text after the 6th occurence of | and before the 7th. In this example, it would be 6.
Can his be achieved using the simple String functions (Mid, Left, Right, InStr)?
In addition, you could use a RegExp to look for the possibly empty sequence of non-| before a | and after 6 such sequences:
>> Set r = New RegExp
>> r.Pattern = "^(?:[^\|]*\|){6}([^\|]*)\|"
>> WScript.Echo r.Execute("0|1|2|3|4|5|6|7|8|9")(0).SubMatches(0)
>>
6
For production code, you'd need a check against non-confirming data.
s = "0|1|2|3|4|5|6|7|8|9"
For i = 1 To 6
intPos1 = InStr(intPos1 + 1, s, "|")
If intPos1 = 0 Then Exit For
Next
If intPos1 > 0 Then
intPos2 = InStr(intPos1 + 1, s, "|")
If intPos2 > intPos1 Then MsgBox Mid(s, intPos1 + 1, intPos2 - intPos1 - 1)
End If
Or, like #Filburt said, it could be a one-liner with Split():
MsgBox Split(s, "|")(6)
Dim s, c, n, i, p, e, r
s = "0|1|2|3|4|5|6|7|8|9" ' examined string
c = "|" ' split char
n = 6 ' occurance to start from
i = 0
p = 0
r = ""
Do
p = InStr(p + 1, s, c)
If p = 0 Then Exit Do
i = i + 1
If i = n Then
e = InStr(p + 1, s, c)
If e > 0 Then r = Mid(s, p + 1, e - p - 1)
Exit Do
End If
Loop
MsgBox r

Convert boolean expression to 3 input NOR

I was taking a look at this link http://lizarum.com/assignments/boolean_algebra/chapter3.html to try and solve an equation I have. The original equation is:
H = MC + MC' + CRD + M'CD'
I simplified it to
H = M + CRD + M'CD'
Here is my attempt:
H = ((M + CRD + M'CD')')'
H = ((M)' * (CRD)' * (M'CD')')'
H = (((M)')' + ((CRD)')' + ((M'CD')')'
H = ((M')' + (C'+ R' + D')' + (M + C' + D)')'
Is that final equation a 3 input NOR equation? I have a feeling that I'm missing a step that makes the first parentheses into three variables.
As a first step, notice that M C + M C' = M
This simplifies your equation to
H = M + CRD + M'CD'
You can then leave out the M'. If it were false, M would be true and thus H.
H = M + CRD + CD'
This allows you to factor-out C:
H = M + C(RD + D')
Term D in the parentheses can be left out (same argument as above)
H = M + C(R + D')
The final result is:
H = M + CR + CD'
You could have arrived at this result using a Karnaugh-Veitch map.
Convince yourself asking WolframAlpha.

Simple equations solving

Think of a equations system like the following:
a* = b + f + g
b* = a + c + f + g + h
c* = b + d + g + h + i
d* = c + e + h + i + j
e* = d + i + j
f* = a + b + g + k + l
g* = a + b + c + f + h + k + l + m
h* = b + c + d + g + i + l + m + n
...
a, b, c, ... element of { 0, 1 }
a*, b*, c*, ... element of { 0, 1, 2, 3, 4, 5, 6, 7, 8 }
+ ... a normal integer addition
Some of the variables a, b, c... a*, b*, c*... are given. I want to calculate as much other variables (a, b, c... but not a*, b*, c*...) as logically possible.
Example:
given: a = 0; b = 0; c = 0;
given: a* = 1; b* = 2; c* = 1;
a* = b + f + g ==> 1 = 0 + f + g ==> 1 = f + g
b* = a + c + f + g + h ==> 2 = 0 + 0 + f + g + h ==> 2 = f + g + h
c* = b + d + g + h + i ==> 1 = 0 + d + g + h + i ==> 1 = d + g + h + i
1 = f + g
2 = f + g + h ==> 2 = 1 + h ==> h = 1
1 = d + g + h + i ==> 1 = d + g + 1 + i ==> d = 0; g = 0; i = 0;
1 = f + g ==> 1 = f + 0 ==> f = 1
other variables calculated: d = 0; f = 1; g = 0; h = 1; i = 0;
Can anybody think of a way to perform this operations automatically?
Brute force may be possible in this example, but later there are about 400 a, b, c... variables and 400 a*, b*, c*... variables.
This sounds a little like constraint propogation. You might find "Solving every Sudoku Puzzle" a good read to get the general idea.
The problem is NP-complete. Look at the system of equations:
2 = a + c + d1
2 = b + c + d2
2 = a + b + c + d3
Assume that d1,d2,d3 are dummy variables that are only used once and hence add no other constraints that di=0 or di=1. Hence from the first equation follows c=1 if a=0. From the second equation follows c=1 if b=0 and from the third one we get c=0 if a=1 and b=1 and hence we get the relation
c = a NAND b.
Thus we can express any boolean circuit using such a system of equations and therefore the boolean satisfyability problem can be reduced to solving such a system of equations.

Resources