Logical Practice Questions - ( DS140823 ) - 13-09-2023

  1. Write a Python program which iterates the integers from 1 to 50. For multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

  2. Write a program that appends the square of each number to a new list

  3. Check if two lists have at-least one element common

1 Like
1.
def create():
    for i in range(1,51):
        if i%3==0 and i%5==0:
            print("FizzBuzz")
        elif i%3==0:
            print("Fizz")
        elif i%5==0:
            print("Buzz")
create()
2.
def vij():
    size=int(input("enter the size of the list:"))
    l1=[]
    for i in range(size):
        element=int(input("enter elements in  the list:"))
        l1.append(element)
    print(l1)
    splist=[]
    for j in l1:
        num=j**2
        splist.append(num)
    print(splist)
vij()
3.
def common():
    l2=[]
    sizel2=int(input("enter the size of the list of l2:"))
    for i in range(sizel2):
        char=int(input("enter elements in the list:"))
        l2.append(char)
    print(l2)
    l3=[]
    sizel3=int(input("enter the size of the list of l3:"))
    for i in range(sizel3):
        vari=int(input("enter elements in the list:"))
        l3.append(vari)
    print(l3)
    for x in l2:
        for y in l3:
            if x==y:
                print("list have one  common element")
                break
            else:
                print("list have no common elements")
common()
1 Like

Vyshnavi

for num in range(1,51):
  
  if(num%3==0 and num%5==0):
    print("FizzBuzz")
  elif(num%3 == 0):
    print("Fizz")
  elif(num%5 == 0):
    print("Buzz")
def printValues():
	l = list()
	for i in range(1,21):
		l.append(i**2)
	print(l)
		
printValues()
def common_elements(list_1, list_2):
  result = False
  for x in list_1:
    for y in list_2:
      if x == y:
        result = True
    return result
  return result

list_one = [1, 2, 5, 7, 58]
list_two = [87, 58, 0,3,1]
print("Are elements common between list_one and list_two?")
print(common_elements(list_one, list_two))

list_three = [0,5,7,89,34,4]
list_four = [45,78,90]
print("Are elements common between list_three and list_four?")
print(common_elements(list_three, list_four))
1 Like
# 1] Write a Python program which iterates the integers from 1 to 50. For multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”.
#    For numbers which are multiples of both three and five print “FizzBuzz”.

for i in range(0,51):
    if i % 3 == 0 and i % 5 == 0:
        print("Fizzbuzz")
        continue
    elif i % 3 == 0:
        print("Fizz")
        continue
    elif i % 5 == 0:
        print("Buzz")
        continue
    print(i)

# 2] Write a program that appends the square of each number to a new list.

def square(a,b): 
    lst=[] 
    for i in range(a,b+1): 
        lst.append(i*i) 
    return(lst) 
     
print(square(1,5))

# 3] Check if two lists have at-least one element common

size = int(input("Enter the size of the list: "))
lst1= []
for i in range(size):
    elements_ = int(input("Enter :"))
    lst1.append(elements_)
print(lst1)

size = int(input("Enter the size of the list: "))
lst2= []
for i in range(size):
    elements_ = int(input("Enter :"))
    lst2.append(elements_)
print(lst2)

result = False

for i in lst1:
    for j in lst2:
        if i == j:
	        result = True
print(result)
if result:
    print("Lists have at least one common element")
else:
    print("Lists do not have any common element")
1 Like
# 1st question

for i in range(1,51):
    if i%3==0 and i%5==0:
        print("FizzBuzz")
    elif i%5==0:
        print("Buzz")
    elif i%3==0:
        print("Fuzz")

# 2nd question

def squares(list1):
    list2=[]
    for i in range(len(list1)):
        list2.append(list1[i]**2)
    return list2

size=int(input("enter your list size:"))
list1=[]
for i in range(size):
    print("enter ",i," element:",end="")
    list1.append(int(input()))
list2=squares(list1)
print("squares:",list2)


# 3rd question

def inputs(size):
    list1=[]
    for i in range(size):
        print("enter ",i," elemnt:",end="")
        list1.append(int(input()))
    return list1

size1=int(input("enter your 1st list size:"))
list1=inputs(size1)
set1=set(list1)
size2=int(input("enter your 2nd list size:"))
list2=inputs(size2)
set2=set(list2)
common=set1.intersection(set2)
print("common elements: ",common)

1 Like
# 1. Write a Python program which iterates the integers from 1 to 50. For multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

for i in range(1,51):
    if i%5 == 0 and i%3 == 0:
        print(i," FizzBuzz")
    elif i%5 == 0:
        print(i," Buzz")
    elif i%3 == 0:
        print(i," Fizz")
    
# 2. Write a program that appends the square of each number to a new list.


lst = []
size = int(input("Enter the size of the list: "))
for i in range(size):
    element=int(input(f"enter element {i+1} in the list: "))
    lst.append(element)
print("Original list:",lst)

new_list = []
for l in lst: 
    square = l*l
    new_list.append(square)
print("New appended list:",new_list)

# 3. Check if two lists have at-least one element common.
lst1 = []
size1 = int(input("Enter the size of the list 1: "))
for i in range(size1):
    element1=int(input(f"Enter element {i+1} in the list 1: "))
    lst1.append(element1)
print("First list:",lst1)

lst2 = []
size2 = int(input("Enter the size of the list 2: "))
for j in range(size2):
    element2=int(input(f"Enter element {j+1} in the list 2: "))
    lst2.append(element2)
print("Second list:",lst2)

for l1 in lst1:
    for l2 in lst2:
        if l1 == l2:
            result = True

set1 = set(lst1)
set2 = set(lst2)

if result:
    print("Lists have at least one common element!")   
    print("Common elements: ",set1.intersection(set2))
else:
    print("Lists do not have any common element!")
1 Like
1)
for i in range(1,50):
    if i%3==0:
        print("Fizz")
    elif i%5==0:
        print("Buzz")  
    elif i%3==0 and i%5==0:
        print("Fizzbuzz")
    else:
        print(i)  

2)

lst=[]
for i in range(size):
    value=int(input("Enter the value: "))
    lst.append(value)
lst1=[]   
for i in lst:
    squ =i *i
    lst1.append(squ)
print(lst1)

3)

size =int(input("Enter the size of the list: "))
lst1=[]
lst2=[]
for i in range(size):
    value=int(input("Enter the value for lst1: "))
    lst1.append(value)
for j in range(size):
    value=int(input("Enter the value for lst2: "))
    lst2.append(value) 
for i in lst1:
    for j in lst2:
        if i ==j:
            print(f"common number found,-->{i}") 

1 Like
1.
size = int(input("Enter the size for FizzBuzz: "))
value = int(input("Enter the value for FizzBuzz: "))

for num in range(1, size + 1):
    if num % 3 == 0 and num % 5 == 0:
        print("FizzBuzz")
    elif num % 3 == 0:
        print("Fizz")
    elif num % 5 == 0:
        print("Buzz")
    else:
        print(num)

2.
squares = []
for num in range(1, size + 1):
    squares.append(num ** 2)
print("Squares:", squares)


3.
size1 = int(input("Enter the size for the first list: "))
size2 = int(input("Enter the size for the second list: "))
list1 = []
list2 = []

for i in range(size1):
    element = int(input("Enter an element for the first list: "))
    list1.append(element)
for i in range(size2):
    element = int(input("Enter an element for the second list: "))
    list2.append(element)
elements = any(element in list1 for element in list2)
if elements:
    print("The two lists have at least one common element.")
else:
    print("The two lists do not have any common elements.")

Ans 1 -

for i in range(1,50+1):
    if i%3 == 0 and i%5 == 0:
        print("FizzBuzz")
    elif i%3 == 0:
        print("Fizz")
    elif i%5 == 0:
        print("Buzz")

Ans 2 -

size = int(input("Enter the size of list:"))
lst1 = []
for i in range(size):
    elements = int(input("Enter the values"))
    lst1.append(elements)
print(lst1)
size = int(input("Enter the size of list:"))
lst2 = []
for i in range(size):
    elements = int(input("Enter the values"))
    lst2.append(elements)
print(lst1)
result = False
for i in lst1:
    for j in lst2:
        if i == j:
            result = True
print(result)
if result:
        print("List have atleast one common element")
else:
    print("List do not have any common element")

Ans 3 -

lst = []
for i in range(1,21):
	lst.append(i*i)
print(lst)
1 Like

def values () :
    for i in range(1,51):
        if i % 3 == 0 and i % 5 == 0 :
            print(f"fizzbuzz")
        elif i % 3 == 0 :
            print(f"fizz")
        elif i % 5 == 0 :
            print(f"buzz")
        else:
            print(f"{i}")
values()





def update_list ():
    lst = []
    for i in range(1,51):
        lst.append(i*i)
    print(f"{lst}")
update_list()




def check():
    size1  = int(input("Enter the size of the list : "))
    lst1 = []

    
    for i in range(size1):
        elements1 = int(input("Enter the elements1 you need  : "))
        lst1.append(elements1)


    print(f"list 1 is {lst1}")

    lst2 = []
    size2 = int(input("Enter the size of the list : "))

  
    for j in range(size2):
        elements2 = int(input("Enter the elements2 you need  : "))
        lst2.append(elements2)


    print(f"list 2 is {lst2}")
    

    common = False

    for val1 in lst1 :
        for val2 in lst2 :
            if val1 == val2 :
                common = True
                break

    if common :
        print("common")
    else:
        print("Not common")                
            
check()

##Write a Python program which iterates the integers from 1 to 50. For multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

def fizz_buzz(num):
    if num%3==0 and num%5==0:
        return 'FizzBuzz'

    elif num % 3 == 0:
        return 'Fizz'

    elif num % 5==0:
        return 'Buzz'
    else:
        return num
n=int(input("enter number"))
for n in range(1,50):
    print(fizz_buzz(n))
output:-
enter number27 
1 2 Fizz
 4 Buzz Fizz
 7 8 Fizz Buzz 
11 Fizz 
13 14 Fizz Buzz 
16 17 Fizz 
19 Buzz Fizz 
22 23 Fizz Buzz 
26 Fizz 
28 29 FizzBuzz 
31 32 Fizz 
34 Buzz Fizz 
37 38 Fizz Buzz 
41 Fizz 
43 44 FizzBuzz 
46 47
 Fizz 49

#Write a program that appends the square of each number to a new list
List=[1,2,3,4,5]
List2=[]
for i in List:
square=i**2
List2.append(square)
print(List2)

output:-
[1, 4, 9, 16, 25]

#write a program two print common element i two list

def common_element(list1, list2):
    for x in list1:
        for y in list2:
          if x == y:
             result = print("both have common element")
    return result
a = [1, 2, 3, 4, 5]
b = [5, 6, 7, 8, 9]
print(common_element(a, b))
output:-
both have common element
#  Write a Python program which iterates the integers from 1 to 50. For multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”.


for i in range(0,51):
    if i % 3 == 0 and i % 5 == 0:
        print("Fizzbuzz")
        continue
    elif i % 3 == 0:
        print("Fizz")
        continue
    elif i % 5 == 0:
        print("Buzz")
        continue
    print(i)

# Write a program that appends the square of each number to a new list.

def square(a,b): 
    lst=[] 
    for i in range(a,b+1): 
        lst.append(i*i) 
    return(lst) 
     
print(square(1,5))

#  Check if two lists have at-least one element common

lst = []
for i in range(1,21):
	lst.append(i*i)
print(lst)