finding a table in a text file and printing it - python

i have text file containing the following data:
EMethod_sel="ID"
Sel_Ele_Search_Pattern="*D=30*" Sel_Ele_Label_Toggle=OFF
Eid=SHOW_OK
All 171665 elements are suitable
ShowFE/XROT
ECHConnect
ShowFE/ZROT
ShowFE/YROT
Asca
ShowFE/YROT
Asca
ShowFE/XROT
EMSameNorm/ALL
ShowFE/ZROT
ECHPenetra ShellThick=Shell-Thickness, ScaleFac=0.9, SetID=9999
* 20 coincident elements
ECHCoincid
Summary
* O B J E C T ( FE ) Total Min-ID Max-ID Show/Defi
* ------------------------------------------------------------------
* Nodes 225965 1 226932 225785
* Elements 171665 1 198299 171665
* Materials 2 100 101 2
* Properties 46 1 1000009 46
* Element Sets 83 1 10000
* ------------------------------------------------------------------
* O B J E C T ( PART ) Total Min-LV Max-LV
* ------------------------------------------------------------------
* Parts 1 0 0
* ------------------------------------------------------------------
* O B J E C T ( GEO ) Total Minimum Maximum Show
* ------------------------------------------------------------------
* No GEO - Objects found
* ------------------------------------------------------------------
Asca
ShowFE/XROT
ShowFE/YROT
EMSameNorm/
ShowFE/ZROT
Inertia EGeo_dist_sel=0.1 ETilt_angle_sel=30 EProperty_limits_sel=ON
ELayer_limits_sel=ON ESame_face_IDs_only_sel=OFF
Eid=SHOW
i need to find if it has any table or not and if it is there it should print it and the table length may vary. so how can we do this ?
is there any module that can do this task?

Edit: Since the lines Summary and Asca mark start and end of a table, the following code can be used.
with open("in.txt") as input:
intable = False
for line in input:
if line.strip() == "Summary":
intable = True
if line.strip() == "Asca":
intable = False
if intable and line.strip().startswith("*"):
print(line.strip())
Output:
* O B J E C T ( FE ) Total Min-ID Max-ID Show/Defi
* ------------------------------------------------------------------
* Nodes 225965 1 226932 225785
* Elements 171665 1 198299 171665
* Materials 2 100 101 2
* Properties 46 1 1000009 46
* Element Sets 83 1 10000
* ------------------------------------------------------------------
* O B J E C T ( PART ) Total Min-LV Max-LV
* ------------------------------------------------------------------
* Parts 1 0 0
* ------------------------------------------------------------------
* O B J E C T ( GEO ) Total Minimum Maximum Show
* ------------------------------------------------------------------
* No GEO - Objects found
* ------------------------------------------------------------------

Related

Brute force solution returns wrong answer for Project Euler problem 31

I'm having trouble with Project Euler problem 31.
The correct answer is 73,682, and my answer is 73,681.
Here is the problem:
In the United Kingdom the currency is made up of pound (£) and pence (p). There are eight coins in general circulation:
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p), and £2 (200p).
It is possible to make £2 in the following way:
1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p.
How many different ways can £2 be made using any number of coins?
And my code:
counter = 0
for a in range(3):
for b in range(5):
if 100 * a + 50 * b > 200:
break
for c in range(11):
if 100 * a + 50 * b + 20 * c > 200:
break
for d in range(21):
if 100 * a + 50 * b + 20 * c + 10 * d > 200:
break
for e in range(41):
if 100 * a + 50 * b + 20 * c + 10 * d + 5 * e > 200:
break
for f in range(101):
if 100 * a + 50 * b + 20 * c + 10 * d + 5 * e + 2 * f <= 200:
counter += 1
print(counter)
The problem is that your code doesn't consider the case where you just have a £2 coin. You can just initialize the variable counter to 1 to fix this issue.

Python: find a sequence of numbers

I need to solve task, there is a condition:
There is a a sequence of 17 consecutive natural numbers starting with N. For any number a of this sequence there is another number b from this sequence, such that GCD (a, b)> 1. Find the minimal N with this condition.
I use this code
for i in range(2, 100000000):
not_division = 0
lst = list(range(i, i+17))
#print(lst)
for j in lst:
counter = 0
for k in lst[1:]:
if gcd_iterative(j, k) > 1 and gcd_iterative(j, k) != k:
counter += 1
if counter == 0:
not_division += 1
#print('%s have no delimiter' % j)
if not_division == 0:
print('%s SUCCESS' % str(lst))
But there is not sequence.
Maybe I do smth wrong.
I would try to tackle this with a less brute-force approach.
Some thought experiment first. Every other number will have the factor 2 in common. For the remaining 8 or 9, you need more factors. So for example you could have a factor of 3 common to some of them. Then another factor, and so on, e.g.:
2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2
* 3 * * 3 * * 3 * * 3 * * 3 * * 3
* * * 5 * * * * 5 * * * * 5 * * *
^ ^ ^ ^
So now do this in a more systematic way. Consider all prime factors smaller than 17. Try each combination of those, and for each combination each possible offset (but only those with at least 2 occurrences in the sequence). See which of these lead to a situation where every number has at least one partner. Then find the corresponding sequence using the Chinese remainder theorem.
Actually there are only 2 candidates:
2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2
3 * * 3 * * 3 * * 3 * * 3 * * 3 *
* 5 * * * * 5 * * * * 5 * * * * 5
7 * * * * * * 7 * * * * * * 7 * *
* * * * * 11 * * * * * * * * * * 11
13 * * * * * * * * * * * * 13 * * *
which is characterized by the first number x satisfying these constraints:
x mod 2 = 0
x mod 3 = 0
x mod 5 = 4
x mod 7 = 0
x mod 11 = 6
x mod 13 = 0
⇒ x mod 30030 = 2184
(computed using Sage function crt) and the mirror image of the above
2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2
* 3 * * 3 * * 3 * * 3 * * 3 * * 3
5 * * * * 5 * * * * 5 * * * * 5 *
* * 7 * * * * * * 7 * * * * * * 7
11 * * * * * * * * * * 11 * * * * *
* * * 13 * * * * * * * * * * * * 13
characterized by
y mod 2 = 0
y mod 3 = 1
y mod 5 = 0
y mod 7 = 5
y mod 11 = 0
y mod 13 = 10
⇒ y mod 30030 = 7810
which is greater, so 2184 … 2200 is the first sequence satisfying your requirements:
2184 = 23 × 3 × 7 × 13
2185 = 5 × 19 × 23
2186 = 2 × 1093
2187 = 37
2188 = 22 × 547
2189 = 11 × 199
2190 = 2 × 3 × 5 × 73
2191 = 7 × 313
2192 = 24 × 137
2193 = 3 × 17 × 43
2194 = 2 × 1097
2195 = 5 × 439
2196 = 22 × 32 × 61
2197 = 133
2198 = 2 × 7 × 157
2199 = 3 × 733
2200 = 23 × 52 × 11
Which should be in range for your loop. Actually it should have been enough to loop up to 30030, the product of the primes up to 17. So if your loop really did finish, but miss this sequence, then there must be a mistake somewhere and knowing the sequence might help you debug that.

Finding the number of k-primes within a range

Write function count_Kprimes with given parameters k, start, nd, that returns a list of the k-primes between start (inclusive) and end (inclusive).
Here is my attempt:
def count_Kprimes(k, start, nd):
ls = []
for x in range(start, nd + 1):
y = x
l = []
for i in range(2, x + 1):
while y % i == 0:
l.append(i)
y /= i
if len(l) == k:
ls.append(x)
return ls
However, my code takes too much time to process and I want to simply my code. How can it be done? Thank you so much!
This task is taken from Codewar
Well, I had fun solving this anyway. Here is a solution based on array-logic
def count_Kprimes(k, start, nd):
x = np.arange(start, nd + 1, dtype=np.float)
# divs will contain all divisors (plus one extra column)
divs = np.ones((x.size, k + 1))
# we have to loop only nd / 2^(k-1) to get all divisors
for i in range(2, int(nd / 2 ** (k - 1)) + 1):
# but each possible divisor "i" may occur up to k times
# we loop until k+1 to catch also number that exceed our target,
# so we can discard them later
for j in range(1, k + 2):
# check for each row (dimension 0) if i is a divisor
# then set the first zero-value in dimension 1 to be this divisor
d = np.prod(divs, axis=1)
divs[[[np.rint(x/d/i)==x/d/i][0],np.argmin(divs[np.rint(x/d/i)==x/d/i], axis=1)]] = i
# The correct result we're looking for is each row that has exactly
# k values != 1 (which equals to exactly one "1" per row)
indices = np.apply_along_axis(lambda x: x[x==1].size == 1, 1, divs)
for val, d in zip(x[indices], divs[indices]):
print "{} = {}".format(int(val), " * ".join([str(int(_)) for _ in d[:-1]]))
count_Kprimes(3, 1, 100)
returns
8 = 2 * 2 * 2
12 = 2 * 2 * 3
18 = 2 * 3 * 3
20 = 2 * 2 * 5
27 = 3 * 3 * 3
28 = 2 * 2 * 7
30 = 2 * 3 * 5
42 = 2 * 3 * 7
44 = 2 * 2 * 11
45 = 3 * 3 * 5
50 = 2 * 5 * 5
52 = 2 * 2 * 13
63 = 3 * 3 * 7
66 = 2 * 3 * 11
68 = 2 * 2 * 17
70 = 2 * 5 * 7
75 = 3 * 5 * 5
76 = 2 * 2 * 19
78 = 2 * 3 * 13
92 = 2 * 2 * 23
98 = 2 * 7 * 7
99 = 3 * 3 * 11

How to make triangle x in python from source code below

Hello I want to ask everybody...
Like this...
I want to make a triangle X or *, like below:
*
***
*****
*******
*********
My Algorithm is like this:
for y:=1 to i do
for x:=1 to j do
for j-x to 1 do write(' ');
for i to 2*(x-1)+1 do write('*');
Can anybody tell me how is the source code for python like pascal in above?
Thanks for your answer
You can simply do this like that:
def triangle(lines):
for i in range(lines):
print(' '*(lines-i) + '*'*(i*2+1))
triangle(5)
Output as expected
*
***
*****
*******
*********
I have some code to do that lying around here, maybe this can help you :)
def pascals_triangle(order):
"""
:brief: Compute the line of pascal's triangle with order 'order'
| line | order |
|---------------------|-------|
| 1 | 0 |
| 1 1 | 1 |
| 1 2 1 | 2 |
| 1 3 3 1 | 3 |
| 1 4 6 4 1 | 4 |
:param order: order of the line in pascal's triangle
(starting with 0, which returns just [1])
:return: a list of the pascal's triangle line of order 'order'
"""
line = [1]
for k in xrange(order):
line.append(line[k] * (order - k) / (k + 1))
return line
Here's a possible solution:
def print_pascals_triangle(levels, debug_char=None):
triangle = []
for order in range(levels):
line = [debug_char] if debug_char is not None else [1]
for k in range(order):
if debug_char is None:
value = line[k] * (order - k) / (k + 1)
line.append(value)
else:
line.append(debug_char)
triangle.append(line)
def format_row(row):
return ' '.join(map(str, row))
triangle_width = len(format_row(triangle[-1]))
for row in triangle:
print(format_row(row).center(triangle_width))
If you want the real pascal triangle you can use it like this:
for level in range(1, 8):
print_pascals_triangle(level)
print('-' * 80)
And you'll get:
1
--------------------------------------------------------------------------------
1
1 1
--------------------------------------------------------------------------------
1
1 1
1 2 1
--------------------------------------------------------------------------------
1
1 1
1 2 1
1 3 3 1
--------------------------------------------------------------------------------
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
--------------------------------------------------------------------------------
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
--------------------------------------------------------------------------------
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
--------------------------------------------------------------------------------
Otherwise, you can fake the numbers with another character, like this:
for level in range(1, 8):
print_pascals_triangle(level, debug_char='*')
print('-' * 80)
And you'll get this:
*
--------------------------------------------------------------------------------
*
* *
--------------------------------------------------------------------------------
*
* *
* * *
--------------------------------------------------------------------------------
*
* *
* * *
* * * *
--------------------------------------------------------------------------------
*
* *
* * *
* * * *
* * * * *
--------------------------------------------------------------------------------
*
* *
* * *
* * * *
* * * * *
* * * * * *
--------------------------------------------------------------------------------
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
--------------------------------------------------------------------------------

Python: what's wrong with my nested loop?

Using the while loop, I wrote a procedure that takes as input a positive whole number, and prints out a multiplication table showing all the whole number multiplications up to and including the input number. The order in which the equations are printed matters.
for example, print_multiplication_table(2) gives:
1 * 1
1 * 2
2 * 1
2 * 2
This is my code:
def print_multiplication_table(n):
a = 1
b = 1
while a <= n:
while b <= n:
print str(a) + " * " + str(b)
b = b + 1
a = a + 1
However, this doesn't seem to work as it only print out
1 * 1
1 * 2
Does anyone know why? thanks!
You need to initialize counter for inner loop before its execution
def print_multiplication_table(n):
a = 1
b = 1 # won't do harm, but doesn't really need now
while a <= n:
b = 1 # <-- note
while b <= n:
print str(a) + " * " + str(b)
b = b + 1
a = a + 1
Consider using for in place of while:
def print_multiplication_table(n):
for a in range(1, n+1):
for b in range(1, n+1):
print str(a) + " * " + str(b)
which gives:
1 * 1
1 * 2
2 * 1
2 * 2
Using for will automatically keep track of your counter variables and avoid the type of error you encountered (this of course doesn't mean you can't make errors with for-loops, but it's easier to avoid the type of error you had)
Easier still with a Python comprehension:
>>> print '\n'.join('{} * {}'.format(a,b) for a in range(1,6) for b in range(1,6))
1 * 1
1 * 2
1 * 3
1 * 4
1 * 5
2 * 1
# etc...
Or, if you want the terminal new line:
>>> gen=('{} * {}'.format(a,b) for a in range(1,6) for b in range(1,6))
>>> print '\n'.join(gen),'\n'
I used a separate gen expression just to more clear about the print with the comma. This also works:
>>> print '\n'.join('{} * {}'.format(a,b) for a in range(1,6) for b in range(1,6)),'\n'
There is no reason to do an explicit call to str in your code. If you don't, you can still use a and b as integers:
>>> gen=('{} * {} = {:2}'.format(a,b,a*b) for a in range(1,3) for b in range(1,4))
>>> print '\n'.join(gen),'\n'
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6

Categories

Resources