How to read /manipulate input in ADA? - ada

Reading from standard input, I want to be able to insert a few sets of integers like so and do some math on every 2 integers.
I.E:
"Hello enter some numbers to get their sum":
1 9
2 10 [enter]
OUTPUT:
1 + 9 = 10.
2 + 10 = 12.
I have been successful with only the user being able to enter only 2 integers as I just do
get(numOne);
get(numTwo);
and then something like:
answer := numOne + numTwo;
put_line(answer'img);
But I am new to ADA and don't know how I can scan through all four of the integers I used in my example and only sum the first two then the second two, and if there is more then keep summing them up by two's. Essentially the program would first scan through all the user input and do math on every two integers and keep concatenating them to a result string that we can print at the end. I know how to do it, I am just new to the language and don't how to put it in code. Please ask if you need more info. All help is appreciated.

Just put a loop around your existing logic. - And notice that Ada has nice "infinite" loops. :-)

Related

closed/fixed:Interpertation of basic R code

I have a basic question in regards to the R programming language.
I'm at a beginners level and I wish to understand the meaning behind two lines of code I found online in order to gain a better understanding. Here is the code:
as.data.frame(y[1:(n-k)])
as.data.frame(y[(k+1):n])
... where y and n are given. I do understand that the results are transformed into a data frame by the function as.data.frame() but what about the rest? I'm still at a beginners level so pardon me if this question is off-topic or irrelevant in this forum. Thank you in advance, I appreciate every answer :)
Looks like you understand the as.data.frame() function so let's look at what is happening inside of it. We're looking at y[1:(n-k)]. Here, y is a vector which is a collection of data points of the same type. For example:
> y <- c(1,2,3,4,5,6)
Try running that and then calling back y. What you get are those numbers listed out. Now, consider the case you want to just call out the number 1 in that vector. How would you do that? Well, this is where the brackets come into play. If you wanted to just call the number 1 in y:
> y[1]
[1] 1
Therefore, the brackets are a way of calling out or indexing specific items in the vector. Note that the indexing starts at the value 1 and goes up to the number of items in the vector, or length. One last thing before we go back to the example you gave. What if we want to index the numbers 1, 2, and 3 from the vector but not the rest?
> y[1:3]
[1] 1 2 3
This is where the colon comes into play. It allows us to reference a subset of the numbers. However, it will reference all the numbers between the index left of the colon and right of it. Try this out for yourself in R! Play around and see what happens.
Finally going back to your example:
y[1:(n-k)]
How would this work based on what we discussed? Well, the colon means that we are indexing all values in the vector y from two index values. What are those values? Well, they are the numbers to the left and right of the colon. Therefore, we are asking R to give us the values from the first position (index of 1) to the (n-k) position. Therefore, it's important to know what n and k are. If n is 4 and k is 1 then the command becomes:
y[1:3]
The same logic can apply to the second as.data.frame() command in your question. Essentially, R is picking out different numbers from a vector y and multiplying them together.
Hope this helps. The best way to learn R is to play around with a command, throw different numbers at it, guess what will happen, and then see what happens!

A very complex combinations task that has been bugging me for 7 months

Some seven months ago I went to a job interview at a very big company. They gave me this task to solve, and for the past 7 months I can't seem to be able to find a solution.
Here is the task:
Some database has A entries. How many combinations (without repetition) with B amount (B < A) elements made out of A are there, that for any given B (contained in those A) different elements always contain at least X% of C entries (C < B) out of B given (C/B)? Include pattern for obtaining all of them. In short, we need:
Formula for calculation of how many combinations satisfy the above conditions
Formula for listing all of them. (any programming language or just detailed descriptive and any format)
Note: Both are mandatory, as those need to be set in a separate table in db.
After 2 hours of being totally clueless I was given a simplified version:
Some database has 50 entries. How many combinations (without repetition) with 9 elements made out of those 50 are there, that for any given 9 different elements (contained in those 50) always contain at least 15% of 6 entries out of given 9 (6/9)? Include pattern for obtaining all of them. In short, we need:
Formula for calculation of how many combinations satisfy the above conditions
Formula for listing all of them. (any programming language or just detailed descriptive and any format)
Note: Both are mandatory, as those need to be set in a separate table in db.
Edit: To explain further. Let us say the result of (1.) is D possible subsets (combinations without repetition) with 9 elements from A. And some user of the database (or software using it) enters random 9 elements (from |A| = 50 set). This always needs to result in, that at least 15% of those D subsets has 6 out of 9 that user entered.
It doesn't matter how many of those D has 1/9, 2/9, 3/9, 4/9, 5/9, 7/9, 8/9 and 9/9, the only thing that matters is that 15% and above have 6/9, for any 9/50 entered. Oh and D needs to be the minimal possible.
Edit2: Even further. Example: Set of A=50 entries is given. We need minimal amount of possible combinations/subsets without repetition with B=9 elements from those 50, that satisfy the following: When a user enters random 9 entries, 15%+ of resulting subsets must have 6 out of 9 that user entered. And the resulting subset must be uniform for any 9 that user can enter.
And I still failed. And I am still clueless of how to solve something like this.
I explain the simplified version: Let's name your database A, with |A|=50 elements in it. Now 6 elements of these 50 are special somehow and we want to keep track of them. We call the set of these 6 elements C.
Now to our job: We should count all subsets X of A with exactly 9 elements and at least 15% of their elements should come from C. Since 15% of 9 is 1.35 we need at least 2 elements of C in our sets X.
We know that there are binomial(50,9)=2505433700 subsets of A with 9 elements. Now lets count how many of them violate your criteria: there are 44 elements in A which are not in C, so there are binomial(44,9) subsets of A that contain no elements from C. Next we count how many 9-element-subsets of A contain exactly one element of C: We take a random 8-element-subset from A without C and put exactly one element from C to it, so we get 6*binomial(44,8) possibilities.
Now we can write our result, by taking all 9-element-subsets from A and subtracting those, that violate your criteria:
binomial(50,9) - binomial(44,9) - 6*binomial(44,8) = 733107430.
Ok... now we know how much there are. But how do we list them?
Let's use some pseude-code to do it:
AminC := setminus(A,C)
for j in 2..6 do
for X1 in subsets(C, j) do
for X2 in subsets(AminC, 9-j) do
print(setadd(X1,X2))
This algorithm yields an alternative way of counting your sets:
binomial(6,2)*binomial(44,7) +...+ binomial(6,6)*binomial(44,3)=733107430.
Hope this helps..

Robot Framework Get Text

I am using Robot Framework Selenium using python. I need help with grabbing a certain part of the string, without getting an exterior library. lets say the text says " Your range price for your product is from $0- 400" So i want to be able to get the 400 and paste is somewhere else in the test. The number isnt always 400 sometimes it may be 55 or something different. So i think i would need a GET TEXT Starting from the dollar sign count two spaces and take whatever is left. or i can get the first number and add 10. Like in this example its 0 so i want it to paste 10. Please Let me Know!
"Fetch From Right" should cover that. You just have to identify the stop point, which in your example looks like it would be the hyphen between the two number values.
for example: to extract the last five digits of this string ABC12345 you would want to create a variable to assign the text to.
${number}= Get Text (defined location of text, minus parentheses)
Then use this command to retrieve the remainder of the string after your identified stop point (C).
${desiredNumber}= Fetch From Right ${number} C
This is essentially creating a new variable, which is defined as the extracted values from the original variable after that point.
Hopefully this helps.
You could use the built-in function Evaluate to use the python underlying system:
${my_string} Get Text <your-identifier-here>
${result} Evaluate ${my_string}[${my_string}.rfind('-') + 1:]
Also, please have a look if you can use one of the standard available libraries: http://robotframework.org/robotframework/

Stopping a large number of zeros being printed (not scientific notation)

What I'm trying to achieve is to have all printed numbers display at maximum 7 digits. Here are examples of what I want printed:
0.000000 (versus the actual number which is 0.000000000029481.....)
0.299180 (versus the actual number which is 0.299180291884922.....)
I've had success with the latter types of numbers by using options(scipen=99999) and options(digits=6). However, the former example will always print a huge number of zeros followed by five non-zero digits. How do I stop this from occurring and achieve my desired result? I also do not want scientific notation.
I want this to apply to ALL printed numbers in EVERY context. For example if I have some matrix, call it A, and I print this matrix, I want every element to just be 6-7 digits. I want this to be automatic for every print in every context; just like using options(digits=6) and options(scipen=99999) makes it automatic for every context.
You can define a new print method for the type you wish to print. For example, if all your numbers are doubles, you can create
print.double=function(x){sprintf("%.6f", x)}
Now, when you print a double (or a vector of doubles), the function print.double() will be called instead of print.default().
You may have to create similar functions print.integer(), print.complex(), etc., depending on the types you need to print.
To return to the default print method, simply delete the function print.double().
Are all your numbers < 1? You could try a simple sprintf( "%.6f", x ). Otherwise you could try wrapping things to sprintf based on the number of digits; check ?sprintf for other details.

Markov Algorithm for Random Writing

I got a litte problem understanding conceptually the structure of a random writing program (that takes input in form of a text file) and uses the Markov algorithm to create a somewhat sensible output.
So the data structure i am using is to use cases ranging from 0-10. Where at case 0: I count the number a letter/symbol or digit appears and base my new text on this to simulate the input. I have already implemented this by using an Map type that holds each unique letter in the input text and a array of how many there are in the text. So I can simply ask for the size of the array for the specific letter and create output text easy like this.
But now I Need to create case1/2/3 and so on... case 1 also holds what letter is most likely to appear after any letter aswell. Do i need to create 10 seperate arrays for these cases, or are there an easier way?
There are a lot of ways to model this. One approach is as you describe, with an multi-dimensional array where each index is the following character in the chain and the final result is the count.
# Two character sample:
int counts[][] = new int[26][26]
# ... initialize all entries to zero
# 'a' => 0, 'b' => 1, ... 'z' => 25
# For example for the string 'apple'
# Note: I'm only writing this like this to show what the result is, it should be in a
# loop or function ...
counts['a'-'a']['p'-'a']++
counts['p'-'a']['p'-'a']++
counts['p'-'a']['l'-'a']++
counts['l'-'a']['l'-'e']++
Then to randomly generate names you would count the number of total outcomes for a given character (ex: 2 outcomes for 'p' in the previous example) and pick a weighted random number for one of the possible outcomes.
For smaller sizes (say up to 4 characters) that should work fine. For anything larger you may start to run into memory issues since (assuming you're using A-Z) 26^N entries for an N-length chain.
I wrote something like a couple of years ago. I think I used random pages from Wikipedia to for seed data to generate the weights.

Resources