What is the form of the for loop code below, if it is converted into a while loop?
This is the code:
def number(a):
b = 1
for c in range(1, a+1):
b*=c
return(b)
a = 6
for d in range(a, 0, -1):
print(number(d),end=' ')
for e in range(d, 0, -1):
print(e, end = ' ')
print('')
This is the output:
720 6 5 4 3 2 1
120 5 4 3 2 1
24 4 3 2 1
6 3 2 1
2 2 1
1 1
a = 6
for d in range(a, 0, -1):
Here it is saying that a=6, and the for loop is running from 6 till 0, and stepping by -1 each time. To turn it into a while loop you can just do:
a = 6
while a > 0:
a -= 1
# and so on
Assuming your question is that you wanted to turn the for loop into a while loop, that is how I would do it, but I don't see the point unless there is something very specific you need to do.
How about this line?
After the for line:
a = 6
for d in range(a, 0, -1):
I still don't understand changing the for loop for the code below, because this line has a for d in it as a prefix:
def number(a):
b = 1
c = 1
while c <= a:
c += 1
b*=c
return(b)
a = 6
while a > 0:
a -= 1
print(number(a))
#for e in range(d, 0, -1):
# print(e, end = ' ')
#print('')
The code that I have hashed, is still my problem, what is the solution in that line of code
The output of the program code has just arrived as below:
720
120
24
6
2
1
Related
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()
t = int(input())
for _ in range(t):
a, b = 0, 1
n = int(input())
count = 0
if n == 1:
print(b)
while count < n:
print(b, end = " ")
a, b = b, a+b
count += 1
When I run this code,the answer of of all the inputs gets printed in the same line.
Input:
2
7
5
my output is:
1 1 2 3 5 8 13 1 1 2 3 5
The expected output is:
1 1 2 3 5 8 13
1 1 2 3 5
I want the output in the exact format as I am failing multiple test cases.
This program prints first n numbers of fibonacci series.
Just add an print() after the while loop as suggested by #Tomerikoo.
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 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))
for i in range(7,0,-1):
for i in range(i-1):
print i,
print
The above code prints:
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0
I want to get rid of the zeroes in the first 4 lines.
Start your range at 1 instead of the default 0:
for i in range(1, i-1):
print i,
Instead of ranging to i - 1, adjust your outer loop to produce the right values but increase the end-point; there is little point in producing a range from 1 to 1 or from 1 to 0.
for i in range(6, 1, -1):
for i in range(1, i):
print i,
print
This saves you from printing too many empty newlines at the end:
>>> for i in range(6, 1, -1):
... for i in range(1, i):
... print i,
... print
...
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
You could also use print i + 1 instead of starting at 1; decrement the values in the outer loop in that case:
for i in range(5, 0, -1):
for i in range(i):
print i + 1,
print