How to make triangle x in python from source code below - python

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:
*
--------------------------------------------------------------------------------
*
* *
--------------------------------------------------------------------------------
*
* *
* * *
--------------------------------------------------------------------------------
*
* *
* * *
* * * *
--------------------------------------------------------------------------------
*
* *
* * *
* * * *
* * * * *
--------------------------------------------------------------------------------
*
* *
* * *
* * * *
* * * * *
* * * * * *
--------------------------------------------------------------------------------
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
--------------------------------------------------------------------------------

Related

How do I create header for rows/columns in a matrix?

I have 4 x 4 matrix and I want to make headers for its row and columns.
1 2 3 4
1 * * * *
2 * * * *
3 * * * *
4 * * * *
header for columns: print(' 1 ',' 2 ',' 3 ',' 4 ') This works
header for rows: print('1', '\n', '2','\n', '3', '\n', '4') This doesn't work
How do I do?
Why bother doing these useless print statements, just use super quick and beautiful pandas:
(an example having * as values):
import pandas as pd
df=pd.DataFrame('*',index=range(1,5),columns=range(1,5))
And now:
print(df)
Is:
1 2 3 4
1 * * * *
2 * * * *
3 * * * *
4 * * * *
If you clicked on the blue pandas text, you we'll learn about pandas, pandas is not only displaying, you can do anything with data, (not the display, the actual data).
and if you really have to do print statement:
print(' 1',' 2',' 3',' 4')
print('1 * * * *\n2 * * * *\n3 * * * *\n4 * * * *')
Output:
1 2 3 4
1 * * * *
2 * * * *
3 * * * *
4 * * * *

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

Substitution trace python code

I'm trying to do a substitution trace for this code:
def hanoi(n):
if n == 1:
return 1
else:
return 2 * hanoi(n - 1) + 1
print hanoi(4)
Output:
15
This is what I did:
2 * (4 - 1) + 1
2 * (3) + 1
6 + 1
7
I'm not sure what I'm doing wrong.
You have a recursion function and after each step it converted to sub functions with n-1 :
Actually you have this :
2 * hanoi(4 - 1) + 1
2 * hanoi(3) + 1
2 * (2 * hanoi(2) + 1) + 1
2 * (2 * (2 * hanoi(1) + 1) + 1) + 1
2 * (2 * (2 * 1 + 1) + 1) + 1 = 15
2 * hanoi(4-1) + 1 is not the same as 2 * (4-1) + 1

finding a table in a text file and printing it

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
* ------------------------------------------------------------------

Categories

Resources