# Mindanao State University
# General Santos City
# Math 108
# Double for Loop for Consecutive Elements in R
# Double for Loop for generating dynamic unique password consisting of integers
# Submitted by: Dongosa, Davy D.
# May 2023

# The task is to generate the sum of consecutive elements using double for loop and generate dynamic unique password consisting integers by using double for loop in R

# Here in this task we will be using my previous set from the first examination

S <- c(3, 7, -9, 12, -2, 2, 15, 24, -17, 10, 21, -12) 

(UniversalSet <- c(3, 7, -9, 12, -2, 2, 15, 24, -17, 10, 21, -12))
##  [1]   3   7  -9  12  -2   2  15  24 -17  10  21 -12
Size <- 12
(RandomSet <- sample(UniversalSet,Size,rep=TRUE))
##  [1] -17  21  21  12  -9  -2   7   7   2  12  -2 -12
dat <- list()
Sums <- list()
counter = 0

for (i in 1:(Size-1)){
  data <- c(RandomSet[i], RandomSet[i+1])
  counter = counter + 1
  dat[[counter]] <- data
  Sums[[counter]] <-sum(data)
}
head(dat)
## [[1]]
## [1] -17  21
## 
## [[2]]
## [1] 21 21
## 
## [[3]]
## [1] 21 12
## 
## [[4]]
## [1] 12 -9
## 
## [[5]]
## [1] -9 -2
## 
## [[6]]
## [1] -2  7
tail(dat)
## [[1]]
## [1] -2  7
## 
## [[2]]
## [1] 7 7
## 
## [[3]]
## [1] 7 2
## 
## [[4]]
## [1]  2 12
## 
## [[5]]
## [1] 12 -2
## 
## [[6]]
## [1]  -2 -12
RandomSet
##  [1] -17  21  21  12  -9  -2   7   7   2  12  -2 -12
# Generating the sums of consecutive elements of the set in R using double for loop
# Create a data frame with subsets and their sums for 4 consecutive subsets

S <- c(3, 7, -9, 12, -2, 2, 15, 24, -17, 10, 21, -12)

subset_length <- 4
num_subsets <- length(S) - subset_length + 1
subsets <- vector("list", num_subsets)
sums <- numeric(num_subsets)

for (i in 1:(num_subsets - 1)) {
  for (j in 1:(num_subsets - 2)) {
    subsets[[i]] <- S[i:(i + subset_length - 1)]
    sums[i] <- sum(subsets[[i]])
  }
}

# Create a data frame with subsets and their sums
subset_data <- data.frame(Subsets = subsets[1:(num_subsets - 1)], Sums = sums[1:(num_subsets - 1)])
print(subset_data)
##   Subsets.c.3..7...9..12. Subsets.c.7...9..12...2. Subsets.c..9..12...2..2.
## 1                       3                        7                       -9
## 2                       7                       -9                       12
## 3                      -9                       12                       -2
## 4                      12                       -2                        2
## 5                       3                        7                       -9
## 6                       7                       -9                       12
## 7                      -9                       12                       -2
## 8                      12                       -2                        2
##   Subsets.c.12...2..2..15. Subsets.c..2..2..15..24. Subsets.c.2..15..24...17.
## 1                       12                       -2                         2
## 2                       -2                        2                        15
## 3                        2                       15                        24
## 4                       15                       24                       -17
## 5                       12                       -2                         2
## 6                       -2                        2                        15
## 7                        2                       15                        24
## 8                       15                       24                       -17
##   Subsets.c.15..24...17..10. Subsets.c.24...17..10..21. Sums
## 1                         15                         24   13
## 2                         24                        -17    8
## 3                        -17                         10    3
## 4                         10                         21   27
## 5                         15                         24   39
## 6                         24                        -17   24
## 7                        -17                         10   32
## 8                         10                         21   38
# Create a data frame with subsets and their sums for 5 consecutive subsets

S <- c(3, 7, -9, 12, -2, 2, 15, 24, -17, 10, 21, -12)

subset_length <- 5
num_subsets <- length(S) - subset_length + 1
subsets <- vector("list", num_subsets)
sums <- numeric(num_subsets)

for (i in 1:(num_subsets - 1)) {
  for (j in 1:(num_subsets - 2)) {
    subsets[[i]] <- S[i:(i + subset_length - 1)]
    sums[i] <- sum(subsets[[i]])
  }
}

print(subset_data)
##   Subsets.c.3..7...9..12. Subsets.c.7...9..12...2. Subsets.c..9..12...2..2.
## 1                       3                        7                       -9
## 2                       7                       -9                       12
## 3                      -9                       12                       -2
## 4                      12                       -2                        2
## 5                       3                        7                       -9
## 6                       7                       -9                       12
## 7                      -9                       12                       -2
## 8                      12                       -2                        2
##   Subsets.c.12...2..2..15. Subsets.c..2..2..15..24. Subsets.c.2..15..24...17.
## 1                       12                       -2                         2
## 2                       -2                        2                        15
## 3                        2                       15                        24
## 4                       15                       24                       -17
## 5                       12                       -2                         2
## 6                       -2                        2                        15
## 7                        2                       15                        24
## 8                       15                       24                       -17
##   Subsets.c.15..24...17..10. Subsets.c.24...17..10..21. Sums
## 1                         15                         24   13
## 2                         24                        -17    8
## 3                        -17                         10    3
## 4                         10                         21   27
## 5                         15                         24   39
## 6                         24                        -17   24
## 7                        -17                         10   32
## 8                         10                         21   38
# Create a data frame with subsets and their sums for 6 consecutive subsets

S <- c(3, 7, -9, 12, -2, 2, 15, 24, -17, 10, 21, -12)

subset_length <- 6
num_subsets <- length(S) - subset_length + 1

subsets <- vector("list", num_subsets)
sums <- numeric(num_subsets)

for (i in 1:(num_subsets - 1)) {
  for (j in 1:(num_subsets - 2)) {
    subsets[[i]] <- S[i:(i + subset_length - 1)]
    sums[i] <- sum(subsets[[i]])
  }
}

# Create a data frame with subsets and their sums
subset_data <- data.frame(Subsets = subsets[1:(num_subsets - 1)], Sums = sums[1:(num_subsets - 1)])
print (subset_data)
##   Subsets.c.3..7...9..12...2..2. Subsets.c.7...9..12...2..2..15.
## 1                              3                               7
## 2                              7                              -9
## 3                             -9                              12
## 4                             12                              -2
## 5                             -2                               2
## 6                              2                              15
##   Subsets.c..9..12...2..2..15..24. Subsets.c.12...2..2..15..24...17.
## 1                               -9                                12
## 2                               12                                -2
## 3                               -2                                 2
## 4                                2                                15
## 5                               15                                24
## 6                               24                               -17
##   Subsets.c..2..2..15..24...17..10. Subsets.c.2..15..24...17..10..21. Sums
## 1                                -2                                 2   13
## 2                                 2                                15   25
## 3                                15                                24   42
## 4                                24                               -17   34
## 5                               -17                                10   32
## 6                                10                                21   55
# To generate dynamic unique password consisting integers by using double for loops in R
# and here we use a sample of a 12 elements subset
# in this case I will be working on my own example from the first examination
S <- c(3, 7, -9, 12, -2, 2, 15, 24, -17, 10, 21, -12)
Size<-12
(RandomSet <- sample(S,Size,rep=TRUE))
##  [1] -12  -2  21  21  -2  21 -17   3   2  24   7   7
# Define Container of subsets
dat <- list()
# Define Container of corresponding sum
Sums <- list()
# Subsets of size 2 consecutive numbers
# just continue the numbering sequence
counter = 0
for (i in 1:(Size-1)){
  for (j in 1:(Size - 2)){
    data <- c(RandomSet[i], RandomSet[i+1])
    counter = counter + 1
    dat[[counter]] <- data
    Sums[[counter]] <-sum(data)
  }
}
# Display results

head(dat)
## [[1]]
## [1] -12  -2
## 
## [[2]]
## [1] -12  -2
## 
## [[3]]
## [1] -12  -2
## 
## [[4]]
## [1] -12  -2
## 
## [[5]]
## [1] -12  -2
## 
## [[6]]
## [1] -12  -2
tail(dat)
## [[1]]
## [1] 7 7
## 
## [[2]]
## [1] 7 7
## 
## [[3]]
## [1] 7 7
## 
## [[4]]
## [1] 7 7
## 
## [[5]]
## [1] 7 7
## 
## [[6]]
## [1] 7 7
password<- RandomSet

# Print the resulting password
print(password)
##  [1] -12  -2  21  21  -2  21 -17   3   2  24   7   7