:::: Python Logic Building ::::

 10 Python Interview Programs with Simple Explanations

1. First Non-Repeating Character in a String

�� Problem: Find the first character in a string that does not repeat.

Input: 'swiss'
Expected Output: 'w'

�� Without Library

# Without using libraries

a="swiss"

c={}

for char in a:

    if char in c:

        c[char]+=1

    else:

        c[char]=1

print(c)

�� With Library

# Using collections.Counter

from collections import Counter

import re

a="swiss"

count=Counter(a)

print(count)

for key,value in count.items():

    if value==1:

        print(key)

        break;

    else:

        continue

�� Explanation

We loop through each character and count how many times it appears. The first character with a count of 1 is the answer. 's' repeats, 'w' doesn't.

2. Check if Two Strings are Anagrams

�� Problem: Two strings are anagrams if they contain the same letters in a different order.

Input: 'listen', 'silent'
Expected Output: True

�� Without Library

# Without using libraries
s1 = 'listen'
s2 = 'silent'
if sorted(s1) == sorted(s2):
    print(True)
else:
    print(False)

�� With Library

# Using collections.Counter

a="listen"

b="silent"

from collections import Counter

print(f"{Counter(a)} {Counter(b)}")

if (Counter(a)==Counter(b)):

    print("anagrams")

�� Explanation

Sorting both strings or comparing their character counts shows if they have the same characters.

3. FizzBuzz

�� Problem: Print numbers from 1 to 100. If a number is divisible by 3, print 'Fizz', by 5 print 'Buzz', by both print 'FizzBuzz'.

Input: 1 to 100
Output: Fizz / Buzz / FizzBuzz

�� Without Library

for i in range(1, 101):
    if i % 3 == 0 and i % 5 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)

�� With Library

No library needed.

�� Explanation

Use modulus (%) to check divisibility and print accordingly.

4. from collections import Counter

a="This is a test this is only a test"

words=a.lower().split(" ")

print(Counter(words))

5. 

a="This is a test this is only a test"
words=a.lower().split(" ")
print(words)
dict1={}
for word in words:
    if word in dict1:
        dict1[word]+=1
    else:
        dict1[word]=1

print(dict1)
    
6. 

import random

def generaterandomClassBIP(count=5):

    for i in range(count):

        f_octet=random.randint(128,191)

        s_octet=random.randint(0,255)

        t_octet=random.randint(0,255)

        l_octet=random.randint(0,255)

    print(f"{f_octet}.{s_octet}.{t_octet}.{l_octet}")


generaterandomClassBIP()


a="""Router# show versionCisco Internetwork Operating System Software


IOS (tm) GS Software (RSP-JV-M), Experimental Version 11.1(12816)


[getchell 108]


Copyright (c) 1986-1996 by cisco Systems, Inc.


Compiled Mon 03-Jun-96 11:39 by getchell


Image text-base: 0x600108A0, data-base: 0x60910000


ROM: System Bootstrap, Version 5.3(16645) [szhang 571], INTERIM SOFTWARE


Router uptime is 4 minutes


System restarted by reload


System image file is "slot0:dirt/vip2/master/rsp-jv-mz.960603", booted via tftp from 172.18.2.3


cisco RSP2 (R4600) processor with 24576K bytes of memory.


R4600 processor, Implementation 32, Revision 2.0


Last reset from power-on


G.703/E1 software, Version 1.0.


SuperLAT software copyright 1990 by Meridian Technology Corp).


Bridging software.


X.25 software, Version 2.0, NET2, BFE and GOSIP compliant.


TN3270 Emulation software (copyright 1994 by TGV Inc).


Primary Rate ISDN software, Version 1.0.


Chassis Interface g0/0 129.0.0.1"""

import re

pattern=r"(?:\d{1,3}\.){3}\d{1,3}"

bol=re.search(pattern,a)

print(bol.group())

print(f"first matched IP is {bol.group()}")

IP1=bol.group()

octets=IP1.split(".")

f_octet=int(octets[0])

print(f"All octets are : {octets} and first octet is {f_octet}")



if f_octet in range(1,126):

    print(f"{f_octet} is in Class A")

elif f_octet in range(128,191):

    print(f"{f_octet} is in Class B")

elif f_octet in range(192,223):

    print(f"{f_octet} is in Class C")

elif f_octet in range(224,239):

    print(f"{f_octet} is in Class D")

elif f_octet in range(240,255):

    print(f"{f_octet} is in Class E")

else:

    print("Invalid Input")



allIPs=re.findall(pattern,a)

print(f"All IPs are {allIPs}, first IP is {allIPs[0]}")


for IP in allIPs:

    octets=IP.split(".")

    f_octet=int(octets[0])

    if f_octet in range(1,126):

        print("A")

    elif f_octet in range(128,191):

        print(f"{IP} is in Class B")

    elif f_octet in range(192,223):

        print("C")

    elif f_octet in range(224,239):

        print("D")

    elif f_octet in range(240,255):

        print("E")


Comments

Popular posts from this blog

TCL Interview Programs

Python Interview Programs

-: Networking interview questions :-