What is the difference in Natural when it comes to MOVE and (=) in statements? - mainframe

I have only been programming in Natural for a couple of weeks over the course of a couple of years. I only do enough of it to get myself by.
Question: What is the difference between the MOVE a TO b and the a = b?
Code:
MOVE A TO B
MOVE D TO Y
Or
A = B
C = D

If you are using a Licensed product, you should have access to documentation at your site.
Software AG are the vendor. I found this with a simple internet search: http://documentation.softwareag.com/natural/nat638vms/general/print.htm
That is a manual for Natural on OpenVMS. It makes references to the Mainframe version, and looks good enough to answer your question.
This seems to be, at the simplest level, they are the same. However, if you want to do a calculation, you need the COMPUTE =, that can't be done with MOVE. There are various formats of the MOVE statement.
I have never used Natural, and can't test it. You have access to the product, that along with documentation will allow you to provide a full answer for yourself.

I think from what I can remember of Natural that basically they are they same. But I also remember that there are some difference.
For the most part I used = just because if you are using C++ that is a more common way of looking at it.
MOVE Your-Value TO Another-Value
is for the most part equal to
Another-Value = Your-Value
But I think where it is different slightly is as to what computations that you can and cannot perform with the = rather than the MOVE. You can MOVE to multiple values like below but the = can only move to a single variable.
MOVE A TO C D BaseBallScore
This is very useful if you have to move a lot of values at one time to several different counters but you could move one at a time. Like below
MOVE A TO C
MOVE A TO D
MOVE A TO BaseBallScore
There are also some functions that you can use with the MOVE that make it a nice option. Such as rounding a number
MOVE ROUNDED Value To NewValue <-- ROUNDED can take different parameters
Here is another function SUBSTRING that will let you move a part of a string to another part of the string. Normally I use the = just because that is how the boss does it but the MOVE statement gives a programmer a bit more flexibility.
MOVE SUBSTRING(#A,5,8) TO #B
An online reference for the move is located here:
http://documentation.softwareag.com/natural/nat638vms/print/sm.pdf

Related

how to write decimal numbers as an atomic write in F#?

the decimal type takes 128 bits, so it is not naturally an atomic write.
I tried:
Interlocked.Exchange(ref myField, some new value)
but then I saw that decimal is not part of the supported types with Interlocked.Exchange.
I was thinking that doing a lock may be a little bit heavy for this write. Are there any other options?
As you said, Interlocked.Exchange can only work with 32bit or 64bit values, so it does not support decimal. Aside from using locks, one suggestion from a related C# StackOverflow post is to wrap the value in an object and then use Interlocked.Exchange to swap the object references. In F#, this would look like this:
type Boxed<'T when 'T : struct>(v:'T) =
member x.Value = v
let mutable d1 = Boxed(1M)
let d2 = Boxed(2M)
Interlocked.Exchange(&d1, d2)
The question is whether the overhead of an additional instance is greater than the overhead of using lock - I think this will depend on your specific case. If you have just a few decimals that you're working with, the extra objects may not be such a big deal, but you'll probably need to run some tests to find out.

Is there any way to specify eg (car|cars) in a cucumber step definition?

So I have 2 scenarios....one starts out
Given I have 1 car
The other starts out
Given I have 2 cars
I'd like them to use the same step definition - ie something like this
Given('I have {int} (car|cars)',
I know it's possible to do specify 2 possible values (ie car or cars), but I can't for the life of me remember how. Does anyone know? I'm using typescript, protractor, angular, selenium.
Have also tried
Given(/^I have {int} (car|cars)$
Within cukeExp, the () become optional characters. That is what you want.
So your expression would be
Given('I have {int} car(s)')
Happy to help - More information can be found here: https://cucumber.io/docs/cucumber/cucumber-expressions/ - Switch to JS code at the top.
Luke - Cucumber contributor.
Luke's answer is great and is definitely standard practice when cuking.
I would (and do) take a different approach. I would strongly argue that the complexity of even a single expression like the one he uses isn't worth the step duplication. Let me explain and illustrate.
The fundamental idea behind this approach is that the internals of each step definition must be a single call to a helper method. When you do this you no longer need expressions or regex's.
I would prefer and use in my projects
module CarStepHelper
def create_car(amount: 1)
Lots of stuff to create cars
end
end
World CarStepHelper
Given 'I have one car' do
create_car
end
Given 'I have two cars' do
create_car(amount: 2)
end
to
Given('I have {int} car(s)')
lots of stuff to create cars
end
because
the step definitions are simpler (no regex, no cucumber expression
the stuff to create the cars is slightly simpler (no processing of the regex or expression)
the helper method supports and encourages a wider range of expression e.g.
Given Fred has a car
Given there is a blue car and a red car
the helper method encourages better communication between steps because you can assign its results relative to the step definition e.g.
Given Fred has a car
#freds_car = create_car
end
Given there are two cars
[#car1, #car2] = create_car(amount: 2)
end
Cucumber expressions and cucumbers regex's are very powerful and quite easy to use, but you can Cuke very effectively without ever using them. Step definition efficiency is a myth and often an anti-pattern, if you ensure each step def is just a single call you no longer have to worry about it, and you will avoid the mistake many cukers fall into which is the writing over-complicated step definitions with lots of parameters, regex's and|or expressions.
As far as I know, your step definition should be as below for it to work.
Given(/^I have "([^"]*)?" (car|cars)*$/, (number, item) => {
You can still simplify the first regular expression.
Cheers!

is there a way to calculate every possible order of operation for 1 operation in Python?

Let's say that I have a = '1+2*5/3', there's a specific order to which my machine will evaluate this statement (with eval(a))
I would like to know if there's a line of code (or a function? just an elegant way that could get the job done) that would calculate :
(1+2)*5/3
1+(2*5)/3
1+2*(5/3)
(1+2*5)/3
1+(2*5/3)
(1+2)*(5/3)
1+2*5/3
In this example, I used an operation with 4 factors, so I could just code 1 function for each possibility, but I need to do the same thing with 6 factors and that would just take way too much time and effort since the possibility of different operation order would increase exponentially
It would be also great that it returns everything in a dictionary in this form {operation:result} with the parentheses included, if not i'll find my way around it
edit: as requested, the main goal is to make a program that find the solution to the game " le compte est bon " brute force method, the rules can be found here : https://en.wikipedia.org/wiki/Des_chiffres_et_des_lettres#Le_compte_est_bon_.28.22the_total_is_right.22.29
This is going to be very hard. I recommend you follow these steps:
Create a list to check if the formula has already been calculated
Randomize the order (such as +-*/ and randomly place numbers
Check if rule number`s one is a valid formula. if not try number 1 again
Randomize the order (such as opening and closing parentheses and ^)
Check if the sentence above is a valid formula. if not try number 3 again.
Check the formula through the list and see if it has already been calculated. if it has been calculated then we don't use it and go back to number two. but...
If it is not in the list then we can use it.
Those are the basic steps for common known math symbols, but what about square root?
Another way to do this is by making python move the symbols over like you did with the parentheses, but for EVERYTHING (numbers and symbols(+-/*))
EDIT:
This was before the original question was changed.

Dynamic Programming algorithm shortest path between two

There's an algorithm currently driving me crazy.
I've seen quite a few variations of it, so I'll just try to explain the easiest one I can think about.
Let's say I have a project P:
Project P is made up of 4 sub projects.
I can solve each of those 4 in two separate ways, and each of those modes has a specific cost and a specific time requirement:
For example (making it up):
P: 1 + 2 + 3 + 4 + .... n
A(T/C) Ta1/Ca1 Ta2/Ca2 etc
B(T/C) Tb1/Cb1 etc
Basically I have to find the combination that of those four modes which has the lowest cost. And that's kind of easy, the problem is: the combination has to be lower than specific given time.
In order to find the lowest combination I can easily write something like:
for i = 1 to n
aa[i] = min(aa[i-1],ba[i-1]) + value(a[i])
bb[i] = min(bb[i-1],ab[i-1]) + value(b[i])
ba[i] = min(bb[i-1],ab[i-1]) + value(b[i])
ab[i] = min(aa[i-1],ba[i-1]) + value(a[i])
Now something like is really easy and returns the correct value every time, the lowest at the last circle is gonna be the correct one.
Problem is: if min returns modality that takes the last time, in the end I'll have the fastest procedure no matter the cost.
If if min returns the lowest cost, I'll have the cheapest project no matter the amount of time taken to realize it.
However I need to take both into consideration: I can do it easily with a recursive function with O(2^n) but I can't seem to find a solution with dynamic programming.
Can anyone help me?
If there are really just four projects, you should go with the exponential-time solution. There are only 16 different cases, and the code will be short and easy to verify!
Anyway, the I'm pretty sure the problem you describe is the knapsack problem, which is NP-hard. So, there will be no exact solution that's sub-exponential unless P=NP. However, depending on what "n" actually is (is it 4 in your case? or the values of the time and cost?) there may be a pseudo-polynomial time solution. The Wikipedia article contains descriptions of these.

A reverse inference engine (find a random X for which foo(X) is true)

I am aware that languages like Prolog allow you to write things like the following:
mortal(X) :- man(X). % All men are mortal
man(socrates). % Socrates is a man
?- mortal(socrates). % Is Socrates mortal?
yes
What I want is something like this, but backwards. Suppose I have this:
mortal(X) :- man(X).
man(socrates).
man(plato).
man(aristotle).
I then ask it to give me a random X for which mortal(X) is true (thus it should give me one of 'socrates', 'plato', or 'aristotle' according to some random seed).
My questions are:
Does this sort of reverse inference have a name?
Are there any languages or libraries that support it?
EDIT
As somebody below pointed out, you can simply ask mortal(X) and it will return all X, from which you can simply pick a random one from the list. What if, however, that list would be very large, perhaps in the billions? Obviously in that case it wouldn't do to generate every possible result before picking one.
To see how this would be a practical problem, imagine a simple grammar that generated a random sentence of the form "adjective1 noun1 adverb transitive_verb adjective2 noun2". If the lists of adjectives, nouns, verbs, etc. are very large, you can see how the combinatorial explosion is a problem. If each list had 1000 words, you'd have 1000^6 possible sentences.
Instead of the deep-first search of Prolog, a randomized deep-first search strategy could be easyly implemented. All that is required is to randomize the program flow at choice points so that every time a disjunction is reached a random pole on the search tree (= prolog program) is selected instead of the first.
Though, note that this approach does not guarantees that all the solutions will be equally probable. To guarantee that, it is required to known in advance how many solutions will be generated by every pole to weight the randomization accordingly.
I've never used Prolog or anything similar, but judging by what Wikipedia says on the subject, asking
?- mortal(X).
should list everything for which mortal is true. After that, just pick one of the results.
So to answer your questions,
I'd go with "a query with a variable in it"
From what I can tell, Prolog itself should support it quite fine.
I dont think that you can calculate the nth solution directly but you can calculate the n first solutions (n randomly picked) and pick the last. Of course this would be problematic if n=10^(big_number)...
You could also do something like
mortal(ID,X) :- man(ID,X).
man(X):- random(1,4,ID), man(ID,X).
man(1,socrates).
man(2,plato).
man(3,aristotle).
but the problem is that if not every man was mortal, for example if only 1 out of 1000000 was mortal you would have to search a lot. It would be like searching for solutions for an equation by trying random numbers till you find one.
You could develop some sort of heuristic to find a solution close to the number but that may affect (negatively) the randomness.
I suspect that there is no way to do it more efficiently: you either have to calculate the set of solutions and pick one or pick one member of the superset of all solutions till you find one solution. But don't take my word for it xd

Resources