Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions apartment_allocation.pyt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def apartment_allocation(n, m, k, applicants, apartments):
applicants.sort()
apartments.sort()
i = j = count = 0

while i < n and j < m:
if abs(applicants[i] - apartments[j]) <= k:
count += 1
i += 1
j += 1
elif apartments[j] < applicants[i] - k:
j += 1
else:
i += 1
return count

# Contoh penggunaan
n, m, k = 4, 3, 5
applicants = [60, 45, 80, 60]
apartments = [30, 60, 75]
print(apartment_allocation(n, m, k, applicants, apartments)) # Output: 2
30 changes: 30 additions & 0 deletions palindrome.pyt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def process_palindrome_queries(n, m, s, operations):
from bisect import bisect_left

s = list(s)

result = []
for op in operations:
parts = op.split()
if parts[0] == '1':
_, k, x = parts
s[int(k) - 1] = x
elif parts[0] == '2':
_, a, b = parts
a, b = int(a) - 1, int(b) - 1
substring = s[a:b+1]
result.append('1' if substring == substring[::-1] else '0')
return result

# Contoh penggunaan
n, m = 7, 5
s = "aybabtu"
operations = [
"2 3 5",
"1 3 x",
"2 3 5",
"1 5 x",
"2 3 5"
]
results = process_palindrome_queries(n, m, s, operations)
print("\n".join(results)) # Output: 1 0 1
17 changes: 17 additions & 0 deletions tower.pyt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def tower_of_hanoi(n, source=1, target=3, auxiliary=2, moves=None):
if moves is None:
moves = []
if n == 1:
moves.append((source, target))
else:
tower_of_hanoi(n-1, source, auxiliary, target, moves)
moves.append((source, target))
tower_of_hanoi(n-1, auxiliary, target, source, moves)
return moves

# Contoh penggunaan
n = 2
moves = tower_of_hanoi(n)
print(len(moves))
for move in moves:
print(move[0], move[1])
42 changes: 42 additions & 0 deletions tsa_php/src/ApartmentAllocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,48 @@ function solveApartment($A, $B, $N, $M, $K) {
*/

// YOUR CODE HERE
<?php

function allocateApartments() {
// Prompt user for inputs
echo "Enter number of applicants (n): ";
$n = (int)trim(fgets(STDIN));

echo "Enter number of apartments (m): ";
$m = (int)trim(fgets(STDIN));

echo "Enter maximum allowed difference (k): ";
$k = (int)trim(fgets(STDIN));

echo "Enter desired apartment sizes for $n applicants (space-separated):\n";
$a = array_map('intval', explode(' ', trim(fgets(STDIN))));

echo "Enter available apartment sizes for $m apartments (space-separated):\n";
$b = array_map('intval', explode(' ', trim(fgets(STDIN))));

// Sort both arrays
sort($a);
sort($b);

$i = 0;
$j = 0;
$count = 0;

while ($i < $n && $j < $m) {
if (abs($a[$i] - $b[$j]) <= $k) {
$count++;
$i++;
$j++;
} elseif ($b[$j] < $a[$i] - $k) {
$j++;
} else {
$i++;
}
}

echo "Number of applicants who got an apartment: $count\n";
}

allocateApartments();
return $matches;
}
86 changes: 81 additions & 5 deletions tsa_php/src/PalindromeQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ public function __construct($n) {
* @param int $value The value to update at the specified index.
*/
public function update($index, $value) {
// YOUR CODE HERE
$index += $this->n;
$this->tree[$index] = $value;
while ($index > 1) {
$index >>= 1;
$this->tree[$index] = ($this->tree[$index * 2] + $this->tree[$index * 2 + 1]) % MOD;
}
}

/**
Expand All @@ -32,7 +37,15 @@ public function update($index, $value) {
* @return int The sum of values in the range [l, r].
*/
public function query($l, $r) {
// YOUR CODE HERE
$l += $this->n;
$r += $this->n + 1;
$result = 0;
while ($l < $r) {
if ($l % 2 == 1) $result = ($result + $this->tree[$l++]) % MOD;
if ($r % 2 == 1) $result = ($result + $this->tree[--$r]) % MOD;
$l >>= 1;
$r >>= 1;
}
return $result;
}
}
Expand All @@ -43,7 +56,10 @@ public function query($l, $r) {
* @return array An array where hashPower[i] = HASH^i % MOD.
*/
function initializeHashPowers($length) {
// YOUR CODE HERE
$hashPower = [1];
for ($i = 1; $i <= $length; $i++) {
$hashPower[$i] = ($hashPower[$i - 1] * HASH) % MOD;
}
return $hashPower;
}

Expand All @@ -55,7 +71,17 @@ function initializeHashPowers($length) {
* @return array An associative array containing the forward and backward hash segment trees.
*/
function initializeHashTables($n, $s, $hashPower) {
// YOUR CODE HERE
$fwdHash = new HashSegmentTree($n);
$bckHash = new HashSegmentTree($n);

for ($i = 0; $i < $n; $i++) {
$val = (ord($s[$i]) * $hashPower[$i]) % MOD;
$fwdHash->update($i, $val);

$revVal = (ord($s[$i]) * $hashPower[$n - 1 - $i]) % MOD;
$bckHash->update($n - 1 - $i, $revVal);
}

return ['fwdHash' => $fwdHash, 'bckHash' => $bckHash];
}

Expand All @@ -67,9 +93,59 @@ function initializeHashTables($n, $s, $hashPower) {
* @return array An array of results for palindrome queries ("YES" or "NO").
*/
function processOperations($n, $operations, $s) {
// YOUR CODE HERE
$hashPower = initializeHashPowers($n);
$trees = initializeHashTables($n, $s, $hashPower);
$fwd = $trees['fwdHash'];
$bck = $trees['bckHash'];
$sArr = str_split($s);
$results = [];

foreach ($operations as $op) {
if ($op[0] == 1) {
// Update operation
$k = $op[1] - 1;
$x = $op[2];
$sArr[$k] = $x;
$fwd->update($k, (ord($x) * $hashPower[$k]) % MOD);
$bck->update($n - 1 - $k, (ord($x) * $hashPower[$n - 1 - $k]) % MOD);
} else if ($op[0] == 2) {
// Query operation
$a = $op[1] - 1;
$b = $op[2] - 1;
$fwdHash = $fwd->query($a, $b);
$bckHash = $bck->query($n - 1 - $b, $n - 1 - $a);

if ($a <= $b) {
$leftShift = $n - 1 - $b - $a;
$fwdHash = ($fwdHash * $hashPower[$leftShift]) % MOD;
}

$results[] = ($fwdHash == $bckHash) ? "YES" : "NO";
}
}

return $results;
}

// Read input
fscanf(STDIN, "%d %d", $n, $m);
$s = trim(fgets(STDIN));

// Read m operations
$operations = [];
for ($i = 0; $i < $m; $i++) {
$line = explode(' ', trim(fgets(STDIN)));
$type = (int)$line[0];
if ($type == 1) {
$operations[] = [1, (int)$line[1], $line[2]];
} else {
$operations[] = [2, (int)$line[1], (int)$line[2]];
}
}

// Process and print results
$results = processOperations($n, $operations, $s);
foreach ($results as $res) {
echo $res . "\n";
}
?>
17 changes: 16 additions & 1 deletion tsa_php/src/TowerOfHanoi.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
function moveDisk($diskNumber, &$moves, $sourceStack, $destinationStack, $auxiliaryStack)
{
// YOUR CODE HERE
if ($diskNumber == 1) {
$moves[] = [$sourceStack, $destinationStack];
return;
}

moveDisk($diskNumber - 1, $moves, $sourceStack, $auxiliaryStack, $destinationStack);
$moves[] = [$sourceStack, $destinationStack];
moveDisk($diskNumber - 1, $moves, $auxiliaryStack, $destinationStack, $sourceStack);
}

/**
Expand All @@ -23,6 +31,13 @@ function moveDisk($diskNumber, &$moves, $sourceStack, $destinationStack, $auxili
function towerOfHanoi($numberOfDisks)
{
// YOUR CODE HERE
$moves = [];
moveDisk($numberOfDisks, $moves, 1, 3, 2);

echo count($moves) . "\n";
foreach ($moves as [$from, $to]) {
echo "$from $to\n";
}
}

?>
?>