Related
mat = [[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]]
Lets say I want to extract upper left 2x2 matrix
[[0, 1,],
[6, 7, ]]
doing mat2=mat[:2][:2] doesnt work.
It extracts the rows correctly but not columns.Seems like I need to loop throughto get the columns.
Additionally I need to do a deepcopy to mat2 suchthat modifying mat2 dont change mat.
This is because [:2] returns a list containing the first 2 elements of your matrix.
For example :-
arr = [[1, 2], [1, 3]]
print(arr[:2]) # will print the first 2 elements of the array, that is [1, 2] and [1, 3], packed into a list. So, Output : [[1, 2], [1, 3]].
In the same way,
mat = [[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]]
mat2 = mat[:2] # => [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]
# Now, if you again try to get the first 2 elements from mat2 you will get the first 2 elements of mat2, not the first 2 elements of the lists inside mat2.
mat3 = mat2[:2] # => [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]
That is where you went wrong, but this concept is quite counter-intuitive, so no worries.
So the solution would be to get the first 2 elements from matrix mat and then loop over its elements and then get the first 2 elements from them.
Therefore, this should work for you:
list(x[:2] for x in mat[:2])
Or, as #warped pointed, if you can use numpy, you can do the following:
import numpy as np
mat = np.array(mat)
mat[:2, :2]
I just came up with a difference between one line and regular for loop.
As an example;
obs = [6, 12, 8, 10, 20 16]
freq = [5, 4, 3, 2, 1, 5]
data = []
data.extend(obs[i:i+1] * freq[i] for i in range(len(obs)))
outputs
[[6, 6, 6, 6, 6], [12, 12, 12, 12], [8, 8, 8], [10, 10], [20], [16, 16, 16, 16, 16]]
However,
for i in range(len(obs)):
data.extend(obs[i:i+1] * freq[i])
outputs
[6, 6, 6, 6, 6, 12, 12, 12, 12, 8, 8, 8, 10, 10, 20, 16, 16, 16, 16, 16]
Can someone kindly explain what causes this?
extending x by y means appending each entry of y to x.
Since the entries of obs[i:i+1] * freq[i] for i in range(len(obs)) are lists of integers, data.extend(obs[i:i+1] * freq[i] for i in range(len(obs))) will append of lists of integers to data, not integers.
On the other hand, the elements of obs[i:i+1] * freq[i] are integers, and therefore data.extend(obs[i:i+1] * freq[i]) will append integers to data.
This would generate the same output as the one-liner:
obs = [6, 12, 8, 10, 20, 16]
freq = [5, 4, 3, 2, 1, 5]
data = []
for i in range(len(obs)):
data.extend([obs[i:i+1] * freq[i]])
Constraints
1<=T<=10
10<=N<=10^2
Input
2
10
17
Output
5
17
This is my code
n=int(input())
for f in range(n):
b=[]
a=int(input())
for i in range(1,a+1):
if i>1:
for j in range(2,i):
if (i%j)==0:
break
else:
if a%i==0:
b.append(i)
print(max(b))
Explanation
10 are {2,5}, so answer 5
17 is 17 itself.
In the Constraints,
if T means there are at most 10 test cases,
and N means the range of the given number is between 10 to 100,
One of the most optimized ways is to create an initialized list in the code and print the pre-computed answer for every input read. :)
answer = [0, 1, 2, 3, 2, 5, 3, 7, 2, 3,
5, 11, 3, 13, 7, 5, 2, 17, 3, 19,
5, 7, 11, 23, 3, 5, 13, 3, 7, 29,
5, 31, 2, 11, 17, 7, 3, 37, 19, 13,
5, 41, 7, 43, 11, 5, 23, 47, 3, 7,
5, 17, 13, 53, 3, 11, 7, 19, 29, 59,
5, 61, 31, 7, 2, 13, 11, 67, 17, 23,
7, 71, 3, 73, 37, 5, 19, 11, 13, 79,
5, 3, 41, 83, 7, 17, 43, 29, 11, 89,
5, 13, 23, 31, 47, 19, 3, 97, 7, 11, 5]
Create a list of primes under 100.
Run a loop for each 'i' starting from 'a' to 'a/2-1' only, and check if 'i' divides 'a' completely and is present in 'primes'.
primes = [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]
n=int(input())
for f in range(n):
a=int(input())
for i in range(a,int(a/2-1),-1):
if(a%i==0 and i in primes):
print(i)
break
If you dont want to create list of primes manually, you can use this first to create primes list through for loop:
import math
def isPrime(n):
if (n <= 1):
return False
#check from sqrt(n) to 2
for i in range(int(math.sqrt(n)),1,-1):
if (n % i == 0):
return False
return True
primes = []
for i in range(100):
if(isPrime(i)):
primes.append(i)
n=int(input())
for f in range(n):
a=int(input())
for i in range(a,int(a/2-1),-1):
if(a%i==0 and i in primes):
print(i)
break
I am trying to return a similar output in the doctest but when the function is called, I get the amount of times the item has been swapped and also the sorted list.
I tried creating a variable for an empty dictionary and tried to include the returned function inside, but I do not know how to.
def bubbleSort(numList):
num_dict = {}
for j in range(1, len(numList)):
swap_check = False
for i in range(len(numList)-1):
if numList[i] > numList[i + 1]:
numList[i], numList[i + 1] = numList[i + 1], numList[i]
swap_check = True
return numList
if swap_check == False:
break
return j, numList
expected result:
Takes a list and returns 2 values
1st returned value: a dictionary with the state of the list after each complete pass of bubble sort
2nd returned value: the sorted list
>>> bubbleSort([9,3,5,4,1,67,78])
({1: [3, 5, 4, 1, 9, 67, 78], 2: [3, 4, 1, 5, 9, 67, 78], 3: [3, 1, 4, 5, 9, 67, 78], 4: [1, 3, 4, 5, 9, 67, 78], 5: [1, 3, 4, 5, 9, 67, 78]}, [1, 3, 4, 5, 9, 67, 78])
Actual result:
>>> bubbleSort([9,3,5,4,1,67,78])
(5, [1, 3, 4, 5, 9, 67, 78])
So the reason you got (5, [1, 3, 4, 5, 9, 67, 78]) as your output is because the last time through the loop j = 5 and [1, 3, 4, 5, 9, 67, 78] is your sorted list which is what you are returning from your function. return j, numList
We can use your num_dict dictionary to store the results of the sorting algorithm as we iterate through the list.
num_dict[j] = num_list[:]
Because we are mutating num_list I make a copy of the list when we store the result of the sort in num_dict. num_list[:] just returns a copy of num_list.
Here it is in the completed function:
def bubble_sort(num_list):
num_dict = {}
for j in range(1, len(num_list)):
swap_check = False
for i in range(len(num_list)-1):
if num_list[i] > num_list[i + 1]:
num_list[i], num_list[i + 1] = num_list[i + 1], num_list[i]
swap_check = True
# store result of sort iteration
num_dict[j] = num_list[:]
if swap_check == False:
break
return num_dict, num_list
And now we get this when we run the bubble_sort function:
>>> bubble_sort([9,3,5,4,1,67,78])
({1: [3, 5, 4, 1, 9, 67, 78],
2: [3, 4, 1, 5, 9, 67, 78],
3: [3, 1, 4, 5, 9, 67, 78],
4: [1, 3, 4, 5, 9, 67, 78],
5: [1, 3, 4, 5, 9, 67, 78]},
[1, 3, 4, 5, 9, 67, 78])
Is there a replacement for the range command that can be used in for loop to add the numbers between 1 and 20 to a list.
Simple for loop
i = 1
l = []
while i <= 20:
l.append(i)
i = i + 1
Outputs
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
You can use np.linspace(1, 20, num=20)
If we are to avoid using a builtin as crucial as range, we might as well have some fun doing so.
l = []
while len(l) < 20:
l.append(len(l) + 1)
print(l)
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]