-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
465 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package assignment | ||
|
||
type BubbleSort struct{} | ||
|
||
func NewBubbleSort() *BubbleSort { | ||
panic("not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"artemis/assignment" | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
// Constants define iteration and random date generation bounds. | ||
const ( | ||
Iterations = 10 | ||
RandomFloor = 5 | ||
RandomCeiling = 15 | ||
) | ||
|
||
// main demonstrates the sorting process. | ||
func main() { | ||
var a, b int | ||
// TODO: Init Context and Policy | ||
|
||
// Run multiple times to simulate different sorting strategies | ||
for i := 0; i < Iterations; i++ { | ||
dates := createRandomDates() | ||
|
||
fmt.Print("a: ") | ||
fmt.Scanln(&a) | ||
// TODO: Configure context | ||
|
||
fmt.Print("b: ") | ||
fmt.Scanln(&b) | ||
fmt.Println("Unsorted Array of course dates:") | ||
printDates(dates) | ||
fmt.Println() | ||
|
||
// TODO: Sort dates | ||
|
||
fmt.Println("Sorted Array of course dates:") | ||
printDates(dates) | ||
fmt.Println() | ||
} | ||
} | ||
|
||
sum := assignment.Add(a, b) | ||
// createRandomDates generates a slice of random dates with size between RandomFloor and RandomCeiling. | ||
func createRandomDates() []time.Time { | ||
listLength := rand.Intn(RandomCeiling-RandomFloor) + RandomFloor | ||
list := make([]time.Time, listLength) | ||
|
||
lowestDate := time.Date(2024, 10, 15, 0, 0, 0, 0, time.UTC) | ||
highestDate := time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC) | ||
|
||
for i := 0; i < listLength; i++ { | ||
list[i] = randomDateWithin(lowestDate, highestDate) | ||
} | ||
return list | ||
} | ||
|
||
// randomDateWithin creates a random time.Time value within the given range. | ||
func randomDateWithin(low, high time.Time) time.Time { | ||
randomTime := rand.Int63n(high.Unix()-low.Unix()) + low.Unix() | ||
return time.Unix(randomTime, 0) | ||
} | ||
|
||
fmt.Printf("a + b = %d\n", sum) | ||
// printDates prints out the given slice of time.Time values as dates. | ||
func printDates(dates []time.Time) { | ||
for _, date := range dates { | ||
fmt.Println(date.Format(time.DateOnly)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package assignment | ||
|
||
type Context struct{} | ||
|
||
func NewContext() *Context { | ||
panic("not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package assignment | ||
|
||
type MergeSort struct{} | ||
|
||
func NewMergeSort() *MergeSort { | ||
panic("not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package assignment | ||
|
||
type Policy struct{} | ||
|
||
func NewPolicy(context *Context) *Policy { | ||
panic("not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package assignment | ||
|
||
type SortStrategy interface{} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package assignment | ||
|
||
import "time" | ||
|
||
type BubbleSort struct{} | ||
|
||
func NewBubbleSort() *BubbleSort { | ||
return new(BubbleSort) | ||
} | ||
|
||
// PerformSort implements bubble sort for BubbleSort | ||
func (b BubbleSort) PerformSort(input []time.Time) { | ||
for i := len(input) - 1; i >= 0; i-- { | ||
for j := 0; j < i; j++ { | ||
if input[j].After(input[j+1]) { | ||
input[j], input[j+1] = input[j+1], input[j] | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package assignment | ||
|
||
import "time" | ||
|
||
type Context struct { | ||
dates []time.Time | ||
sortAlgorithm SortStrategy | ||
} | ||
|
||
func NewContext() *Context { | ||
return new(Context) | ||
} | ||
|
||
// GetDates returns the slice of dates in the context | ||
func (c *Context) GetDates() []time.Time { | ||
return c.dates | ||
} | ||
|
||
func (c *Context) SetDates(dates []time.Time) { | ||
c.dates = dates | ||
} | ||
|
||
func (c *Context) GetSortAlgorithm() SortStrategy { | ||
return c.sortAlgorithm | ||
} | ||
|
||
// SetSortAlgorithm sets the sorting strategy. | ||
func (c *Context) SetSortAlgorithm(strategy SortStrategy) { | ||
c.sortAlgorithm = strategy | ||
} | ||
|
||
// Sort runs the configured sort algorithm. | ||
func (c *Context) Sort() { | ||
if c.sortAlgorithm == nil { | ||
panic("sortAlgorithm has not been set") | ||
} | ||
|
||
if c.dates == nil { | ||
panic("dates has not been set") | ||
} | ||
|
||
c.sortAlgorithm.PerformSort(c.dates) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package assignment | ||
|
||
import "time" | ||
|
||
type MergeSort struct{} | ||
|
||
func NewMergeSort() *MergeSort { | ||
return new(MergeSort) | ||
} | ||
|
||
// PerformSort is a wrapper method for the real MergeSort algorithm. | ||
func (m MergeSort) PerformSort(input []time.Time) { | ||
mergeSort(input, 0, len(input)-1) | ||
} | ||
|
||
// mergeSort recursively applies the MergeSort algorithm. | ||
func mergeSort(input []time.Time, low, high int) { | ||
if high-low < 1 { | ||
return | ||
} | ||
mid := (low + high) / 2 | ||
mergeSort(input, low, mid) | ||
mergeSort(input, mid+1, high) | ||
merge(input, low, mid, high) | ||
} | ||
|
||
// merge merges two ranges within input defined from low to mid and from mid+1 to high. | ||
func merge(input []time.Time, low, mid, high int) { | ||
temp := make([]time.Time, high-low+1) | ||
left, right, k := low, mid+1, 0 | ||
|
||
for left <= mid && right <= high { | ||
if input[left].Before(input[right]) || input[left].Equal(input[right]) { | ||
temp[k] = input[left] | ||
left++ | ||
} else { | ||
temp[k] = input[right] | ||
right++ | ||
} | ||
k++ | ||
} | ||
|
||
for left <= mid { | ||
temp[k] = input[left] | ||
left++ | ||
k++ | ||
} | ||
for right <= high { | ||
temp[k] = input[right] | ||
right++ | ||
k++ | ||
} | ||
|
||
for i, val := range temp { | ||
input[low+i] = val | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package assignment | ||
|
||
type Policy struct { | ||
context *Context | ||
} | ||
|
||
const datesSizeThreshold = 10 | ||
|
||
func NewPolicy(context *Context) *Policy { | ||
return &Policy{context} | ||
} | ||
|
||
// Configure chooses a strategy depending on the number of date objects. | ||
func (p *Policy) Configure() { | ||
if len(p.context.GetDates()) > datesSizeThreshold { | ||
p.context.SetSortAlgorithm(NewMergeSort()) | ||
} else { | ||
p.context.SetSortAlgorithm(NewBubbleSort()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package assignment | ||
|
||
import "time" | ||
|
||
type SortStrategy interface { | ||
// PerformSort sorts a slice of dates. | ||
PerformSort(input []time.Time) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.