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()
Related
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
I am trying to experiment how the while loop works.
docs = ['123867', '256789', '3aa', '4gg', '5yy', '6abc']
for i in range(0,len(docs)):
for j in range(i,len(docs[i])):
print(i, j)
My output for the above code is
0 0
0 1
0 2
0 3
0 4
0 5
1 1
1 2
1 3
1 4
1 5
2 2
I attempt to play with the while loop with
docs = ['123867', '256789', '3aa', '4gg', '5yy', '6abc']
i = 0
j = i
while i < len(docs):
while j < len(docs[i]):
print(i, j)
j += 1
i += 1
but the output is
0 0
0 1
0 2
0 3
0 4
0 5
How can I fix my while loop to match the for loop? Thanks!
docs = ['123867', '256789', '3aa', '4gg', '5yy', '6abc']
i = 0
while i < len(docs):
j = i # should be moved here
while j < len(docs[i]):
print(i, j)
j += 1
i += 1
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))
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
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))