---
title: "Assignment_1"
author: "Manan Patel"
date: "2026-02-25"
output: word_document
---


# TASK 1A – Generate Production Data (Nested Loops)

set.seed(123)

BaseOutput <- 50

Sol_vec <- c()
Station_vec <- c()
Team_vec <- c()
Output_vec <- c()

for (sol in 1:30) {
  for (station in 1:6) {
    for (team in 1:4) {
      
      RandomNoise <- sample(-5:5, 1)
      
      Output_value <- BaseOutput + (station * 3) + (team * 2) + RandomNoise
      
      Sol_vec <- c(Sol_vec, sol)
      Station_vec <- c(Station_vec, station)
      Team_vec <- c(Team_vec, team)
      Output_vec <- c(Output_vec, Output_value)
    }
  }
}

production_data <- data.frame(
  Sol = Sol_vec,
  Station = Station_vec,
  Team = Team_vec,
  Output = Output_vec
)

head(production_data, 10)



# TASK 1B – Total Production per Station

Station_list <- c()
TotalOutput_list <- c()

for (station in 1:6) {
  
  total_output <- 0
  
  for (i in 1:nrow(production_data)) {
    
    if (production_data$Station[i] == station) {
      total_output <- total_output + production_data$Output[i]
    }
  }
  
  Station_list <- c(Station_list, station)
  TotalOutput_list <- c(TotalOutput_list, total_output)
}

station_totals <- data.frame(
  Station = Station_list,
  TotalOutput = TotalOutput_list
)

station_totals

# TASK 1C – Detect Production Anomalies

Sol_list <- c()
Station_list <- c()
Team_list <- c()
Output_list <- c()
Anomaly_list <- c()

for (sol in 1:30) {
  for (station in 1:6) {
    
    station_total <- 0
    
    for (i in 1:nrow(production_data)) {
      if (production_data$Sol[i] == sol &&
          production_data$Station[i] == station) {
        station_total <- station_total + production_data$Output[i]
      }
    }
    
    station_avg <- station_total / 4
    
    for (i in 1:nrow(production_data)) {
      if (production_data$Sol[i] == sol &&
          production_data$Station[i] == station) {
        
        team_output <- production_data$Output[i]
        
        deviation <- abs(team_output - station_avg) / station_avg
        
        if (deviation > 0.10) {
          
          Sol_list <- c(Sol_list, sol)
          Station_list <- c(Station_list, station)
          Team_list <- c(Team_list, production_data$Team[i])
          Output_list <- c(Output_list, team_output)
          Anomaly_list <- c(Anomaly_list, TRUE)
        }
      }
    }
  }
}

anomalies <- data.frame(
  Sol = Sol_list,
  Station = Station_list,
  Team = Team_list,
  Output = Output_list,
  Anomaly = Anomaly_list
)

head(anomalies, 10)

# TASK 1D – Identify Peak Productivity Days

Peak_Station <- c()
Peak_Sol <- c()
Peak_TotalOutput <- c()

for (station in 1:6) {
  
  max_output <- -Inf
  max_sol <- 0
  
  for (sol in 1:30) {
    
    daily_total <- 0
    
    for (i in 1:nrow(production_data)) {
      
      if (production_data$Station[i] == station &&
          production_data$Sol[i] == sol) {
        
        daily_total <- daily_total + production_data$Output[i]
      }
    }
    
    if (daily_total > max_output) {
      max_output <- daily_total
      max_sol <- sol
    }
  }
  
  Peak_Station <- c(Peak_Station, station)
  Peak_Sol <- c(Peak_Sol, max_sol)
  Peak_TotalOutput <- c(Peak_TotalOutput, max_output)
}

peak_days <- data.frame(
  Station = Peak_Station,
  Sol = Peak_Sol,
  TotalOutput = Peak_TotalOutput
)

peak_days

# TASK 1E – Efficiency Score

EfficiencyScore_vec <- c()

for (i in 1:nrow(production_data)) {
  
  efficiency_value <- production_data$Output[i] /
    (production_data$Station[i] +
       production_data$Team[i])
  
  EfficiencyScore_vec <- c(EfficiencyScore_vec, efficiency_value)
}

production_data$EfficiencyScore <- EfficiencyScore_vec

head(production_data, 10)

















