What does the MID mean in this pseudo code? - pseudocode

this is the picture of the the algorithim, i was just wondering if anyone here knew what they mean by MID() at the bottom and explain what its actually doing.

As Illya said in the comments, MID is a substring function.
So what that code is doing is generating a random number between 1 and 10, if the number is 3 or 6 it will loop and create another random number, probably because the format of the string is either dd/mm/yyyy, mm/dd/yyyy, dd-mm-yyyy etc. where the 3rd and 6th characters are not numeric.
If the random number is not 3 or 6, it will exit the loop and return the character that is at the random position.
Not sure why the While has a condition P>10 as the RANDINT should not return a value greater than 10.

Related

How many times does a zero occur on an odometer

I am solving how many times a zero occus on an odometer. I count +1 everytime I see a zero.
10 -> +1
100-> +2 because in 100 I see 2 zero's
10004 -> +3 because I see 3 zero's
So I get,
1 - 100 -> +11
1 - 500 -> +91
1 - 501 -> +92
0 - 4294967295-> +3825876150
I used rubydoctest for it. I am not doing anything with begin_number yet. Can anyone explain how to calculate it without a brute force method?
I did many attempts. They go well for numbers like 10, 1000, 10.000, 100.000.000, but not for numbers like 522, 2280. If I run the rubydoctest, it will fail on # >> algorithm_count_zero(1, 500)
# doctest: algorithm_count_zero(begin_number, end_number)
# >> algorithm_count_zero(1, 10)
# => 1
# >> algorithm_count_zero(1, 1000)
# => 192
# >> algorithm_count_zero(1, 10000000)
# => 5888896
# >> algorithm_count_zero(1, 500)
# => 91
# >> algorithm_count_zero(0, 4294967295)
# => 3825876150
def algorithm_count_zero(begin_number, end_number)
power = Math::log10(end_number) - 1
if end_number < 100
return end_number/10
else
end_number > 100
count = (9*(power)-1)*10**power+1
end
answer = ((((count / 9)+power)).floor) + 1
end
end_number = 20000
begin_number = 10000
puts "Algorithm #{algorithm_count_zero(begin_number, end_number)}"
As noticed in a comment, this is a duplicate to another question, where the solution gives you correct guidelines.
However, if you want to test your own solution for correctness, i'll put in here a one-liner in the parallel array processing language Dyalog APL (which i btw think everyone modelling mathemathics and numbers should use).
Using tryapl.org you'll be able to get a correct answer for any integer value as argument. Tryapl is a web page with a backend that executes simple APL code statements ("one-liners", which are very typical to the APL language and it's extremely compact code).
The APL one-liner is here:
{+/(c×1+d|⍵)+d×(-c←0=⌊(a|⍵)÷d←a×+0.1)+⌊⍵÷a←10*⌽⍳⌈10⍟⍵} 142857
Copy that and paste it into the edit row at tryapl.org, and press enter - you will quickly see an integer, which is the answer to your problem. In the code row above, you can see the argument rightmost; it is 142857 this time but you can change it to any integer.
As you have pasted the one-liner once, and executed it with Enter once, the easiest way to get it back for editing is to press [Up arrow]. This returns the most recently entered statement; then you can edit the number sitting rightmost (after the curly brace) and press Enter again to get the answer for a different argument.
Pasting teh code row above will return 66765 - that many zeroes exist for 142857.
If you paste this 2 characters shorter row below, you will see the individual components of the result - the sum of these components make up the final result. You will be able to see a pattern, which possibly makes it easier to understand what happens.
Try for example
{(c×1+d|⍵)+d×(-c←0=⌊(a|⍵)÷d←a×+0.1)+⌊⍵÷a←10*⌽⍳⌈10⍟⍵} 1428579376
0 100000000 140000000 142000000 142800000 142850000 142857000 142857900 142857930 142857937
... and see how the intermediate results contain segments of the argument 1428579376, starting from left! There are as many intermediate results as there are numbers in the argument (10 this time).
The result for 1428579376 will be 1239080767, ie. the sum of the 10 numbers above. This many zeroes appear in all numbers between 1 and 1428579376 :-).
Consider each odometer position separately. The position x places from the far right changes once every 10^x times. By looking at the numbers to its right, you know how long it will be until it next changes. It will then hold each value for 10^x times before changing, until it reaches the end of the range you are considering, when it will hold its value at that time for some number of times that you can work out given the value at the very end of the range.
Now you have a sequence of the form x...0123456789012...y where you know the length and you know the values of x and y. One way to count the number of 0s (or any other digit) within this sequence is to clip off the prefix from x.. to just before the first 0, and clip off the suffix from just after the last 9 to y. Look for 0s n in this suffix, and measure the length of the long sequence from prefix to suffix. This will be of a length divisible by 10, and will contain each digit the same number of times.
Based on this you should be able to work out, for each position, how often within the range it will assume each of its 10 possible values. By summing up the values for 0 from each of the odometer positions you get the answer you want.

Show last digit only in os.time()

I'm trying to code a function that prints something different every second.
Easiest way to do this (I think) is by checking if the integer is even or odd.
On even numbers, it should print 'x'.
On odd numbers, it should print 'y'.
My problem is that I cant figure out how to change the integer value of os.time to only show me the very last digit (only show 0 to 9) so I can easily implement this in my code.
This is what I have so far:
local seconds = os.time() --but only show last digit, e.g. 0 to 9
function changeStr()
if (seconds % 2 == 0) then
print("x")
else
print("y")
end
end
how to change the integer value of os.time to only show me the very last digit (only show 0 to 9)
Divide by 10 and take the remainder. That'll give you only the 10s place.
seconds % 10
If you want a string containing the last digit of the time, try
string.sub(os.time(),-1)

Formula to return random string from list of strings?

I want to display random courses (MBA, MSc) in OpenOffice Calc. I tried:
=RANDBETWEEN('MBA', 'MSc')
and
=RAND('MBA', 'MSc')`
but they don't work as desired.
In OpenOffice Calc, the RAND function returns a value between 0 and 1 - so you will have to combine different formulas to get a random selection from two text values. The following steps are needed:
round the result of rand to an integer;
based on that integer, select from list.
Try the following formula:
=CHOOSE(ROUND(RAND()+1);"MBA";"MSc")
or split up on different lines:
=CHOOSE(
ROUND(
RAND()+1
);
"MBA";
"MSc"
)
Depending on you localization, you max have to replace the argument separators ; by :.
Explanation:
the CHOOSE formula chooses from a list of values; the selection is based on the first argument (here: the rounded random value);
the ROUND formula rounds the decimal to integer;
RAND() + 1 makes sure that the resulting random value is either 1 or 2.
I'm not a user with a deep understanding of spreadsheets, but I thought this was an interesting question. I wanted to play around with an example with more than two choices and tried an exercise with six choices.
The OpenOffice wiki for the RAND function says...
RAND()*(b-a) + a
returns a random real number between a and b.
Since the CHOOSE function needed integers 1 to 6 to make the 6 choices, RAND would need to output numbers from 1 to 6, I let a=1 and b=6.
This was tested,
=CHOOSE(ROUND(5*RAND()+1);"Business";"Science";"Art";"History";"Math";"Law")
That output a random selection of the six courses, but I found the six choices did not have equal chances of selection. Business and Law had a 1 in 10 chance of being selected and Science, Art, History, and Math had a 2 in 10 chance of being selected.
=CHOOSE(ROUNDUP(6*RAND()+0.00001);"Business";"Science";"Art";"History";"Math";"Law")
Seems to give all six courses a practically equal chance for selection.

DBC Exercise 9 - Calculate the Missing Number

Exercise 9 - 45 minutes
You have been given a list of sequential numbers from 1 to 10,000, but they are all out of order; furthermore, 1 number is missing from the list. The goal is to find which number is missing.Write out in plain English your strategy for solving this problem. Be as concise as possible.
Write Ruby code that takes this list of numbers as an argument, and returns the missing number.
My initial impression is some sort of sort function will help me put the array into order, but then I reread the problem and its not asking for a sorted sequence, it's asking for a missing number. The next step to consider is how do you determine a number that is the next sequence and I think of the 99 bottles challenge in Chris Pine's book and realize that that "n + 1"or "n - 1" will be a part of the solution as will a 'range statement' that begins with 1 and ends with 10,000 (1..10,000).
I next think about indexing and that I'll need to loop through the range using #upto or #each to determine the missing number as well as some sort of conditional statement that allows me to return the missing value. I'll be defining a method "missing_number" but what is the input?
Is it an array? Or is it a range? I am going to go with array since most of the time arrays are unsorted and when I test it I'll define the input as a range.
After doing a bit of research I came across a strategy that indicated the key step would be to sum all of the numbers in the array and subtract the
difference from the sum of the given range. This makes a lot of sense as a good approach because you are dealing with a constant value, so I selected this approach
to inform the code.
def missing_number(array)
grand_sum = (array.length + 1) * (array.length + 2) / 2
sum = 0
array.each {|n| sum += n}
grand_sum - sum
end
x=(1..10000).to_a
x.delete rand(10000)
puts missing_number(x)

What does this Pascal syntax mean

Here's this code:
for i:= 1 to n
do Write(a[i]:6:2);
Writeln;
For loop outputs data from array..
Please, help - What does :6:2 in Write() mean?
Thanks in advance!
It's output formatting. This means use 6 digits for output with 2 decimal positions
this will format your output in case you have a number/decimal in a. If i remember correctly, in your case, the 2 means the max number of decimal places below 0 and the 6 the maximum digits printed for numbers above 0.
HTH
Dominik
The spec x:n:m means a field width of n and m decimal places. It will be formatted as a real. If x=17.8 then x:6:2 comes out as " 17.80". Note that it is one leading space and that it is right justified. If m is 0 then no decimal point and no trailing digits. If you have x:n as the format you get scientific notation in a field width of n.
Also, n and m can be integer variables, so the field widths and decimal points can be changed during execution.

Resources