range() function not producing expected result - python

I have a program:
def num4():
def tp(nums):
res = []
for i in range(len(nums)):
for j in range(i+1,len(nums)):
res.append(nums[i] + nums[j])
return res
nums = [ 1, 5, 7, -2 ]
print(tp(nums)
I walked through what it does, marked it up, and expected it to produce this result:
def tp(nums):
res = []
for i in range(len(nums)):
# startvalue = len(nums), stopvalue = 0, inc = 1
for j in range(i+1,len(nums)):
# startvalue = i + 1, stopvalue = len(nums) - 1, inc = 1
res.append(nums[i] + nums[j])
return res
nums = [ 1, 5, 7, -2 ]
print(tp(nums))
for i in range(4):
# range(4) = 1, 2, 3, 4
i = 1:
for j in range(i + 1, 4):
# range(1 + 1, 4) = 2, 3
res = [nums[1] + nums[2]] = 5 + 7 = 12
res = [nums[1] + nums[3]] = 5 - 2 = 3
i = 2:
for j in range(i + 1, 4):
# range(2+1, 4) = 3
res = [nums[2] + nums[3]] = 7 - 2 = 5
i = 3:
for j in range(i + 1, 4):
# range(3+1, 4) = n/a
res = [nums[3] + n/a] = -2
i = 4
for j in range(i + 1, 4):
# range(4+1, 4) = n/a
res = [nums[4] + n/a] = 1
PREDICTED OUTPUT: res = [ 12, 3, 5, -2, 1 ]
Instead, when I did this in a Python interactive session:
from ExamCheck1 import num4
num4()
It produced this output:
[6, 8, -1, 12, 3, 5]
I got the 12, 3, 5 right, but where did the 6, 8, -1 part come from? I'm very lost and confused.

The values you expect from the range function are a bit flawed. There are three possible ways you can use the range function:
range(x): Generates an array with values from 0 ~ x-1. So range(4) = [0,1,2,3]
range(x,y): Generates values from x ~ y-1. So range(1,4) = [1,2,3]
range(x,y,z): Generating values from x~y-1 in steps of z. So range(1,10,2) = [1, 3, 5, 7, 9]
Walk through your code with these values of range and it will make sense to you.

Related

How to get many sums stepping through varying length lists that have specific orders?

My Problem:
I have a list of lists. These lists are varying length e.g. [[2, 1, 5, 3], [2,4,8]
For each item in each list I need to print its sum with the next list item, then the next 2 list items, until I print the sum of all of the list items. Then I move to the second list item and do the same until I have reached the last list item.
The output I need is:
My Desired Output:
2 + 1 = 3
2 + 1 + 5 = 8
2 + 1 + 5 + 3 = 11
1 + 5 = 6
1 + 5 + 3 = 9
5 + 3 = 8
2 + 4 = 6
2 + 4 + 8 = 14
4 + 8 = 12
My (bad) Attempt:
I have tried for hours but have not been able to get close. I was doing something along the lines of the below code but I am wondering if I need to make a recursive function??
for cluster in [[2, 1, 5, 3], [2,4,8]]:
for trip in cluster:
for trip_cluster_index in range(len(cluster)):
if trip != cluster[trip_cluster_index]:
print(cluster, trip, cluster[trip_cluster_index])
O(n^3)
list_sum = [[2, 1, 5, 3], [2,4,8]]
list_out = []
for l in list_sum:
for i in range(1, len(l)):
aux = l[i-1]
for j in range(i, len(l)):
aux += l[j]
list_out.append(aux)
print(list_out)
[3, 8, 11, 6, 9, 8, 6, 14, 12]
O(n^2)
list_sum = [[2, 1, 5, 3], [2,4,8]]
list_out = []
for l in list_sum:
list_1 = []
aux = l[0]
for i in range(1, len(l)):
aux += l[i]
list_1.append(aux)
list_out.extend(list_1)
sum_list = 0
for j in range(0, len(list_1)-1):
sum_list += l[j]
list_2 = [x-sum_list for x in list_1[j+1:]]
list_out.extend(list_2)
print(list_out)
[3, 8, 11, 6, 9, 8, 6, 14, 12]
Inverted O(n^3)
list_sum = [[2, 1, 5, 3], [2,4,8]]
list_out = []
for l in list_sum:
for i in range(0,len(l)-1):
aux = sum(l[i:])
list_out.append(aux)
for j in range(len(l)-1,i+1,-1):
aux -= l[j]
list_out.append(aux)
print(list_out)
[11, 8, 3, 9, 6, 8, 14, 6, 12]
This should give you what you want.
n = -1
listy = [[1,1,1],[2,2,2],[3,3,3]]
for l in listy:
while n < len(listy)-1:
n +=1
total = sum(l) + sum(listy[n])
print(total)
I assumed that your output must contain the whole equations, and this is what I came up with:
L=[[2, 1, 5, 3], [2,4,8]]
for i in L:
for j in range(len(i)):
for k in range(j+2, len(i)+1):
print(' + '.join([str(n) for n in i[j:k]]), '=', sum(i[j:k]))
Hope it is what you were looking for!

Algorithm to find shortest continuous subarray that contains all values from a set

I have the following problem to solve:
Given a set of integers, e.g. {1,3,2}, and an array of random integers, e.g.
[1, 2, 2, -5, -4, 0, 1, 1, 2, 2, 0, 3,3]
Find the shortest continuous subarray that contains all of the values from the set. If the subarray can not be found, return an empty array.
Result: [1, 2, 2, 0, 3]
Or
[1, 2, 2, -5, -4, 3, 1, 1, 2, 0], {1,3,2}.
Result: [3, 1, 1, 2]
I have tried the following put there seems to be something wrong with my second loop. I'm not sure what I need to change:
def find_sub(l, s):
i = 0
counts = dict()
end = 0
while i < len(s):
curr = l[end]
if curr in s:
if curr in counts:
counts[curr] = counts[curr] + 1
else:
counts[curr] = 1
i += 1
end += 1
curr_len = end
start = 0
for curr in l:
if curr in counts:
if counts[curr] == 1:
if end < len(l):
next_item = l[end]
if next_item in counts:
counts[next_item] += 1
end += 1
else:
counts[curr] -= 1
start += 1
else:
start += 1
if (end - start) < curr_len:
return l[start:end]
else:
return l[:curr_len]
You are using two-pointer approach, but move both indexes only once - until the first match found. You should repeat move right - move left pattern to get the best index interval.
def find_sub(l, s):
left = 0
right = 0
ac = 0
lens = len(s)
map = dict(zip(s, [0]*lens))
minlen = 100000
while left < len(l):
while right < len(l):
curr = l[right]
right += 1
if curr in s:
c = map[curr]
map[curr] = c + 1
if c==0:
ac+=1
if ac == lens:
break
if ac < lens:
break
while left < right:
curr = l[left]
left += 1
if curr in s:
c = map[curr]
map[curr] = c - 1
if c==1:
ac-=1
break
if right - left + 1 < minlen:
minlen = right - left + 1
bestleft = left - 1
bestright = right
return l[bestleft:bestright]
print(find_sub([1, 2, 2, -5, -4, 3, 1, 0, 1, 2, 2, 0, 3, 3], {1,3,2}))
print(find_sub([1, 2, 2, -5, -4, 3, 1, 0, 1, 2, 2, 1, 0, 3, 3], {1,3,2}))
>>[2, -5, -4, 3, 1]
>>[2, 1, 0, 3]
You can use a sliding window approach (using a generator), the idea is to generate all subsets of size n (size of the set) to size N (size of the list), and check if any of them exists, stopping when finding the first one:
from itertools import islice, chain
def window(seq, n=2):
"Returns a sliding window (of width n) over data from the iterable"
" s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... "
it = iter(seq)
result = tuple(islice(it, n))
if len(result) == n:
yield result
for elem in it:
result = result[1:] + (elem,)
yield result
l = [1, 2, 2, -5, -4, 3, 1, 1, 2, 0]
s = {1,3,2}
def minimum_subset(l, s):
for w in chain.from_iterable(window(l, i) for i in range(len(s), len(l)+1)):
if s == set(w):
return w
return []
print(minimum_subset(l, s))
Result (3, 1, 1, 2)
Here you have the live example
This should be the most performant solution, running in O(n):
def find_sub(l, s):
if len(l) < len(s):
return None
# Keep track of how many elements are in the interval
counters = {e: 0 for e in s}
# Current and best interval
lo = hi = 0
best_lo = 0
best_hi = len(l)
# Increment hi until all elements are in the interval
missing_elements = set(s)
while hi < len(l) and missing_elements:
e = l[hi]
if e in counters:
counters[e] += 1
if e in missing_elements:
missing_elements.remove(e)
hi += 1
if missing_elements:
# Array does not contain all needed elements
return None
# Move the two pointers
missing_element = None
while hi < len(l):
if missing_element is None:
# We have all the elements
if hi - lo < best_hi - best_lo:
best_lo = lo
best_hi = hi
# Increment lo
e = l[lo]
if e in counters:
counters[e] -= 1
if counters[e] == 0:
missing_element = e
lo += 1
else:
# We need more elements, increment hi
e = l[hi]
if e in counters:
counters[e] += 1
if missing_element == e:
missing_element = None
hi += 1
return l[best_lo:best_hi]
assert find_sub([1, 2, 2, -5, -4, 3, 1, 0, 1, 2, 2, 0, 3, 3], {1, 3, 2}) == [2, -5, -4, 3, 1]
assert find_sub([1, 2, 2, -5, -4, 3, 1, 0, 1, 2, 2, 1, 0, 3, 3], {1, 3, 2}) == [2, 1, 0, 3]
assert find_sub([1, 2, 2, -5, -4, 3, 1, 0, 1, 2, 2, 1, 0, 3, 3], {1, 3, 7}) is None
Joining in the fun, here's my attempt. I'm not familiar with algorithm names, but this would seem like a sliding window approach based on #Netwave's description for his answer.
I = {1, 3, 2}
A = [1, 2, 2, -5, -4, 0, 1, 1, 2, 2, 0, 3, 3]
setcount = {i: 0 for i in I}
stage = []
shortest = A
for i in range(len(A)):
# Subset
stage.append(A[i])
# Update the count
if A[i] in I:
setcount[A[i]] += 1
while 0 not in setcount.values():
# Check if new subset is shorter than existing's
if len(stage) < len(shortest):
shortest = stage.copy()
# Consume the head to get progressively shorter subsets
if stage[0] in I:
setcount[stage[0]] -= 1
stage.pop(0)
>>>print(shortest)
[1, 2, 2, 0, 3]

Splitting a heap at given key

Given a list: [10, 4, 9, 3, 2, 5, 8, 1, 0]
that has the heap structure of below:
8
9
5
10
2
4
0
3
1
What is a good algorithm in python to get [4,3,2,1,0] which is basically the left child of 10.
parent is (index+1)//2
left child is 2i+1, right child is 2i+2
L = [10, 4, 9, 3, 2, 5, 8, 1, 0]
index = 1
newheap = []
newheap.append(L[index])
leftc = 2 * index + 1
rightc = 2 * index + 2
while(leftc < len(L)):
newheap.append(L[leftc])
if(rightc < len(L)):
newheap.append(L[rightc])
leftc = 2 * leftc + 1
rightc = 2 * rightc + 2
print(newheap)
which outputs
[4,3,2,1]
but I need [4,3,2,1, 0], so not what I wanted. I started the index at 1 which points to 4.
Would recursion be better? Not sure how to go about this.
You can try something like that :
L = [10, 4, 9, 3, 2, 5, 8, 1, 0]
index = 0
offset = 1
newheap = []
while index < len(L):
index += offset
for i in range(offset):
if index+i == len(L):
break
newheap += [L[index+i]]
offset = 2 * offset
print(newheap)

Dynamic Programming - Primitive Calculator Python [duplicate]

This question already has an answer here:
Dynamic programming for primitive calculator
(1 answer)
Closed 6 years ago.
This assignment aims to implement a dynamic programming approach to a primitive calculator that can only add 1, multiply by 2 and multiply by 3. So with an input of n determine the minimum number of operations to reach n. I've implemented a very naive dp or what I think is a dp approach. It is not working. I have no-one else to ask. For an input of n = 5 the output of the below is: ([0, 1, 2, 2, 3, 4], [1, 1, 2, 3, 4, 5]) whereas there are two correct outputs for the list numbers = [1, 2, 4, 5] or [1, 3, 4, 5]. Some help would be greatly appreciated.
def DPmin_operations(n):
numbers = []
minNumOperations = [0]*(n+1)
numOps = 0
numbers.append(1)
for k in range(1,n+1):
minNumOperations[k] = 10000
# for *3 operator
if k % 3 == 0:
numOps = minNumOperations[k//3] + 1
if numOps < minNumOperations[k]:
minNumOperations[k] = numOps
numbers.append(k)
# for *2 operator
elif k % 2 == 0:
numOps = minNumOperations[k//2] + 1
if numOps < minNumOperations[k]:
minNumOperations[k] = numOps
numbers.append(k)
# for + 1 operator
elif k >= 1:
numOps = minNumOperations[k - 1] + 1
if numOps < minNumOperations[k]:
minNumOperations[k] = numOps
numbers.append(k)
return (minNumOperations, numbers)
Note that the elif blocks should really be if blocks. Currently, you're using a greedy algorithm of always trying to divide by 3; if that fails, then trying to divide by 2; if that fails, then subtracting 1. It's possible that a number is divisible by 6 so that all three options are possible, and yet dividing by 2 is more optimal then dividing by 3.
As for getting your list of numbers, do that at the end. Store all possible parents, then work backwards from your goal to see how you got there.
def dp_min_ops(n):
all_parents = [None] * (n + 1)
all_min_ops = [0] + [None] * n
for k in range(1, n + 1):
curr_parent = k - 1
curr_min_ops = all_min_ops[curr_parent] + 1
if k % 3 == 0:
parent = k // 3
num_ops = all_min_ops[parent] + 1
if num_ops < curr_min_ops:
curr_parent, curr_min_ops = parent, num_ops
if k % 2 == 0:
parent = k // 2
num_ops = all_min_ops[parent] + 1
if num_ops < curr_min_ops:
curr_parent, curr_min_ops = parent, num_ops
all_parents[k], all_min_ops[k] = curr_parent, curr_min_ops
numbers = []
k = n
while k > 0:
numbers.append(k)
k = all_parents[k]
numbers.reverse()
return all_min_ops, numbers
print(dp_min_ops(5)) # ([0, 1, 2, 2, 3, 4], [1, 3, 4, 5])
print(dp_min_ops(10)) # ([0, 1, 2, 2, 3, 4, 3, 4, 4, 3, 4], [1, 3, 9, 10])

python 3.5.0 :::: MAGIC SQUARE

I'm trying to built a magic square:
A magic square is one divided in rows and columns, with a number in each position and which the sum of rows, columns and diagonals is the same.
Example (3x3 - numbers from 1 to 9):
8 3 4
1 5 9
6 7 2
I tried to use a matrix 3x3 and a vector with 9 indexes.
import random
#j(column)
matriz = [[1, 2, 3],#i(row)
[4, 5, 6],
[7, 8, 9]]
res = False
#DEFINE A FUNCTION TO CALCULATE ALL SUMS OF ALL SIDES
def magicsquare():
if matriz[0][0] + matriz[1][0] + matriz[2][0] == matriz[0][1] + matriz[1][1] + matriz[2][1] == matriz[0][2] + matriz[1][2] + matriz[2][2] == matriz[0][0] + matriz[0][1] + matriz[0][2] == matriz[1][0] + matriz[1][1] + matriz[1][2] == matriz[2][0] + matriz[2][1] + matriz[2][2] == matriz[0][0] + matriz[1][1] + matriz[2][2] == matriz[0][2] + matriz[1][1] + matriz[2][0]:
return res = True
else:
return res = False
#DEFINE A LOOP TO GENERATE RANDOM NUMBER UNTIL FIND THE ONES THAT
#SATISFY THE CONDITIONS OF A MAGIC SQUARE
seq = [1, 2, 3, 4, 5, 6, 7, 8, 9]
while res == False:
for i in range(2):
for j in range(2):
z = random.choice(seq)
matriz[i][j] = z
x = seq.index(z)
seq[x] = []
magicsquare()
print (matriz)
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
res = False
def magicsquare():
if vetor[0] + vetor[1] + vetor[2] == vetor[3] + vetor[4] + vetor[5] == vetor[6] + vetor[7] + vetor[8] == vetor[0] + vetor[3] + vetor[6] == vetor[1] + vetor[4] + vetor[7] == vetor[2] + vetor[5] + vetor[8] == vetor[0] + vetor[4] + vetor[8] == vetor[2] + vetor[4] + vetor[6]:
return res == True
else:
return res == False
# 0 1 2 3 4 5 6 7 8
vetor = [1, 2, 3, 4, 5, 6, 7, 8, 9]
seq = [1, 2, 3, 4, 5, 6, 7, 8, 9]
if res == False:
for i in range(8):
w = random.choice(seq)
#Replace the value w in index i
vetor.insert(i, w)
#Eliminate the valyes already used
x = seq.index(w)
seq[x] =[]
magicsquare()
print (vetor)
The result is always: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Can anyone help me built a magic square and tell what's wrong with my code?
Using Python 3.5.0
Your randomizing code is wrong.
>>>
Traceback (most recent call last):
File "C:/Users/xiao/Desktop/ms.py", line 20, in <module>
magicsquare()
File "C:/Users/xiao/Desktop/ms.py", line 5, in magicsquare
if vetor[0] + vetor[1] + vetor[2] == vetor[3] + vetor[4] + vetor[5] == vetor[6] + vetor[7] + vetor[8] == vetor[0] + vetor[3] + vetor[6] == vetor[1] + vetor[4] + vetor[7] == vetor[2] + vetor[5] + vetor[8] == vetor[0] + vetor[4] + vetor[8] == vetor[2] + vetor[4] + vetor[6]:
TypeError: unsupported operand type(s) for +: 'int' and 'list'
>>> vetor
[9, 4, 8, 2, 3, [], [], [], 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
random module has a method called shuffle for shuffling a list. If you have no idea about your code, just use random.shuffle.
Plus, there are 2 ways to stop the while loop. The first way is to change the global variable res in function magicsquare:
def magicsquare():
global res
if ...:
res = True
else:
res = False
The other way is to return the condition in magicsquare:
import random
vetor = [1, 2, 3, 4, 5, 6, 7, 8, 9]
def magicsquare():
return vetor[0]+vetor[1]+vetor[2]==\
vetor[3]+vetor[4]+vetor[5]==\
vetor[6]+vetor[7]+vetor[8]==\
vetor[0]+vetor[3]+vetor[6]==\
vetor[1]+vetor[4]+vetor[7]==\
vetor[2]+vetor[5]+vetor[8]==\
vetor[0]+vetor[4]+vetor[8]==\
vetor[2]+vetor[4]+vetor[6]
while not magicsquare():
random.shuffle(vetor)
print (vetor)
def forEvenNumber(n):
arr = [[(n * y) + x + 1 for x in range(n)] for y in range(n)]
for i in range(0, n // 4):
for j in range(0, n // 4):
arr[i][j] = (n * n + 1) - arr[i][j];
for i in range(0, n // 4):
for j in range(3 * (n // 4), n):
arr[i][j] = (n * n + 1) - arr[i][j];
for i in range(3 * (n // 4), n):
for j in range(0, n // 4):
arr[i][j] = (n * n + 1) - arr[i][j];
for i in range(3 * (n // 4), n):
for j in range(3 * (n // 4), n):
arr[i][j] = (n * n + 1) - arr[i][j];
for i in range(n // 4, 3 * (n // 4)):
for j in range(n // 4, 3 * (n // 4)):
arr[i][j] = (n * n + 1) - arr[i][j];
print("\nSum of all row, column and diagonals = ",
n * (n * n + 1) // 2, "\n")
for i in range(n):
for j in range(n):
print('%2d ' % (arr[i][j]), end=" ")
print()
def forOddNumber(n):
mgsqr = [[0 for x in range(n)]
for y in range(n)]
r = n // 2
c = n - 1
num = 1
while num <= (n * n):
if r == -1 and c == n:
c = n - 2
r = 0
else:
if c == n:
c = 0
if r < 0:
r = n - 1
if mgsqr[int(r)][int(c)]:
c = c - 2
r = r + 1
continue
else:
mgsqr[int(r)][int(c)] = num
num = num + 1
c = c + 1
r = r - 1
print("\nSum of all row, column and diagonals = ",
n * (n * n + 1) // 2, "\n")
for i in range(0, n):
for j in range(0, n):
print('%2d ' % (mgsqr[i][j]), end='')
print()
print("\nWELCOME:)\n")
n = int(input("Please Enter Number of Rows and Column (n*n): "))
if n%2==0:
forEvenNumber(n)
else:
forOddNumber(n)
print("\nThank You :)")
try my code..
box = []
num = [1, 2, 3,
4, 5, 6,
7, 8, 9]
val = []
for i in num:
for j in num:
for k in num:
if ((i+j+k) == 15) and i != j and j != k and k != i:
val.append((i, j, k))
def check_sum(lst: list):
rtn = []
res = 0
lst = lst[0]
for c in range(3):
for r in range(3):
res += lst[r][c]
if res == 15:
rtn.append(True)
else:
rtn.append(False)
res = 0
res = 0
for x in range(3):
for y in range(3):
if x == y:
res += lst[x][y]
if res == 15:
rtn.append(True)
else:
rtn.append(False)
res = 0
for x in range(3):
for y in range(3):
if x+y == 2:
res += lst[x][y]
if res == 15:
rtn.append(True)
else:
rtn.append(False)
return all(rtn)
def is_unique(a, b, c):
st = set()
for x in a:
st.add(x)
for x in b:
st.add(x)
for x in c:
st.add(x)
if len(st) == 9:
return True
else:
return False
def print_box(b):
for row in b[0]:
print(row)
print()
for i in val:
for j in val:
for k in val:
if is_unique(i, j, k):
box.append([i, j, k])
if check_sum(box):
print_box(box)
if len(box) == 1:
box.clear()
output is:
(2, 7, 6)
(9, 5, 1)
(4, 3, 8)
(2, 9, 4)
(7, 5, 3)
(6, 1, 8)
(4, 3, 8)
(9, 5, 1)
(2, 7, 6)
(4, 9, 2)
(3, 5, 7)
(8, 1, 6)
(6, 1, 8)
(7, 5, 3)
(2, 9, 4)
(6, 7, 2)
(1, 5, 9)
(8, 3, 4)
(8, 1, 6)
(3, 5, 7)
(4, 9, 2)
(8, 3, 4)
(1, 5, 9)
(6, 7, 2)

Categories