From 954db83ffb00b9e5a91738474f0b2532003d8183 Mon Sep 17 00:00:00 2001 From: Konstantinos Fezos <161313113+feezz8@users.noreply.github.com> Date: Wed, 19 Feb 2025 13:57:12 +0200 Subject: [PATCH 1/9] Linemarket dana implementation This is an implemenation of my solution for the first exercise in the first coding exercises set for the Algorithms course during the 2024-2025 winter Semester @ ECE/NTUA. The full description of the problem can be found in the web page of the course in Helios. --- dana/programs/linemarket.dana | 125 ++++++++++++++++++++++++++++++++ dana/programs/linemarket.input | 11 +++ dana/programs/linemarket.output | 5 ++ 3 files changed, 141 insertions(+) create mode 100644 dana/programs/linemarket.dana create mode 100644 dana/programs/linemarket.input create mode 100644 dana/programs/linemarket.output diff --git a/dana/programs/linemarket.dana b/dana/programs/linemarket.dana new file mode 100644 index 0000000..cd78324 --- /dev/null +++ b/dana/programs/linemarket.dana @@ -0,0 +1,125 @@ +(* + This is an implemenation of my solution for the first exercise in + the first coding exercises set for the Algorithms course during the + 2024-2025 winter Semester @ ECE/NTUA. The full description of the + problem can be found in the web page of the course in Helios. + +*) + +def main + def bsort: n as int, x as int[] + def swap: x y as ref int + var t is int + t := x + x := y + y := t + + var changed is byte + var i is int + + loop: + changed := false + i := 0 + loop: + if i < n-1: + if x[i] > x[i+1]: + swap x[i], x[i+1] + changed := true + i := i + 1 + else: break + if not changed: break + + def possible_store is byte: arr as int[], mid spaces stores as int: + var last_position placed_stores i is int + last_position := -1000000000 + placed_stores := 0 + i := 0 + loop: + if i >= (spaces/2): break + else: + var j is int + j := 2*i + var position is int + if (last_position + mid) > arr[j]: position := last_position + mid + else: position := arr[j] + loop: + if position > arr[j + 1]: break + else: + placed_stores := placed_stores + 1 + last_position := position + position := position + mid + if placed_stores >= stores: return true + i := i + 1 + return false + + def largest_minimum_distance is int: arr as int[], spaces stores as int: + bsort(arr, spaces) + var result left right is int + result := 0 + left := 0 + right := arr[spaces - 1] - arr[0] + + #Simple Binary Search for finding the sweet spot + loop: + if left > right: break + else: + var distance is int + distance := (left + right) / 2 + if possible_store(arr, distance, spaces, stores): + result := distance + left := distance + 1 + else: + right = distance + 1 + return result + + var stores size s f N tail is int + tail := 0 + stores := readInteger() + N := readInteger() + + var MAX_SIZE MAX_PLACES is int + + MAX_STORES := 1000000 #Change accordingly for larger sizes + MAX_PLACES := 100000 #Change accordingly for larger sizes + + (* + The program handles out of bound inputs by + asking the user again for a correct one. + Maybe the two "if" statements can be combined into one. + *) + + if stores > MAX_STORES or stores < 1: + writeString "Wrong starting input, let's try again!" + stores := readInteger() + + if N > MAX_PLACES or N < 1: + writeString "Wrong starting input, let's try again!" + N := readInteger() + + var arr is int[2*N] + + var i is int + i := 0 + + loop: + if i >= N: break + else: + s := readInteger() #We assume correct values for the limits of the spaces + f := readInteger() + + arr[i*2] := s + arr[i*2 + 1] := f + tail := tail + 2 + i := i + 1 + + writeInteger: largest_minimum_distance(arr, tail, stores) + writeString: "\n" + + + + + + + + + \ No newline at end of file diff --git a/dana/programs/linemarket.input b/dana/programs/linemarket.input new file mode 100644 index 0000000..f077daa --- /dev/null +++ b/dana/programs/linemarket.input @@ -0,0 +1,11 @@ +#Input 1 +5 3 +5 9 +0 3 +12 12 + +#Input 2 +6 3 +10 12 +0 4 +5 8 \ No newline at end of file diff --git a/dana/programs/linemarket.output b/dana/programs/linemarket.output new file mode 100644 index 0000000..4f4f467 --- /dev/null +++ b/dana/programs/linemarket.output @@ -0,0 +1,5 @@ +#Output 1 +3 + +#Output 2 +2 \ No newline at end of file From af8cf609c901a9a9f56653117bb083f7a954c39d Mon Sep 17 00:00:00 2001 From: Kostis Sagonas Date: Wed, 19 Feb 2025 23:03:03 +0100 Subject: [PATCH 2/9] Fix spellcheck error & cleanup --- dana/programs/linemarket.dana | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/dana/programs/linemarket.dana b/dana/programs/linemarket.dana index cd78324..c876b4b 100644 --- a/dana/programs/linemarket.dana +++ b/dana/programs/linemarket.dana @@ -1,9 +1,8 @@ (* - This is an implemenation of my solution for the first exercise in + This is an implementation of my solution for the first exercise in the first coding exercises set for the Algorithms course during the 2024-2025 winter Semester @ ECE/NTUA. The full description of the problem can be found in the web page of the course in Helios. - *) def main @@ -114,12 +113,3 @@ def main writeInteger: largest_minimum_distance(arr, tail, stores) writeString: "\n" - - - - - - - - - \ No newline at end of file From de38fd3937c621715347a16cc967904b449749a1 Mon Sep 17 00:00:00 2001 From: Konstantinos Fezos <161313113+feezz8@users.noreply.github.com> Date: Wed, 26 Feb 2025 13:23:13 +0200 Subject: [PATCH 3/9] Update linemarket.dana Added problem description as a comment --- dana/programs/linemarket.dana | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/dana/programs/linemarket.dana b/dana/programs/linemarket.dana index c876b4b..55ab4a0 100644 --- a/dana/programs/linemarket.dana +++ b/dana/programs/linemarket.dana @@ -3,6 +3,27 @@ the first coding exercises set for the Algorithms course during the 2024-2025 winter Semester @ ECE/NTUA. The full description of the problem can be found in the web page of the course in Helios. + + Our goal is to place N stores in M non-overlapping integer intervals[s_i, x_i]. + A store can be placed in any integer number that belongs to an interval. We want our + program to calculate the smallest possible distance between two consecutive stores such as + that distance is maximized. + + Input: The program reads from the standard input two positive integers, N amount of stores and M amount of spaces(intervals) to place them. + After that for the next M lines it reads from the standard input two non-negative integers defining the begining and the end of each space(interval). + We assume the spaces are given in a random order and their total length is not shorter than N. + + Output: Our program prints in the standard output a positive integer, + the minimum required distance between two consecutive stores in a + placement of all stores such as that minimum distance is maximized. + + Example Input: 6 3 + 10 12 + 0 4 + 5 8 + + Example Output: 2 + *) def main From 03867f553aa6baf998f605c2cce1c212aa8ad3ef Mon Sep 17 00:00:00 2001 From: Konstantinos Fezos <161313113+feezz8@users.noreply.github.com> Date: Wed, 26 Feb 2025 13:26:38 +0200 Subject: [PATCH 4/9] Update linemarket.dana fixed typo in problem desc --- dana/programs/linemarket.dana | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dana/programs/linemarket.dana b/dana/programs/linemarket.dana index 55ab4a0..15a2586 100644 --- a/dana/programs/linemarket.dana +++ b/dana/programs/linemarket.dana @@ -10,7 +10,7 @@ that distance is maximized. Input: The program reads from the standard input two positive integers, N amount of stores and M amount of spaces(intervals) to place them. - After that for the next M lines it reads from the standard input two non-negative integers defining the begining and the end of each space(interval). + After that for the next M lines it reads from the standard input two non-negative integers defining the beginning and the end of each space(interval). We assume the spaces are given in a random order and their total length is not shorter than N. Output: Our program prints in the standard output a positive integer, From 60db36ecd9b6675824e75caae10fbef38d8a20eb Mon Sep 17 00:00:00 2001 From: Konstantinos Fezos <161313113+feezz8@users.noreply.github.com> Date: Wed, 26 Feb 2025 13:33:39 +0200 Subject: [PATCH 5/9] Update and rename linemarket.output to linemarket.result --- dana/programs/linemarket.output | 5 ----- dana/programs/linemarket.result | 2 ++ 2 files changed, 2 insertions(+), 5 deletions(-) delete mode 100644 dana/programs/linemarket.output create mode 100644 dana/programs/linemarket.result diff --git a/dana/programs/linemarket.output b/dana/programs/linemarket.output deleted file mode 100644 index 4f4f467..0000000 --- a/dana/programs/linemarket.output +++ /dev/null @@ -1,5 +0,0 @@ -#Output 1 -3 - -#Output 2 -2 \ No newline at end of file diff --git a/dana/programs/linemarket.result b/dana/programs/linemarket.result new file mode 100644 index 0000000..8148ef7 --- /dev/null +++ b/dana/programs/linemarket.result @@ -0,0 +1,2 @@ +3 + From c19bf88972f6fad1d2fe7ab469beb896579dafb1 Mon Sep 17 00:00:00 2001 From: Konstantinos Fezos <161313113+feezz8@users.noreply.github.com> Date: Wed, 26 Feb 2025 13:34:03 +0200 Subject: [PATCH 6/9] Update linemarket.input --- dana/programs/linemarket.input | 6 ------ 1 file changed, 6 deletions(-) diff --git a/dana/programs/linemarket.input b/dana/programs/linemarket.input index f077daa..642ab94 100644 --- a/dana/programs/linemarket.input +++ b/dana/programs/linemarket.input @@ -1,11 +1,5 @@ -#Input 1 5 3 5 9 0 3 12 12 -#Input 2 -6 3 -10 12 -0 4 -5 8 \ No newline at end of file From e8ee4058d8b8168086d0510b23057e2ceeae8f24 Mon Sep 17 00:00:00 2001 From: Konstantinos Fezos <161313113+feezz8@users.noreply.github.com> Date: Wed, 26 Feb 2025 13:35:28 +0200 Subject: [PATCH 7/9] Update linemarket.result --- dana/programs/linemarket.result | 1 - 1 file changed, 1 deletion(-) diff --git a/dana/programs/linemarket.result b/dana/programs/linemarket.result index 8148ef7..00750ed 100644 --- a/dana/programs/linemarket.result +++ b/dana/programs/linemarket.result @@ -1,2 +1 @@ 3 - From 3d8ab8cf36264d820f98b9f86c176403c22313d9 Mon Sep 17 00:00:00 2001 From: Konstantinos Fezos <161313113+feezz8@users.noreply.github.com> Date: Wed, 26 Feb 2025 13:35:35 +0200 Subject: [PATCH 8/9] Update linemarket.input --- dana/programs/linemarket.input | 1 - 1 file changed, 1 deletion(-) diff --git a/dana/programs/linemarket.input b/dana/programs/linemarket.input index 642ab94..d866ca8 100644 --- a/dana/programs/linemarket.input +++ b/dana/programs/linemarket.input @@ -2,4 +2,3 @@ 5 9 0 3 12 12 - From 4dd621b15159f26ee46a44e1a68e70ef22833da0 Mon Sep 17 00:00:00 2001 From: Kostis Sagonas Date: Wed, 26 Feb 2025 12:35:45 +0100 Subject: [PATCH 9/9] Cosmetic change --- dana/programs/linemarket.dana | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dana/programs/linemarket.dana b/dana/programs/linemarket.dana index 15a2586..b659880 100644 --- a/dana/programs/linemarket.dana +++ b/dana/programs/linemarket.dana @@ -5,9 +5,9 @@ problem can be found in the web page of the course in Helios. Our goal is to place N stores in M non-overlapping integer intervals[s_i, x_i]. - A store can be placed in any integer number that belongs to an interval. We want our - program to calculate the smallest possible distance between two consecutive stores such as - that distance is maximized. + A store can be placed in any integer number that belongs to an interval. + We want our program to calculate the smallest possible distance between + two consecutive stores such as that distance is maximized. Input: The program reads from the standard input two positive integers, N amount of stores and M amount of spaces(intervals) to place them. After that for the next M lines it reads from the standard input two non-negative integers defining the beginning and the end of each space(interval).