Logical Practice Questions - ( DS140823 ) - 10-10-2023

  • Write a function takes a two-word string and returns True if both words begin with same letter

  • Write a function for the given problem: Given two integers, return True if the sum of the integers is 20 or if one of the integers is 20. If not, return False

  • Write a function that capitalizes the first and fourth letters of a name

  • Write a function in the given problem : Given an integer n, return True if n is within 10 of either 100 or 200

  • Write a function takes a two-word string and returns True if both words begin with same letter
def good_name(str1,str2):
    if str1[0].lower() == str2[0].lower():
        print("True")
    else:
        print("False")

name1=input("Enter a name")
name2=input("Enter a name")
good_name(name1,name2)
  • Write a function for the given problem: Given two integers, return True if the sum of the integers is 20 or if one of the integers is 20. If not, return False
def Fav_20(num1,num2):
    if (num1 == 20 or num2 ==20) or num1+num2 == 20:
        print("True")
    else:
        print("False")

num1=int(input("Enter a number "))
num2=int(input("Enter a number "))
Fav_20(num1,num2)
  • Write a function that capitalizes the first and fourth letters of a name
def capitalization(name):
    str=""
    for i in range(len(name)):
        if i==0 or i==3:
            str+=name[i].upper()
        else:
            str+=name[i]
    print(str)

name=input("Enter a name")
capitalization(name)
  • Write a function in the given problem : Given an integer n, return True if n is within 10 of either 100 or 200
def near_num(num1):
    if 90<n<110 or 190<n<210:
        return True
    else:
        return False
    
num1 = int(input("Enter a number: "))
near_num(num1)
# 1st Write a function takes a two-word string and returns True if both words begin with same letter.
def tow_words (str1,str2):
    if str1[0].lower()==str2[0].lower():
        return True
    else:
        return False
str1=input("Enter First String :")  
str2=input("Enter Second String :")  
res=tow_words(str1,str2)
print(res)


# 2nd Write a function for the given problem: Given two integers, return True if the sum of the integers is 20 or if one of the integers is 20. If not, return False

def integers(int1,int2):
   if int1==20 or int2==20 or int1+int2==20:
      return True
   else:
      return False

int1 = int(input("Enter First integer :"))
int2 = int(input("Enter second integer :"))
res = integers(int1,int2)
print(res)



# 3rd Write a function that capitalizes the first and fourth letters of a name

def letters(name):
    if len(name) > 0:
        print()
    return name[0].capitalize()+name[1:3]+name[3:4].capitalize()+name[4:]
name=input("Enter Name :")
res = letters(name)
print(res)

# 4th Write a function in the given problem : Given an integer n, return True if n is within 10 of either 100 or 200

def near_hundred(n):
    return (abs(n - 100) <= 10) or (abs(n - 200) <= 10)
n=int(input("Enter Number :"))
res = near_hundred(n)
print(res)
1.#Write a function takes a two-word string and returns True if both words begin with same letter
def check_letter(string1,string2):
        word1 = string1.split()
        word2=string2.split()
        if word1[0] == word2[0]:
            return True
        else:
            return False
string1=input("enter string1:")
string2=input("enter string2:")
check_letter(string1,string2)
output:
enter string1:hello how are you
enter string2:hello fine an you
True

2.#Write a function for the given problem: Given two integers, return True if the sum of the integers is 20 or if one of the integers is 20. If not, return False
def integer_same(num1,num2):
    sum=num1+num2
    if sum==20 or (num1,num2)==20:
       print("True")
    else:
      print("False")
num1=int(input("enter num1:"))
num2=int(input("enter num2:"))
integer_same(num1,num2)
o/p:enter num1:12
enter num2:8
True
3.#Write a function that capitalizes the first and fourth letters of a name
s = "welcome to python"
print("String before:", s)
a = s.split()
res = []
for i in a:
    x = i[0].upper()+i[1:3]+i[3:4].upper()+i[4:]
    res.append(x)
res = " ".join(res)
print("String after:", res)
o/p:String before: welcome to python
String after: WelCome To PytHon
4.#Write a function in the given problem : Given an integer n, return True if n is within 10 of either 100 or 200
def almost_there(n):
    if ((abs(100 - n) <= 10) or (abs(200 - n) <= 10)):
       print("true")
    else:
      print("false")
n=int(input("enter number"))
almost_there(n)
output: enter number110
true
# Write a function takes a two-word string and returns True if both words begin with same letter

def same_letter(str1,str2):
    if str1[0].lower()==str2[0].lower():
        return True
    else:
        return False
    
str1=input("Enter First String : ")  
str2=input("Enter Second String : ")  
res=same_letter(str1,str2)
print(res)


# Write a function for the given problem: Given two integers, return True if the sum of the integers is 20 or if one of the integers is 20. 
# If not, return False


def sum_20(int1,int2):
    if int1 == 0 or int2 == 0 or int1+int2 == 20:
        return True
    else:
        return False
    
int1 = int(input("Enter First Integer: "))
int2 = int(input("Enter Second Integer: "))

print("Result =",sum_20(int1,int2))


# Write a function that capitalizes the first and fourth letters of a name

def cap(name):
    new = ""
    for i in range(len(name)):
        if i==0 or i==3:
            new += name[i].upper()
        else:
            new += name[i]
    return new
name = input("Enter Name: ")
print("Updated Name: ",cap(name))


# Write a function in the given problem : Given an integer n, return True if n is within 10 of either 100 or 200

def near_100_200(n):
    if 89<n<111 or 189<n<211:
        return True
    else:
        return False
    
n = int(input("Enter a number: "))
print("Result: ",near_100_200(n))



     ```
# Write a function takes a two-word string and returns True if both words begin with same letter

def same_letter(str1,str2):
    if str1[0].lower()==str2[0].lower():
        return True
    else:
        return False
    
str1=input("Enter First String : ")  
str2=input("Enter Second String : ")  
res=same_letter(str1,str2)
print(res)


# Write a function for the given problem: Given two integers, return True if the sum of the integers is 20 or if one of the integers is 20. 
# If not, return False


def sum_20(int1,int2):
    if int1 == 0 or int2 == 0 or int1+int2 == 20:
        return True
    else:
        return False
    
int1 = int(input("Enter First Integer: "))
int2 = int(input("Enter Second Integer: "))

print("Result =",sum_20(int1,int2))


# Write a function that capitalizes the first and fourth letters of a name

def cap(name):
    new = ""
    for i in range(len(name)):
        if i==0 or i==3:
            new += name[i].upper()
        else:
            new += name[i]
    return new
name = input("Enter Name: ")
print("Updated Name: ",cap(name))


# Write a function in the given problem : Given an integer n, return True if n is within 10 of either 100 or 200

def near_100_200(n):
    if 89<n<111 or 189<n<211:
        return True
    else:
        return False
    
n = int(input("Enter a number: "))
print("Result: ",near_100_200(n))



     ```