100 Networking Questions
Basic Python Programming
- Write a Python program to reverse a string. (I have also added code for reverse a list)
#Reverse a string
a="Hello"
try:
print(a[::-1])
#Reverse a list
b=[1,'a',"Suchi"]
print(b[::-1])
print(b[2])
c=b[2]
print(c[::-1])
except Exception as e:
print("Error Found"+e)
Reasoning:
[::-1]
is Python slicing syntax::
refers to the entire sequence.-1
specifies the step (reverse direction).
- This avoids using loops, making the program concise and efficient.
2. Implement a function to check if a number is prime.
num=23
if num<2:
print("Not Prime")
for i in range(2,int(num**0.5)+1):
if num%i==0:
print("Not Prime")
print(num," is Prime ")
Reasoning:
- A prime number is only divisible by
1
and itself. - Checking divisors up to the square root of
n
reduces time complexity toO(√n)
.
3. Write a program to calculate the factorial of a number using recursion.
number = 90
fact = 1
for i in range(1, number + 1): # Loop from 1 to number (inclusive)
fact = fact * i # Multiply each i to fact
print("Factorial of ", number, " is ", fact) # Display the result
Reasoning:
- The factorial of
n
isn * (n-1)!
. - The base case ensures the recursion stops when
n
reaches 0 or 1.
4. Create a program to find the GCD (Greatest Common Divisor) of two numbers.
def gcd(a, b):
while b: # Use the Euclidean algorithm
a, b = b, a % b
return a
# Example usage
num1, num2 = 56, 98
print(f"GCD of {num1} and {num2}:", gcd(num1, num2))
Reasoning:
- The Euclidean algorithm works by repeatedly replacing the larger number with its remainder when divided by the smaller number.
- When the remainder becomes 0, the other number is the GCD.
def decimal_to_binary(n):
return bin(n)[2:] # Use Python's built-in bin() function and remove the '0b' prefix
# Example usage
number = 42
print(f"Binary representation of {number}:", decimal_to_binary(number))
Reasoning:
- The
bin()
function converts a decimal number to its binary representation as a string prefixed with0b
. - Slicing off the prefix gives the pure binary format.
6. Implement a program to check if a string is a palindrome.
#palindrome string
a="MadaM"
b=a[::-1]
print("value of a and b are: ",a,b)
if a==b:
print("Palindrome")
else:
print("Not Palindrome")
Reasoning:
- A palindrome reads the same forward and backward.
7. Write a program to generate the Fibonacci sequence up to
n
terms.#Fibonacci
seq=[0,1]
num=10
for num in range(2,num):
seq.append(seq[-1]+seq[-2])
print(seq)
Reasoning:
- The Fibonacci sequence starts with
0
and1
, with each subsequent term being the sum of the previous two. - Using a loop and list ensures clarity and efficiency.
8. Create a program to find the largest and smallest numbers in a list.
#largest and smallest number in a list
a=[1,2,90,76,3,0]
print(min(a))
print(max(a))
#without using builtin function :
mini=a[0]
maxi=a[0]
for num in a:
if num<mini:
mini=num
if num>maxi:
maxi=num
print(mini,maxi)
Reasoning:
- Python's
min()
andmax()
functions efficiently find the smallest and largest elements in a list.
9. Write a Python program to count the frequency of each character in a string.
#freq of each character
from collections import Counter
string="Nokia"
print(dict(Counter(string)))
Reasoning:
- The
Counter
class from thecollections
module efficiently counts occurrences of elements in a string.
10. Implement a function to check if two strings are anagrams.
#anagrams
a="mena"
b="name"
print(sorted(a.lower())==sorted(b.lower()))
Reasoning:
- Anagrams contain the same characters in different orders.
- Converting to lowercase and sorting ensures a case-insensitive comparison.
Data Structures
- Write a program to implement a stack using a list.
- Implement a queue using a list in Python.
- Create a Python program to perform binary search on a sorted list.
- Write a program to find the sum of elements in a matrix.
- Implement a program to transpose a matrix.
- Write a Python program to find the maximum depth of a binary tree.
- Implement a function to perform in-order traversal of a binary tree.
- Write a program to merge two sorted linked lists.
- Implement a function to detect a cycle in a linked list.
- Create a Python program to sort a list using the merge sort algorithm.
Algorithms
- Write a Python program to implement the bubble sort algorithm.
- Implement the quicksort algorithm in Python.
- Write a program to find the shortest path in a graph using Dijkstra’s algorithm.
- Create a Python program to implement the Knapsack problem.
- Implement a function to find all subsets of a given set.
- Write a program to find the longest common subsequence of two strings.
- Implement a function to calculate the edit distance (Levenshtein distance) between two strings.
- Write a program to solve the N-Queens problem.
- Implement a function to find the minimum spanning tree of a graph using Kruskal’s algorithm.
- Write a program to perform depth-first search (DFS) and breadth-first search (BFS) on a graph.
Networking and Automation
- Write a program to check if a specific port on a server is open.
- Implement a script to ping a list of IP addresses and display the reachable ones.
- Write a program to fetch the MAC address of a device using Python.
- Create a Python script to establish an SSH connection and execute a command on a remote server.
- Write a program to simulate a simple DHCP server using Python.
- Implement a program to parse and display OSPF or BGP configuration from a router.
- Write a script to monitor network traffic using Python (e.g., with
scapy
). - Create a program to check if a specific website is reachable (HTTP status).
- Implement a script to retrieve and parse SNMP data from a network device.
- Write a Python program to generate random passwords for network devices.
String Manipulation
- Write a program to remove duplicates from a string.
- Implement a function to find all permutations of a string.
- Write a Python program to count the number of vowels and consonants in a string.
- Create a program to find the first non-repeating character in a string.
- Implement a program to reverse words in a given sentence.
- Write a function to check if a string contains only unique characters.
- Implement a Python program to convert camel case to snake case and vice versa.
- Write a program to find the longest palindrome substring in a string.
- Implement a function to compress a string using the counts of repeated characters.
- Write a Python program to sort a string alphabetically.
File Handling
- Write a program to read a file and count the number of lines and words.
- Create a script to merge two files into one.
- Write a Python program to find duplicate lines in a file.
- Implement a function to search for a specific string in a file.
- Write a script to read and parse a CSV file in Python.
- Create a program to write a list of dictionaries to a JSON file.
- Write a script to monitor changes in a log file in real-time.
- Implement a program to count the number of occurrences of each word in a file.
- Write a script to find and replace a string in a text file.
- Create a program to split a large file into smaller chunks.
Mathematical Problems
- Write a program to check if a number is an Armstrong number.
- Implement a function to find the LCM (Least Common Multiple) of two numbers.
- Write a program to find the sum of the digits of a number.
- Create a program to generate all prime numbers up to
n
. - Write a program to check if a number is a perfect square.
- Implement a Python program to calculate the power of a number without using
**
. - Write a program to find the roots of a quadratic equation.
- Implement a Python program to calculate the area of different shapes (circle, triangle, rectangle).
- Write a program to convert Roman numerals to integers.
- Create a Python program to find the sum of all prime factors of a number.
Advanced Topics
- Write a Python script to create and manipulate threads.
- Implement a program to demonstrate inter-thread communication.
- Create a Python program to implement a producer-consumer problem using threads.
- Write a program to serialize and deserialize Python objects using
pickle
. - Implement a Python program to schedule tasks using
schedule
orapscheduler
. - Write a program to simulate a REST API client in Python.
- Create a Python script to parse and display XML data.
- Write a program to solve the Tower of Hanoi problem.
- Implement a Python script to encrypt and decrypt data using the
cryptography
library. - Write a Python program to create a simple chatbot using NLP libraries.
Machine Learning and Data
- Write a program to perform linear regression using Python.
- Create a script to read and visualize data using
matplotlib
. - Write a program to cluster data using the K-means algorithm.
- Implement a script to process and clean raw data using
pandas
. - Write a program to predict house prices using a sample dataset.
- Create a Python script to analyze text sentiment using
TextBlob
. - Write a program to perform image classification using a pre-trained model (e.g., ResNet).
- Implement a Python script to scrape data from a website.
- Write a program to calculate the confusion matrix for a classification model.
- Create a Python script to automate Excel operations using
openpyxl
.
Miscellaneous
- Write a program to simulate a simple calculator.
- Implement a function to check if a number is a power of two.
- Write a Python script to generate a QR code for a given URL.
- Create a program to display the current weather using an API.
- Implement a script to fetch emails from an inbox using IMAP.
- Write a program to schedule and send emails automatically using Python.
- Create a program to scrape trending topics from Twitter using
tweepy
. - Write a script to compress and decompress files using Python.
- Implement a program to generate and validate a UUID.
- Create a Python program to simulate a simple game like Tic-Tac-Toe or Hangman.
- Fetch only all elements of a nested list to another list.
result = [] # Initialize an empty list 'result' to store the extracted elements
for element in a: # Iterate over each element in the list 'a'
if isinstance(element, list): # Check if the current element is a list
result.extend(element) # If it is a list, extend 'result' with the elements of the current list
else: # If the current element is not a list
result.append(element) # Append the current element to 'result'
print(result) # Print the resulting list 'result'
Comments
Post a Comment