Trying to find out how to get exact change in coral - google-coral

the problem goes like this
Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. End each line with a newline. The coin types are dollars, quarters, dimes, nickels, and pennies. Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies.
Ex: If the input is:
0
or less than 0, the output is:
no change
Ex: If the input is:
45
the output is:
1 quarter
2 dimes

Related

SPSS Compute a new variable with the last four digits of a numeric variable

I have a numeric variable (year of birth) in SPSS and i would like to take the last for digits out of it. Most values are like 1988, 2001, 1948 etc. But about 250 respondents entered their year of birth like 30-2-1947, or 2-9-1984 etc. That means not all values have the same length. By taking the last 4 digits into a new variable I could create an age category for all the respondents.
How can I do that?
I tried by converting the variable to a string and using substr to get a part of the value, but I always had to choose a starting point. I want to start from the last digit and then move backwards.
Instead of using SUBSTR() you can try using RIGHT() to grab the last four digits.
* convert yob to string variable.
ALTER TYPE yob (A10).
EXE .
* use RIGHT to extract the last 4 digits and convert to numeric.
COMPUTE n_yob = NUMBER(RIGHT(yob, 4, F4)) .
EXE .
You can now use n_yob to calcuate age (ex: COMPUTE age = 2022-n_yob .). You can also use ALTER_TYPE again if you want to convert yob back to it's original type.

analyze and validate string and substring

hello I want to select a subchain and analyze it example:
my chain to analyze
NADSU 78000mc0cl0Css
NADBY 7808810008659
PAT 1 21D 089
I need to read and then analyze line by line of a .txt file after that I would like to verify through an if or case cycle that the paramenters could be fulfilled. I clarify the txt file has about 300 lines
the first three characters must be letters with a specific format the following is usually the 4 character or 5 that has a number depends on that number means something and the last is the purchase order number or the amount or type of wrap ... ... this is a generalized idea I'm only giving the minimum to understand ...
Annex example and my code
I put two real cases of the line to analyze
--- here the letters nad = purchase order BY = supplier the remaining one is the purchase order number
NADBY 7808810008659
--- here Pat means = type of payment. the first number = the type of payment if it is one the type of payment is counted if it is two credits. the average 21d = number of days to issue clearance and the 089 = number of days to pay
PAT 1 21D 089
--- here the letters nad = purchase order BY = buyer css = a boxes but it can be ss = minicams
NADSU 78000mc0cl0Css
fileopen ('nombre_doc.txt',r) do |fichero1|
fileopen ('copianombre_doc.txt',w) do |fichero2|
while linea= fichero1.gets
fichero1=gsub(/\s+/,'')

Lua random number to the 8th decimal place

How do I get a random number in Lua to the eighth decimal?
Example : 0.00000001
I have tried the following and several variations of this but can not get the format i need.
math.randomseed( os.time() )
x = math.random(10000000,20000000) * 0.00000001
print(x)
i would like to put in say 200 and get this 0.00000200
Just grab a random number from 0-9, and slide it down 6 places. You can use format specifiers to create the string representation of the number that you desire. For floats we use %f, and indicate how many decimal places we want to have with an intermediate .n, where n is a number.
math.randomseed(os.time())
-- random(9) to exclude 0
print(('%.8f'):format(math.random(0, 9) * 1e-6))
--> '0.00000400'
string.format("%.8f",math.random())
to help anyone else. my question should have been worded a bit better. i wanted to be able to get random numbers and get it to the 8th decimal place.
but i wanted to be able to have those numbers from 1-10,000 so he is updated how i wanted it and the help of Oka got me to this
math.randomseed(os.time())
lowest = 1
highest = 7000
rand=('%.8f'):format(math.random(lowest, highest) / 100000000)
print(rand)
Hope this helps someone else or if it can be cleaned up please let me know

How to evaluate a search/retrieval engine using trec_eval?

Is there any body who has used TREC_EVAL? I need a "Trec_EVAL for dummies".
I'm trying to evaluate a few search engines to compare parameters like Recall-Precision, ranking quality, etc for my thesis work. I can not find how to use TREC_EVAL to send queries to the search engine and get a result file which can be used with TREC_EVAL.
Basically, for trec_eval you need a (human generated) ground truth. That has to be in a special format:
query-number 0 document-id relevance
Given a collection like 101Categories (wikipedia entry) that would be something like
Q1046 0 PNGImages/dolphin/image_0041.png 0
Q1046 0 PNGImages/airplanes/image_0671.png 128
Q1046 0 PNGImages/crab/image_0048.png 0
The query-number identifies therefore a query (e.g. a picture from a certain category to find similiar ones). The results from your search engine has then to be transformed to look like
query-number Q0 document-id rank score Exp
or in reality
Q1046 0 PNGImages/airplanes/image_0671.png 1 1 srfiletop10
Q1046 0 PNGImages/airplanes/image_0489.png 2 0.974935 srfiletop10
Q1046 0 PNGImages/airplanes/image_0686.png 3 0.974023 srfiletop10
as described here. You might have to adjust the path names for the "document-id". Then you can calculate the standard metrics trec_eval groundtrouth.qrel results.
trec_eval --help should give you some ideas to choose the right parameters for using the measurements needed for your thesis.
trec_eval does not send any queries, you have to prepare them yourself. trec_eval does only the analysis given a ground trouth and your results.
Some basic information can be found here and here.

Constrained Sequence to Index Mapping

I'm puzzling over how to map a set of sequences to consecutive integers.
All the sequences follow this rule:
A_0 = 1
A_n >= 1
A_n <= max(A_0 .. A_n-1) + 1
I'm looking for a solution that will be able to, given such a sequence, compute a integer for doing a lookup into a table and given an index into the table, generate the sequence.
Example: for length 3, there are 5 the valid sequences. A fast function for doing the following map (preferably in both direction) would be a good solution
1,1,1 0
1,1,2 1
1,2,1 2
1,2,2 3
1,2,3 4
The point of the exercise is to get a packed table with a 1-1 mapping between valid sequences and cells.
The size of the set in bounded only by the number of unique sequences possible.
I don't know now what the length of the sequence will be but it will be a small, <12, constant known in advance.
I'll get to this sooner or later, but though I'd throw it out for the community to have "fun" with in the meantime.
these are different valid sequences
1,1,2,3,2,1,4
1,1,2,3,1,2,4
1,2,3,4,5,6,7
1,1,1,1,2,3,2
these are not
1,2,2,4
2,
1,1,2,3,5
Related to this
There is a natural sequence indexing, but no so easy to calculate.
Let look for A_n for n>0, since A_0 = 1.
Indexing is done in 2 steps.
Part 1:
Group sequences by places where A_n = max(A_0 .. A_n-1) + 1. Call these places steps.
On steps are consecutive numbers (2,3,4,5,...).
On non-step places we can put numbers from 1 to number of steps with index less than k.
Each group can be represent as binary string where 1 is step and 0 non-step. E.g. 001001010 means group with 112aa3b4c, a<=2, b<=3, c<=4. Because, groups are indexed with binary number there is natural indexing of groups. From 0 to 2^length - 1. Lets call value of group binary representation group order.
Part 2:
Index sequences inside a group. Since groups define step positions, only numbers on non-step positions are variable, and they are variable in defined ranges. With that it is easy to index sequence of given group inside that group, with lexicographical order of variable places.
It is easy to calculate number of sequences in one group. It is number of form 1^i_1 * 2^i_2 * 3^i_3 * ....
Combining:
This gives a 2 part key: <Steps, Group> this then needs to be mapped to the integers. To do that we have to find how many sequences are in groups that have order less than some value. For that, lets first find how many sequences are in groups of given length. That can be computed passing through all groups and summing number of sequences or similar with recurrence. Let T(l, n) be number of sequences of length l (A_0 is omitted ) where maximal value of first element can be n+1. Than holds:
T(l,n) = n*T(l-1,n) + T(l-1,n+1)
T(1,n) = n
Because l + n <= sequence length + 1 there are ~sequence_length^2/2 T(l,n) values, which can be easily calculated.
Next is to calculate number of sequences in groups of order less or equal than given value. That can be done with summing of T(l,n) values. E.g. number of sequences in groups with order <= 1001010 binary, is equal to
T(7,1) + # for 1000000
2^2 * T(4,2) + # for 001000
2^2 * 3 * T(2,3) # for 010
Optimizations:
This will give a mapping but the direct implementation for combining the key parts is >O(1) at best. On the other hand, the Steps portion of the key is small and by computing the range of Groups for each Steps value, a lookup table can reduce this to O(1).
I'm not 100% sure about upper formula, but it should be something like it.
With these remarks and recurrence it is possible to make functions sequence -> index and index -> sequence. But not so trivial :-)
I think hash with out sorting should be the thing.
As A0 always start with 0, may be I think we can think of the sequence as an number with base 12 and use its base 10 as the key for look up. ( Still not sure about this).
This is a python function which can do the job for you assuming you got these values stored in a file and you pass the lines to the function
def valid_lines(lines):
for line in lines:
line = line.split(",")
if line[0] == 1 and line[-1] and line[-1] <= max(line)+1:
yield line
lines = (line for line in open('/tmp/numbers.txt'))
for valid_line in valid_lines(lines):
print valid_line
Given the sequence, I would sort it, then use the hash of the sorted sequence as the index of the table.

Resources