exercise A, using R instead of Python

Mick McQuaid

2025-01-13

I used the following prompt.

sgpt "In R, generate 32 random numbers from 2 to 13 with replacement
      but with the constraint that any given number may be repeated
      no more than three times and no fewer than twice"

The output went up to the line that begins with three hashtags, then I manually added the remainder to make it look better.

 set.seed(123) # For reproducibility

 # Initialize the vector
 numbers <- c()

 # Ensure each number appears at least twice
 for (i in 2:13) {
   numbers <- c(numbers, rep(i, 2))
 }

 # Calculate remaining slots
 remaining_slots <- 32 - length(numbers)

 # Randomly select numbers to fill remaining slots,
 #   ensuring no number appears more than 3 times
 while (remaining_slots > 0) {
   num <- sample(2:13, 1)
   if (sum(numbers == num) < 3) {
     numbers <- c(numbers, num)
     remaining_slots <- remaining_slots - 1
   }
 }

 # Shuffle the numbers
 numbers <- sample(numbers)

 # Print the result
 print(numbers)
 [1] 11  3  4 11  5  4  7  6  3  5  5  6 10 12  3  8  2  7 12  6  7 12 10 11  9
[26]  2 13 10 13  4  8  9
#.### my additions follow here

 names <- c("ayesha", "jui-chien", "xinyan", "john", "siddarth",
            "palak", "dhruvi", "maitreyee", "xiuer", "prathyusha",
            "amogh", "kylie", "miranda", "emily", "jasmine",
            "shadrack", "sharon", "sairam", "muhammad", "vibha",
            "sneha", "anusha", "sainath", "zeel", "srishti",
            "tianyu", "vanshita", "puneeth", "shuai", "tianyi",
            "ziqi", "harold")
pacman::p_load(tidyverse)
p <- as_tibble(cbind(names,numbers))
p$numbers <- parse_number(p$numbers)
p |> arrange(numbers) |> print(n=32)
# A tibble: 32 × 2
   names      numbers
   <chr>        <dbl>
 1 sharon           2
 2 tianyu           2
 3 jui-chien        3
 4 xiuer            3
 5 jasmine          3
 6 xinyan           4
 7 palak            4
 8 tianyi           4
 9 siddarth         5
10 prathyusha       5
11 amogh            5
12 maitreyee        6
13 kylie            6
14 vibha            6
15 dhruvi           7
16 sairam           7
17 sneha            7
18 shadrack         8
19 ziqi             8
20 srishti          9
21 harold           9
22 miranda         10
23 sainath         10
24 puneeth         10
25 ayesha          11
26 john            11
27 zeel            11
28 emily           12
29 muhammad        12
30 anusha          12
31 vanshita        13
32 shuai           13