diff --git a/alternative.hs b/alternative.hs new file mode 100644 index 0000000..6a0783c --- /dev/null +++ b/alternative.hs @@ -0,0 +1,47 @@ +import Data.List +import Control.Applicative + + + +combinations :: Int -> [a] -> [[a]] +combinations 0 _ = [[]] +combinations n xs = [ xs !! i : x | i <- [0..(length xs)-1] + , x <- combinations (n-1) (drop (i+1) xs)] +firstpos xs = [x | x <- xs, head x == 1 ] +secondpos xs = [x | x <- xs, (head (tail x)) == 2 ] + +showall xs = [y | x <- xs, y <-x, y ] + +each xs stu = [checkindividual y stu | y <- x,x <-xs] + +studentstruct ss = [(x,[x]) | x <- ss] + +fixstruct index student ss = do + let (fh,_:sh) = splitAt (index - 1) ss + fh ++ student : sh + +checkindividual lookingfor currentstudent + |(elem lookingfor $ snd currentstudent )== True = True + |otherwise = False + + + + + + +groupedTogether xs ys = [ y : ys | y <- xs, (not (elem y ys))] + +--checkeach xs cur +-- | (length xs) == numbuniquegroups = xs +-- | [ | (a,b,_,_) <- xs, a != b] +--buildassignmnet xs = [x:[] | x <- xs ] + +--eliminate xs +-- | (length xs) == numbuniquegroups/8 = xs +-- | +--capture :: Ord => [a] -> [b] -> Int +capture xs ys = length [x | x <- xs,(not (elem x ys))] + + +x = combinations 4 [1..10] +--capture x [] diff --git a/assignment3.hs b/assignment3.hs index fb4e8a1..8831c69 100644 --- a/assignment3.hs +++ b/assignment3.hs @@ -1,8 +1,81 @@ import System.Environment import System.IO import Data.List -import System.IO +import Control.Applicative + + + +{- + Combinations gets all possible combinations of a list of numbers +-} +combinations :: Int -> [a] -> [[a]] +combinations 0 _ = [[]] +combinations n xs = [ xs !! i : x | i <- [0..(length xs)-1] + , x <- combinations (n-1) (drop (i+1) xs)] + +{- + firstpos gets the first list if the item is 1 +-} +firstpos xs = [x | x <- xs, head x == 1 ] + +{- + scondpos gets the first list with first item 2 +-} +secondpos xs = [x | x <- xs, (head (tail x)) == 2 ] + +{- + Print all the lists within combinations by taking them out of the list +-} +showall xs = [y | x <- xs, y <-x, y ] + +{- + +-} +each xs stu = [checkindividual y stu | y <- x,x <-xs] + + +{- + creates a list of tuples containing an int and a + list of other ints meant to contain the student and + the students it has been grouped with +-} +studentstruct ss = [(x,[x]) | x <- ss] + +{- + +-} +fixstruct index student ss = do + let (fh,_:sh) = splitAt (index - 1) ss + fh ++ student : sh + +{- + +-} +checkindividual lookingfor currentstudent + |(elem lookingfor $ snd currentstudent )== True = True + |otherwise = False + + + +{- + TODO make output True for true conidtion + Checks if the total number of students can be split into said group size +-} +checkmod :: Int -> Int -> Bool +checkmod x y + | mod x y == 0 = True + | otherwise = False + +{- + +-} +groupedTogether xs ys = [ y : ys | y <- xs, (not (elem y ys))] + + +capture xs ys = length [x | x <- xs,(not (elem x ys))] + +x = combinations 4 [1..10] --http://www.reddit.com/r/haskell/comments/1vras3/haskell_io_how_to_read_numbers/ @@ -14,14 +87,17 @@ main = progName <- getProgName let students = read(head args) :: Int --now we can use ints! let grpsize = read(head $tail args) :: Int - putStrLn "The number of stoodents aer:" - putStrLn students - putStrLn "Teh greup siez is: " - putStrLn grpsize - putStrLn "Program name is:" - putStrLn progName - return (students, grpsize) - return () + if checkmod students grpsize + then + listGen (splitList students grpsize) [1..students] grpsize + + else + do + putStrLn $"Cannot pair up " ++ + show(students) ++ " students into groups of " + ++ show(grpsize) ++ " across eight assignments" + + diff --git a/backup.c b/backup.c index 98095af..78f7823 100644 --- a/backup.c +++ b/backup.c @@ -41,17 +41,53 @@ int contains(Array *a, int target) { } return 0; +} + +int validPartner(Array *a, int perspectivePartner, int** currentGroups) { + + int x; + int y; + for (x = 0; x < sizeof(currentGroups)/sizeof(currentGroups[0]); x++) { + for (y = 0; x < sizeof(currentGroups[0])/sizeof(currentGroups[0][0]); x++ ) { + if (currentGroups[x][y] == validPartner) { + return 0; + } + } + } + + if (contains(a, perspectivePartner)) { + + return 0; + } + + return 1; + + + } int main (int argc, char *argv[]) { - int groupsize = argv[2]; - int studentAmount = argv[1]; + + + + + int groupsize = atoi(argv[2]); + int studentAmount = atoi(argv[1]); Array students[studentAmount]; - int i; - printf("The groupsize %d The students %d", groupsize, studentAmount); + int currentGroups[studentAmount][groupsize]; + int x; + for (x = 0; x < studentAmount; x++) { + initArray(&students[x], 1); + } + +int i; + + printf("The groupsize %d The students %d \n", groupsize, studentAmount); + + for (i = 0; i < 8; i++ ) { printf("Assignment %d ", i+1); @@ -61,17 +97,22 @@ int main (int argc, char *argv[]) { printf("( %d", j+1); int k; for (k = 0; k < studentAmount; k++) { - - if ((!contains(students[j], k)) && counter < groupsize) { - insertArray(students[j], k); - insertArray(students[k], j); + + if ((validPartner(&students[j], k, currentGroups)) && counter+1 < groupsize) { + //currentGroups[0][0] = 10; + insertArray(&students[j], k); + insertArray(&students[k], j); counter= counter + 1; - printf(", %d", k+1); + printf(", %d", k); - } + } + } - printf(") /n"); + counter = 0; + printf(")"); } + printf(" \n"); } + } diff --git a/mattsjunk.hs b/mattsjunk.hs index 9b1bae4..966ae83 100644 --- a/mattsjunk.hs +++ b/mattsjunk.hs @@ -1,3 +1,4 @@ +type StudentStruct = [(Integer,[Int])] {- Takes list of total students [1..howevermany] Outputs a list of tuples first element being the student ID number @@ -5,10 +6,10 @@ for storing the students already been grouped with -} --studentstruct :: Int => [Int] -> StudentStruct -studentstruct :: [t] -> [(t, [t])] +studentstruct :: [Integer] -> StudentStruct studentstruct ss = [(x,[x]) | x <- ss] -type StudentStruct = [(Int,[Int])] + {- Short and stupid function to allow inplace element manipulation @@ -16,7 +17,7 @@ type StudentStruct = [(Int,[Int])] Index value starts at 1 not zero so studentId value 1 will give you the first student -} -fixstruct :: Int -> a -> [a] -> [a] +--fixstruct :: Integer -> a -> [a] -> [a] fixstruct index student ss = do let (fh,_:sh) = splitAt (index - 1) ss fh ++ student : sh @@ -26,7 +27,7 @@ fixstruct index student ss = do curstu = the tuple where first element is the student id and the second element is the list of already grouped students -} -addstudent :: a -> (t, [a]) -> (t, [a]) +--addstudent :: a -> (t, [a]) -> (t, [a]) addstudent s curstu = head [ (y, s: x) | let x = snd curstu , let y = fst curstu] @@ -44,19 +45,16 @@ checkindividual lookingfor currentstudent --------------------------------------------------------------------------------- -outputlist :: Int -> [[Int]] +--outputlist :: Integer -> [[Integer]] outputlist numgroup = map (drop 1) (map (:[]) [1..(numgroup)]) ---checkoutputlist (ss) lookfor ol= --- if (not . (elem curstu ol)) --- then if --- map currentstudent map lookfir `checkindividual` --- then -{- - lookfor = studentid to check - currentstudent = tuple --} -checkisinoutputlist :: StudentStruct -> Int -> [Int] -> Bool +addoutputlist groupindex stuId outlist = do + let x = stuId :(outlist !! groupindex) + let (fh,_:sh) = splitAt (groupindex - 1) outlist + fh ++ x : sh + + +--checkisinoutputlist :: StudentStruct -> Int -> [Int] -> Bool checkisinoutputlist ss curstu ol |(elem curstu ol) = True |otherwise = False @@ -77,9 +75,8 @@ checkgrouplist gl studenttuple =[ checkindividual x studenttuple | x <- gl ] n = the student id to remove l = the list of students not yet grouped -} -updategroupedlist :: Eq a => a -> [a] -> [a] -updategroupedlist n l = filter (not . (==n)) l - +updateNotGroupedList :: Eq a => a -> [a] -> [a] +updateNotGroupedList n l = filter (not . (==n)) l --------------------------------------------------------------------------------- @@ -91,5 +88,18 @@ checkmod x y = ( if mod x y == 0 else putStrLn $"Cannot pair up " ++ (show x) ++ " students into groups of " ++ show(y) ++ " across eight assignments") +addAction + :: Monad m => + [(t, [Int])] -> (t, [Int]) -> Int -> [Int] -> a -> m a + +addAction ss studTup stuId notgroupedlist= do + let newnotgroupedlist = updateNotGroupedList stuId notgroupedlist + let tempTup = addstudent stuId studTup + let newSS = fixstruct stuId tempTup ss + return + + +--addAction x (2,[2]) 1 y +