I'm doing an assignment in Eiffel and I'm having trouble implementing my ensure clause. Is there some special syntax you need to include a variable or function?
This is my code at the moment for my 'put' function
put(key: K; value: V)
require
key /= void
local
tmp:ITEM[K,V]
do
create tmp.make(key, value)
list.put_front (tmp)
count := count + 1
ensure
count = old count + 1 and list.has(key, value)
end
This is the code for the 'has' function
has(key:K; val:V):BOOLEAN
require
key /= void
local
flag: INTEGER
do
flag := 0
from
list.start
until
list.exhausted
loop
if list.item.getkey = key then
if list.item.getvalue = val then
flag := 1
end
end
list.forth
end
if flag = 1 then
Result := true
else
Result := false
end
ensure
--???
end
The assignment is to implement a map adt via linked list. The 'put' function inserts item(key, value) into the list. The 'has' function checks whether the list contains (key value) pair.
Any help will be greatly appreciated.
It could be just
Result = across list as c some (c.item.key = key and c.item.value = value) end
But then there are several other comments on the code:
I do not see where the property key /= Void is used. So it looks like it is not required.
Given that the code of put inserts elements of type ITEM, the postcondition of put should also use across list ... end instead of list.has (key, value). So I suspect it should be just has (key, value) without list. in front of it.
I do not see any point of using an auxiliary variable flag. The reserved variable Result will do just fine.
All variables in Eiffel are initialized by the default values, so there is no need to assign 0 to flag (or false to Result in a simplified version) at the beginning of a routine.
Usually there is no need to have dedicated getters in Eiffel, so normally the code list.item.getkey looks like list.item.key.
One can preemptively exit from the loop by calling list.finish that moves cursor to the last item of the list when a required element is found. Then after list.forth the loop exit condition is satisfied and the loop terminates.
Related
Consider the given two arrays A and B without repetitions (that
is, no double occurrences of the same element). The task is to check whether
each element of B is also an element of A without regard to the order.
For instance if A = [1, 2, 3, 4] and B = [2, 3, 1] then the answer is YES. If
however B = [1, 2, 5] then the answer is NO because 5 is not in A.
Design a recursive algorithm (no use of loops) for the above problem.
I am trying to solve the above problem, and there is no way I can find to solve it without using the loop. Would anyone know a way to solve this with recursion without using a loop?
I can not use any builtin functions, this is a recursion exercise for algorithms and data structures.
You can convert a loop to recursion by designing a recursive function/method that operates on a part of your original array (technically on a sub-array) (initially the sub-array will be your complete array) and reducing the size of the array (each time you are passing to your recursive method) by 1.
The following method (I've the implementation in Java) simply checks if the given number is present in the array/list. But notice that it also takes startIndex and endIndex which specifically denotes our boundaries of sub-array/sub-list.
In simple words the following method checks whether the given number is present in list or not but the check is done only between startIndex and endIndex both inclusive. Consider that you pass each element of your array B (listB in my case) to this method, and list argument is actually a reference to your array A (listA in my case).
/**
* This method recursively checks whether given
* number is contained in the given list or not.
*
* For this method startIndex and endIndex
* correspond to the indices of listA
*/
private static boolean contains(List<Integer> list, int startIndex, int endIndex, int number) {
if (startIndex == endIndex) {
return list.get(startIndex) == number;
} else if (startIndex < endIndex) {
return list.get(startIndex) == number || contains(list, startIndex + 1, endIndex, number);
}
// should never be the case
return true;
}
Now, once you have the above method, you can now device a recursive method that pics up all the elements of listB one at a time, and "plugs it in" inside the above method. This can be preciously done as follows:
/**
* This method recurse over each element of listB and checks
* whether the current element is contained in listA or not
*
* for this method startIndex and endIndex correspond to the
* indices of listB
*/
private static boolean contains(List<Integer> listA, List<Integer> listB, int startIndex, int endIndex) {
if (startIndex > endIndex) {
return true;
}
boolean c = contains(listA, 0, listA.size() - 1, listB.get(startIndex));
if (!c) {
return false;
}
return contains(listA, listB, startIndex + 1, endIndex);
}
And a call to above method will look like contains(listA, listB, 0, listB.size() - 1)
Bingo!! You are done.
I'd like you to think of recursive functions in a specific manner. Think of them as like what arguments it take, and what it does. So when you have a recursive call inside the recursive method, you don't need to think how that recursive call will work, rather have an abstraction there and believe that the recursive call will give you the result. Now focus on how you can use this returned result to make this recursive method correctly work.
One way is to turn a simple iterative algorithm -- with a nested loop -- into a recursive one.
An iterative solution, which is not optimised to use a map, but has a O(n²) time complexity, would look like this:
function includesValue(A, v):
for i = 0 to length(A) - 1:
if A[i] == v:
return true
return false
function includesArray(A, B):
for j = 0 to length(B) - 1:
if not includesValue(A, B[j]):
return false
return true
If you translate this into a recursive pattern, you could pass the current index as an extra argument:
function recIncludesValueAfter(A, v, i):
if i >= length(A):
return false
if A[i] == v:
return true
return recIncludesValuesAfter(A, v, i + 1)
function recIncludesSubArray(A, B, j):
if j >= length(B):
return true
if not recIncludesValueAfter(A, B[j], 0):
return false
return recIncludesSubArray(A, B, j + 1)
You would call it with the third argument as 0:
recIncludesSubArray(A, B, 0)
Each of the two recursive functions uses this pattern:
The first if block corresponds to the end of the loop in the iterative version
The second if block corresponds to the body of the for loop in the iterative version (including its potential break-out)
The final recursive call corresponds to the launch of a next iteration in the iterative version.
Optimised by using a Map/Set
If you would need an optimised version, using a set (a map where only the key is important, not the value associated with it), then the iterative version would look like this:
function includesArray(A, B):
setA = new Set
for i = 0 to length(A):
setA.add(A[i])
for j = 0 to length(B) - 1:
if setA.has(B[j]):
return false
return true
Again, we can convert this to a recursive version:
function recAddSubArrayToSet(setA, B, j):
if j >= length(B):
return setA
setA.add(B[j])
return recAddSubArrayToSet(setA, B, j + 1)
function recSetIncludesSubArray(setA, B, j):
if j >= length(B):
return true
if not setA.has(B[j]):
return false
return recSetIncludesSubArray(A, B, j + 1)
function recIncludesSubArray(A, B):
setA = new empty Set
recAddSubArrayToSet(setA, B, 0)
return recSetIncludesSubArray(setA, B, 0)
About built-in functions
You wrote that built-in functions are not allowed. This is a constraint that only makes sense when you have a specific target programming language in mind. In pseudo code there is no concept of built-in functions.
Some languages will provide maps/sets/dictionaries in a way where you can only do something useful with them by calling methods on them (built-in), while other languages will allow you to apply operators (like +) on them, and use an in operator to test membership.
But even getting the size of an array may need a function call in some languages. So this constraint really only makes sense in the context of a specific programming language.
Pseudocode for a function that recursively tests whether the array A contains a given element, x might look something like this:
function isMember(x, A):
if A = [] then return false
if x = A[0] then return true
return isMember(x, A[1..-1])
end
This function is built on the premise that to test if x is a member of A, we can test to see if the first element of A, denoted A[0], is the same as x. If so, we can terminate the function and return true. If not, then call the function again, passing it all the elements of array A except the first one that we already tested. I've denoted the remaining elements of A with A[1..-1], i.e. element numbers 1 through to -1, which in some languages is another way to refer to the last element.
Now during the second call to this function, x is being compared to the first element of A[1..-1], which is, of course, the second element of A. This recurses over and over, each time shrinking the size of array A the element at the top of the list is tested and discarded.
Then we eventually reach the final case, where there are no more elements left in A to test, which results in the final recursive call to the function being passed an empty array. In this situation, we can infer that every element in A failed to match with x, and so we can safely return false, stating the x is not a member of A.
Now, in order to determine whether a given array B is contained by array A, each element in B needs to undergo the test described above. If isMember() returns true for every element in B, then B must be contained by A. If any one element causes isMember() to return false, then we can stop further testing because B contains elements that are not in A, so it cannot be a subarray.
Here's some pseudocode that illustrates this recursive process:
function containedBy(B, A):
if B = [] then return true
let x := B[0]
if not isMember(x, A) then return false
return containedBy(B[1..-1], A)
end
It's very similar in many ways to the first function, which isn't surprising. This time, however, array B is reduced in size with each recursion, as its lead element is passed through to the isMember() function, then discarded upon isMember() returning true. As before, the final case after the last recursive call to containedBy() passes through an empty list in place of B. This can only mean that every element of B successfully passed the membership test with A, so we return true to confirm that B is, indeed, contained by A.
How would I write this programming logic into a functional method signature? I am attempting to loop/traverse an array until a condition is met, then break upon that condition. I'm mostly trying my best to avoid var and breakable from scala.util.control.Breaks. It makes use of a closure, in this case, dictionary, to check if a condition/predicate is met. The idea is that I am looping through an array until the predicate is met. I'm also avoiding converting my array to list. Would use of an array not allow me to splice the array, for example, to do pattern matching?
val dictionary = Array.fill(128)(false)
def isUnique(array: Array[Char]): Option[Char] = {
// traverse each element of the array {
// if a character.toInt is in the dictionary, insert into dictionary
// exit loop, with the character which broke the loop
// else
// set dictionary(character.toInt) to true and continue looping
// }
}
Here's an example use case:
val word = "abcdefggghijklmnopqrstuvqxyz".toArray
val charThatBrokeIt = isUnique(word)
Edit: Feel free to suggest or propose other return types as well, such as a Boolean, Tuple, Case Class, or any others. Option[Char] might not be a good resultant value on my part. For example. I may have returned false in the case that loop broke out early (short-circuited) or not.
First, a String already acts like a collection, so you should just use String instead of Array[Char]. Second, you can take advantage of laziness to allow short-circuiting while still splitting the algorithm into parts, using .view.
def breaksUnique(word: String): Option[Char] = {
val cumulativeSets = word.view.scanLeft(Set.empty[Char]){_ + _}
val zipped = cumulativeSets zip word
val nonDupsDropped = zipped dropWhile {case (set, char) => !(set contains char)}
nonDupsDropped.map{_._2}.headOption
}
The first two lines are written as if they process the entire word, but because they operate on a view, they are only calculated as needed.
cumulativeSets is a sequence of sets of every character that has been seen up to that point. If you ran it on "abb", you would get Set(), Set(a), Set(a,b), Set(a,b). That is combined with the original word using zip, giving (Set(),a), (Set(a),b), (Set(a,b),b). We then just have to drop all the pairs where the character doesn't appear in the set, then return the first element that wasn't dropped.
Early breakout always suggests recursion.
def isUnique(array: Array[Char]): Option[Char] = {
def getDup(index: Int, acc: Set[Char]): Option[Char] =
if (array.isDefinedAt(index))
if (acc(array(index))) Some(array(index))
else getDup(index+1, acc + array(index))
else None
getDup(0, Set.empty[Char])
}
Usage:
val word = "abcdefggghijklmnopqrstuvqxyz".toArray
val charThatBrokeIt = isUnique(word)
//charThatBrokeIt: Option[Char] = Some(g)
I'm trying to read in multiple files (csv, 2 columns) with belonging names (String) and interpolation methods smoothness (String). By using a record I get a nice GUI in Dymola:
To explain my problem, here is a simplified model:
model Test_Strings
parameter String x[:];
parameter Integer k = size(x,1);
Integer i;
initial algorithm
i := 0;
Modelica.Utilities.Streams.print(String(i));
Modelica.Utilities.Streams.print(String(k));
equation
algorithm
when sample(1,1) then
i :=pre(i) + 1;
Modelica.Utilities.Streams.print(String(i));
for j in 1:k loop
Modelica.Utilities.Streams.print(x[j]);
end for;
end when;
end Test_Strings;
The GUI for this looks as follows:
The code to run it:
model Test_Strings_System
Test_Strings test_Strings(x={"a","b","c","d"});
end Test_Strings_System;
This will give the following result in the console:
Now, if I try to use a record:
record MyRecord2
parameter String name = "NoName";
end MyRecord2;
And adapt the model (only the first parameter line MyRecord2 x[:] and within the for loop x[j].name changed):
model Test_Strings2
parameter MyRecord2 x[:];
parameter Integer k = size(x,1);
Integer i;
initial algorithm
i := 0;
Modelica.Utilities.Streams.print(String(i));
Modelica.Utilities.Streams.print(String(k));
equation
algorithm
when sample(1,1) then
i :=pre(i) + 1;
Modelica.Utilities.Streams.print(String(i));
for j in 1:k loop // k must be fixed number if equation
Modelica.Utilities.Streams.print(x[j].name); // j must be fixed number if algorithm
end for;
end when;
end Test_Strings2;
Then I get a translation error: Internal error: failed to expand string.
If I fix k or j within the for loop to a given number (let's say 3) then it works, but depending if it's within an algorithm or equation section (see comments within code).
I had similar problems with flexible array sizes and still don't understand how to solve it.
Do I have to use functions?
How can I use flexible array sizes which are defined depending on external data, selected as parameters prior to simulation (e.g. the length of a table)?
Or is the problem in this case somewhere else?
Thanks.
You can change the model to have an array of records as visible parameters, but internally use an array of strings (tested with Dymola 2017 and later):
model Test_Strings2
parameter MyRecord2 x[:];
parameter Integer k = size(x,1);
Integer i;
protected
parameter String s[:]=x.name; // Hack
initial algorithm
i := 0;
Modelica.Utilities.Streams.print(String(i));
Modelica.Utilities.Streams.print(String(k));
equation
algorithm
when sample(1,1) then
i :=pre(i) + 1;
Modelica.Utilities.Streams.print(String(i));
for j in 1:k loop
Modelica.Utilities.Streams.print(s[j]);
end for;
end when;
end Test_Strings2;
I’m trying to change every i-th element of an array, as an event occurs. This could be reached by the following code. In this example every second element should be changed. Due to parameter defined array size, the second argument of the ‘inner’ loop should be set by the variable ‘Upper’.
model Test
Boolean Event;
Real Vector[6](start = ones(6), fixed = true);
Integer Upper;
algorithm
when Event then
Vector := ones(6);
Upper := 3;
for i in {2*j for j in 1:Upper} loop
Vector[i] := Vector[i]+1;
end for;
end when;
equation
Event = if time <=1 then false else true;
end Test;
Dymola doesn’t accept this solution, stating ‘Failed to calculate the last argument of 1:Upper’. When I enter 3 directly instead of ‘Upper’, the code works.
Is there a possibility to fix this? Or is there an easier way to change every i-th element in algorithm section?
Thanks in advance.
I would say this is a Dymola issue. OpenModelica handles it just fine.
This is an alternative formulation that might work better for you (hiding everything in a function should make Dymola happier since there are no dimension sizes that are hard to deduce).
function incrementEveryIth
input Real v[:];
input Integer i;
input Real increment := 1;
output Real o[size(v,1)] := v;
algorithm
o := {if mod(n,i)<>0 then v[n] else (v[n]+increment) for n in 1:size(v,1)};
end incrementEveryIth;
model Test
Boolean Event;
Real Vector[6](start = ones(6), each fixed = true);
algorithm
when Event then
Vector := incrementEveryIth(pre(Vector),2);
end when;
equation
Event = if time <=1 then false else true;
end Test;
Or perhaps this one:
model Test
Boolean Event;
Real Vector[6](start = ones(6), each fixed = true);
algorithm
when Event then
Vector := pre(Vector); // This should not really be needed, but OpenModelica insists
Vector[1:2:6] := Vector[1:2:6] + {1,1,1};
end when;
equation
Event = if time <=1 then false else true;
end Test;
I need to write a program to find the mode. Or the most occurrence of an integer or integers.
So,
1,2,3,4,1,10,4,23,12,4,1 would have mode of 1 and 4.
I'm not really sure what kind of algorithm i should use. I'm having a hard time trying to think of something that would work.
I was thinking of a frequency table of some sort maybe where i could go through array and then go through and create a linked list maybe. If the linked doesn't contain that value add it to the linked, if it does then add 1 to the value.
So if i had the same thing from above. loop through
1,2,3,4,1,10,4,23,12,4,1
Then list is empty so add node with number = 1 and value = 1.
2 doesnt exist so add node with number = 2 and value = 1 and so on.
Get to the 1 and 1 already exists so value = 2 now.
I would have to loop through the array and then loop through linked list everytime to find that value.
Once i am done then go through the linked list and create a new linked list that will hold the modes. So i set the head to the first element which is 1. Then i go through the linked list that contains the occurences and compare the values. If the occurences of the current node is > the current highest then i set the head to this node. If its = to the highest then i add the node to the mode linked list.
Once i am done i loop through the mode list and print the values.
Not sure if this would work. Does anyone see anything wrong with this? Is there an easier way to do this? I was thinking a hash table too, but not really sure how to do that in C.
Thanks.
If you can keep the entire list of integers in memory, you could sort the list first, which will make repeated values adjacent to each other. Then you can do a single pass over the sorted list to look for the mode. That way, you only need to keep track of the best candidate(s) for the mode seen up until now, along with how many times the current value has been seen so far.
The algorithm you have is fine for a homework assignment. There are all sorts of things you could do to optimise the code, such as:
use a binary tree for efficiency,
use an array of counts where the index is the number (assuming the number range is limited).
But I think you'll find they're not necessary in this case. For homework, the intent is just to show that you understand how to program, not that you know all sorts of tricks for wringing out the last ounce of performance. Your educator will be looking far more for readable, structured, code than tricky optimisations.
I'll describe below what I'd do. You're obviously free to use my advice as much or as little as you wish, depending on how much satisfaction you want to gain at doing it yourself. I'll provide pseudo-code only, which is my standard practice for homework questions.
I would start with a structure holding a number, a count and next pointer (for your linked list) and the global pointer to the first one:
typedef struct sElement {
int number;
int count;
struct sElement *next;
} tElement;
tElement first = NULL;
Then create some functions for creating and using the list:
tElement *incrementElement (int number);
tElement *getMaxCountElement (void);
tElement *getNextMatching (tElement *ptr, int count);
Those functions will, respectively:
Increment the count for an element (or create it and set count to 1).
Scan all the elements returning the maximum count.
Get the next element pointer matching the count, starting at a given point, or NULL if no more.
The pseudo-code for each:
def incrementElement (number):
# Find matching number in list or NULL.
set ptr to first
while ptr is not NULL:
if ptr->number is equal to number:
return ptr
set ptr to ptr->next
# If not found, add one at start with zero count.
if ptr is NULL:
set ptr to newly allocated element
set ptr->number to number
set ptr->count to 0
set ptr->next to first
set first to ptr
# Increment count.
set ptr->count to ptr->count + 1
def getMaxCountElement (number):
# List empty, no mode.
if first is NULL:
return NULL
# Assume first element is mode to start with.
set retptr to first
# Process all other elements.
set ptr to first->next
while ptr is not NULL:
# Save new mode if you find one.
if ptr->count is greater than retptr->count:
set retptr to ptr
set ptr to ptr->next
# Return actual mode element pointer.
return retptr
def getNextMatching (ptr, number):
# Process all elements.
while ptr is not NULL:
# If match on count, return it.
if ptr->number is equal to number:
return ptr
set ptr to ptr->next
# Went through whole list with no match, return NULL.
return NULL
Then your main program becomes:
# Process all the numbers, adding to (or incrementing in) list .
for each n in numbers to process:
incrementElement (n)
# Get the mode quantity, only look for modes if list was non-empty.
maxElem = getMaxCountElement ()
if maxElem is not NULL:
# Find the first one, whil exists, print and find the next one.
ptr = getNextMatching (first, maxElem->count)
while ptr is not NULL:
print ptr->number
ptr = getNextMatching (ptr->next, maxElem->count)
If the range of numbers is known in advance, and is a reasonable number, you can allocate a sufficiently large array for the counters and just do count[i] += 1.
If the range of numbers is not known in advance, or is too large for the naive use of an array, you could instead maintain a binary tree of values to maintain your counters. This will give you far less searching than a linked list would. Either way you'd have to traverse the array or tree and build an ordering of highest to lowest counts. Again I'd recommend a tree for that, but your list solution could work as well.
Another interesting option could be the use of a priority queue for your extraction phase. Once you have your list of counters completed, walk your tree and insert each value at a priority equal to its count. Then you just pull values from the priority queue until the count goes down.
I would go for a simple hash table based solution.
A structure for hash table containing a number and corresponding frequency. Plus a pointer to the next element for chaining in the hash bucket.
struct ItemFreq {
struct ItemFreq * next_;
int number_;
int frequency_;
};
The processing starts with
max_freq_so_far = 0;
It goes through the list of numbers. For each number, the hash table is looked up for a ItemFreq element x such that x.number_ == number.
If no such x is found, then a ItemFreq element is created as { number_ = number, frequency_ = 1} and inserted into the hash table.
If some x was found then its frequency_ is incremented.
If frequency_ > max_freq_so_far then max_freq_so_far = frequency
Once traversing through the list of numbers of complete, we traverse through the hash table and print the ItemFreq items whose frequency_ == max_freq_so_far
The complexity of the algorithm is O(N) where N is the number of items in the input list.
For a simple and elegant construction of hash table, see section 6.6 of K&R (The C Programming Language).
This response is a sample for the idea of Paul Kuliniewicz:
int CompInt(const void* ptr1, const void* ptr2) {
const int a = *(int*)ptr1;
const int b = *(int*)ptr2;
if (a < b) return -1;
if (a > b) return +1;
return 0;
}
// This function leave the modes in output and return the number
// of modes in output. The output pointer should be available to
// hold at least n integers.
int GetModes(const int* v, int n, int* output) {
// Sort the data and initialize the best result.
qsort(v, v + n, CompInt);
int outputSize = 0;
// Loop through elements while there are not exhausted.
// (look there is no ++i after each iteration).
for (int i = 0; i < n;) {
// This is the begin of the new group.
const int begin = i;
// Move the pointer until there are no more equal elements.
for (; i < n && v[i] == v[begin]; ++i);
// This is one-past the last element in the current group.
const int end = i;
// Update the best mode found until now.
if (end - begin > best) {
best = end - begin;
outputSize = 0;
}
if (end - begin == best)
output[outputSize++] = v[begin];
}
return outputSize;
}