0% found this document useful (0 votes)
41 views

Python Infosys Question

The document contains Python code snippets for various programming exercises including checking if a number is even or odd, finding the maximum of three numbers, checking if a number is prime, generating the Fibonacci series, calculating salary hike based on job level, finding the factorial of a number, checking if a number is a palindrome, checking if two numbers are amicable, performing right shift rotation of a number, finding proper divisors of a number, run length encoding of a string, encrypting a sentence by reversing words in odd positions and rearranging characters in even words, counting characters and words in a string, and finding vowel count in a sentence.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Python Infosys Question

The document contains Python code snippets for various programming exercises including checking if a number is even or odd, finding the maximum of three numbers, checking if a number is prime, generating the Fibonacci series, calculating salary hike based on job level, finding the factorial of a number, checking if a number is a palindrome, checking if two numbers are amicable, performing right shift rotation of a number, finding proper divisors of a number, run length encoding of a string, encrypting a sentence by reversing words in odd positions and rearranging characters in even words, counting characters and words in a string, and finding vowel count in a sentence.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

LAB-5

Check whether the given number is even or odd


def check_even_or_odd(number):
if number % 2 == 0:
print(f"{number} is even.")
else:
print(f"{number} is odd.")
user_input = int(input("Enter a number: "))
check_even_or_odd(user_input)

output:

Write a Python program to find and display the maximum


of three given numbers.

def find_maximum(num1, num2, num3):


max_num = max(num1, num2, num3)
return max_num

num1 = float(input("Enter the first number: "))


num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

maximum = find_maximum(num1, num2, num3)


print("The maximum of the three numbers is:", maximum)

• Check wether the number is prime or not


def is_prime(number):
if number <= 1:
return False
elif number == 2:
return True
elif number % 2 == 0:
return False
else:
for i in range(3, int(number**0.5) + 1, 2):
if number % i == 0:
return False
return True
user_input = int(input("Enter a number: "))
if is_prime(user_input):
print(f"{user_input} is a prime number.")
else:
print(f"{user_input} is not a prime number.")

output:
• Write a program to print this series
1 1 2 3 5 8 13
def fibonacci_series(n):
fib_series = [1, 1]

for i in range(2, n):


next_number = fib_series[i - 1] + fib_series[i - 2]
fib_series.append(next_number)
return fib_series
num_terms = 7
result = fibonacci_series(num_terms)
print("Fibonacci Series:", " ".join(map(str, result)))

output:
An organization has decided to provide salary
hike to its employees based on their job level.
Employees can be in job levels 3 , 4 or 5. Hike
percentage based on job levels are given below:
Job level
Hike Percentage (applicable on current salary)

In case of invalid job level, consider hike


percentage to be 0.
Given the current salary and job level, write a
Python program to find and display the new
salary of an employee.

def calculate_new_salary(current_salary, job_level):


if job_level == 3:
hike_percentage = 15
elif job_level == 4:
hike_percentage = 7
elif job_level == 5:
hike_percentage = 5
else:
hike_percentage = 0

hike_amount = current_salary * (hike_percentage / 100)


new_salary = current_salary + hike_amount
return new_salary

# Example usage:
current_salary = float(input("Enter the current salary: "))
job_level = int(input("Enter the job level (3, 4, or 5): "))
new_salary = calculate_new_salary(current_salary, job_level)
print("The new salary of the employee is:", new_salary)

• Find a factorial of a number


def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
user_input = int(input("Enter a non-negative integer: "))
if user_input < 0:
print("Please enter a non-negative integer.")
else:
result = factorial(user_input)
print(f"The factorial of {user_input} is: {result}")

output :
Write a Python Function is_palindrome(num)
that accepts an integer num as argument and
returns True if the num is palindrome else
returns false. Invoke the function and based on
return value print the output.
def is_palindrome(num):

num_str = str(num)

if num_str == num_str[::-1]:
return True
else:
return False
num = int(input("Enter an integer: "))

if is_palindrome(num):
print(num, "is a palindrome.")
else:
print(num, "is not a palindrome.")
Write a Python function
check_amicable_numbers(num1, num2) that
accepts two numbers num1 and num2 as
arguments and returns True if the given pair of
numbers are amicable numbers else return
false. Invoke the function and based on return
value print the numbers are amicable numbers
or not.

def sum_of_divisors(n):
divisor_sum = 0
for i in range(1, n):
if n % i == 0:
divisor_sum += i
return divisor_sum

def check_amicable_numbers(num1, num2):


if sum_of_divisors(num1) == num2 and sum_of_divisors(num2) ==
num1:
return True
else:
return False
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

if check_amicable_numbers(num1, num2):
print(num1, "and", num2, "are amicable numbers.")
else:
print(num1, "and", num2, "are not amicable numbers.")

Write a Python function right_shift(num, n) that


takes two numbers num and n as arguments
and returns value of the integer num rotated to
the right by n positions. Assume both the
numbers are unsigned. Invoke the function and
print the return value.

def right_shift(num, n):

rotated_num = (num >> n) | (num << (32 - n)) & 0xFFFFFFFF


return rotated_num

num = int(input("Enter an unsigned integer: "))


n = int(input("Enter the number of positions to right shift: "))

rotated_value = right_shift(num, n)
print("The value after right shift rotation by", n, "positions is:",
rotated_value)
Write a Python function proper_divisors(num)
which returns a list of all the proper divisors of
given number.
def proper_divisors(num):
divisors = []
for i in range(1, num):
if num % i == 0:
divisors.append(i)
return divisors
num = int(input("Enter a number to find its proper divisors: "))

divisors_list = proper_divisors(num)
print("Proper divisors of", num, "are:", divisors_list)

Given a string containing uppercase characters


(A-Z), compress the string using Run Length
encoding. Repetition of character has to be
replaced by storing the length of that run.

Write a python function encode(message) which


performs the run length encoding for a given
String and returns the run length encoded
String.
def encode(message):
if not message:
return ""

encoded_message = ""
count = 1
prev_char = message[0]

for char in message[1:]:


if char == prev_char:
count += 1
else:
encoded_message += str(count) + prev_char
count = 1
prev_char = char
encoded_message += str(count) + prev_char
return encoded_message
message = "AAABBBCCCCDDDD"
encoded_message = encode(message)
print("Original message:", message)
print("Encoded message:", encoded_message)
Write a python function, encrypt_sentence(msg)
which accepts a message and encrypts it based
on rules given below and returns the encrypted
message.
Words at odd position -> Reverse It
Words at even position -> Rearrange the
characters so that all consonants appear before
the vowels and their order should not change
def encrypt_sentence(msg):
def reverse_string(word):
return word[::-1]
def rearrange_consonants_vowels(word):
vowels = 'aeiouAEIOU'
consonants = ''
vowels_and_consonants = ''
for char in word:
if char in vowels:
vowels_and_consonants += char
else:
consonants += char
return consonants + vowels_and_consonants
words = msg.split()
encrypted_words = []
for i, word in enumerate(words):
if i % 2 == 0:
encrypted_words.append(rearrange_consonants_vowels(word))
else:
encrypted_words.append(reverse_string(word))
return ' '.join(encrypted_words)
message = "Hello World This is Python"
encrypted_message = encrypt_sentence(message)
print("Original message:", message)
print("Encrypted message:", encrypted_message)

Write a Python program to find the number of


characters present the given string.
def count_characters(string):
return len(string)
input_string = input("Enter a string: ")
character_count = count_characters(input_string)
print("Number of characters in the string:", character_count)
Write a Python program to find the numbers of
words present in the given sentence.
def count_words(sentence):
words = sentence.split()
return len(words)
input_sentence = input("Enter a sentence: ")
word_count = count_words(input_sentence)
print("Number of words in the sentence:", word_count)

Write a Python function vowel_count(sentence)


to return a dictionary with vowels, consonants,
others as key and respective number of vowels,
consonants, others characters as value.
def vowel_count(sentence):
vowels = 'aeiouAEIOU'
consonants = 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'
counts = {'vowels': 0, 'consonants': 0, 'others': 0}
if char in vowels:
counts['vowels'] += 1
elif char in consonants:
counts['consonants'] += 1
else:
counts['others'] += 1

return counts
input_sentence = input("Enter a sentence: ")
result = vowel_count(input_sentence)
print("Count of vowels, consonants, and other characters:", result)

You might also like