I have a this funtion
n=5
nums=5 1 4 2 3
def LIS(nums, n):
dp = []
dp_list = []
for i in range(n):
dp.append(1)
dp_list.append([nums[i]])
for j in range(i):
if nums[j] < nums[i]:
dp[i] = max(dp[i], dp[j] + 1)
if len(dp_list[i]) <= len(dp_list[j]):
dp_list[i] = dp_list[j] + [nums[i]]
print(dp_list[dp.index(max(dp))])
return dp_list
And I get this result:
[1, 2, 3]
But I wan something like this as result
1 2 3
I have tried using this, but the result still been the same
values = ''.join([str(i) for i in LIS(nums,n)])
print(*dp_list[dp.index(max(dp))])
Related
I am trying to convert the code by replacing for loop with while loop.
This is the code which I want to be converted
Functions for the code in the below snippet. Asked by #Sameer
from random import sample
def shuffle(s): return sample(s,len(s))
rBase = range(base)
base = 2
side = base*base
def pattern(r,c):
return (base*(r%base)+r//base+c)%side
Code in which I have the problem
rows = []
cols = []
data = []
for r in shuffle(rBase):
for g in shuffle(rBase):
rows.append(g*base+r)
for c in shuffle(rBase):
for g in shuffle(rBase):
cols.append(g*base+c)
nums = shuffle(range(1,side+1))
for r in rows:
for c in cols:
data.append(nums[pattern(r,c)])
Here is my attempted code
rows = []
cols = []
data = []
s = shuffle(rBase)
i,j = 0,0
while i < len(s):
r = s[i]
while j < len(s):
s2 = shuffle(rBase)
g = s2[i]
rows.append(g*base+r)
j+=1
i += 1
i,j = 0,0
s = shuffle(rBase)
while i < len(s):
c = s[i]
while j < len(s):
s2 = shuffle(rBase)
g = s2[i]
cols.append(g*base+c)
j += 1
i += 1
nums = shuffle(range(1,side+1))
i,j =0,0
while i < len(rows):
r = rows[i]
while j < len(cols):
c = cols[i]
data.append(nums[pattern(r,c)])
j += 1
i += 1
Some part of the code is omitted due to privacy reasons.
Expected output, ie output of for loop snippet is a list with length (base ** 4) containing each number from 1 to base ** 2 exactly base ** 2 times.
For Eg:
if base is 2:
expected output would be like [3, 2, 1, 4, 4, 1, 3, 2, 1, 4, 2, 3, 2, 3, 4, 1]
Actual output is a list of length base with each element is a random number between 1 and base ** 2.(Including base ** 2)
For Eg:
if base is 2:
Actual output is like [1,1] or [2,2] or [3,3] or [4,4]
Variable j = 0 should be defined before the inner while loop. Currently, your inner while is running 'len(s)' times only also you are indexing with 'i' in your second loop, you should have used j there.
This Should Work
from random import sample
def shuffle(s): return sample(s,len(s))
base = 2
rBase = range(base)
side = base*base
def pattern(r,c):
return (base*(r%base)+r//base+c)%side
rows = []
cols = []
data = []
for r in shuffle(rBase):
for g in shuffle(rBase):
rows.append(g*base+r)
for c in shuffle(rBase):
for g in shuffle(rBase):
cols.append(g*base+c)
print(rows)
print(cols)
nums = shuffle(range(1,side+1))
for r in rows:
for c in cols:
data.append(nums[pattern(r,c)])
print(data)
rows = []
cols = []
data = []
s = shuffle(rBase)
s2 = shuffle(rBase)
i = 0
while i < len(s):
r = s[i]
j = 0
while j < len(s):
g = s2[j]
j = j + 1
rows.append(g*base+r)
i = i + 1
i = 0
s = shuffle(rBase)
s2 = shuffle(rBase)
while i < len(s):
c = s[i]
j = 0
while j < len(s):
g = s2[j]
cols.append(g*base+c)
j += 1
i += 1
nums = shuffle(range(1,side+1))
i =0
while i < len(rows):
r = rows[i]
j = 0
while j < len(cols):
c = cols[j]
data.append(nums[pattern(r,c)])
j += 1
i += 1
print(rows)
print(cols)
print(data)
I'm currently learning about functions and I came across a recursions example on w3schools.com. This code is giving me a list of triangular numbers for an argument k > 0. My question is how exactly is it printing out a list of triangular numbers with "result" defined as result = k + tri_recursion(k-1) in the body of the code. The output for an input of k = 6, for example, gives me 1, 3, 6, 10, 15, 21 but I just don't understand how I'm getting a list of triangular numbers from such a simple setting of the return variable. Help would be much appreciated :)
def tri_recursion(k):
if k > 0:
result = k + tri_recursion(k-1)
print(result)
else:
result = 0
return result
print("\n\nexample result")
tri_recursion(6)
you need create a list to storage numbers:
tri_list = []
def tri_recursion(k):
if k > 0:
result = k + tri_recursion(k-1)
tri_list.append(result)
print(result)
else:
result = 0
return result
print("\n\nexample result")
tri_recursion(6)
print(tri_list)
Then you have:
k = 6
6 + tri_recursion(5)
5 + tri_recursion(4)
4 + tri_recursion(3)
3 + tri_recursion(2)
2 + tri_recursion(1)
1 + tri_recursion(0)
1 + 0 = 1
2 + 1 = 3
3 + 3 = 6
4 + 6 = 10
5 + 10 = 15
6 + 15 = 21
This happens because you are printing the sum of the previous numbers in each return of each recursion
I'm trying to find the largest 1's secuence inside a random array.
I'm trying to find the longest connection of numbers 1 inside a binary array.
I tried to compare each value of the array with its previous values (i+-1 and j+-1), but I failed.
rows = 5
cols = 5
matrix = np.random.randint(2, size=(rows, cols))
print(matrix)
count = 0
result = 0
for i in matrix:
for j in i:
if j <= cols and j <= len(i):
if j == 1:
count += 1
result = max(result, count)
else:
count = 0
print(result)
This is an example of a random array given in my code
matrix =
[[1 1 0 1 1]
[1 0 0 0 1]
[1 1 1 1 1]
[0 0 1 0 0]
[1 0 1 0 0]]
Once we have found the largest conection of numbers 1, the result will be like that:
matrix =
[[(1) (1) 0 (1) (1)]
[(1) 0 0 0 (1)]
[(1) (1) (1) (1) (1)]
[ 0 0 (1) 0 0]
[ 1 0 (1) 0 0]]
The numbers inside the parentheses, those are the largest conection and the result would be like this:
result = 13
Complete Code
import numpy as np
rows = 200
cols = 200
arr = np.random.rand(rows, cols)
for i in range(len(arr)):
for j in range(len(arr[i])):
if arr[i][j] > 0.4:
arr[i][j] = 1
else:
arr[i][j] = 0
for ii in range(len(arr)):
for jj in range(len(arr[ii])):
if arr[ii][jj] == 1.0 or arr[ii][jj] == 1:
arr[ii][jj] = 1
else:
arr[ii][jj] = 0
arr = arr.astype(int)
print(arr)
dimension = rows * cols
danger = eval("dimension * .0002")
def find_largest(arr):
max_count = 0
visited = set() # coordinates visited by DFS
for y in range(0, len(arr)):
for x in range(0, len(arr[y])):
if arr[y][x] == 1 and (y, x) not in visited:
# Runs DFS search on (y,x) and marks cell as visited
max_count = max(max_count, explore(arr, y, x, visited))
probable_danger = suma(max_count)
print("Quantity of connections: " + str(max_count) +
" ,probability of danger: " + str(probable_danger) + " ,comparing to the total; " +
str(danger))
return max_count
def explore(arr, y, x, visited):
if arr[y][x] == 1 and (y, x) not in visited:
count = 1
visited.add((y, x))
for i, j in adj(arr, y, x):
count += explore(arr, i, j, visited)
return count
else:
return 0
def adj(arr, y, x):
# Generates valid adjacent coordinates for (y, x)
if y - 1 > 0 and arr[y - 1][x] == 1:
yield y - 1, x
if y + 1 < len(arr) and arr[y + 1][x] == 1:
yield y + 1, x
if x - 1 > 0 and arr[y][x - 1] == 1:
yield y, x - 1
if x + 1 < len(arr[y]) and arr[y][x + 1] == 1:
yield y, x + 1
def suma(max_count):
dimension = rows * cols
danger = eval("dimension * .0002")
total_ones = 0
total_zeros = 0
for i in arr:
for j in i:
if j % 2 != 0:
total_ones += 1
else:
total_zeros += 1
found = max_count / total_ones
ones = total_ones/dimension
zeros = total_zeros/dimension
print("Total of 1's "+str(ones) +
" , total of 0's: "+str(zeros))
if (found >= danger):
return found
else:
return 0
find_largest(arr)
You can look at groups of 1s in your matrix as strongly connected components in a graph. You can solve this problem using DFS.
def find_largest(arr):
max_count = 0
visited = set() # coordinates visited by DFS
for y in range(0, len(arr)):
for x in range(0, len(arr[y])):
if arr[y][x] == 1 and (y, x) not in visited:
# Runs DFS search on (y,x) and marks cell as visited
max_count = max(max_count, explore(arr, y, x, visited))
return max_count
Now on DFS itself:
def explore(arr, y, x, visited):
if arr[y][x] == 1 and (y, x) not in visited:
count = 1
visited.add((y, x))
for i, j in adj(arr, y, x):
count += explore(arr, i, j, visited)
return count
else:
return 0
def adj(arr, y, x):
# Generates valid adjacent coordinates for (y, x)
if y - 1 > 0 and arr[y - 1][x] == 1:
yield y - 1, x
if y + 1 < len(arr) and arr[y + 1][x] == 1:
yield y + 1, x
if x - 1 > 0 and arr[y][x - 1] == 1:
yield y, x - 1
if x + 1 < len(arr[y]) and arr[y][x + 1] == 1:
yield y, x + 1
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My professor wants us to solve this code and several other ones that are similar by hand. Since I'm new to programming I have no clue how to do that at all and I'm completely lost
num = 0
for i in range(2,5):
for j in range(-1, 1):
num = num*j + i
print("i =", i, "j =", j, "num =", num)
I take a stab at this.
for i in range(2,5):
for j in range(-1, 1):
translates to
2
-1, 0
3
-1, 0
4
-1, 0
Why? the for loop says "For each number in the range of 2 to 5 (not including 5) do SOMETHING"
That something is another loop.
"For the number 2, give me the range of -1 to 1 (not including 1).
After that its just some basic math.
num = 0 * -1 + 2 = 2 (new value of num is now 2, not the original 0, remember this going to next iteration)
num = 2 * 0 + 2 = 2
num = 2 * -1 +3 = 1
etc
I would do it this way.
This is your code:
num = 0
for i in range(2,5):
for j in range(-1, 1):
num = num*j + i
print("i =", i, "j =", j, "num =", num)
range(2, 5) = [2, 3, 4] and range(-1, 1) = [-1, 0], So:
num = 0
for i in [2, 3, 4]:
for j in [-1, 0]:
num = num*j + i
print("i =", i, "j =", j, "num =", num)
Now for the first iteration, i = 2, j = -1 and num = 0, So:
num = 0 * -1 + 2 = 0 + 2 = 2
^ ^ ^
| | |
num j i
So, the print statement print("i =", i, "j =", j, "num =", num) would print the following:
"i = 2 j = -1 num = 2"
For the second iteration, i = 2, j = 0 and num = 2, So:
num = 2 * 0 + 2 = 0 + 2 = 2
^ ^ ^
| | |
num j i
So, the print statement print("i =", i, "j =", j, "num =", num) would print the following:
"i = 2 j = 0 num = 2"
For the third iteration, i = 3, j = -1 and num = 2, So:
num = 2 * -1 + 3 = -2 + 3 = 1
^ ^ ^
| | |
num j i
So, the print statement print("i =", i, "j =", j, "num =", num) would print the following:
"i = 3 j = -1 num = 1"
Altogether your print statements would look like this:
"i = 2 j = -1 num = 2"
"i = 2 j = 0 num = 2"
"i = 3 j = -1 num = 1"
"i = 3 j = 0 num = 3"
"i = 4 j = -1 num = 1"
"i = 5 j = 0 num = 4"
Here is how I would follow the program:
#A
num = 0
#B
for i in range(2,5):
for j in range(-1, 1):
#C
num = num*j + i
#D
print("i =", i, "j =", j, "num =", num)
#E
A: num is undefined, i is undefined, j is undefined.
B: num is 0, i is undefined, j is undefined.
C: num is 0, i is 2, j is -1.
D: num is 2, i is 2, j is -1.
E: same as D, but this just got printed: i = 2, j = -1, num = 2
C: num is 2, i is 2, j is 0.
...
im trying to return the divisors of 2 numbers that i appended to an empty list. why is nothing being printed? im expecting for 1,2,3 to be returned to mw but i get returned "[]" and "none"
def fun (t,g) :
list1 = []
i = 1
r = 1
while i < t :
if i % t == 0 :
list1.append(i)
i = i + 1
while r < g :
if r % g == 0 :
list1.append(r)
r = r + 1
print list1
x = 4
y = 6
t = fun(x,y)
print t
i % t is never 0 since you are exiting the while loop when i == t. Perhaps you meant t % i?
Likewise for r and g.
Your function doesn't have a return so it will implicitly return None
You should add return list1 to the end of it.
def fun (t,g) :
list1 = []
i = 1
r = 1
while i < t :
if t % i == 0 :
list1.append(i)
i = i + 1
while r < g :
if g % r == 0 :
list1.append(r)
r = r + 1
print list1
return list1
x = 4
y = 6
t = fun(x,y)
print t
prints
[1, 2, 1, 2, 3]
[1, 2, 1, 2, 3]
So you still need to work out the duplicates