So this code is suppose to display an "*" if the terrain number is less than or equal to the flood level number. If it is larger a " " is replaced in the matrix.
The output I am suppose to get looks like this:
100 104 107 103 109 106 112 115
102 101 105 100 106 110 115 120
103 99 102 96 101 105 110 122
97 94 98 100 104 100 109 113
94 93 95 98 100 103 108 110
97 99 101 101 104 108 110 115
99 101 104 107 110 110 115 125
Flooding at elevation = 95
*
* * *
Flooding at elevation = 100
*
*
* *
* * * * *
* * * * *
* *
*
Flooding at elevation = 105
* * *
* * * *
* * * * * *
* * * * * *
* * * * * *
* * * * *
* * *
The spacing might be a little off due to transfer issues with the PDF it is on.
My output looks like this:
100 104 107 103 109 106 112 115
102 101 105 100 106 110 115 120
103 99 102 96 101 105 110 122
97 94 98 100 104 100 109 113
94 93 95 98 100 103 108 110
97 99 101 101 104 108 110 115
99 101 104 107 110 110 115 125
Flood elevation is: 95
* * * * * * *
* * * * * * *
* * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
*
*
* *
*
* * *
* * * *
* * * *
* * *
* *
* * *
* *
Flood elevation is: 100
* * * * * * *
* * * * * * *
* * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
*
*
* *
*
* * *
* * * *
* * * *
* * *
* *
* * *
* *
Flood elevation is: 105
* * * * * * *
* * * * * * *
* * * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
*
*
* *
*
* * *
* * * *
* * * *
* * *
* *
* * *
* *
As you can see, I am getting way too many asterisks. I know the problem is in one of the loops but I'm just not sure where. I was thinking in my main function. What the output SHOULD be was very messed up in the transfer from PDF text, as you can see at elevation 100 and 105 it was very messed up. 95 is perfect and servers as the best example.
This is my code, any help would be more than appreciated.
def main():
# Getting terrain from read terrain and flood elevations from flood levels
data = readTerrain()
floodElevation = floodLevels()
# Printing the matrix of terrain numbers
printTerrain(data)
# Making the matrix of "*" and " " ready to print
floodMatrix = floodMap(data, floodElevation)
# Move through the list of flood elevations and print each map for that number
for num in floodElevation:
print()
print("Flood elevation is: ", num)
print()
printFlood(floodMatrix)
# Def readTerrain
# #return returns the matrix of numbers. A list of lists.
def readTerrain():
inFile = open("terrain.txt", "r")
data = inFile.readlines()
matrix = []
for line in data:
matrix.append(line.rstrip().split(" "))
inFile.close()
return matrix
# Def printTerrain
# #param matrix - gets the matrix from readTerrain and prints the matrix.
# #return returns the printed matrix
def printTerrain(matrix):
for i in range(len(matrix)):
for j in range(len(matrix[0])):
print("%5s" % matrix[i][j], end="")
print()
return matrix
# Def floodMap
# #param matrix, passing the data in to compare the numbers.
# #param h2OElevation, passing in the flood levels to compare to the terrain levels
# #return map, returns the matrix of "*" and " "
def floodMap(matrix, h2OElevation):
map = []
for num in h2OElevation:
for i in range(len(matrix)):
row = []
for j in range(len(matrix)):
if matrix[i][j] <= num:
row.append("*")
else:
row.append(" ")
map.append(row)
return map
# Def floodLevels
# #return floodElevation, returns a list of numbers of the h2OElevations
def floodLevels():
inFile = open("flood.txt", "r")
elevation = inFile.readlines()
floodElevation = []
for line in elevation:
floodElevation.append(line.rstrip())
inFile.close()
return floodElevation
# Def printFlood
# #param floodMatrix, brings the floodMap of "*" and " " to print the matrix
# #return floodMatrix, returns the printed matrix of flooded areas
def printFlood(floodMatrix):
for i in range(len(floodMatrix)):
for j in range(len(floodMatrix[0])):
print("%5s" % floodMatrix[i][j], end="")
print()
return floodMatrix
# Call main
main()
You're doing string comparison instead of integer. Make sure to convert the numbers to ints when reading from file.
The string "100" is less than "95" which is why you get an "*" there.
Fixes you can make:
From the readTerrain() function:
line = line.split(" ")
becomes
line = map(int, line.split(" "))
And from the floodLevels() function:
line = line.rstrip()
Becomes
line = int(line.rstrip())
Since the values are now ints instead of strings, you'll need to convert them back or use a different string formatting. Easiest change would be to convert back to str to keep you spacing.
That means changing the line in printTerrain(matrix) from:
print("%5s" % matrix[i][j], end="")
To
print("%5s" % str(matrix[i][j]), end="")
That should output the right thing. Your loops look correct.
Related
I have a solution for a mathematical problem that looks like this (sorry for the formatting, that's what PyCharm is doing):
sigma = (wf_s[0] * u ** 4 + wf_s[1] * u ** 3 * v + wf_s[2] * u ** 3 + wf_s[3] * u ** 2 * v ** 2 + wf_s[
4] * u ** 2 * v + wf_s[5] * u ** 2 + wf_s[6] * u * v ** 3 + wf_s[7] * u * v ** 2 + wf_s[8] * u * v + wf_s[
9] * u + wf_s[10] * v ** 4 + wf_s[11] * v ** 3 + wf_s[12] * v ** 2 + wf_s[13] * v + wf_s[14]) / (
wf_s[15] * u ** 4 + wf_s[16] * u ** 3 * v + wf_s[17] * u ** 3 + wf_s[18] * u ** 2 * v ** 2 +
wf_s[19] * u ** 2 * v + wf_s[20] * u ** 2 + wf_s[21] * u * v ** 3 + wf_s[22] * u * v ** 2 +
wf_s[23] * u * v + wf_s[24] * u + wf_s[25] * v ** 4 + wf_s[26] * v ** 3 + wf_s[
27] * v ** 2 + wf_s[28] * v + wf_s[29])
sigma = sqrt(sigma)
x1 = (wf_x1[0] * u ** 2 * sigma + wf_x1[1] * u ** 2 + wf_x1[2] * u * v * sigma + wf_x1[3] * u * v + wf_x1[
4] * u * sigma + wf_x1[5] * u + wf_x1[6] * v ** 2 * sigma + wf_x1[7] * v ** 2 + wf_x1[8] * v * sigma +
wf_x1[9] * v + wf_x1[10] * sigma + wf_x1[11]) / (
wf_x1[12] * u ** 2 + wf_x1[13] * u * v + wf_x1[14] * u + wf_x1[15] * v ** 2 + wf_x1[16] * v +
wf_x1[17])
x2 = (wf_x2[0] * u ** 2 * sigma + wf_x2[1] * u ** 2 + wf_x2[2] * u * v * sigma + wf_x2[3] * u * v + wf_x2[
4] * u * sigma + wf_x2[5] * u + wf_x2[6] * v ** 2 * sigma + wf_x2[7] * v ** 2 + wf_x2[8] * v * sigma +
wf_x2[9] * v + wf_x2[10] * sigma + wf_x2[11]) / (
wf_x2[12] * u ** 2 + wf_x2[13] * u * v + wf_x2[14] * u + wf_x2[15] * v ** 2 + wf_x2[16] * v +
wf_x2[17])
y1 = (wf_y1[0] * u ** 2 * sigma + wf_y1[1] * u ** 2 + wf_y1[2] * u * v * sigma + wf_y1[3] * u * v + wf_y1[
4] * u * sigma + wf_y1[5] * u + wf_y1[6] * v ** 2 * sigma + wf_y1[7] * v ** 2 + wf_y1[8] * v * sigma +
wf_y1[9] * v + wf_y1[10] * sigma + wf_y1[11]) / (
wf_y1[12] * u ** 2 + wf_y1[13] * u * v + wf_y1[14] * u + wf_y1[15] * v ** 2 + wf_y1[16] * v +
wf_y1[17])
y2 = (wf_y2[0] * u ** 2 * sigma + wf_y2[1] * u ** 2 + wf_y2[2] * u * v * sigma + wf_y2[3] * u * v + wf_y2[
4] * u * sigma + wf_y2[5] * u + wf_y2[6] * v ** 2 * sigma + wf_y2[7] * v ** 2 + wf_y2[8] * v * sigma +
wf_y2[9] * v + wf_y2[10] * sigma + wf_y2[11]) / (
wf_y2[12] * u ** 2 + wf_y2[13] * u * v + wf_y2[14] * u + wf_y2[15] * v ** 2 + wf_y2[16] * v +
wf_y2[17])
This calculation is running relatively slow in Python so I wanted to increase the speed using numpy. The result looks like this:
u2 = u ** 2
u3 = u ** 3
u4 = u ** 4
v2 = v ** 2
v3 = v ** 3
v4 = v ** 4
uv_s = np.array((u4, u3 * v, u3, u2 * v2, u2 * v, u2, u * v3, u * v2, u * v, u, v4, v3, v2, v, 1))
sigma = np.dot(wf_s[:15], uv_s) / np.dot(wf_s[15:], uv_s)
sigma = sqrt(sigma)
uv_xy_n = np.array((u2 * sigma, u2, u * v * sigma, u * v, u * sigma, u, v2 * sigma, v2, v * sigma, v, sigma, 1))
uv_xy_d = np.array((u2, u * v, u, v2, v, 1))
x12 = np.dot(wf_x1[:12], uv_xy_n) / np.dot(wf_x1[12:], uv_xy_d)
x22 = np.dot(wf_x2[:12], uv_xy_n) / np.dot(wf_x2[12:], uv_xy_d)
y12 = np.dot(wf_y1[:12], uv_xy_n) / np.dot(wf_y1[12:], uv_xy_d)
y22 = np.dot(wf_y2[:12], uv_xy_n) / np.dot(wf_y2[12:], uv_xy_d)
In theory this works fine and runs 3 times faster than my first method but the accuracy of the results is horrible. In my case, x and y are in the hundreds and the second method produces results that differ up to 0.2 from the real result. This accuracy is of no use in my case. Plotting the results of the first method results in a perfect plot as expected. Plotting the results of the second method results in a super noisy plot.
Why is the accuracy of the numpy version so bad and how can I improve it?
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 * * * *
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.
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:
*
--------------------------------------------------------------------------------
*
* *
--------------------------------------------------------------------------------
*
* *
* * *
--------------------------------------------------------------------------------
*
* *
* * *
* * * *
--------------------------------------------------------------------------------
*
* *
* * *
* * * *
* * * * *
--------------------------------------------------------------------------------
*
* *
* * *
* * * *
* * * * *
* * * * * *
--------------------------------------------------------------------------------
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
--------------------------------------------------------------------------------
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
* ------------------------------------------------------------------