Given a list of n integers, count the number of integers in the list that are either a multiple of 3 or a multiple of 5. (All the numbers are guaranteed to be distinct).
Input Format:
Single line of input contains a list of space separated integers
Output Format:
Print the count of numbers that are divisible either by 3 or 5
Example:
Input:
1 3 5 6 7 9 11 13 15 18 20 21
Output:
8
My Code:
x=input()
a=list(map(float, input().strip().split()))[:x]
c=0
for i in range(1,x+1):
if ((i%3==0) & (i%5==0)):
c=c+1
print(c, end="")
output after running my code:
Looking at your code, you don't have to use 2x input() (one time is sufficient). Also, don't convert the numbers to float, the int is sufficient:
# your input, you can substitute for `s = input()` later:
s = "1 3 5 6 7 9 11 13 15 18 20 21"
# convert numbers to integer:
numbers = [int(n) for n in s.split()]
print(sum(n % 3 == 0 or n % 5 == 0 for n in numbers))
Prints:
8
NOTE:
n % 3 == 0 or n % 5 == 0 will give us True or False. We can use sum() here to sum True values together (True is equal to 1).
Check this out.
a=list(map(float, input().split()))
c=0
for i in a:
if (i%3==0) or (i%5==0):
c=c+1
print(c, end="")
I have a code that generates triangular numbers, calculates the divisors for each triangular number and prints out 3 columns where the first column is the input numbers, the second column is the triangular numbers and the third column is the divisors of each triangular number. How can I count the number of divisors of each triangular number(count the numbers in the third column)?
for num in range(10):
triangle_number = num*(num+1)//2
print(num, end = ' ')
print(triangle_number, end = ' ')
for divisor in range(1, triangle_number+1):
if triangle_number%divisor == 0:
print(divisor, end = ',')
print()
Output looks like this:
0 0
1 1 1,
2 3 1,3,
3 6 1,2,3,6,
4 10 1,2,5,10,
5 15 1,3,5,15,
6 21 1,3,7,21,
7 28 1,2,4,7,14,28,
8 36 1,2,3,4,6,9,12,18,36,
9 45 1,3,5,9,15,45,
You can add a variable inside for loop and increment it when you get the divisor and print a the end.
for num in range(10):
count = 0
triangle_number = num*(num+1)//2
print(num, end = '\t')
print(triangle_number, end = '\t')
for divisor in range(1, triangle_number+1):
if triangle_number%divisor == 0:
print(divisor, end = ',')
count += 1
print("\t", count)
##Output:
##0 0 0
##1 1 1, 1
##2 3 1,3, 2
##3 6 1,2,3,6, 4
##4 10 1,2,5,10, 4
##5 15 1,3,5,15, 4
##6 21 1,3,7,21, 4
##7 28 1,2,4,7,14,28, 6
##8 36 1,2,3,4,6,9,12,18,36, 9
##9 45 1,3,5,9,15,45, 6
##Output order : input number, count of divisors divisors, triangular number
or you can create a list of divisors and measure length before printing.
for num in range(10):
divisors = []
triangle_number = num*(num+1)//2
print(num, end = '\t')
print(triangle_number, end = '\t')
for divisor in range(1, triangle_number+1):
if triangle_number%divisor == 0:
divisors.append(divisor)
print(len(divisors), "\t", ",".join(str(div )for div in divisors))
##Output:
##0 0 0
##1 1 1 1
##2 3 2 1,3
##3 6 4 1,2,3,6
##4 10 4 1,2,5,10
##5 15 4 1,3,5,15
##6 21 4 1,3,7,21
##7 28 6 1,2,4,7,14,28
##8 36 9 1,2,3,4,6,9,12,18,36
##9 45 6 1,3,5,9,15,45
##Output order : input number, triangular number, count of divisors divisors
You might replace your inner for loop using list comprehension and then just get length of that list using len function, that is replace:
for divisor in range(1, triangle_number+1):
if triangle_number%divisor == 0:
print(divisor, end = ',')
with:
divisors = [divisor for divisor in range(1, triangle_number+1) if triangle_number%divisor == 0]
print(*divisors, sep=',', end=' ')
print(len(divisors), end='')
list comprehension is succint way of creating list which might be created via appending inside for loop, * (star operator) does unpacking, if you would have list x = [1, 2, 3] then print(*x) is equivalent of print(1, 2, 3) and so on.
this is my code right now:
loop_count = 1
for i in range(mystery_int):
for x in range(1,mystery_int):
print(x*loop_count, end=" ")
print (loop_count)
loop_count+=1
this is what it is supposed to print:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
But it prints:
1 2 3 4 1
2 4 6 8 2
3 6 9 12 3
4 8 12 16 4
5 10 15 20 5
You need to range till mystery_int + 1 because in range, second argument is exclusive. So, for example, range(1,6) gives numbers from 1 to 5.
Also, I added an empty print() which basically adds a newline to match with desired output.
Using end='\t' further aligns output properly.
loop_count = 1
mystery_int = 5
for i in range(mystery_int):
for x in range(1, mystery_int + 1):
print(x * loop_count, end='\t')
print()
loop_count += 1
the range for x should be range(1,mystery_int+1), and you also incorrectly print loop_count at the end of each line (which I replaced with the empty string, just to produce a newline).
loop_count = 1
for i in range(mystery_int):
for x in range(1,mystery_int+1):
print(x*loop_count, end=" ")
print('')
loop_count+=1
Note that the loop_count variable is not really needed. You could write the program as:
for i in range(1,mystery_int+1):
for x in range(1,mystery_int+1):
print(x*i, end=" ")
print('')
or even better as:
for i in range(1,mystery_int+1):
print(*[x*i for x in range(1,mystery_int+1)], sep=" ")
you are running on two for loops in addition to using another counter, i would recommend sticking only to the loops:
for i in range(1,mystery_int+1):
for x in range(1,mystery_int+1):
print(i*x, end=" ")
print("") # new line
I want to print the following sequence of integers in a pyramid (odd rows sorted ascending, even rows sorted descending). If S=4, it must print four rows and so on.
Expected output:
1
3 2
4 5 6
10 9 8 7
I tried out the following code but it produced the wrong output.
S=int(input())
for i in range(1,S+1):
y=i+(i-1)
if i%2!=0:
print(*range(i,y+1))
elif i%2==0:
print(*range(y,i-1,-1))
# Output:
# 1
# 3 2
# 3 4 5
# 7 6 5 4
You need some way of either keeping track of where you are in the sequence when printing each row, generating the entire sequence and then chunking it into rows, or... (the list of possible approaches goes on and on).
Below is a fairly simple approach that just keeps track of a range start value, calculates the range stop value based on the row number, and reverses even rows.
rows = int(input())
start = 1
for n in range(1, rows + 1):
stop = int((n * (n + 1)) / 2) + 1
row = range(start, stop) if n % 2 else reversed(range(start, stop))
start = stop
print(*row)
# If rows input is 4, then output:
# 1
# 3 2
# 4 5 6
# 10 9 8 7
Using itertools.count and just reversing the sublist before printing on even rows
from itertools import count
s = 4
l = count(1)
for i in range(1, s+1):
temp = []
for j in range(i):
temp.append(next(l))
if i % 2:
print(' '.join(map(str, temp)))
else:
print(' '.join(map(str, temp[::-1])))
1
3 2
4 5 6
10 9 8 7
I am learning python and I am really struggling to figure out how to write this code where I get an input a that is bigger than 1 and the output should look like this:
Sum from 1 to a
Sum from 2 to a
Sum from 3 to a
.....
a
E.g. for 5, the output should be:
15
14
12
9
5
This is what I have so far
a=int(input())
for t in range(a):
b=a*(a+1)/2
b=b-t
print(a+t)
I cant seem to figure out how to subtract it from reverse and how to print each results in the process
The following will work:
a = int(input())
# s = sum(range(1, a+1))
s = a * (a+1) // 2
for t in range(1, a+1):
print(s)
s -= t
Produces for a = 5:
15
14
12
9
5
Instead of only subtracting the counter t, you need to subtract the sum of 1 ... t.
Otherwise your code does not need to be changed, I just added the forcing to int.
a = int(input())
for t in range(a):
b=a*(a+1)//2
c=t*(t+1)//2
b=b-c
print(b)
Output:
15
14
12
9
5