You are here : InfyTQ Archive >> InfyTQ Coding Problems
You are given a list of numbers from 1 to 9, in which each word is seperated by ‘,’. Your job is to find the sum of two numbers. These two numbers are needed to be calculated as per following rules :
1. First number should be calculated as :
Add all the numbers that do not come between 5 and 8 in the input.
2. Second number should be calculated as :
Append all the numbers to each other that comes between 5 and 8 (inclusive).
Find the sum of both numbers.
Note : 5 always comes before 8.
Number of 5’s = Number of 8’s.
Input :
First Input : List of numbers
Output :
“Sum of both numbers”
Sample Testcases :
I/P 1:
3,4,5,2,7,9,8,3,2
O/P 1:
52810
Solution :
#taking list as input where each element is seperated by ','
lst = list(map(int,input().split(",")))
#find sum of numbers that occur before 5
a = sum(lst[:lst.index(5)])
#find sum of numbers that occur after 8
b = sum(lst[lst.index(8)+1:])
#finding first number
n1 = a+b
#finding second number
rest = lst[lst.index(5):lst.index(8)+1]
n2 = ""
for i in rest:
n2+=str(i)
print(int(n2)+n1)
#TEST CASE PROBLEM
INPUT:
1,2,3,5,3,2,5,3,1,8,1,2,1,4,8,1,4,1
(satisfying both input condition :
no. of 5=no. of 8
5 always comes before 8)
INPUT1:
5325346(5325318+1+2+3+1+2+1+4+8+1+4+1) #AS PER THE ABOVE CODE
OUTPUT2:
532531812160(532531812148+1+2+3+1+4+1) #AS PER MY CODE,WHERE I CHOOSE THE NUMBER STARING FROM 5 AND END WITH 8,HAVING MAXIMUM LENGTH.
#MY CODE
l=list(map(int,input().split(“,”)))
rev=l[::-1]
start=l.index(5)
end=len(l)-rev.index(8)
string=”
for i in range(start,end):
string+=str(l[i])
string2=sum(l[:start])+sum(l[end:])
print(int(string)+string2)
#code if there are 1 or more than 1 5’s and 8’s
lst = list(map(int, input().split(‘,’)))
b = []
a = 0
def calc(c):
global a
#global c
global b
if c == len(lst):
return
for i in range(c, len(lst)):
if lst[i] != 5:
a = a + lst[i]
#print(a)
c = i
else:
break
if c+1 == len(lst):
return
for i in range(c+1,len(lst)):
if lst[i] == 8:
b.append(str(8))
c = i+1
calc(c)
break
else:
b.append(str(lst[i]))
return
calc(0)
#print(b,a)
res = int(”.join(b))
outt= res+a
print(outt)
import java.util.*;
public class TwoNUmbersTQ {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ArrayListnum=new ArrayList();
for(int i=0;i<9;i++)
{
int x=sc.nextInt();
num.add(x);
}
int a=0;
int b=0;
for (int i = 0; i < num.indexOf(5); i++)
{
a+=num.get(i);
}
for (int i = num.indexOf(8)+1; i < 9; i++)
{
b+=num.get(i);
}
int sum1=a+b;
String sum2="";
for(int i=num.indexOf(5);i<num.indexOf(8)+1;i++)
{
sum2+=num.get(i);
}
int sum=Integer.parseInt(sum2);
System.out.println(sum+sum1);
}
}