0% found this document useful (0 votes)
1 views14 pages

Velocity - Python Coding Questions

The document contains a series of Python programming questions and solutions related to string manipulation, including concepts of mutable vs immutable objects, accessing list elements, reversing strings, merging strings, sorting characters, and checking for anagrams and palindromes. Each question is followed by example code demonstrating the solution. The document serves as a practical guide for learning Python string operations and functions.

Uploaded by

abijeet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views14 pages

Velocity - Python Coding Questions

The document contains a series of Python programming questions and solutions related to string manipulation, including concepts of mutable vs immutable objects, accessing list elements, reversing strings, merging strings, sorting characters, and checking for anagrams and palindromes. Each question is followed by example code demonstrating the solution. The document serves as a practical guide for learning Python string operations and functions.

Uploaded by

abijeet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Python Question – Velocity Bigdata Batch

Q) What is the difference between mutable and immutable.


Mutable objects: They can’t be updated once defined. eg- tuples

Immutable objects: They can be updated when required. eg- list

Q) How to access an element of a list?


The element in a list can be accessed using list_name [index]. For instance-

Given a list [1, 2, 3, 4]

Q)Convert a given string to int using a single line of code.


We can convert a given string to an integer using a built-in function int(). e.g.-

a = ‘5’

print(int(a))

Q)Convert following list to String.


s = ['I', 'want', 4, 'apples', 'and', 18, 'bananas']

# using list comprehension

listToStr = ' '.join([str(elem) for elem in s])

print(listToStr)

o/p  I want 4 apples and 18 bananas


Python Question – Velocity Bigdata Batch

Q1) Write a Program To REVERSE content of the given String by using


slice operator?
1) input: velocity
2) output: yticolev
3)
4) s = input('Enter Some String to Reverse:')
5) output = s[::-1]
6) print(output)

Q2) Write a Program To REVERSE content of the given String by using


reversed() function?
1) input: velocity
2) output: yticolev
3)
4) s=input('Enter Some String to Reverse:')
5) r=reversed(s)
6) output=''.join(r)
7) print(output)

Q3) Write a Program To REVERSE content of the given String by using


while loop?
1) input: velocity
2) output: yticolev
3)
4) s=input('Enter Some String to Reverse:')
5) output=''
6) i=len(s)-1
7) while i>=0:
8) output=output+s[i]
9) i=i-1
10) print(output)

2
Python Question – Velocity Bigdata Batch

Q4) Write a Program To REVERSE order of words present in the given


string?
1) input: Learning Python Is Very Easy
2) output: Easy Very Is Python Learning
3)
4) s=input('Enter Some String:')
5) l=s.split()
6) l1=l[::-1]
7) output=' '.join(l1)
8) print(output)

Q5) Write a Program To REVERSE internal content of each word?


1) input: 'Velocity Software Solutions'
2) output: 'yticolev erawtfoS snoituloS'
3)
4) s=input('Enter Any String:')
5) l=s.split()
6) l1=[]
7) for word in l:
8) l1.append(word[::-1])
9) output=' '.join(l1)
10) print(output)

Q6) Write a Program To REVERSE internal content of every second


word present in the given string?
1) i/p: one two three four five six 3)
2) l=s.split()
5) o/p: one owt three ruof five xis
7) i=0
9)if i%2 == 0:
4) s='one two three four five six'
11)else:
6) l1=[]

8) while i<len(l):

10) l1.append(l[i])

3
Python Question – Velocity Bigdata Batch

12) l1.append(l[i][::-1])
13)i=i+1
14) output=' '.join(l1)
15) print(output)
Python Question – Velocity Bigdata Batch

Q8) Write a program to merge characters of 2 strings into a single


string by taking characters alternatively?
Input:
s1='RAVI'
s2='TEJA'

Output: RTAEVJIA

If strings are having same length:

1) s1='RAVI'
2) s2='TEJA'
3) output=''
4) i,j=0,0
5) while i<len(s1) or j<len(s2):
6) output=output+s1[i]+s2[j]
7) i=i+1
8) j=j+1
9) print(output)

Output: RTAEVJIA

2nd way by using map():

1) s1='RAVI'
2) s2='TEJA'
3) l=list(map(lambda x,y:x+y,s1,s2))
4) print(''.join(l))

Note: The above program can work if the lengths of 2 strings are same.

5
Python Question – Velocity Bigdata Batch

If strings having different lengths:

1) s1=input('Enter First String:')


2) s2=input('Enter Second String:')
3) output=''
4) i,j=0,0
5) while i<len(s1) or j<len(s2):
6) if i<len(s1):
7) output=output+s1[i]
8) i=i+1
9) if j<len(s2):
10) output=output+s2[j]
11) j=j+1
12) print(output)

Output:
py test.py Enter First
String:RAVIKIRAN Enter
Second String:TEJA
RTAEVJIAKIRAN

py test.py Enter First


String:RAVI
Enter Second String:TEJAKIRAN
RTAEVJIAKIRAN

Q9) Assume input string contains only alphabet symbols and digits.
Write a program to sort characters of the string, first alphabet
symbols followed by digits?
1) input: B4A1D3
2) output: ABD134
3)
4) s='B4A1D3'
5) alphabets=[]
6) digits=[]
7) for ch in s:
8) if ch.isalpha():
9) alphabets.append(ch)
10) else:

6
11) digits.append(ch)
12) output=''.join(sorted(alphabets)+sorted(digits))
13) print(output)

Q10) Write a program for the following requirement?


1) input: a4b3c2
2) output: aaaabbbcc
3)
4) s=input('Enter Some String where alphabet symbol should be followed by digit:')
5) output=''
6) for ch in s:
7) if ch.isalpha():
8) x=ch
9) else:
10) d=int(ch)
11) output=output+x*d
12) print(output)

7
Python Question – Velocity Bigdata Batch

Q11) Write a program for the following requirement?

1) input: a3z2b4
2) output: aaabbbbzz (sorted String)
3)
4) s=input('Enter Some String where alphabet symbol should be followed by digit:')
5) target=''
6) for ch in s:
7)if ch.isalpha():
8) x=ch
9)else:
10) d=int(ch)
11) target=target+x*d
12) output = ''.join(sorted(target))
13) print(output)

Q12) Write a program for the following requirement?

1) input: aaaabbbccz
2) output: 4a3b2c1z
3)
4) s='aaaabbbccz'
5) output='' 7) c=1
9) while
6) i<len(s):
previous=s[0]

8) i=1

10)if s[i]==previous:
11) c=c+1
12)else:
13) output=output+str(c)+previous
14) previous=s[i]
15) c=1
16)if i ==len(s)-1:
17) output=output+str(c)+previous
18)i=i+1
19) print(output)

8
Python Question – Velocity Bigdata Batch

Q13) Write a program for the following requirement?


Input: a4k3b2
Output: aeknbd

In this example the following two functions are required to use


1) ord(): To find unicode value for the given
character Eg: print(ord('a')) #97
2) chr(): To find corresponding character for the given unicode value
Eg: print(chr(97)) # a

1) s='a4k3b2'
2) output=''
3) for ch in s:
4)if ch.isalpha():
5) x=ch
6) output=output+ch
7)else:
8) d=int(ch)
9) newc= chr(ord(x)+d)
10) output=output+newc
11) print(output)

Q14) Write a program to remove duplicate characters from the given


input String?
Input: AZZZBCDABBCDABBBBCCCCDDDDEEEEEF
Output: AZBCDEF

1st way:

1) s='AZZZBCDABBCDABBBBCCCCDDDDEEEEEF'
2) output=''
3) for ch in s:
4) if ch not in output:
5) output=output+ch
6) print(output) # AZBCDEF

9
Python Question – Velocity Bigdata Batch

2nd way:

1) s='AZZZBCDABBCDABBBBCCCCDDDDEEEEEF'
2) l=[]
3) for ch in s:
4) if ch not in l:
5) l.append(ch)
6) output=''.join(l)
7) print(output) # AZBCDEF

3rd way by using set (but no guarantee for the order)

1) s='ABCDABXXXBCDABBBBCCCZZZZCDDDDEEEEEF'
2) s1=set(s)
3) output=''.join(s1)
4) print(output) #CAEZBFD

Q15) Write a program to find the number of occurrences of each


character present in the given string?
By using count() method and List:

1) s='ABCDABXXXBCDABBBBCCCZZZZCDDDDEEEEEF'
2) l=[]
3) for ch in s:
4) if ch not in l:
5) l.append(ch)
6)
7) for ch in sorted(l):
8) print('{} occurrs {} times'.format(ch,s.count(ch)))

Without using count() method:

1) s='ABCDABXXXBCDABBBBCCCZZZZCDDDDEEEEEF'
2) d={}
3) for ch in s:
4) d[ch]=d.get(ch,0)+1
5) for k,v in d.items():
6) print('{} occurrs {} times'.format(k,v))

10
Python Question – Velocity Bigdata Batch

For sorting purpose:

1) for k,v in sorted(d.items()):


2)print('{} occurrs {} times'.format(k,v))

Q16) Write the program for the following requirement:


Input: ABAABBCA
Output: 4A3B1C

1) s='ABAABBCA'
2) output=''
3) d={}
4) for ch in s:
5) d[ch]=d.get(ch,0)+1
6) for k,v in sorted(d.items()):
7) output=output+str(v)+k
8) print(output)

Q17) Write the program for the following requirement:


Input: ABAABBCA
Output: A4B3C1

1) s='ABAABBCA'
2) output=''
3) d={}
4) for ch in s:
5) d[ch]=d.get(ch,0)+1
6) for k,v in sorted(d.items()):
7) output=output+k+str(v)
8) print(output)

11
Python Question – Velocity Bigdata Batch
6)d[ch]=d.get(ch,0)+1

Q19) Write a program to check whether the given two strings are
anagrams or not?
Two strings are said to be anagrams iff both are having same content irrespective of
characters position.

Eg: lazy and zaly

1) s1=input("Enter first string:")


2) s2=input("Enter second string:")
3) if(sorted(s1)==sorted(s2)):
4) print("The strings are anagrams.")
5) else:
6) print("The strings aren't anagrams.")

Output: py test.py Enter


first string:lazy
Enter second string:zaly
The strings are anagrams.

py test.py Enter first


string:velocity Enter second
string: danger
The strings aren't
anagrams.

12
Python Question – Velocity Bigdata Batch

Q20) Write a program to check whether the given string is palindrome


or not ?
A string is said to be palindrome iff original string and its reversed strings are equal.

1) s=input("Enter Some string:")


2) if s==s[::-1]:
3) print('The given string is palindrome')
4) else:
5) print('The given string is not palindrome')

py test.py Enter Some


string:level
The given string is palindrome

py test.py Enter Some


string:madam The given
string is palindrome

py test.py Enter Some


string:apple
The given string is not palindrome

Q21) Write the program for the following requirement:

1) inputs:
2) s1='abcdefg'
3) s2='xyz'
4) s3='12345'
5) output: ax1, by2,cz3,d4,e5,f,g
6)
7) s1='abcdefg' 9) s3='12345'
8)
11)s2='xyz'
while i<len(s1) or j<len(s2) or k<len(s3):

13)ifi=j=k=0
10) i<len(s1):

12)output=''

14) output=output+s1[i]
15) i=i+1

13
Python Question – Velocity Bigdata Batch

16)if j<len(s2):
17) output=output+s2[j]
18) j=j+1
19)if k<len(s3):
20) output=output+s3[k]
21) k=k+1
22)print(output)

Output:
ax1
by2
cz3
d4
e5
f
g

14

You might also like