1. ID Generation

We first generate all possible 3-digit IDs using digits 0 to 3.

# Generate all combinations of 3 digits (0-3)
ids <- expand.grid(0:3, 0:3, 0:3)
# Combine digits into string IDs
id_list <- apply(ids, 1, paste0, collapse = "")
# Display all generated IDs
id_list
##  [1] "000" "100" "200" "300" "010" "110" "210" "310" "020" "120" "220" "320"
## [13] "030" "130" "230" "330" "001" "101" "201" "301" "011" "111" "211" "311"
## [25] "021" "121" "221" "321" "031" "131" "231" "331" "002" "102" "202" "302"
## [37] "012" "112" "212" "312" "022" "122" "222" "322" "032" "132" "232" "332"
## [49] "003" "103" "203" "303" "013" "113" "213" "313" "023" "123" "223" "323"
## [61] "033" "133" "233" "333"
length(id_list)
## [1] 64
check_permission <- function(id){
  d1 <- as.numeric(substr(id,1,1))
  d2 <- as.numeric(substr(id,2,2))
  d3 <- as.numeric(substr(id,3,3))
  
  # Rule 1: No repeated digits
  if(d1 == d2 || d2 == d3 || d1 == d3) return(FALSE)
  # Rule 2: Sum of digits must be greater than 2
  if((d1 + d2 + d3) <= 2) return(FALSE)
  # Rule 3: No increasing consecutive digits
  if(d1 + 1 == d2 && d2 + 1 == d3) return(FALSE)
  # Rule 4: Last digit cannot be 0
  if(d3 == 0) return(FALSE)
  # Rule 5: No decreasing consecutive digits
  if(d1 - 1 == d2 && d2 - 1 == d3) return(FALSE)
  
  return(TRUE)
}

Test examples

check_permission("132")  # TRUE
## [1] TRUE
check_permission("123")  # FALSE
## [1] FALSE
check_permission("231")  # TRUE
## [1] TRUE
check_permission("111")  # FALSE
## [1] FALSE

Apply permission function to all generated IDs

result <- sapply(id_list, check_permission)
# Separate valid and invalid IDs
allowed_ids <- id_list[result == TRUE]
rejected_ids <- id_list[result == FALSE]

length(allowed_ids)
## [1] 15
length(rejected_ids)
## [1] 49
# Initialize category vector
category <- character(length(allowed_ids))

for(i in 1:length(allowed_ids)){
  d1 <- as.numeric(substr(allowed_ids[i],1,1))
  if(d1 == 0) category[i] <- "Drinkable"
  else if(d1 == 1) category[i] <- "Eatable"
  else if(d1 == 2) category[i] <- "Cosmetics"
  else if(d1 == 3) category[i] <- "Learning Materials"
}
final_data <- data.frame(ID = allowed_ids, Category = category)
head(final_data)
##    ID           Category
## 1 201          Cosmetics
## 2 301 Learning Materials
## 3 021          Drinkable
## 4 031          Drinkable
## 5 231          Cosmetics
## 6 102            Eatable
# Define all categories to control order in barplot
all_categories <- c("Drinkable","Eatable","Cosmetics","Learning Materials","Other")
final_data$Category <- factor(final_data$Category, levels = all_categories)

# Barplot of category counts
barplot(table(final_data$Category),
        col = c("blue","green","pink","orange","red"),
        main = "Shop Category Distribution")

check_buy_permission_num <- function(id, item_code){
  if(!check_permission(id)) return("ID NOT ALLOWED")
  
  d1 <- as.numeric(substr(id,1,1))
  if(d1 == item_code) return("PURCHASE ALLOWED")
  else return("PURCHASE NOT ALLOWED")
}
# Test examples
check_buy_permission_num("132", 1)  # Allowed
## [1] "PURCHASE ALLOWED"
check_buy_permission_num("123", 1)  # Not allowed
## [1] "ID NOT ALLOWED"
check_buy_permission_num("231", 2)  # Allowed
## [1] "PURCHASE ALLOWED"
check_buy_permission_num("312", 3)  # Allowed
## [1] "PURCHASE ALLOWED"
# Batch test with multiple IDs and items
test_ids <- c("132","231","312","031","123")
test_items <- c(1,2,3,0,1)
mapply(check_buy_permission_num, test_ids, test_items)
##                132                231                312                031 
## "PURCHASE ALLOWED" "PURCHASE ALLOWED" "PURCHASE ALLOWED" "PURCHASE ALLOWED" 
##                123 
##   "ID NOT ALLOWED"

Conclusion

This project demonstrates how R can be used to implement a shop classification and permission system. It applies rules to ensure valid IDs, organizes data efficiently, and provides a clear visual summary of shop categories. ## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: