Nitin K

mail@pastecode.io avatar
unknown
python
2 years ago
1.4 kB
2
Indexable
Never
//Calculate sum of digits using recursion

def sumOfDigits(n):
sum=0
if(n>0):
sumOfDigits(n%10)
sum=sum+n //Wrong
return sum

n = int(input())
s=sumOfDigits(n)
print(s)

---------------------------------------

//Print repeating elements present in integer array


n=int(input("enter no of elements:"))
l=[]
for i in range(n):
l.append(int(input()))
nl=[]
final=[]
for i in l:
if(i in nl):
continue
else:
nl.append(i)
print(nl)
for i in nl:
c=0
for j in l:
if(i==j):
c=c+1
if(c>1):
final.append(i)

---------------------------------------

//Explain method overloading and overriding with code

class A{

void m1(int a){
System.out.println("a in m1");
}
void m1(String b){
System.out.println("b in m1");
}
void m2(int c){
System.out.println("c in m2 in class A");
}
}
class B extend A{

void m2(int c){
System.out.println("c in m2 in class B");
}
}

---------------------------------------

// Print all combinations of numbers from 1 to `n` having sum `n`
   E.g. For n = 5, the following combinations are possible:

{ 5 }
{ 1, 4 }
{ 2, 3 }
{ 1, 1, 3 }
{ 1, 2, 2 }
{ 1, 1, 1, 2 }
{ 1, 1, 1, 1, 1 }


-- As per candidate following code will work for rows where values are not repeating.
sum=n
l=[1 to n]
finalA=[]
tA=[]
for i in l:
if(sum==0):
if(tA not in finalA):
finalA.append(tA)
elif(sum>0):
sum=sum-i
tA.append(i)
elis(sum<0):
break