TechM Codes

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

1.

Multiplication of digits
345
3*4*5 =60

print("Hello world")

a=str(input("Enter a n of number of digit : "))

str_a=str(a)

z=1

for i in str_a:

x=int(i)

z*=x

print(z)

2. Build a program for calculating and returning the sums of absolute differences between adjacent numbers in
arrays of positive integers. This must be calculated from the position determined by the current user. For
example, suppose the total number of elements is 5 and these are the elements: 12364 Then, if we decide to
start from the 3rd position or enter 3 as input, the function will occur from '3', the 3rd number in the array.
Hence, the sum would be a total of (6-3)+(4-6)= 5

Input 2:

1,2,3,6,4,6,3

Output:

10

def calculate_absolute_differences_sum(arr, position):

if position < 1 or position >= len(arr):

return "Invalid position"

total_sum = 0

for i in range(position - 1, len(arr) - 1):

total_sum += abs(arr[i] - arr[i + 1])

return total_sum

# Example usage:
count=int(input("Enter how many values : ")) #count

elements =[]

for i in range(count):

b=int(input())

elements.append(b) #list with values

start_position = #from this position it starts.....process

result = calculate_absolute_differences_sum(elements, start_position)

print(result)

3. A G.P is defined as a sequence of numbers where each element after the first is found by multiplying the
previous one by a fixed, non-zero number called the common ratio.

Example - 1,3,9,27,81 is in G.P, here ratio is 3.

Determine the nth term, if the second term and third term of G.P is given.

Input:

1. second term of GP in double

2. third number of GP in double

3. nth term in integer.

Output:

nth term of series

def nth_term_of_gp(second_term, third_term, n):

# Calculate the common ratio

common_ratio = third_term / second_term

# Calculate the nth term

nth_term = second_term * (common_ratio ** (n - 1))

return nth_term

# Example usage

second_term = float(input("Enter the second term of the GP: "))

third_term = float(input("Enter the third term of the GP: "))

n = int(input("Enter the value of n: "))

result = nth_term_of_gp(second_term, third_term, n)

print(f"The {n}th term of the GP is: {result}")


4. A company sent its message in encrypted form. Then it is decrypted on other side. The encryption is done by
replacing each letter with the letter at 3 positions to the left. Like a is replaced with x, b with y,d with a .

You have been provided an encrypted ciphertext as input determine the corresponding original text, text is
always in lowercase and consists of alphabets only.

Input: encrypted text

Output: original text

Example

input1: nrfzh

Output: quick

Explanation: Since encryption is done by replacing each letter with the letter at 3 positions to the left,
therefore to decrypt, we need to find letters at 3 positions to the right.

def decrypt_message(encrypted_text):

original_text = ""

for char in encrypted_text:

if char.isalpha():

decrypted_char = chr((ord(char) - 3 - ord('a')) % 26 + ord('a'))

original_text += decrypted_char

else:

original_text += char

return original_text

# Example

encrypted_input = "nrfzh"

decrypted_output = decrypt_message(encrypted_input)

print("Output:", decrypted_output)

5. Write a program to calculate and return the sum of distances between the adjacent number in an array of
positive integers.

Note: You are expected to write code in the findTotalDistance function only which receive the first parameter
as the numbers of items in the array and second paramter as the array itself. You are not required to take the
input from the console.

Example

Finding the total distance between adjacent items of a list of 5 numbers.


Input

input 1:5

input 2: 10 11 7 12 14

Output

12

Explanation

The first parameter 5 is the size of the array. Next is an array of integers. The total of distances is 12 as per
calculation below.

10-11=1

11-7-4

7-12-5

12-14-2

Total Distance = 1+4+5+2=12

def findTotalDistance (n, numbers):

total = 0

for i in range(n-1):

total+= abs(numbers[i]-numbers[i+1])

return total

n = int(input())

numbers = list(map(int, input().split()))

print(findTotalDistance (n, numbers))

-----

Find Odd Even Difference


10 – 11 = 1
11 – 7 = 4
7 – 12 = 5
12-14 = 2
Total Distance = 1 + 4 + 5 + 2= 12

Find max difference


10-11= -1
11-7= 4
7-12 = -5
12-14= -2
Ans: 4

Total bill tax


1000 2000 3000 4000
1000

Find total feet

Diff between largest and smallest from a list of 5 num

https://www.scribd.com/document/505937158/TechMahindra-Codes

https://www.naukrimessenger.com/question-paper/tech-mahindra-coding-questions-and-answers/

Andy wants to go on a vacation

Khaled has an array of A of N elements.

Write a program to cal and return the sum of absolute diff between the adj num of positive integer from the
position entered by user.

Write a program to find the diff between the element at odd index and even index.

Write a program to return the diff between the count of odd num and even numbers.

Cloth mercchant 12 feet

You might also like