Вы находитесь на странице: 1из 5

****http://www.bogotobogo.com/python/python_interview_questions_5.

php***
1)a='''host,host1
host2,host3
host4,host5'''
b=a.split('\n')#Converts string to list
c=[x.strip() for x in b] #Removes whitespaces
d=','.join(c)
e=d.split(',')
print(e)

o/p: ['host', 'host1', 'host2', 'host3', 'host4', 'host5']

split converts string object to list object.

cmd_list=['hostname','ip']
for cmd in cmd_list:
print(cmd)
args=cmd.split()
print(args)

O/P:hostname
['hostname']
ip
['ip']

eg:
2)Convert List to dictionary:

a=[1,2,3,4,5,6]
b={}
for i in range(0,len(a),2):
if i==len(a)-1:
print(i)
break
else:
b[a[i]]=a[i+1]
print(b)

o/p: {1: 2, 3: 4, 5: 6}

3)Convert two lists to a dictionary.

a=[1,2,3]
b=[4,5,6]
c=dict(zip(a,b))
print(c)

4)Convert dictionary to List:

a={1: 2, 3: 4, 5: 6}
b=[]
for i,j in a.items():
b.append(i)
b.append(j)

print(b)

5)Mobile number validation.


Sample Input

2
9587456281
1252478965

Sample Output

YES
NO

import re
b=int(input())
for j in range(b):
i=raw_input()
pat='^[789]\d{9}'

try:
mat=re.match(pat,i)
num=mat.group()
if num==str(i):
print("YES")
else:
print("NO")
except Exception,e:
print("NO")

6)E-mail validation.

^[a-z][-|.|_]?[0-9]?\w+?@\w+.\w+$

7)Please write a program which accepts a string from console and print the
characters that have even indexes.

Example:
If the following string is given as input to the program:
H1e2l3l4o5w6o7r8l9d
Then, the output of the program should be:
Helloworld

a="H1e2l3l4o5w6o7r8l9d"
print(a[::2])

8)Please write a program which count and print the numbers of each character in a
string input by console.

Example:
If the following string is given as input to the program:

abcdefgabc

Then, the output of the program should be:

a,2
c,2
b,2
e,1
d,1
g,1
f,1

from collections import OrderedDict


a="abcdefgabc"
b=list(a)
c=OrderedDict()
for i in range(len(b)):
c[a[i]]=c.get(a[i],0)+1

for i,j in c.items():


print("%s,%d" %(i,j))

9) By using list comprehension, please write a program to print the list after
removing the value 24 in [12,24,35,24,88,120,155].

Hints:
Use list's remove method to delete a value.

Solution:

li = [12,24,35,24,88,120,155]
li = [x for x in li if x!=24]
print li

10)By using list comprehension, please write a program to print the list after
removing the 0th, 2nd, 4th,6th numbers in [12,24,35,70,88,120,155].

li = [12,24,35,70,88,120,155]
li = [x for (i,x) in enumerate(li) if i%2!=0]
print li

11)Please write a program to shuffle and print the list [3,6,7,8].

from random import shuffle


li = [3,6,7,8]
shuffle(li)
print li

12) Check Palindrome


class Palindrome:

@staticmethod
def is_palindrome(word):
flag=0
rev1=word.lower()
rev=rev1[::-1]
#print(rev)
for i in range(len(word)-1):
if rev1[i]==rev[i]:
pass
else:
flag=1
break
if flag==1:
return False
else:
return True
13), for dictionary {'Input.txt': 'Randy', 'Code.py': 'Stan', 'Output.txt':
'Randy'} the group_by_owners function should return {'Randy': ['Input.txt',
'Output.txt'], 'Stan': ['Code.py']}.
class FileOwners:

@staticmethod
def group_by_owners(files):
file1={}
for i,j in files.items():
file1[j]=file1.get(j,[])+[i]

return file1

files = {
'Input.txt': 'Randy',
'Code.py': 'Stan',
'Output.txt': 'Randy'
}
print(FileOwners.group_by_owners(files))
print(Palindrome.is_palindrome('Deleveled'))

14) Space separated input array:


a = [int(x) for x in input().split()]
print(a)
i/p:1 2 3
o/p:[1,2,3]

15)Comma separated input array:


a = [int(x) for x in input().split(',')]
print(a)
i/p:1,2,3
o/p:[1,2,3]

16) Multi line input array


a=[]
try:
while True:
n=input()
if n!='-1':
a.append(n)
else:

break
except EOFError:
pass
print(a)

17) End the list when -1 is encountered and find odd numbers in that list.

a=[]
b=[]
try:
while True:
n=input()
if n!='-1':
a.append(n)
else:
break
except EOFError:
pass
print(a)
for i in range(len(a)-1):
if int(a[i])%2!=0:
b.append(a[i])
for j in b:
print(j)

18) Find length of a string omitting blank space,\n,\t.

a='''count
qwerty

words '''
a=a.strip()
a=a.replace(" ","")
a=a.replace("\n","")
a=a.replace("\t","")
b=len(a)
print(b)

o/p : 16

19) Swap 2 nos withou using temporary variable.


a=5
b=9
a=(a+b)
b=a-b
a=a-b
print(a)
print(b)

Вам также может понравиться