delete first/last occurrence in list python - python

I need to delete all first occurrence in list.
time limit = 2 s.
memory limit = 256 mb.
Given list a. Some elements in a repeated. If len(a) = 1 print 0.
Input: 1 1 5 2 4 3 3 4 2 5.
Output: 1 3 4 2 5
My solution: For 48 test 22 - good. Else - limited time. How it solve without .count
n = int(input())
A = list(map(int, input().split()))
C = []
if len(A) == 1:
print(0)
else:
for j in range(n):
if A[:j].count(A[j]):
C.append(A[j])
print(len(C))
print(*C)
Pls help

Another way than using sets
First
n = input().split(" ")
u = []
if(len(n) == 1): print(0, end="")
else:
d = {}
for a in n:
if a in d: u.append(int(a))
else: d[a] = 1
print(*u, end="")
# input: 1 1 5 2 4 3 3 4 2 5
# output: 1 3 4 2 5
# input: 5
# output: 0
# input: 1 1 1 1
# output: 1 1 1
Last
n = input().split(" ")
u = []
if(len(n) == 1): print(0, end="")
else:
n.reverse()
d = {}
for a in n:
if a in d: u.append(int(a))
else: d[a] = 1
u.reverse()
print(*u, end="")
# # input: 1 1 5 2 4 3 3 4 2 5
# # output: 1 5 2 4 3
# # input: 5
# # output: 0
# # input: 1 1 1 1
# # output: 1 1 1

To remove duplicates use set()
_ = input()
A = set(map(int, input().split()))
print(*list(A))

Related

How to receive a num at each step and continue until zero is entered; then this program should print each entered num as its own num

Here I have this code:
N = int(input())
Tmp=n
While tmp>0 :
Print(n)
Tmp-=1
But for ex: when I have:
3
2
1
0
As entered nums, it just prints:
3
3
3
But I need to print:
3
3
3
2
2
1
Here I have this code:
N = int(input())
Tmp=n
While tmp>0 :
Print(n)
Tmp-=1
But there is a problem!bc it just prints
3
3
3
Instead of:
3
3
3
2
2
1
you need to print(tmp) instead of print(n)
this will print 3 2 1.
To get 3 3 3 2 2 1 you need to change your code more:
n = int(input())
tmp=n
while tmp > 0:
for _ in range(tmp)
print(tmp)
tmp -= 1
You need to add if statement when tmp equals 0 then subtract n by 1 and assign tmp back to n:
n = int(input('input : '))
tmp = n
while tmp > 0:
print(n)
tmp -= 1
if tmp == 0:
n -= 1
tmp = n
Output:
# input : 3
# 3
# 3
# 3
# 2
# 2
# 1

How to reflect a number output in Python?

I have a programm which draws a picture from numbers in certain way
n = int(input(" Введіть ваше число "))
m = n * 2 - 1
pp = " "
i = 0
while m != 0:
l = []
while m > n:
while i < n:
i += 1
j = n - i
k = i
while j != 0:
l.append(pp)
j -= 1
while k != 0:
l.append(str(k))
k -= 1
m -= 1
a = " "
print(a.join(l))
l = []
i = 0
OUTPUT:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
But now I get a task to draw this picture
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Is there any hint how to reflect it without overwriting the whole code?
For the output you are expecting, a very simple way to do it is with this code :
n = 5
for i in range(n):
print(' '.join([str(x+1) for x in range(i+1)]))
Output :
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
You can try this. Its simple.
for i in range(1,n+1):
for j in range(1, i+1):
print(j, end="")
print()

how to \n in certain places in a list

Trying to opening a new line in different points in a generated list.
ive tried to use this to seperate a list but it doesnt work.
for j in range (num,0,-1):
for i in range(0,len(num),j):
blist[i:j]
print(blist)
heres my code
num=int(input('Size: '))
list=[]
blist=[]
for k in range(num,-1,-1):
for i in range(0,num,1):
list.append(i)
num-=1
print(list)
for j in range (num,0,-1):
for i in range(0,len(num),j):
blist[i:j]
print(blist)
heres the expected result
Size: 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
Size: 3
0 1 2
0 1
0
This works:
n = int(input('Size: '))
L = [str(i) for i in range(n)]
for i in range(n):
print(' '.join(L[:n-i]))
Line by line explanation:
L = [str(i) for i in range(n)] Create a list of string digits from 0 up to n-1
for i in range(n): set i from 0 up to n-1
L[:n-i] slices the list L from start up to n-i (not inclusive)
' '.join(L[:n-i]) just glues together all the elements of the resulting slice with a white space ' '
How about this?
num=int(input('Size: '))
list_=[]
for k in range(num,-1,-1):
list_.append([str(i) for i in range(0,num,1)])
num -= 1
result = '\n'.join([' '.join(l) for l in list_])
print(result)
The result takes each list in list_ and joins them by a space, then the next join separates them by newlines.
Below code can work for me.
num=int(input('Size: '))
list_data = list(range(num))
for i in reversed(range(1, num +1 )):
print( list_data[0:i])
You can unpack the output of range as arguments to print instead:
num = int(input('Size: '))
for n in range(num):
print(*range(num - n))
Sample input/output:
Size: 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0

How to use while in python?

I should only use while and print to complete the homework. I have tried a different way to deal with that but still stuck.
Expected output:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
what I got instead:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
Here is my code:
j = 1
i = 1
t = 6
x = 10
d = 1
while i <= 6:
n = 1
space = -3
while space <= j:
print(" " * x, end="")
space += 1
break
while n <= i:
print('%d '%n, end="")
n += 1
print("")
i += 1
x -= 2
x = [i for i in range(1, 7)]
n = len(x)
j =1
while j <= n:
print(' '*(n-j), end="")
print(*x[0:j][::-1])
j +=1
Output
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
You are almost there. Just count backwards; i.e. change the following line
n = 1
to
n = i
and
while n <= i:
print('%d '%n, end="")
n += 1
to
while n > 0:
print('%d '%n, end="")
n -= 1
Also try the one-liner solution for fun:
>>> print("\n".join([" " * (7 - i) * 2 + " ".join([str(x) for x in reversed(range(1, i))]) for i in range(2, 8)]))
you have to print reverse order from your current one :
n =6
i = 1
tCol = n*2 -1
while i <=n:
cCount = i*2
spaceCount = tCol - cCount +1
s=1
while s<=spaceCount:
print(" ",end="")
s+=1
t =i
while t>=1:
print(t, end="")
if(t!=1):
print(" ", end="")
t-=1
print()
i+=1
output:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
you can change the value of n to get upto any number

Conway sequence in python

I have an assignement for which I have to write an algorithm who generates the l lines of the Conway sequence of a chosen r integer.
Example for l=8 and r=1 :
1
1 1
2 1
1 2 1 1
1 1 1 2 2 1
3 1 2 2 1 1
1 3 1 1 2 2 2 1
1 1 1 3 2 1 3 2 1 1
I have tried to write an algorithm in Python3, but I can't manage to get the correct output. I'd appreciate if you could help me to figure out what's wrong... Anyway, here's what I've wrote so far :
r = int(input())
l = int(input())
cur=str(r).strip()
print(r)
for j in range(l-1):
r=cur
cur=""
i=0
while i<len(r) :
c=1 #counts the consecutive same "numbers"
while ((i+c*2)<len(r)) and (r[i]==r[i+c*2]):
c+=1
cur+=str(c)+" "+r[i]+" "
i+=c+1
cur=cur.strip()
print(cur)
And that's the output I'm getting for l=8 and r=1 :
1
1 1
2 1
1 2 1 1
1 1 1 2 2 1
3 1 1 1 2 2 1
1 3 3 1 1 1 2 2 1
1 1 2 3 6 2 2 1
I also feel that my code is pretty ugly so feel free to give your remarks
Really, groupby is built for this:
from itertools import groupby
def lookandsay(i):
digits = str(i) #convert to string
newdigits = []
for k, g in groupby(digits):
newdigits += [sum(1 for _ in g), k]
return int("".join(map(str, newdigits)))
def conway(i):
yield i
while True:
i = lookandsay(i)
yield i
cw01 = conway(1)
for _ in range(8):
print(" ".join(str(next(cw01))))
Look at this logic,
I got below code from: HERE
r = [input()]
l = int(input())
# Write an answer using print
# To debug: print("Debug messages...", file=sys.stderr, flush=True)
for i in range(l - 1):
temp = list()
lastChar = r[0]
counter = 0
for char in r:
if lastChar == char:
counter += 1
else:
temp.extend([str(counter), lastChar])
lastChar = char
counter = 1
temp.extend([str(counter), lastChar])
r = temp
print(r)
print(" ".join(r))

Categories