1: duplicate.append(i) print(duplicate) if __name__ == '__main__': arr = [1,2,3,4,2,3,5,6,3] print_duplicates(arr) ``` This program imports Counter from collections module. Counter creates a dictionary with elements of the list as keys and their counts as values. It then iterates through this dictionary and checks if">1: duplicate.append(i) print(duplicate) if __name__ == '__main__': arr = [1,2,3,4,2,3,5,6,3] print_duplicates(arr) ``` This program imports Counter from collections module. Counter creates a dictionary with elements of the list as keys and their counts as values. It then iterates through this dictionary and checks if">
Nothing Special   »   [go: up one dir, main page]

TCS NQT

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

TCS

NQT
• The TCS NQT 2020 is attempted by lakhs of
students. The examiners are not going to read
everyone’s code. Instead, they will use a
computerized evaluation which will automatically
assign score based ONLY ON THE OUTPUT.

• The main part of the coding section is “Test


Cases”. Your code will be Validated against test
cases. You will be given partial marks based on
how many test cases are cleared.
• STEP 1: Understanding the story.
• You are given 45 minutes to write a program and
clear all the test cases. Of this, spend the first five
to six minutes on understanding the story given
and figuring out what needs to be calculated.

• you need to spend more time in reading and


understanding the task. Don’t jump into typing
your program before you understand what is
going on.
• STEP 2: Reading the inputs
• Once you know what is needed, we need to think
in terms of how you write the code. 
• STEP 3: Cracking the core logic.
• The next step is to figure out how to convert this
data into an answer. By the time we reach this
step in the exam.
Syllabus

• Operations on strings
• Number Pattern/number series/pattern printing
• Number Conversion(binary/octal/hexa/decimal)
• Operations on matrices
• Operations on Arrays or numpy
• Operations on numbers (prime no,armstrong
no,palindrome,etc)

Oops
Anonyomus functions(lambda,map,redce,filter)
Checking if a given year is leap year or not

To check whether a year is leap or not


Step 1:
We first divide the year by 4.
If it is not divisible by 4 then it is not a leap year.
If it is divisible by 4 leaving remainder 0 
Step 2:
We divide the year by 100
If it is not divisible by 100 then it is a leap year.
If it is divisible by 100 leaving remainder 0
Step 3:
We divide the year by 400
If it is not divisible by 400 then it is a leap year.
If it is divisible by 400 leaving remainder 0 
Then it is a leap year
#python program to check if a year number taken from the user is a leap year or not, using nested if-
else.

num = int(input("Enter the year you want to check if is leap year or not: "))

#take input year from the user to check if it is a leap year

if(num%4 == 0):

 #check if the number is divisible by 4 and if true move to next loop

   if(num%100 == 0):

     #check if the input year is divisible by 100 and if true move to next loop

       if(num%400 == 0):

           print("The year {} is a leap year".format(num))

           #the input year is divisible by 4, 100 and 400, hence leap year.

       else:

           print("The year {} is Not a leap year".format(num))

 
  else:

       print("The year {} is a leap year".format(num))

       #if the number is divisible by both 4 and 100 it is a leap year

else:

   print("The year {} is Not a leap year".format(num))

   #if the input num is not divisible by 4 then it can not be a leap
year altogether.

 
Prime Numbers with a Twist

• Ques. Write a code to check whether no is prime or not.


Condition use function check() to find whether entered
no is positive or negative ,if negative then enter the
no, And if yes pas no as a parameter to prime() and
check whether no is prime or not?
• Whether the number is positive or not, if it is negative
then print the message “please enter the positive
number”
• It is positive then call the function prime and check
whether the take positive number is prime or not.
Prime Numbers with a Twist

• def prime(n):

   if n > 1:

    for i in range(2, n):

        if (n % i) == 0:

            print(n, "is not a prime number")

            break

    else:

        print(n, "is a prime number")

num = int(input("enter a number: "))

if (num > 0):

    prime(num)

else:

    print("please enter a positive number")


Number Series with a Twist – 1

Find the 15th term of the series?


0,0,7,6,14,12,21,18, 28 24 35 30 42 36 49
Explanation : 
In this series the odd term is increment of 7 {0, 7,
14, 21, 28, 35 – – – – – – }
 And even term is a increment of 6 {0, 6, 12, 18, 24,
30 – – – – – – }

Nth :15 : 49
Number Series with a Twist – 1

num = int(input('enter the number: '))

a=0

b=0

for i in range(1,num+1):

    if(i%2!=0):

        a= a+7

    else:

        b = b+6

if(num%2!=0):

    print(' {} term of series is {}'.format(num,a-7))

else:

    print('{} term of series is {}'.format(num,b-6))


String with a Twist

The program will receive 3 English words inputs from STDIN


These three words will be read one at a time, in three
separate line
The first word should be changed like all vowels should be
replaced by %
The second word should be changed like all consonants
should be replaced by #
The third word should be changed like all char should be
converted to upper case
Then concatenate the three words and print them other
than these concatenated word, no other characters/string
should or message should be written to STDOUT
String with a Twist

For example if you print how are you then output


should be h%wa#eYOU.
You can assume that input of each word will not
exceed more than 5 chars
Consider the below series :

• 0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8…….n


• Find out the 1000th term
• This series is a mixture of 2 series all the odd terms in
this series form even numbers in ascending order and
every even terms is derived from the previous  term
using the formula (x/2)
• Write a program to find the nth term in this series.
• The value n in a positive integer that should be read
from STDIN the nth term that is calculated by the
program should be written to STDOUT. Other than the
value of the nth term no other characters /strings or
message should be written to STDOUT.
• For example if n=10,the 10 th term in the series is
to be derived from the 9th term in the series. The
9th term is 8 so the 10th term is (8/2)=4. Only the
value 4 should be printed to STDOUT.
• You can assume that the n will not exceed 20,000.
• Find the nth term of the series.
• 1,1,2,3,4,9,8,27,16,81,32,243,….
• Problem Statement
• Our hoary culture had several great persons since time
immemorial and king vikramaditya’s nava ratnas (nine gems)
belongs to this ilk.They are named in the following shloka:

• Among these, Varahamihira was an astrologer of eminence and


his book Brihat Jataak is recokened as the ultimate authority in
astrology.
• He was once talking with Amarasimha,another gem among the
nava ratnas and the author of Sanskrit thesaurus, Amarakosha.
• Amarasimha wanted to know the final position of a person, who
starts from the origin 0 0 and travels per following scheme.
• Scheme
• He first turns and travels 10 units of distance
• His second turn is upward for 20 units
• Third turn is to the left for 30 units
• Fourth turn is the downward for 40 units
• Fifth turn is to the right(again) for 50 units

… And thus he travels, every time increasing the travel


distance by 10 units.
Test Cases
Case 1
Input : 3
Expected Output :-20 20
Case 2
Input: 4
Expected Output: -20 -20
Case 3
Input : 5
Expected Output : 30 -20
Case 4
Input : 7
Expected Output : 90 -20
n = int(input())
c = 'R'
dis = 10 x,y=0,0
for i in range(n):
if c=='R':
x=x+dis c='U‘
dis=dis+10
elif c=='U':
y=y+dis c='L'
dis=dis+10
elif c=='L':
x=x-dis c='D'
dis=dis+10
elif c=='D':
y=y-dis c='A‘
dis=dis+10
elif c=='A':
x=x+dis c='R‘
dis=dis+10
print(x,y)
Factorial of a number
Area of a circle
Leap year or not
GCD of two number
Check whether a given number is a prime number or not
Print all prime numbers in a given range
Check whether a given number is a Strong number or not
Check whether a number is a Palindrome or not
Check whether a string is a Palindrome or not
Check whether a given number is an Armstrong number or not
Print all Armstrong numbers between two intervals
Fibonacci series generation upto N terms
Binary to decimal conversion
Decimal to binary conversion
Decimal to octal conversion
Octal to decimal conversion
Binary to octal conversion
Octal to binary conversion
Sum of all prime numbers within a range
Reversing a given number
Reverse a string
Pyramid pattern using stars
Pyramid pattern using numbers
Diamond pattern printing using stars
Diamond pattern printing using numbers
Second smallest element in an array
Remove duplicate elements in an array
• Write a program to print string in reverse order
using recursion.
Write a Program to print all the duplicate elements in an
array (list)
Write a Program to print all the duplicate elements in an
array (list)

Using collections module


Write a Program to print all the duplicate elements in an array (list)

You might also like