REGEX
As part of a forum development project, you need to ensure that usernames
meet specific criteria for validity, such as containing only alphanumeric
characters and underscores, with a required minimum and maximum length.
How would you implement a Python function using regular expressions to
validate usernames according to these rules, ensuring they adhere to the
platform's standards?
INPUT
import re
def validate_username():
username=input("Enter user name: ")
min_length = 3
max_length = 15
pattern = re.compile(f'^[\w]{{{min_length}, {max_length}}}$')
if pattern.search(username):
print("valid")
else:
print("invalid")
validate_username()
validate_username()
Output:
Enter user name:Ammu2005
invalid
Enter user name: Ammu@2005
invalid
NUMPY
In order to represent the game board in the game
you developed, you must create a grid of random
numbers. How might you use NumPy to construct
and modify this grid?
INPUT
import numpy as np
rows = 5
cols = 5
grid = np.random.randint(0, 10, size=(rows, cols))
print("Original grid:")
print(grid)
grid[2, 2] = 99
print("\nModified grid:")
print(grid)
OUTPUT
Original grid:
[[5 0 8 9 1]
[7 2 5 0 7]
[9 0 2 9 3]
[8 3 4 6 3]
[2 7 5 5 7]]
Modified grid:
[[ 5 0 8 9 1]
[ 7 2 5 0 7]
[ 9 0 99 9 3]
[ 8 3 4 6 3]
[ 2 7 5 5 7]]
NUMPY
C. Calculating the Hamming distance between two DNA
sequences is
necessary when examining genetic data. How would you do
this with
NumPy?
INPUT
arr2 = np.array(list(seq2)
distance = np.sum(arr1 != arr2)
return distance
sequence1 = "AGCTAGCT"
sequence2 = "AGCTTGCT"
distance = hamming_distance(sequence1, sequence2)import numpy as
np
def hamming_distance(seq1, seq2):
arr1 = np.array(list(seq1))
print("Hamming distance between sequences:", distance)
OUTPUT
Hamming distance between sequences: 1
Panda & matplotlib
1.You have a dataset containing information about employee
salaries, and you need to calculate the salary range (i.e., the
difference between the highest and lowest salaries). How can you
use pandas to calculate this range?
Input:
import pandas as pd
data = {'salary': [100000,50000,12000,40000,10000]}
df = pd.DataFrame(data)
max_salary = df['salary'].max()
min_salary = df['salary'].min()
salary_range = max_salary - min_salary
print(f"The salary range is: {salary_range}")
Output:
The salary range is: 90000
Maplotlib
In a bioinformatics project, you have data on gene expression
levels for different samples. How might you use Matplotlib to
create a heatmap to visualize the expression patterns across
genes and samples?
Input:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns # Seaborn is also used for better visualization
df = pd.read_csv('gene_expression.csv', index_col=0)
normalized_df = df / df.max().max()
plt.figure(figsize=(10, 8))
sns.heatmap(normalized_df, cmap='viridis')
plt.title('Gene Expression Heatmap')
plt.xlabel('Samples')
plt.ylabel('Genes')
plt.show()
Output: