0% found this document useful (0 votes)
3 views

HackAPTS - Challenges

The document outlines three programming challenges: a String Command Processor that processes nested commands to transform text, an Emergency Medical Supply Distribution system that optimizes delivery routes for hospitals, and a Dynamic Bus Route Optimizer that adjusts bus routes based on demand and traffic. Each challenge includes input and output formats, examples, and constraints. Additionally, code stubs in various programming languages are provided for each challenge.

Uploaded by

gowthamiseelam2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

HackAPTS - Challenges

The document outlines three programming challenges: a String Command Processor that processes nested commands to transform text, an Emergency Medical Supply Distribution system that optimizes delivery routes for hospitals, and a Dynamic Bus Route Optimizer that adjusts bus routes based on demand and traffic. Each challenge includes input and output formats, examples, and constraints. Additionally, code stubs in various programming languages are provided for each challenge.

Uploaded by

gowthamiseelam2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

CHALLENGES

1. String Command Processor


Title: String Command Processor

Description:
Process a string containing nested commands to transform text based on the following rules:

 {R:text}: Reverse the text.

 {A:text}: Alternate the case of each character, starting with uppercase.

 {D:n:text}: Duplicate each character n times.

Commands are processed from innermost to outermost. If the input contains invalid syntax,
output "Error".

Input Format:

 A single line containing the command string.

Output Format:

 A single line: the transformed text or "Error".

Examples:

1. Input: {R:{A:hello}}
Output: OlLeH
Explanation:

o {A:hello} → HeLlO

o {R:HeLlO} → OlLeH

2. Input: {D:2:{R:code}}
Output: eeddoocc
Explanation:

o {R:code} → edoc

o {D:2:edoc} → eeddoocc

3. Input (Error Case): {R:{D:2:code}


Output: Error

Constraints:

 1 ≤ length of string ≤ 1000

 Commands: {R:text}, {A:text}, {D:n:text}

 For {D:n:text}, 1 ≤ n ≤ 5

 Maximum nesting depth: 3

 Input contains only valid ASCII characters.


2. Emergency Medical Supply Distribution
Title: Emergency Medical Supply Distribution

Description:
Optimize the delivery of medical supplies from a warehouse to hospitals based on their
locations, supply needs, and priority.
Hospitals with higher priority must be served first, and the delivery route must minimize travel
time. If constraints are violated, output "Error".

Input Format:

1. An integer n (number of hospitals).

2. n lines, each containing: x y supplies priority (space-separated).

3. An integer m (number of vehicles).

4. m lines, each containing: capacity speed (space-separated).

Output Format:

1. A single line: route (space-separated hospital indices in the order of visits).

2. A single line: times (space-separated delivery times in minutes).

3. "Error" in case of constraint violations.

Examples:

1. Input:
3
0 0 100 1
10 10 50 2
20 20 75 2
1
250 40

Output:
012
30 60 105

2. Input:
4
0 0 50 1
5 5 50 1
10 10 50 2
15 15 50 2
1
250 50

Output:
0123
15 30 55 85
3. Input (Error Case):
2
0 0 300 1
10 10 200 2
1
250 40

Output:
Error

Constraints:

 1 ≤ n ≤ 50 (number of hospitals).

 1 ≤ m ≤ 10 (number of vehicles).

 1 ≤ priority ≤ 5 (1 is highest).

 0 ≤ x, y ≤ 1000 (coordinates).

 1 ≤ supplies ≤ 1000.

 1 ≤ vehicle speed ≤ 100.

3. Dynamic Bus Route Optimizer


Title: Dynamic Bus Route Optimizer

Description:
Optimize bus routes between cities based on passenger demand, traffic patterns, and time-
based variations. The goal is to minimize travel time while maximizing passenger service. If
constraints are violated, output "Error".

Input Format:

1. An integer n (number of cities).

2. n lines, each containing: city_id daily_passengers (space-separated).

3. An integer m (number of routes).

4. m lines, each containing: city1 city2 distance peak_factor (space-separated).

Output Format:

1. r lines: each line contains route (space-separated city indices).

2. r lines: each line contains buses (space-separated bus counts per time slot).

3. "Error" in case of constraint violations.

Examples:

1. Input:
3
0 1000
1 800
2 600
2
0 1 100 1.2
1 2 120 1.3

Output:
012
468

2. Input:
4
0 1500
1 1000
2 800
3 600
3
0 1 80 1.3
1 2 120 1.4
2 3 90 1.2

Output:
012
6 8 10
123
468

3. Input (Error Case):


3
0 5000
1 1000
2 800
1
0 1 2000 1.5

Output:
Error

Constraints:

 5 ≤ n ≤ 30 (number of cities).

 1 ≤ daily passengers ≤ 10,000.

 1 ≤ distance ≤ 1000.

 1.0 ≤ traffic peak factor ≤ 3.0.

 Bus capacity = 50 passengers.

 Maximum route duration = 12 hours.


CODE STUBS
Challenge-1:
C

#include <stdio.h>

void process_string(char* input) {


// Your code goes here
}

int main() {
char input[1001];
scanf("%1000[^\n]", input);
process_string(input);
return 0;
}

C++

#include <iostream>
#include <string>
using namespace std;

void processString(const string& input) {


// Your code goes here
}

int main() {
string input;
getline(cin, input);
processString(input);
return 0;
}

C#

using System;

class Program {
static void ProcessString(string input) {
// Your code goes here
}

static void Main() {


string input = Console.ReadLine();
ProcessString(input);
}
}

Python

def process_string(input_string):
# Your code goes here

if __name__ == "__main__":
input_string = input().strip()
process_string(input_string)
Java

import java.util.Scanner;

public class Main {


public static void processString(String input) {
// Your code goes here
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
processString(input);
sc.close();
}
}

JavaScript

function processString(input) {
// Your code goes here
}

const input = require('fs').readFileSync(0, 'utf-8').trim();


processString(input);

Go

package main

import (
"bufio"
"fmt"
"os"
"strings"
)

func processString(input string) {


// Your code goes here
}

func main() {
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
processString(strings.TrimSpace(input))
}

Ruby

def process_string(input)
# Your code goes here
end

input = gets.chomp
process_string(input)
TypeScript

function processString(input: string): void {


// Your code goes here
}

const input = require('fs').readFileSync(0, 'utf-8').trim();


processString(input);

Challenge-2:
C

#include <stdio.h>

void optimize_distribution(int n, int m, int hospitals[][4], int vehicles[][2]) {


// Your code goes here
}

int main() {
int n, m;
scanf("%d", &n);
int hospitals[n][4];
for (int i = 0; i < n; i++) {
scanf("%d %d %d %d", &hospitals[i][0], &hospitals[i][1], &hospitals[i][2],
&hospitals[i][3]);
}
scanf("%d", &m);
int vehicles[m][2];
for (int i = 0; i < m; i++) {
scanf("%d %d", &vehicles[i][0], &vehicles[i][1]);
}
optimize_distribution(n, m, hospitals, vehicles);
return 0;
}

C++

#include <iostream>
#include <vector>
using namespace std;

void optimizeDistribution(int n, int m, vector<vector<int>>& hospitals, vector<vector<int>>&


vehicles) {
// Your code goes here
}

int main() {
int n, m;
cin >> n;
vector<vector<int>> hospitals(n, vector<int>(4));
for (int i = 0; i < n; i++) {
cin >> hospitals[i][0] >> hospitals[i][1] >> hospitals[i][2] >> hospitals[i][3];
}
cin >> m;
vector<vector<int>> vehicles(m, vector<int>(2));
for (int i = 0; i < m; i++) {
cin >> vehicles[i][0] >> vehicles[i][1];
}
optimizeDistribution(n, m, hospitals, vehicles);
return 0;
}
C#

using System;
using System.Collections.Generic;

class Program {
static void OptimizeDistribution(int n, int m, List<int[]> hospitals, List<int[]> vehicles)
{
// Your code goes here
}

static void Main() {


int n = int.Parse(Console.ReadLine());
var hospitals = new List<int[]>();
for (int i = 0; i < n; i++) {
string[] input = Console.ReadLine().Split();
hospitals.Add(Array.ConvertAll(input, int.Parse));
}
int m = int.Parse(Console.ReadLine());
var vehicles = new List<int[]>();
for (int i = 0; i < m; i++) {
string[] input = Console.ReadLine().Split();
vehicles.Add(Array.ConvertAll(input, int.Parse));
}
OptimizeDistribution(n, m, hospitals, vehicles);
}
}

Python

def optimize_distribution(n, m, hospitals, vehicles):


# Your code goes here

if __name__ == "__main__":
n = int(input().strip())
hospitals = [list(map(int, input().split())) for _ in range(n)]
m = int(input().strip())
vehicles = [list(map(int, input().split())) for _ in range(m)]
optimize_distribution(n, m, hospitals, vehicles)

Java

import java.util.*;

public class Main {


public static void optimizeDistribution(int n, int m, int[][] hospitals, int[][] vehicles) {
// Your code goes here
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] hospitals = new int[n][4];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
hospitals[i][j] = sc.nextInt();
}
}
int m = sc.nextInt();
int[][] vehicles = new int[m][2];
for (int i = 0; i < m; i++) {
for (int j = 0; j < 2; j++) {
vehicles[i][j] = sc.nextInt();
}
}
optimizeDistribution(n, m, hospitals, vehicles);
sc.close();
}
}

JavaScript

function optimizeDistribution(n, m, hospitals, vehicles) {


// Your code goes here
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf-8').trim().split('\n');
const n = parseInt(input[0], 10);
const hospitals = input.slice(1, n + 1).map(line => line.split(' ').map(Number));
const m = parseInt(input[n + 1], 10);
const vehicles = input.slice(n + 2).map(line => line.split(' ').map(Number));
optimizeDistribution(n, m, hospitals, vehicles);

Go

package main

import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)

func optimizeDistribution(n, m int, hospitals [][]int, vehicles [][]int) {


// Your code goes here
}

func main() {
reader := bufio.NewReader(os.Stdin)
var n int
fmt.Scanln(&n)
hospitals := make([][]int, n)
for i := 0; i < n; i++ {
line, _ := reader.ReadString('\n')
parts := strings.Fields(line)
hospitals[i] = make([]int, 4)
for j := range parts {
hospitals[i][j], _ = strconv.Atoi(parts[j])
}
}
var m int
fmt.Scanln(&m)
vehicles := make([][]int, m)
for i := 0; i < m; i++ {
line, _ := reader.ReadString('\n')
parts := strings.Fields(line)
vehicles[i] = make([]int, 2)
for j := range parts {
vehicles[i][j], _ = strconv.Atoi(parts[j])
}
}
optimizeDistribution(n, m, hospitals, vehicles)
}

Ruby

def optimize_distribution(n, m, hospitals, vehicles)


# Your code goes here
end

n = gets.to_i
hospitals = Array.new(n) { gets.split.map(&:to_i) }
m = gets.to_i
vehicles = Array.new(m) { gets.split.map(&:to_i) }
optimize_distribution(n, m, hospitals, vehicles)

TypeScript

function optimizeDistribution(n: number, m: number, hospitals: number[][], vehicles:


number[][]): void {
// Your code goes here
}

const input = require('fs').readFileSync(0, 'utf-8').trim().split('\n');


const n = parseInt(input[0], 10);
const hospitals = input.slice(1, n + 1).map(line => line.split(' ').map(Number));
const m = parseInt(input[n + 1], 10);
const vehicles = input.slice(n + 2).map(line => line.split(' ').map(Number));
optimizeDistribution(n, m, hospitals, vehicles);

Challenge-3:
C

#include <stdio.h>

void optimize_bus_routes(int n, int m, int cities[][2], int routes[][4]) {


// Your code goes here
}

int main() {
int n, m;
scanf("%d", &n);
int cities[n][2];
for (int i = 0; i < n; i++) {
scanf("%d %d", &cities[i][0], &cities[i][1]);
}
scanf("%d", &m);
int routes[m][4];
for (int i = 0; i < m; i++) {
scanf("%d %d %d %f", &routes[i][0], &routes[i][1], &routes[i][2], &routes[i][3]);
}
optimize_bus_routes(n, m, cities, routes);
return 0;
}
C++

#include <iostream>
#include <vector>
using namespace std;

void optimizeBusRoutes(int n, int m, vector<vector<int>>& cities, vector<vector<float>>& routes)


{
// Your code goes here
}

int main() {
int n, m;
cin >> n;
vector<vector<int>> cities(n, vector<int>(2));
for (int i = 0; i < n; i++) {
cin >> cities[i][0] >> cities[i][1];
}
cin >> m;
vector<vector<float>> routes(m, vector<float>(4));
for (int i = 0; i < m; i++) {
cin >> routes[i][0] >> routes[i][1] >> routes[i][2] >> routes[i][3];
}
optimizeBusRoutes(n, m, cities, routes);
return 0;
}

C#

using System;
using System.Collections.Generic;

class Program {
static void OptimizeBusRoutes(int n, int m, List<int[]> cities, List<float[]> routes) {
// Your code goes here
}

static void Main() {


int n = int.Parse(Console.ReadLine());
var cities = new List<int[]>();
for (int i = 0; i < n; i++) {
string[] input = Console.ReadLine().Split();
cities.Add(Array.ConvertAll(input, int.Parse));
}
int m = int.Parse(Console.ReadLine());
var routes = new List<float[]>();
for (int i = 0; i < m; i++) {
string[] input = Console.ReadLine().Split();
routes.Add(Array.ConvertAll(input, float.Parse));
}
OptimizeBusRoutes(n, m, cities, routes);
}
}

Python

def optimize_bus_routes(n, m, cities, routes):


# Your code goes here

if __name__ == "__main__":
n = int(input().strip())
cities = [list(map(int, input().split())) for _ in range(n)]
m = int(input().strip())
routes = [list(map(float, input().split())) for _ in range(m)]
optimize_bus_routes(n, m, cities, routes)

Java

import java.util.*;

public class Main {


public static void optimizeBusRoutes(int n, int m, int[][] cities, float[][] routes) {
// Your code goes here
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] cities = new int[n][2];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2; j++) {
cities[i][j] = sc.nextInt();
}
}
int m = sc.nextInt();
float[][] routes = new float[m][4];
for (int i = 0; i < m; i++) {
for (int j = 0; j < 4; j++) {
routes[i][j] = sc.nextFloat();
}
}
optimizeBusRoutes(n, m, cities, routes);
sc.close();
}
}

JavaScript

function optimizeBusRoutes(n, m, cities, routes) {


// Your code goes here
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf-8').trim().split('\n');
const n = parseInt(input[0], 10);
const cities = input.slice(1, n + 1).map(line => line.split(' ').map(Number));
const m = parseInt(input[n + 1], 10);
const routes = input.slice(n + 2).map(line => line.split(' ').map(Number));
optimizeBusRoutes(n, m, cities, routes);

Go

package main

import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)

func optimizeBusRoutes(n, m int, cities [][]int, routes [][]float64) {


// Your code goes here
}

func main() {
reader := bufio.NewReader(os.Stdin)
var n int
fmt.Scanln(&n)
cities := make([][]int, n)
for i := 0; i < n; i++ {
line, _ := reader.ReadString('\n')
parts := strings.Fields(line)
cities[i] = make([]int, 2)
for j := range parts {
cities[i][j], _ = strconv.Atoi(parts[j])
}
}
var m int
fmt.Scanln(&m)
routes := make([][]float64, m)
for i := 0; i < m; i++ {
line, _ := reader.ReadString('\n')
parts := strings.Fields(line)
routes[i] = make([]float64, 4)
for j := range parts {
routes[i][j], _ = strconv.ParseFloat(parts[j], 64)
}
}
optimizeBusRoutes(n, m, cities, routes)
}

Ruby

def optimize_bus_routes(n, m, cities, routes)


# Your code goes here
end

n = gets.to_i
cities = Array.new(n) { gets.split.map(&:to_i) }
m = gets.to_i
routes = Array.new(m) { gets.split.map(&:to_f) }
optimize_bus_routes(n, m, cities, routes)

TypeScript

function optimizeBusRoutes(n: number, m: number, cities: number[][], routes: number[][]): void {


// Your code goes here
}

const input = require('fs').readFileSync(0, 'utf-8').trim().split('\n');


const n = parseInt(input[0], 10);
const cities = input.slice(1, n + 1).map(line => line.split(' ').map(Number));
const m = parseInt(input[n + 1], 10);
const routes = input.slice(n + 2).map(line => line.split(' ').map(Number));
optimizeBusRoutes(n, m, cities, routes);

You might also like