Posts

Showing posts from December, 2020

Inverted L-Shaped Matrix

Inverted L-Shaped Matrix  Problem: Given a square matrix of size S, print the matrix in the format given in Example Input/Output section.  Input Format:  The first line contains the value size S of the matrix. The next S lines contain S values of a given row separated by a space.  Output Format:  The S lines denote the inverted L-shaped matrix.  Boundary Condition:  1 <= S <= 100  Example Input/Output 1:  Input:  3  11 12 13  21 22 23  31 32 33  Output:  11  21 22 12  31 32 33 23 13  Example Input/Output 2:  Input:  6  1 2 3 4 5 6  1 2 3 4 5 6  1 2 3 4 5 6  1 2 3 4 5 6  1 2 3 4 5 6  1 2 3 4 5 6  Output:  1  1 2 2  1 2 3 3 3  1 2 3 4 4 4 4  1 2 3 4 5 5 5 5 5  1 2 3 4 5 6 6 6 6 6 6 Program: n = int(input()) l = [] for i in range(n):     l.append(list(map(int,input().split()))) for i in range(n):     p...

Hare & Tortoise - 001

Hare & Tortoise - 001   Problem: Most of us would know the Hare & the Tortoise story. As the hare can run faster than the tortoise, god gives some magical power points X to the tortoise which never diminishes. But to be fair, god also gives an array of L magical power points to the Hare before each lap involved in the race. When the hare acquires N or more magical power points due to laxity it goes to sleep and during sleep it loses exactly N magical power points. Then the hare wakes up and completes the remaining laps. If in the last lap the magical power points R remaining with the hare is more than X, Hare wins. Else tortoise wins. If R is equal to X, the match ends in a draw and the program must print -1. If the hare does not sleep, it will always win the race. The hare will sleep only once or not sleep at all.  Input Format:  First line contains N and X power points separated by a space Second line contains L which denotes the number of laps in the race....

Archery Game

Archery Game   Problem: In an archery game, a bow and arrow is used to aim at the center of the circular target. The points are awarded based on how close the arrow hits the center of the target. The points for a shoot is calculated by the formula (3 - DISTANCE FROM CENTER OF THE TARGET) * 100. The target has radius of 3 units and any arrow hitting outside of the target will result in 0 points. Given N records with archer's name and the arrow's landing coordinates print the leaderboard (sorted by the total points, with highest score first) rounded up to 2 decimal places. The archers name could be repeated as a single archer can shoot multiple times.  Boundary Condition:  1<= N <= 1000  Input Format:  First line contains N. Next N lines contain the archers name and arrow's landing coordinates separated by a space.  Output Format:  D lines containing the name of the archer and their total points scored separated by a space . D denotes the unique ar...

FLAMES Game

FLAMES Game   Problem: FLAMES game is a relationship calculating game. Letters in FLAMES stand for,  F - FRIENDS  L - LOVER  A - AFFECTION  M - MARRIAGE  E - ENEMY  S - SIBLING  Calculation Logic - FLAMES:  1. Get two names S1 and S2.  2. Remove the common characters in both the names.  3. Find the count of the characters that are left, the count is called FLAMES count FC.  4. Consider FLAMES letters ('F', 'L', 'A', 'M', 'E', 'S') and start removing the letters in a circular manner using the FC.  5. The letter which is finally left desides the output,  If it's F - FRIENDS L - LOVER A - AFFECTION M - MARRIAGE E - ENEMY S - SIBLING  Input Format:  First line contains S1 Second line contains S2  Output Format:   First line contains FLAMES relationship in uppercase.  Boundary Conditions:  2 <= S1, S2 <= 100  Example Input/Output 1:  Input:  raja  rani  Outp...

Solve Equations

Solve Equations  Problem: Given two equations EQN1 and EQN2 containing the values of x co-efficient, y co-efficient and the resulting constant, the program must print the value of x and y separated by a space.  Input Format:  The first line contains the equation EQN1. The second line contains the equation EQN2.  Output Format:  The first line contains the value of x and y separated by a space.  Boundary Conditions:  1 <= Co-efficient values <= 1000  Example Input/Output 1:  Input:  5x+2y=14  -4y+3x=-2  Output:  2 2  Example Input/Output 2:  Input:  4Y-5X=17  3X+4Y=9  Output:  -1 3 Program: def extractnum(s):      a = ""      k = []     for i in s:          if i not in "xy= ":             a += i          else:             if a!= "":  ...

Matrix Column-wise Sort

Matrix Column-wise Sort  Problem: Given a matrix M containing the elements in R rows and C columns. Sort the matrix elements in ascending order along each column. Print the column-wise sorted matrix in the matrix format.  Input Format:  The first line contains the values of R and C separated by a space. The next R lines (denote the values of matrix M) each containing C values separated by a space.  Output Format:  R lines contain the column-wise sorted matrix each containing C values separated by a space.  Boundary Conditions: 1 < R, C < 50  Example Input/Output 1:  Input:  2 2  1 0  0 1  Output:  0 0  1 1  Example Input/Output 2:  Input:  3 5  4 8 9  4 1 2  6 1 9  4 0 5  4 2 6  Output:  0 5 1  2 1 2  6 4 4  4 4 8  9 9 6 Program: r,c = map(int,input().split()) l = [] for i in range(r):      l.append(list(map(int,input().split())...

Wall Painting TCS NQT

Wall Painting  TCS NQT  Problem: Interior wall painting cost is X rupees per square feet and exterior wall painting cost is Y rupees per square feet. A house has M interior walls and N exterior walls to paint. The program must accept the surface area (in units of square feet) of the M interior walls and N exterior walls as the input. The program must print the total estimated cost with the precision up to 2 decimal places as the output.  Boundary Condition(s):  1 <= X, Y, M, N <= 100  Input Format:  The first line contains X and Y separated by a space. The second line contains M and N separated by a space. The next (M+N) lines, each contains a floating point value representing the surface area of a wall (Interior walls followed by exterior walls).  Output Format:  The first line contains the total estimated cost with the precision up to 2 decimal places.  Example Input/Output 1:  Input:  18 12  6 3  14.3 15.2 10.5 12...

Water Tank & Buckets TCS NQT

Water Tank & Buckets TCS NQT  Problem: There is a water tank with the maximum capacity of X litres and a bucket with the maximum capacity of Y litres. A boy wants to fill greater than or equal to 80% of the water tank with the water using the bucket. The amount of water taken at a time in the bucket is not fixed as it can be any value less than or equal to Y. The program must accept the values of X, Y and a list of integers representing the amount of water taken in the buckets to fill the water tank. The program must print the number of buckets B poured into the water tank to reach greater than or equal to 80% of water. If it is not possible to fill 80% of the water tank, the program must print the string value "TANK NOT FULL" as the output.  Boundary Condition(s):  1 <= Y <= X <= 1000  Input Format:  The first line contains X. The second line contains Y. The line(s), each contains an integer representing the amount of water taken in the bucket....

Infected Crops

Infected Crops   Problem: A farm is represented as a R*C 2D matrix. Due to the soil condition, some crops are very weak. These crops are vulnerable to insects. The weak crops are represented by W and good crops are represented by G. When a crop is infected, the weak crops adjacent to it (in all directions north, south, east and west) are also infected and destroyed. The crops which are already infected are represented as I in the input.  Boundary Condition:  1<= R,C <= 50  Input Format:  First line contains R and C separated by space. Next R lines contain the matrix representation of the farm.  Output Format:  The first R lines contain the farm status with the destroyed crops represented by D, weak crops by W and good crops by G.  Sample Input/Output 1:  Input:  3 3  W G W  W G G  W I G  Output:  D G W  D G G  D D G  Sample Input/Output 2:  Input:  5 5  W W G W W  I W...

Fair Share

Fair Share  Problem Statement:  Raju and Kumar are friends and they order S items in a hotel but Kumar does not eat the Ith item ordered. Raju eats the Ith item alone. They decide to share the cost of all items they share and eat. The amount paid by Kumar is also passed as the input. If the bill amount is split equally after deducting the Ith item price the program must print "FAIR SHARE" otherwise the program must print the absolute difference between amount paid by Kumar and the half of the amount for the items shared by Raju and Kumar. Input Format: First line contains S & I [S - Number of Items & I - the item ate by Raju alone]. Second line contains items cost separated by space. Third line contains the amount paid by Kumar Output Format: First Line contains "FAIR SHARE" or the absolute difference between the amount paid by Kumar and the half of the amount for the items shared by Raju and Kumar rounded up to 1 decimal place.  Example Input/Output 1:  Inp...

Chocolate Vending Machine - TCS NQT

Chocolate Vending Machine - TCS NQT  Problem: There is a chocolate vending machine which contains at most N chocolates. The chocolate vending machine is never remaining empty as when last K chocolates are left, the machine is refilled with new chocolates in such a way that the machine gets full. There are C customers numbered from 1 to C who want to buy some chocolates from the machine. The program must accept the values of N, K and C integers representing the number of chocolates that the C customers want to buy as the input. After processing each customer's request, the program must print the output based on the following conditions. - The program must print the string value "Customer: " followed by the customer's number. - If it is possible to sell the chocolates to a customer's request, the program must print the string value "Number of chocolates sold: " followed by the number of chocolates sold to the customer. Then the program must print the strin...

Next Date with Same Weekday

Next Date with Same Weekday Problem:  Calculate the date of next 7th day from the given date.  Input:  31-03-2019  Output:  07-04-2019 Program: def leap(y):      return (y%400==0 or (y%4==0 and y%100!=0)) s = list(map(int,input().split("-"))) s[0] += 7  a = [1,3,5,7,8,10,12] b = [2] c = [4,6,9,11]  if s[1] in a and s[0] > 31:     s[0] = s[0] % 31      s[1] += 1      if s[1] > 12:          s[1] = s[1]%12          s[2] += 1  if s[1] in c and s[0] > 30:      s[0] = s[0] % 30     s[1] += 1  if s[1] in b:      if leap(s[2]) and s[0] > 29:          s[0] = s[0] % 29          s[1] += 1      elif not(leap(s[2])) and s[0] > 28:          s[0] = s[0] % 28          s[1] += 1  ...

Largest & Smallest Unit Digit Numbers Difference

Largest & Smallest Unit Digit Numbers Difference  Problem: Given N numbers, the program must find and print the difference between numbers having the largest and smallest unit digits. - If multiple numbers have the largest unit digit, then choose the largest number among them. - If multiple numbers have the smallest unit digit, then choose the smallest number among them.  Input Format: The first line contains N. The second line contains N positive integers separated by a space.  Output Format: The first line contains the difference between the numbers having the largest and smallest unit digits.  Boundary Conditions: 1 <= N <= 9999  Example Input/Output 1:  Input:  5  19 21 32 41 89  Output:  68  Explanation: Two numbers 19 and 89 have the largest unit digit 9. We choose 89 as per the condition given. Two numbers, 21 and 41 have the smallest unit digit 1. We choose 21 as per the condition given. Hence the difference is 89-...

Metro Train Fare - TCS NQT

Metro Train Fare - TCS NQT  Problem: A metro train is a ring route train which runs in a circular fashion. There are N stations in the train's route. The names of the N stations and the distance between each station (from the previous station to the current station) are passed as the input to the program. For every 1000 meters, the ticket fare is X rupees. The name of the source station, the name of destination station and the value of X are also passed as the input to the program. The program must print the total ticket fare F as the output. The total ticket fare F must be ceiled.  Boundary Condition(s):  1 <= N <= 30 Length of each station's name = 2  1 <= Distance between two stations <= 5000 1 <= X <= 25  Input Format: The first line contains N. The second line contains the names of the N stations separated by a space. The third line contains the distance between each station separated by a space. The fourth line contains the name of the so...

Physical Fitness - TCS NQT

Physical Fitness -  TCS NQT  Problem: A physical fitness test (running test) is conducted to select candidates for military training in a country. A batch of 3 candidates appearing for the running test on track for 3 rounds. The program must accept 9 integers indicating the oxygen level after each round for 3 candidates as the input. For each candidate, the program must calculate the average oxygen level over the 3 rounds. The candidate who has the highest average oxygen level is the most fit candidate. The program must print the string value "Candidate Number: " followed by the most fit candidate's number as the output. If more than one trainee attains the same highest average level, they all need to be printed.  Boundary Condition(s): 1 <= Oxygen level <= 100  Input Format: The first 3 lines, each contains an integer representing the oxygen level of the 3 candidates after the round 1. The middle 3 lines, each contains an integer representing the oxygen level ...

Move Hundreds to End

Move Hundreds to End  Problem: Fill in the missing lines of code to implement the method (function) moveHundreds(int array[], int size) so that the method moves all the integer values with 100 to the end of the array. The order of the remaining integers must be maintained.  Boundary Condition(s): 1 <= N <= 1000  Example Input/Output 1:  Input:  6  12 100 45 8 100 25  Output:  12 45 8 25 100 100  Explanation:  There are two integer values as 100 which are moved to the end. The remaining integers 12 45 8 25 are printed in the same order as given in the input.  Example Input/Output 2:  Input:  8  100 100 65 100 14 100 100 56  Output:  65 14 56 100 100 100 100 100 Program: void moveHundreds(int array[], int size) {     int a[size], k = 0;      for(int i=0; i<size; i++)     {         if(array[i] != 100)         {     ...

Inverse Two Triangle Pattern

Inverse Two Triangle Pattern Problem: Fill in the missing lines of code to implement the method (function) printPattern(int N) so that the method prints the pattern given in the Example Input/Output section.  Boundary Condition(s): 1 <= N <= 1000  Example Input/Output 1:  Input:  5  Output:  54321 4321  321  21  1  12  123  1234  12345 Example Input/Output 2:  Input:  7  Output:  7654321  654321  54321  4321  321  21  1  12  123  1234  12345  123456  1234567 Program: void printPattern(int N) {     for(int i = N; i>0; i--)     {         for(int j=i;j>0;j--)         {             printf("%d",j);         }         printf("\n");     }     for(int i=1;i<N;i++)     {   ...

Integers - Maximum and Count

Integers - Maximum and Count  Problem: N integers are passed as the input. The program must repeat the steps given below.  Step 1: Find the maximum M of the available integers and check if M is a single digit integer. If M is a single digit integer exit the program after printing M and the count of integers C present finally.  Step 2: If M is not a single digit integer then divide M into M/2 and M-M/2. Remove M from the list of integers and add M/2 and M-M/2 to the list of integers. Now repeat steps 1 and 2 until M is a single digit integer.  Boundary Condition(s): 2 <= N <= 10^5  Example Input/Output 1:  Input:  4  12 6 20 8  Output:  8 8  Explanation:  20 is the maximum. So the integers become 12 6 10 10 8. Now 12 is the maximum. So the integers become 6 6 6 10 10 8. Now 10 in the maximum. So the integers become 6 6 6 5 5 10 8. Again 10 in the maximum. So the integers become 6 6 6 5 5 5 5 8. Now 8 is the maximum and is ...

Sum of Non-prime Digits - WIPRO

Sum of Non-prime Digits - WIPRO  Problem: The program must accept an integer N as the input. The program must print the sum of non-prime digits in N as the output. If there is no such digit, the program must print -1 as the output.  Input 1:  219517  Output 1:  11    Input 2:   3005  Output 2:  0  Input 3:   725  Output 3:   -1 Program: number = int(input()) total = 0  non_prime = 0  while number > 0:      rem = number % 10      if rem not in [2,3,5,7]:          total += rem          non_prime = 1      number //= 10     if non_prime:     print(total) else:         print(-1)        

Swap Every Two Digits - WIPRO

Swap Every Two Digits - WIPRO  Problem: The program must accept two integers X and Y as the input. The program must swap every two digits in both X and Y. Then the program must print the sum of the revised values of X and Y as the output. If the number of digits in an integer is odd, the last digit remains the same as there is no digit to swap.  Input:  27521 7809  Output:  81041 Program: def swap_two(number):      res = ""     while number != "":          res += number[:2][::-1]          number = number[2:]      return int(res)     a,b = input().split()  a = swap_two(a) b = swap_two(b) print(a+b)

Valid Mobile Number

Valid Mobile Number Problem: The program must accept a list of mobile numbers (in separate lines) as the input. The character q or Q represents the end of the input. The program must print the number of invalid mobile numbers among the given list of mobile numbers as the output. A valid mobile number contains exactly 10 digits. Input: 9854653221  997878A1576  9992224578  999225789900  986578989B  817524990  Q Output: 4 Program: count = 0  total_count = 0  while 1:      number = input().strip()     if number == 'q' or number == 'Q':         break     digit_count = 0      invalid = 0      for i in number:          if i.isdigit():             digit_count += 1          else:              invalid = 1              break ...

Count of Non-repeated Characters - Wipro

Count of Non-repeated Characters - Wipro Problem: The program must accept a string S as the input. The program must print the count of characters that are not repeated in the string S as the output.  Input: Abcdef#bcd Output: 4 Program: s = input().strip() count = 0 for i in s:      if s.count(i) == 1:         count += 1  print(count)        

Decode Digital Clock - Wipro

Decode Digital Clock - Wipro  Problem: The program must accept an integer X representing the encoded key of a digital lock. There are two ways to decode the key of the digital lock. - If X is an armstrong number, then the key is the sum of even digits in the encoded key. - If X is not an armstrong number, then the key is the sum of odd digits in the encoded key. An armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits in the number. The program must decode the key X and print it as the output. Input:  1634  Output:  10 Program: def is_armstrong(number):      power = len(str(number))     temp = number     total = 0      while number > 0:          total += (number%10)**power          number //= 10      return total != temp   number = int(input()) result = is_armstrong(number) decode = 0...

Generate Key - Accenture

Generate Key - Accenture   Problem: The program must accept three integers X, Y and Z as the input. Each of these are four-digit integers. The program must generate a four-digit key K based on the following conditions. - The 1st digit in K must be equal to the SMALLEST digit in the thousands place of all three integers. - The 2nd digit in K must be equal to the LARGEST digit in the hundreds place of all three integers. - The 3rd digit in K must be equal to the SMALLEST digit in the tens place of all three integers. - The 4th digit in K must be equal to the LARGEST digit in the units place of all three integers. 1000 <= X, Y, Z = 9999 Input:   5107 1234 2948  Output:   1908 Program: l = list(map(int,input().split())) k = [9,0,9,0] ans = 0  for i in l:      k[0] = min(k[0],i//1000)     k[1] = max(k[1],i//100%10)     k[2] = min(k[2],i//10%10)     k[3] = max(k[3],i%10) for i in k:     ans = ans * 10 + i ...

Count Strong Points - Accenture

 Count Strong Points - Accenture Problem: The program must accept an integer matrix of size RxC as the input. The program must print the number of strong points in the given matrix as the output. The strong points are located at the element(s) whose all the surrounding elements are smaller than the given element. Input:  3 3  56 92 45  19 41 51  55 31 80 Output: 3  Program: n,m = map(int,input().split()) l = []  for i in range(n):      l.append(list(map(int,input().split()))) count = 0      for i in range(n):      for j in range(m):         c = 0         if i-1 >= 0 and l[i-1][j] >= l[i][j]:             c = 1          if i+1 < n and l[i+1][j] >= l[i][j]:              c = 1          if j-1 >= 0 and l[i][j-1] >= l[i][j]:     ...

Grouping Colorful LEDs - Accenture

Grouping Colorful LEDs - Accenture Problem: The program must accept the names of the colors in an LED serial set as the input. The program must find the number of groups of LEDs having the same color at the beginning and the end in the given LED serial set. Also consider each LED in the given LED serial set as a group. Finally, the program must print the number of groups of LEDs as the output. Input 1: Red Blue Green Blue Output 1: 5 Input 2: Yellow Red Yellow Green Blue Yellow Green Output 2: 11 Program: color = input().split()  count = 0 for i in range(len(color)):     if color[i] in color[i+1:]:          count  +=  color[i+1:].count(color[i]) print(count + len(color))     Explanation:  1. Accept a list of color names.  2. Iterate through the list and find the count of each color from the position next to the current position. Since to cover all the possible sublists that start and end with the same color. 3. ...

Distinct Length Pairs

Distinct Length Pairs  Program:  The program must accept M integers and N integers as the input. The program must form pairs of integers by selecting one integer from M integers and another integer from N integers. Then the program must print the number of pairs containing two integers with different numbers of digits as the output.  Input:  3 4  12 1 454  988 2 112 47  Output:  8  Program:  n1,n2 = map(int,input().split()) list1 = list(map(str,input().split())) list2 = list(map(str,input().split())) dict1 = dict()  dict2 = dict() for i in list1:      try:          dict1[len(i)] += 1      except:          dict1[len(i)] = 1  for i in list2:      try:          dict2[len(i)] += 1      except:          dict2[len(i)] = 1  count = 0  for i in dict1:    ...

Pronic Integers in N - InfyTQ question

Pronic Integers in N - InfyTQ question Problem:  The program must accept an integer N as the input. The program must print all the pronic integers formed by a series of continuously occurring digits (in the same order) in N as the output. The pronic integers can be represented as n*(n+1). Input:  93042861 Output: 930 30 0 42 2 6 Program: import math  s = input().strip() def check_pronic(num):      sq = math.floor(math.sqrt(num))      return (sq*(sq+1)) == num  l = []   for i in range(len(s)):      for j in range(i,len(s)):          substr = int(s[i:j+1])          if s[i:j+1] == "0":             l.append(substr)         elif s[i:j+1][0]!='0' and check_pronic(substr):             l.append(substr)  print(*l)             Expl...