How to print output for different test cases in different lines? - python

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.

Related

Unable to print a range of results with a space between every 3 results

How can I print a range of results with a space between every three results? 
When I print something like the following:
for i in range(1, 11):
print(i)
Output I get:
1
2
3
4
5
6
7
8
9
10
Expected output:
1
2
3
4
5
6
7
8
9
10
for i in range(1, 11):
print(i)
if i % 3 == 0:
print('')
Should do it!
Basically, it just checks that i is modulo 3. If it is, it will just print an empty line.
Example with a more complex loop
In the case that you have a loop that goes from a to b and that you want to print an empty line every c print, you can do:
for i in range(a, b):
print(i)
if (i - a + 1) % c == 0:
print("")

Function that takes the limit as an argument of the Fibonacci series and prints till that limit in Python3

I've to write a function that takes the limit as an argument of the Fibonacci series and prints till that limit. Here, if the limit is fibonacci(10) then the output should be 0 1 1 2 3 5 8
I tried:
def fibonacci(number):
a = 0
b = 1
print(a,end = ' ')
print(b, end = ' ')
for num in range( 2 , number):
c = a + b
a = b
b = c
print(c, end = " ")
fibonacci(10)
The output came:
0 1 1 2 3 5 8 13 21 34
What should I do?
You can simply just add a check to see if the number resulting from a+b is going to be larger than the limit.
for num in range( 2 , number):
if a+b > number: return
c = a + b
a = b
b = c
print(c, end = " ")
Output:
0 1 1 2 3 5 8
The break statement exits the for loop so that no numbers over the limit will be printed. You can also use return instead of break. However, if you end up adding logic after the for loop, it will not be reached because return will exit the entire function.
SUGGESTION (courtesy of #OneCricketeer)
You may also use a while loop here instead of a range. This achieves the same output with cleaner, easier to understand code.
while (a+b < number):
c = a + b
a = b
b = c
print(c, end = " ")
def fibonacci(limit):
a=0
b=1
print(a,end=" ")
print(b,end=" ")
for i in range(limit):
c=a+b
if c<=limit:
print(c,end=" ")
a=b
b=c
x=int(input())
fibonacci(x)
Input:
10
Output:
0 1 1 2 3 5 8

delete first/last occurrence in list 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))

Splitting a string of integers with spaces without converting to a list in python

Basically, I need a code that takes an integer and then prints out the string of numbers with the certain range.
For example:
n = 11
1 2 2 3 3 3 4 4 4 4
n = 7
1 2 2 3 3 3 4
a = []
n = int(input())
if n == 0:
print(n)
for i in range(int(n) + 1):
a += [i] * i
a = ' '.join(map(str, a))
print(a[:n])
This does the job but it counts spaces as characters, so I tried to convert it to an int
n = int(n)
print(' '.join(a[:n]))
But when the n >= 47, it starts to print out 10 as 1 0 which is incorrect
I also tried this code
n = int(input())
for i in range(n):
b = (str(i) * i)
print(b, end = ' ')
But I don't understand how to separate the b with spaces without converting the string to a list and printing it in one line either.
I am not sure if it is even possible.
Maybe something like this?
# initialize variables:
number = 11
counter = 0
for i in range(number):
for j in range(i):
# print number:
print('%d ' %i, end='')
# increment counter (tracks how many numbres to print):
counter += 1
# exit inner loop if counter reaches number:
if counter >= number-1: break
# exit outer loop if counter reaches number:
if counter >= number-1: break
Output:
1 2 2 3 3 3 4 4 4 4
Here's a solution using itertools. Generators, chain, repeat and islice are all lazily evaluated, so this uses O(1) space.
>>> n = 7
>>> from itertools import chain, repeat, islice
>>> gen = chain.from_iterable(repeat(i, i) for i in range(1, n+1))
>>> for i in islice(gen, n):
... print(i, end=' ')
...
1 2 2 3 3 3 4
This seems simple. But this does solve the problem?
>>> for i in t:
... if i!=" ": print i
...
1
2
2
3
3
3
4
4
4
4
or even string replace like,
print t.replace(" ","\n")
1
2
2
3
3
3
4
4
4
4

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

Categories