Category Archives: Computer Programming

Using Python to find the Needle in the Haystack

This is in continuation of my earlier article:

https://chandrahasblogs.wordpress.com/2019/12/08/finding-a-needle-in-a-haystack/

Given below is the Python code to solve the given problem.

def prem_num():

lst = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

mob_num = input(“Your 10 digit mobile number: “)

dgt = 0

while (dgt < 10):

lst[dgt] = int(mob_num[dgt])

dgt += 1

stp = 1

while (stp <= 10):

noChange = True

dgt = 0

while (dgt < 10):

cnt = 0

pos = 0

while (pos < 10):

if (dgt == lst[pos]):

cnt += 1

pos += 1

if (lst[dgt] != cnt):

lst[dgt] = cnt

noChange = False

dgt += 1

print(lst)

if(noChange):

break

stp += 1

prem_num()