Haskell1 2
Haskell1 2
Haskell1 2
SPRING 2024
Module: Haskell
1
Assignment Guidelines
- The assignment is due at 7 PM on Feb 17, 2024 (Saturday)
- There are a total of 7 questions.
- You are allowed to use take, drop, head, tail, div, rem, + , - , * , !!, ++, where, otherwise, let, guards,
: , → , ← , if-then-else, &&, ∥, not, ==, and list compression.
- You can add Eq and/or Ord to the function signatures if you want to.
- We have provided you with a haskell file ”code.hs” with function signatures and some test cases.
- Please note that the test cases may not cover all the cases for the questions. There may be other
(corner) cases which students have to figure out themselves.
- There are NO grace days for any of the assignments in this course.
- There is NO late days policy. No submission will be accepted after the deadline.
- Plagiarism is strictly prohibited. Students are not allowed to discuss their solutions with others
or look over the internet. However, they may seek help from the course staff ONLY.
- Students may discuss their queries or clear their confusions about the questions in office hours or on
slack (make use of the assignment channel).
- There will be NO partial marking for any of the questions. This is being done to be fair to all the
students in this course. However, the questions will be graded according to the number of test cases
that are passing. For example: if a 10 marks question has 10 test cases and 5 of them are passing for
a student, then they will get 5/10 for that question.
- The submissions will be done using GitHub Classroom. Your latest commit in your assign-
ment repository before the deadline will be considered for the final submission.
- You will have to install hspec which is a testing framework for haskell that we are using for this
assignment. It can be simply installed using the following command:
– “cabal update && cabal install --lib hspec”
2
Questions Category: Medium (5 Marks Each)
3
Return an integer array ans where ans[i] represents the height described above
after dropping the ith square.
Explanation:
After the first drop, square 1 lands on the X-axis and its height is 2.
After the second drop, the tallest stack is squares 1 and 2 with a height of 5.
After the third drop, square 3 lands on the X-axis and its height is 1.
Thus, we return [2, 5, 1].
4
Question 3: Post-Order to Breadth-First Conversion
According to Wikipedia, a complete binary tree is a binary tree ”where every
level, except possibly the last, is completely filled, and all nodes in the last
level are as far left as possible.”
The Wikipedia page referenced above also mentions that ”Binary trees can
also be stored in breadth-first order as an implicit data structure in lists, and
if the tree is a complete binary tree, this method wastes no space.”
Your task is to write a function that takes a list of integers and, assuming
that the list is ordered according to a post-order traversal of a complete
binary tree, returns a list that contains the values of the tree in breadth-first
order. Example 1: Let the input list be [1, 3, 2, 5, 6, 4, 8, 10, 9, 7]. This list
contains the values of the following complete binary tree
Output: The output of the function shall be a list containing the values of
the nodes of the binary tree read top-to-bottom, left-to-right. In this exam-
ple, the returned list should be: [7, 4, 9, 2, 6, 8, 10, 1, 3, 5]
5
Questions Category: Hard (8 Marks Each)
Attempt any 3 of the 4 questions
6
modified tree. The swapping should meet this condition for every pair of
nodes that are swapped.
As you can see, there are 3 possible swaps (10 and 5, 8 and 32, 6 and 36).
First, we have to do the swap that involves node at the lowest level. In our
case, there are two swaps (10 and 5, 8 and 32) that involve nodes (10 and 8)
at the lowest level. So, according to our rule, first, we will perform the swap
that includes the leftmost node, which in this case is 10.
7
Now, we have to do two more swaps (8 and 32, 6 and 36). According to our
rule, we perform the swap that involves node at the lowest level, which in
this case in 8.
8
Here is another example.
There are 2 possible swaps (3 and 6, 10 and 5). First, we swap 3 and 6
because 3 is at the lowest level. Because there’s a parent-child relationship
between 3 and 6, we will only swap their values and not the sub-trees.
9
Question 3: Basic Calculator
Given a string which represents a mathematical expression, evaluate this ex-
pression and return its value. Integer division should truncate towards zero.
The possible operators include addition, subtraction, multiplication, and di-
vision.
Sample Input: string = “2 + 9/ 3”
Output: 5
Sample Input: string = “(3 + 12 ) / 5”
Output: 3
Sample Input: string = “ 2 * 4 / 3”
Output: 2
10
11