Python
1. Write a program which will find all such numbers which are divisible by 3 but are not a multiple of 7,between 2000 and 3200 (both included).
soln :
def filter_numbers():
"""
function to filter out numbers by extracting numbers
which is divisible by 3 but not multiple of 7.
"""
filtered_list=[]
for i in range(2000, 3201):
if (i%3==0) and (i%7!=0):
filtered_list.append(str(i))
print(','.join(filtered_list))
2. Write a program which can compute the factorial of a given numbers.
soln :
def fact(x):
"""
function to get factorial of given number
"""
if x == 0:
return 1
return x * fact(x - 1)
x=int(raw_input())
print fact(x)
3. Write a program, which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number.
soln :
def even_digit_finder():
"""
function is to filter out numbers in
which each digit is even number.
"""
even_digit_in_number = []
for i in range(1000, 3001):
s = str(i)
if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0) and (int(s[3])%2==0):
even_digit_in_number.append(s)
print(",".join(even_digit_in_number))
4. Write a program that accepts a sentence and calculate the number of letters and digits.
soln :
def digit_character_counter():
"""
"""
sentence = input("enter sentence")
counter={"DIGITS":0, "LETTERS":0}
for character in sent:
if character.isdigit():
counter["DIGITS"]+=1
elif character.isalpha():
counter["LETTERS"]+=1
else:
pass
print("Total number of letters is {}".format(counter["LETTERS"]))
print("Total number of letters is {}".format(counter["DIGITS"]))
Comments
Post a Comment