Python, Prime number project - python

I'm trying to create a program in python that asks for a number of prime numbers to print. The program should then print them ten at a line and then continue on the next line. I managed to solve the prime number bit, but I can't seem to find a solution to the ten at a line bit.
I would really appreciate the help
Input:
num = int(input("How many primes: "))
count = 0
prime = 2
while count < num:
if all(prime%j!=0 for j in range(2, prime)):
print(prime, end =" ")
count+=1
prime +=1
Output: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79
83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173
179 181 191 193 197 199 211 223 227 229
But I need this output
How many primes? 50
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113
127 131 137 139 149 151 157 163 167 173
179 181 191 193 197 199 211 223 227 229

num = int(input("How many primes: "))
count = 0
prime = 2
while count < num:
if all(prime % j != 0 for j in range(2, prime)):
print(prime, end=" ")
count += 1
if(count%10 == 0):
print("\n")
prime += 1

Related

Recursion - Creating a matrix to paint a PPM file

We have a .ppm file representing an image, that is converted into matrix form. Like so:
208 21 139 96 38 169 0 172 123 115 172 154 0 227 153 29 234 109 222 39
5 241 176 62 133 69 0 152 145 154 99 93 0 74 85 47 241 23 207 45
25 92 229 196 163 139 0 189 76 0 0 220 0 2 152 0 79 44 249 203
5 8 75 228 108 125 0 129 0 39 0 18 0 144 30 0 0 0 172 54
222 3 25 196 240 0 0 1 0 11 0 226 0 202 20 203 235 169 0 93
238 184 0 0 0 0 249 123 0 178 0 252 0 91 152 49 119 200 0 31
0 0 220 170 165 11 148 0 0 52 0 233 0 241 131 83 173 196 0 0
204 0 0 0 0 0 0 0 92 225 0 0 0 141 159 182 0 0 0 143
141 178 217 74 0 174 243 164 200 98 138 122 67 44 34 96 0 0 68 118
133 227 39 0 0 118 234 247 38 0 0 0 0 0 0 0 243 247 108 153
54 185 145 0 0 9 102 9 57 0 159 210 128 152 171 4 0 0 118 139
225 161 52 17 0 0 115 129 0 0 170 0 0 0 0 83 45 0 204 91
212 57 167 39 174 0 0 0 0 89 178 0 197 0 0 219 0 0 0 0
173 113 78 184 115 48 107 253 0 0 53 216 0 0 109 245 0 102 42 26
251 187 218 234 139 140 84 101 0 0 64 102 0 0 0 0 106 111 237 26
164 142 31 222 63 218 252 0 0 228 151 76 169 0 95 153 168 195 157 127
141 157 99 86 156 0 0 109 0 227 97 54 0 0 144 11 237 169 67 53
171 211 226 0 0 156 208 207 0 0 0 0 0 249 56 229 194 48 216 197
29 200 99 0 188 160 178 199 145 244 0 0 162 163 254 201 0 120 239 5
51 134 175 0 193 216 79 49 89 86 180 0 0 0 0 0 35 37 42 2
In this matrix zeroes (0) represent walls and positive numbers represent colors. As you can see the matrix is divided into areas&islands by walls (i.e. zeros)(diagonal zeros count as walls as well). I need a program that returns a list of islands including all numbers in that area. (So for example a list including all numbers in first island, then a list including all in the second etc.) I wrote a program below (it's incomplete) but it hits the recursion limit.
To give some perspective, what I am trying to build here is a program that averages the colors in every island. That is, I will need to convert every number within a certain island into an average number that is the average value of all numbers in that island, but I got stuck midway. I used a recursive algorithms as it made most sense to me.
def rec_appender(img,r,c,lst):
n_rows,n_cols=len(img),len(img[0])
if r<0 or c<0 or r>=n_rows or c>=n_cols: # check out actual borders
return
if img[r][c] == 0:
return
lst.append(img[r][c])
neigh_list=[[-1,0],[+1,0],[0,-1],[0,+1]]
for neigh in neigh_list:
rec_appender(img,r+neigh[0],c+neigh[1],lst)
def averager(img):
lst = []
n_rows,n_cols=len(img),len(img[0])
for r in range(0,n_rows):
for c in range(0,n_cols):
if img[r][c] != 0: # is wall
rec_appender(img,r,c,lst)
The second function checks for all points in matrix and if they are not walls refers to first function.
First function appends that point into a list then checks it neighbors whether they are part of the same island and adds them too recursively to the list if they are part of the island. (the code is incomplete as you can see islands won't be separated but my problem is the recursive limit)
Well this should work, in the method dfs() you iterate for every cell in the board, then when you find a cell not visited you tray to visit the entire island using the method _dfs(), every time you end the visit of an island you will have the sum of the colors then you divide by the total cells visited by _dfs(). I use a modified version of the algorithm DFS, you can find more info about it here .
def dfs(colors: list[list[int]]):
mask=[ [False]*len(colors[0]) for _ in range(len(colors))]
islands_average:list[float] = []
for i in range(len(colors)):
for j in range(len(colors[0])):
if not mask[i][j] and colors[i][j]!=0 :
total, sum_for_average=_dfs(i,j,colors, mask )
average = sum_for_average/total
islands_average.append(average)
return islands_average
def verify_pos(colors,x,y):
return x>=0 and x < len(colors) and\
y>=0 and y < len(colors[0])
def _dfs(x:int,y:int,colors, mask) -> tuple[int, int]:
mask[x][y] = True
sum_for_average = colors[x][y]
total = 1
for k in range(4):
xx = x + helper_x[k]
yy = y + helper_y[k]
if verify_pos(colors,xx,yy) and colors[xx][yy]!=0 and not mask[xx][yy]:
current=_dfs(xx,yy,colors,mask)
total+=current[0]
sum_for_average+=current[1]
return total, sum_for_average
EDIT: I assumed you had how to convert colors into matrix, you can do it as follow
def get_matrix(string:str):
sol=[]
for row in string.split("\n"):
sol.append([ int(c) for c in row.split(' ') if c!=''])
return sol
EDIT2: The start method of the algorithm is dfs(colors) and the argument colors is a list of lists of colors, you can use the method get_matrix(string) to get the colors from the input string format.

Why my code is wrong for large input in subarray?

My code is working fine for small inputs but throwing error for large inputs:
#User function Template for python3
#Function to find a continuous sub-array which adds up to a given number.
class Solution:
def subArraySum(self,arr, n, s):
#Write your code here\
i=0
j=0
a=[]
sum=arr[0]
while(i<n):
if(sum == s):
a.append(i+1)
a.append(j+1)
return a
elif((sum<s) and j<n):
j+=1
sum+=arr[j]
elif(sum>s):
sum-=arr[i]
i+=1
a.append(-1)
return a
Error:
Runtime Error
Traceback (most recent call last):
File "/home/381d13fbccd456f6ad3f1e6fd0fe5554.py", line 54, in <module>
main()
File "/home/381d13fbccd456f6ad3f1e6fd0fe5554.py", line 43, in main
ans=ob.subArraySum(A, N, S)
File "/home/381d13fbccd456f6ad3f1e6fd0fe5554.py", line 20, in subArraySum
sum+=arr[j]
IndexError: list index out of range
Expected Output:
Sample input: (error)
74 665
142 112 54 69 148 45 63 158 38 60 124 142 130 179 117 36 191 43 89 107 41 143 65 49 47 6 91 130 171 151 7 102 194 149 30 24 85 155 157 41 167 177 132 109 145 40 27 124 138 139 119 83 130 142 34 116 40 59 105 131 178 107 74 187 22 146 125 73 71 30 178 174 98 113
question
The logic you followed is a bit flawed, If i < n and sum < s , j will get the value of len(arr), because j+=1 is executed before the calculation of the sum.

How to read binary file and convert it to text in Python

I have a time series from 1946-2020 for the discharges of gauge. The file is binary and if I open it in a text editor, or even in a hex-editor, I see values which do not make sense. I have searched a lot and found some code but I don't see any time series and values.
I can imagine that the time series is looking like that:
These values are also correct and are in the data.
t Q
17.11.1972 8,66
04.02.2020 28,2
I copied the beginning part of the file:
##4.00
à?š™™™™™é?ÍÌÌÌÌÌì?ffffffî?¸…ëQ¸î?\Âõ(\ï?®Gáz®ï?×£p=
×ï?V-²ïï?§èH.ÿï?Sš ä ÍÌL= ÿÿÍÌL= _ B €#
## NASIM26760601m³/sffûB°FAˆ¢A ¥¼x?  §=,ðñ=ÿ9jŒA´¯DA;Âò#¿‡Ø½ =|?0¥‡=?1=ÿ]”:A þA ¨ï¿eV4#)¡? i3|?`d‹=ek=ÿ‘_î#5Ý#¼˜DA
©]? cÂ{?Œ%¿=+>ÿÚÍ# %µ#À#•9AN? ýô{?h«=×Í­=ÿð½¢#»MAòöî# ¤¼x?¸~=Xä—=ÿ9jŒA
+BAïÕ#yBѾ ‚Äw?èrÈ=¯k“=ÿ]”:A¼/±#>. #„×9AG€
I copied the last part of the file, because I know there must be the time-discharge of 2020. Maybe it is in the end of the file.
×ï?V-²ïï?+‡ÙÎ÷ï? ÍÌL= ÿÿÍÌL= _ B €#
##
in the following screenshot you see the data , when I open it in Notepad++.
here is my python code and output
with open("time-serie_1946 bis 2020.hqr", "rb") as file:
data = file.read()
with open("out.txt", "w") as f:
f.write(" ".join(map(str,data)))
f.write("\n")
the beginning of output:
6 64 64 52 46 48 48 10 0 0 0 0 0 0 0 224 63 154 153 153 153 153 153 233 63 205 204 204 204 204 204 236 63 102 102 102 102 102 102 238 63 184 30 133 235 81 184 238 63 92 143 194 245 40 92 239 63 174 71 225 122 20 174 239 63 215 163 112 61 10 215 239 63 86 14 45 178 157 239 239 63 30 167 232 72 46 255 239 63 83 78 101 117 98 101 114 101 99 104 110 117 110 103 32 98 105 115 32 50 48 50 48 32 109 105 116 32 117 110 98 101 115 116 228 116 105 103 116 101 110 32 72 81 32 118 111 110 32 49 57 52 54 45 49 57 55 50 32 40 65 110 102 114 97 103 101 32 83 99 104 117 104 109 97 99 104 101 114 44 32 84 82 41 154 7 0 0 228 7 0 0 0 0 0 0
How can I decode it to get the time series?

cumsum in numpy ndarray (edited)

I had an issue with cumsum in a dataframe, which was nicely resolved here : https://stackoverflow.com/a/61842690/7937578
But when I tried to do it with my entire dataframe, I couldn't fit all my data into pandas, so I tried converting it to numpy arrays only, but I can't seem to reproduce the code in numpy only.
So far I have this :
test = np.arange(200).reshape(4, 50)
test[2] = np.random.choice([-1, 0, 1], size=50)
TARGET_SUM = 10
x = np.cumsum(test[2] != 0)
changing = np.roll(x, 1) != x
indices = np.where(changing & (x % TARGET_SUM == 0) & (x > 0))[0]
indices = np.concatenate(([-1,], indices))
indices += 1
for i1, i2 in zip(indices[0:-1], indices[1:]):
print(i1, i2)
print(test[i1:i2])
But the output is this :
0 13
[[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
36 37 38 39 40 41 42 43 44 45 46 47 48 49]
[ 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
86 87 88 89 90 91 92 93 94 95 96 97 98 99]
[ -1 1 0 -1 -1 0 0 -1 1 -1 1 1 -1 -1 0 -1 0 0
0 -1 0 0 -1 1 -1 1 1 -1 -1 0 1 0 0 -1 1 -1
1 0 0 0 1 0 -1 1 1 1 1 1 1 1]
[150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
186 187 188 189 190 191 192 193 194 195 196 197 198 199]]
13 29
[]
29 46
[]
Where it should be more like this :
0 13
[[ 0 1 2 3 4 5 6 7 8 9 10 11 12]
[ 50 51 52 53 54 55 56 57 58 59 60 61 62]
[ -1 1 0 -1 -1 0 0 -1 1 -1 1 1 -1]
[ 150 151 152 153 154 155 156 157 158 159 160 161 162]]
13 29
etc...
The solution of #Ben.T juste above worked perfectly !
Quoting : "I think suppr is a list of arrays, so I guess what you need is print(np.array(suppr)[:, i1:i2]) and if it is already an array, then suppr[:, i1:i2] should be enough. "

Using subprocess to ping an address and get the average ping output in Python?

With this:
import subprocess
hostname = '104.160.142.3'
pingResponse = subprocess.Popen(["ping", hostname, "-n", '1'], stdout=subprocess.PIPE).stdout.read()
I get a string pingResponse:
b'\r\nPinging 104.160.142.3 with 32 bytes of data:\r\nReply from 104.160.142.3: bytes=32 time=159ms TTL=60\r\n\r\nPing statistics for 104.160.142.3:\r\n Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),\r\nApproximate round trip times in milli-seconds:\r\n Minimum = 159ms, Maximum = 159ms, Average = 159ms\r\n'
and I basically want to get the average ms part and store it in another string, but if I try to print out word by word:
for i in pingResponse:
print(i)
I just get a bunch of numbers:
58
32
83
101
110
116
32
61
32
49
44
32
82
101
99
101
105
118
101
100
32
61
32
49
44
32
76
111
115
116
32
61
32
48
32
40
48
37
32
108
111
115
115
41
44
13
10
65
112
112
114
111
120
105
109
97
116
101
32
114
111
117
110
100
32
116
114
105
112
32
116
105
109
101
115
32
105
110
32
109
105
108
108
105
45
115
101
99
111
110
100
115
58
13
10
32
32
32
32
77
105
110
105
109
117
109
32
61
32
52
52
109
115
44
32
77
97
120
105
109
117
109
32
61
32
52
52
109
115
44
32
65
118
101
114
97
103
101
32
61
32
52
52
109
115
13
10
How can I store the average ms into another string?
You are getting numbers because that is a binary string (note the b in the beginning).
You will need to decode it first, then you can use regex:
import re
s = b'\r\nPinging 104.160.142.3 with 32 bytes of data:\r\nReply from 104.160.142.3: bytes=32 time=159ms TTL=60\r\n\r\nPing statistics for 104.160.142.3:\r\n Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),\r\nApproximate round trip times in milli-seconds:\r\n Minimum = 159ms, Maximum = 159ms, Average = 159ms\r\n'
s = s.decode()
print(re.search(r'Average = (\d+)', s, re.MULTILINE).group(1))
>> 159

Categories