Remove elements from the array which appear more than k times - python

This program gives out the output as
1 6 7 9 11
how can I make it give output as
[1, 6, 7, 9, 11]
Code:
def RemoveElements(my_lst2, n, k):
mp = {i:0 for i in range(len(my_lst2))}
for i in range(n):
mp[my_lst2[i]] += 1
for i in range(n):
if (mp[my_lst2[i]] <= 1):
print(my_lst2[i], end = " ",)
if __name__ == '__main__':
my_lst2 = [0,1,2,3,2,3,4,5,4,5,6,7,9,11,0,5]
n = len(my_lst2)
k = 2
RemoveElements(my_lst2, n, k)

Right now you are doing print(my_lst2[i], end = " ",) which prints it out as you currently have. If you store the my_lst2[i] in a list and then print out that list, you will get what you want.
def RemoveElements(my_lst2, n, k):
mp = {i:0 for i in range(len(my_lst2))}
for i in range(n):
mp[my_lst2[i]] += 1
l = []
for i in range(n):
if (mp[my_lst2[i]] <= 1):
l.append(my_lst2[i])
print(l)

Related

To generate laundau's prime numbers using python

I want to generate list of primes of the format x2+1 but I am not getting any output.
My code:
def LPrime(n):
for i in range(1,n):
x = i**2+1;
for j in range(2,x):
if x % j != 0:
print(x,i)
Please tell me what am I doing wrong.
The expected output should be something like
2,1
5,2
and so on
Also I am not able to execute it properly, I am using Ubuntu 22.04
There is no output when I try to
If if x % j == 0: you have a divisor. and j will not be 1 or x. So you should mark p as False (there is no prime). else, it is a prime; print it.
def LPrime(n):
for i in range(0, n):
x = i**2+1
p = True
for j in range(2, x):
if x % j == 0:
p = False
if p:
print(f"Prime = {x},\t i = {i}")
Don't forget to call it
>>> LPrime(4)
Prime = 1, i = 0
Prime = 2, i = 1
Prime = 5, i = 2
If you want n primes, use a while loop:
def LPrime(n):
count = 0
i = 0
while count < n:
x = i**2+1
p = True
for j in range(2, x):
if x % j == 0:
p = False
if p:
print(f"Prime = {x},\t i = {i}")
count += 1
i += 1
>>> LPrime(4)
Prime = 1, i = 0
Prime = 2, i = 1
Prime = 5, i = 2
Prime = 17, i = 4

The number of combinations of the number 196225

I wanted to write a program that will show the number of combinations of the number 196225 but I failed because it showed very strange numbers and I can't quite understand why
my code:
list = []
liczba = [1,9,6,2,2,5]
n_num = 0
iter=0
#ijlkmn
for i in range(6):
n_num = liczba[i]*10**5
for j in range(6):
if j != i:
n_num += liczba[j]*10**4
for l in range(6):
if l != i and l != j:
n_num += liczba[l]*10**3
for k in range(6):
if k != i and k != j and k!= l:
n_num += liczba[k]*10**2
for m in range(6):
if m != i and m != j and m != l and m!= k:
n_num += liczba[m]*10
for n in range(6):
if n != i and n != j and n != l and n!= k and n!= m:
n_num += liczba[n]
if (n_num in list) == False:
list.append(n_num)
iter += 1
I know it looks very primitive but I only wanted the result which turned out to be incorrect, here are some of its numbers ~
196225, 196277, 196502, 196554, 197076, 197098, 199723, 199775, 200040
could someone tell me where did these numbers come from?
if you want to make all the possible numbers using tose digits , you can use itertools module :
import itertools
liczba = [1, 9, 6, 2, 2, 5]
numbers = itertools.permutations(liczba, 6)
for num in numbers:
print(num)
You are adding to previous answers on each of your internal loops. Because the value of n_num is not reset at each iteration. For example:
digits = [1,2,3]
for k in range(3):
n_num = digits[k]*10**2
for k2 in range(3):
if k2!=k:
n_num += digits[k2]*10
print(n_num)
prints
120
150
210
240
310
330
because when k2 = 2 the first time, 30 is added to 120 yielding 150... etc etc
I see what's wrong with your code - let's look into smaller example to see it clearly and find a solution
liczba = [1, 2, 3]
for i in range(3):
n_num = liczba[i] * 10 ** 2 # it start with 1 * 100 = 100, good one!
for j in range(3):
if j != i: # for j = 0 it just pass
n_num += liczba[j] * 10 # j = 1, n_num is now 100 + 20 = 120
for l in range(3):
if l != i and l != j: we skip 0 and 1
n_num += liczba[l] # n_num = 123, great
list.append(n_num)
Now we back to second loop:
for j in range(3): # j jumps to 2
if j != i:
n_num += liczba[j] * 10 # wait - here is the problem, our n_num should be 100 here again but now it's 123, so we add 30 to previous solution
for l in range(3):
if l != i and l != j: # l = 1
n_num += liczba[l] # adding 2, but instead of 100 + 30 + 2 we have 123 + 30 + 2
list.append(n_num)
How to fix it? Very simple - you can just calculate number once at the end. You have all indexes already, so write
n_num = liczba[i] * 10 ** 5 + liczba[j] * 10 ** 4 + ...
And one small hint for the end - if you use set instead of list you won't need a check if n_num is already in result.

Im printing my value but it appends the initial value

I have this code, which runs an algorithm called Tomkins-Paige algorithm. The algorithm creates permutations of a sequence.
The problem is that the code prints the different permutations p, but when i try to append it to a list, it only appends the initial p, i.e. p = [1,2,3,4].
import numpy as np
n = 4
p = [i for i in range(1,n+1)]
c = [1 for i in range(1,n+1)]
i = 2
print(p)
listp = []
while i <= n:
shift = np.roll(p[:i],-1)
for k in range(len(shift)):
p[k] = shift[k]
if c[i-1] < i:
c[i-1] += 1
i = 2
print(p, c, i )
listp.append(p)
else:
c[i-1] = 1
i += 1
more information about the algorithm: https://en.wikipedia.org/wiki/Tompkins%E2%80%93Paige_algorithm
Thanks in advance :)

How to fix my code for Traverse Diagonal Matrix (alterate version)?

I have the following code which is used to print diagonal matrix alternatively. Please look at my code and tell me what is wrong with it ?
class Solution:
def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]:
# No. of rows
m=len(matrix)
# No. of columns
n=len(matrix[0])
# Counter to alternatively switch the direction.
up=True
# list to store the result
l=[]
i,j,count=0,0,0
# loop for traversing all the elements.
while(count<=m*n):
if(up):
while(i>=0 and j<n):
l.append(matrix[i][j])
count+=1
i-=1
j+=1
if(i<0 and j<n):
i=0
if(j==n):
i=i+2
j=j-1
else:
while(j>=0 and i<m):
l.append(matrix[i][j])
count+=1
j-=1
i+=1
if(j<0 and i<m):
j=0
if(i==m):
j=j+2
i=i-1
up= not up
print(l)
Input :
[
[1,2,3],
[4,5,6],
[7,8,9]
]
Expected Answer :
[1,2,4,7,5,3,6,8,9]
Actual Answer :
Line 22: IndexError: list index out of range
Your code works, just change your while (count <= m * n) to while (count < m * n).
def findDiagonalOrder(matrix):
# No. of rows
m = len(matrix)
# No. of columns
n = len(matrix[0])
# Counter to alternatively switch the direction.
up = True
# list to store the result
l = []
i, j, count = 0, 0, 0
# loop for traversing all the elements.
while (count < m * n):
if (up):
while (i >= 0 and j < n):
l.append(matrix[i][j])
count += 1
i -= 1
j += 1
if (i < 0 and j < n):
i = 0
if (j == n):
i = i + 2
j = j - 1
else:
while (j >= 0 and i < m):
l.append(matrix[i][j])
count += 1
j -= 1
i += 1
if (j < 0 and i < m):
j = 0
if (i == m):
j = j + 2
i = i - 1
up = not up
print(l)
matrix = [
[1,2,3],
[4,5,6],
[7,8,9]
]
findDiagonalOrder(matrix)
#[1, 2, 4, 7, 5, 3, 6, 8, 9]

Merge sort implementation in python giving incorrect result

I am trying to implement the merge sort algorithm described in these notes by Jeff Erickson on page 3. but even though the algorithm is correct and my implementation seems correct, I am getting the input list as output without any change. Can someone point out the anomalies, if any, in it.
def merge(appnd_lst, m):
result = []
n = len(appnd_lst)
i, j = 0, m
for k in range(0, n):
if j < n:
result.append(appnd_lst[i])
i += 1
elif i > m:
result.append(appnd_lst[j])
j += 1
elif appnd_lst[i] < appnd_lst[j]:
result.append(appnd_lst[i])
i += 1
else:
result.append(appnd_lst[j])
j += 1
return result
def mergesort(lst):
n = len(lst)
if n > 1:
m = int(n / 2)
left = mergesort(lst[:m])
right = mergesort(lst[m:])
appnd_lst = left
appnd_lst.extend(right)
return merge(appnd_lst, m)
else:
return lst
if __name__ == "__main__":
print mergesort([3, 4, 8, 0, 6, 7, 4, 2, 1, 9, 4, 5])
There are three errors in your merge function a couple of indexing errors and using the wrong comparison operator. Remember python list indices go from 0 .. len(list)-1.
* ...
6 if j > n-1: # operator wrong and off by 1
* ...
9 elif i > m-1: # off by 1
* ...

Categories