Hslip20
<?php
// Function to split an array into chunks
function splitArray($array, $chunkSize) {
return array_chunk($array, $chunkSize, true);
}
// Function to sort an array by values without changing keys
function sortArray($array) {
asort($array);
return $array;
}
// Function to filter even elements from an array
function filterEven($array) {
return array_filter($array, function($value) {
return $value % 2 == 0;
});
}
// Sample associative array
$associativeArray = array("a" => 4, "b" => 7, "c" => 2, "d" => 9, "e" => 6);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$operation = $_POST["operation"];
switch ($operation) {
case 'split':
$chunkSize = $_POST["chunkSize"];
$result = splitArray($associativeArray, $chunkSize);
break;
case 'sort':
$result = sortArray($associativeArray);
break;
case 'filter':
$result = filterEven($associativeArray);
break;
default:
$result = "Invalid operation";
break;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Associative Array Operations</title>
</head>
<body>
<h2>Associative Array Operations</h2>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<label>Select Operation:</label>
<select name="operation">
<option value="split">Split Array into Chunks</option>
<option value="sort">Sort Array by Values</option>
<option value="filter">Filter Even Elements</option>
</select>
<?php if ($operation == 'split'): ?>
<label>Chunk Size:</label>
<input type="number" name="chunkSize" value="2">
<?php endif; ?>
<input type="submit" value="Submit">
</form>
<?php
// Displaying the result
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo "<p>Original Array: " . json_encode($associativeArray) . "</p>";
echo "<p>Result: " . json_encode($result) . "</p>";
}
?>
</body>
</html>
Q2]
import numpy as np
import matplotlib.pyplot as plt
Q.2 A) Generate a random array of 50 integers
np.random.seed(42)
data = np.random.randint(1, 100, 50)
Display using line chart, scatter plot, histogram, and box plot
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
Line chart
axes[0, 0].plot(data, color='green')
axes[0, 0].set_title('Line Chart')
Scatter plot
axes[0, 1].scatter(range(1, 51), data, color='red')
axes[0, 1].set_title('Scatter Plot')
Histogram
axes[1, 0].hist(data, bins=10, color='blue', alpha=0.7)
axes[1, 0].set_title('Histogram')
Box plot
axes[1, 1].boxplot(data, vert=False)
axes[1, 1].set_title('Box Plot')
plt.tight_layout()
plt.show()
Q.2 B) Add two outliers and display the box plot
data_with_outliers = np.append(data, [150, 200])
plt.figure(figsize=(6, 4))
plt.boxplot(data_with_outliers, vert=False)
plt.title('Box Plot with Outliers')
plt.show()