break a long list into multiple columns in restructured text - python-sphinx

Is there a way to break a long list into multiple columns in restructured text?
source:
* a
* b
* c
* d
* e
* a
* c
* a
* d
* e
* f
result:
* a * e * d
* b * a * e
* c * c * f
* d * a
The goal is to provide the source list in a reST directive
rather than using some other language to read the source list and then write the result text with the ".. raw::" directive in reST. Such as this hypothetical list-multicol directive:
.. list-multicol::
:columns: 3
* a
* b
* c
* d
* e
* a
* c
* a
* d
* e
* f
Extra credit for option to balance columns (same number of items +/- 1) no wider than the page or a specified number of columns.

Apparently, the hlist directive does this.
http://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-hlist
Here's the example:
.. hlist::
:columns: 3
* A list of
* short items
* that should be
* displayed
* horizontally

Related

How to avoid errors in trigonometry functions in Oracle?

I have a curson in my stored procedure
SELECT a.id, a.full_address, me.answer medid, me.name NAME, a.nlat, a.nlong, a.parent_table
FROM example_table_1 a,
(SELECT pnradius AS radius,
111.045 AS distance_unit,
57.2957795 AS rad2deg,
0.01745329251994 AS deg2rad
FROM dual) geo,
example_table me
WHERE a.nlat BETWEEN pnlatitude - (geo.radius / geo.distance_unit) AND
pnlatitude + (geo.radius / geo.distance_unit)
AND a.nlong BETWEEN pnlongitude - (geo.radius / (geo.distance_unit * cos(deg2rad * (pnlatitude)))) AND
pnlongitude + (geo.radius / (geo.distance_unit * cos(deg2rad * (pnlatitude))))
AND geo.distance_unit * rad2deg *
(acos(cos(deg2rad * (pnlatitude)) * cos(deg2rad * (a.nlat)) * cos(deg2rad * (pnlongitude - a.nlong)) +
sin(deg2rad * (pnlatitude)) * sin(deg2rad * (a.nlat)))) < pnradius
AND a.parent_id = me.answer
AND a.parent_table = 'example_table'
pnlatitude and pnlongiture are paramenters of procedure.
In most cases this cursor works great. But sometimes in some areas in Russia this cursor cause this error:
I understand what is going on here, but I can't track where does it happen. I can adjust deg2rad value and it helps, but then this error will appear with other coordinates.
Is it possible to reduce the value of trigonometry function paramentr to 1 when it is more than 1?
Use the LEAST function to ensure you don't pass an argument greater than 1 to ACOS:
SELECT a.id, a.full_address, me.answer medid, me.name NAME, a.nlat, a.nlong, a.parent_table
FROM example_table_1 a,
(SELECT pnradius AS radius,
111.045 AS distance_unit,
57.2957795 AS rad2deg,
0.01745329251994 AS deg2rad
FROM dual) geo,
example_table me
WHERE a.nlat BETWEEN pnlatitude - (geo.radius / geo.distance_unit)
AND pnlatitude + (geo.radius / geo.distance_unit)
AND a.nlong BETWEEN pnlongitude - (geo.radius / (geo.distance_unit * cos(deg2rad * (pnlatitude))))
AND pnlongitude + (geo.radius / (geo.distance_unit * cos(deg2rad * (pnlatitude))))
AND geo.distance_unit * rad2deg *
(acos(LEAST(cos(deg2rad * (pnlatitude)) * cos(deg2rad * (a.nlat)) * cos(deg2rad * (pnlongitude - a.nlong)) +
sin(deg2rad * (pnlatitude)) * sin(deg2rad * (a.nlat)), 1))) < pnradius
AND a.parent_id = me.answer
AND a.parent_table = 'example_table'
This is the same as your original with a LEAST(big-long-calc, 1) added inside the ACOS call. Hopefully I counted the parentheses right - if not, adjust as necessary. :-)
Best of luck.

a to the power of b - recursive algorithm

I am having a hard time understanding the following recursive algorithm in terms of the multiplication operation used in the code.
int power(int a, int b) {
if (b < 0) {
return 0;
} else if (b == 0) {
return 1;
} else {
return a * power(a, b - 1);
}
}
For inputs (3,7) the result would be 2187. There are total of 6 recursive calls being made:
Initial values - 3,7
First recursive call(3,6)
Second recursive call(3,5)
Third recursive call(3,4)
Fourth recursive call(3,3)
Fifth recursive call(3,2)
Sixth recursive call(3,1)
Given the following formula:
a * power(a, b - 1)
is each recursive call multiplying the values of a & b? Which wouldn't make sense, since that would return 81 at the end. I am trying to understand the factors and product in the multiplication operation of each recursive call.
You have to keep in mind that a is multiplied by the result of the recursive function call at every step. You might look at it like this:
power(3,7)
= 3 * power(3,6)
= 3 * 3 * power(3,5)
= 3 * 3 * 3 * power(3,4)
= 3 * 3 * 3 * 3 * power(3,3)
= 3 * 3 * 3 * 3 * 3 * power(3,2)
= 3 * 3 * 3 * 3 * 3 * 3 * power(3,1)
= 3 * 3 * 3 * 3 * 3 * 3 * 3 * power(3,0)
= 3 * 3 * 3 * 3 * 3 * 3 * 3 * 1 // by definition when b = 0
At each step we replace the call to power(a,b) with a * power(a,b-1), as the function defines, until we get to power(3,0). Does that help clear up what's going on?
Your function int power(int a, int b) returns an int.
So every time return a * power(a, b - 1); is called, a is multiplied by the value returned by power(a, b - 1) until you get b == 0 which returns 1.
At the end you get:
return (3 * (3 * (3 * (3 * (3 * (3 * (3 * 1)))))));
The value of b is the one that stops the recursivity and make you get a result. If b wasn't decreased, you'd be in an infinite loop.
So to answer to your question, neither a or b are multiplied, since all is in the return value. Only b is decreased to make the number of loops expected.
I hope this helped you understand.
return a * power(a, b - 1);
This line has a lot of information to convey. Actually a the the base and b is the power, which is to be raised to a. Now, each time we multiply a with the returned value, we are actually raising it to some power.
When return statement is executed for the first time, it stores product of a and a call to the power function. During the subsequent calls to power function, the value of b, reduces by 1 every time.
Hence in the end, when recursion, unfolds, you get something like this:
return a*a*a*a*a*a*a*1
Finally in the end, the result is computed and is sent back to the main method. (The calling method)
actually what is happening here, each call is made with a new value of variables, for instance the last call(3* power(3, 0)) would return 3, as (3* 1) = 3, and last call returns the '1' first, and them back tracks,, for last call b = 0, so 1 is returned which is multiplied with 3, which becomes 3
for rest of calls, this 3 would be simply being multiplied each time,,
for fifth call,
it would return value 3 * 3
for forthh it would return,
3 * 9 ,,
and so on
is each recursive call multiplying the values of a & b? Which wouldn't
make sense
This is the wrong assumption. Each recursive call multiplying the values of a & (result of exectuing the same fucntion with a & b-1).
On your last recursive call (a = 3, b = 0), you are in this case :
} else if (b == 0) {
return 1;
This means, the previous caller (a = 3, b = 1) that was on the line return a * power(a, b - 1); can be replaced with values return 3 * power(3, 1 - 1); -> return 3 * 1; // 1 being the last recursive call return value.
Taking the previous one (a = 3, b = 2) and doing the same operation
return a * power(a, b - 1);
-->
return 3 * power(3, 2 - 1);
-->
return 3 * 3 * 1; //the result of the a = 3, b = 1 call
And so on...
a = 3, b = 3
return 3 * 3 * 3 * 1;
a = 3, b = 4
return 3 * 3 * 3 * 3 * 1;
a = 3, b = 5
return 3 * 3 * 3 * 3 * 3 * 1;
a = 3, b = 6
return 3 * 3 * 3 * 3 * 3 * 3 * 1;
a = 3, b = 7
return 3 * 3 * 3 * 3 * 3 * 3 * 3 * 1;

How to make out Reverse triangle and pyramid in Ruby using for loop?

I want to print triangle & pyramid "*" using for loop.
can somebody help me on this?
Output like :
*****
****
***
**
*
and
*
**
***
****
*****
1: Print triangle using While loop
n = 5
while n >= 1
puts "* " * n
n = n - 1
end
* * * * *
* * * *
* * *
* *
*
n = 1
while n <= 5
puts ("* " * n).rjust(10)
n += 1
end
*
* *
* * *
* * * *
* * * * *
2: Print pyramid using loop
n = 4 # Set number of rows
i = 1
1.upto(n) do
print ' ' * n
print '*' * (2 * i - 1)
print "\n"
n -= 1
i += 1
end
*
***
*****
*******
5.downto(1).each{|n| puts ("*" * n).ljust(5)}
1.upto(5).each{|n| puts ("*" * n).rjust(5)}

Square with diagonal in Pascal

I have written an application which will write square with diagonal (from left side) - output:
+ * * * *
* + * * *
* * + * *
* * * + *
* * * * +
Code for first application:
PROGRAM cycle4;
USES CRT;
VAR a,r,s:INTEGER;
BEGIN
CLRSCR;
WRITE (‘Enter the number of lines :‘) ;
READLN(a);
FOR r:= 1 TO a DO
BEGIN
FOR s:=1 TO a DO
IF r = s THEN WRITE(‘+‘)
ELSE WRITE(‘*‘) ;
WRITELN;
END;
READLN;
END.
And now I have to create an application which will write square with diagonal (from right side) - output:
* * * * +
* * * + *
* * + * *
* + * * *
+ * * * *
But I don't know how can I write it. Can you help me?
Thanks :)
The line of code which defines the position of + sign is that:
IF r = s THEN WRITE(‘+‘)
and this is the only line you need to change:
IF r + s = a + 1 THEN WRITE(‘+‘)
I think this should work, check with Pascal compiler, haven't used it for about 10 years :)

How to do pattern program in Ruby?

I am using Ruby 1.9.3. I have done pattern program like follow:
n = 1
while n <= 5
n.downto 1 do |i|
print "* "
end
puts
n += 1
end
Output of above program is like follow:
*
* *
* * *
* * * *
* * * * *
Now I am trying to do pattern program like follow:
*
* *
* * *
* * * *
* * * * *
I am not getting idea how can I do it?
Could anyone help me on this?
Thank you.
You can use rjust:
n = 1
while n <= 5
puts "* " * n
n += 1
end
*
* *
* * *
* * * *
* * * * *
n = 1
while n <= 5
puts ("* " * n).rjust(10)
n += 1
end
*
* *
* * *
* * * *
* * * * *
A shortened version of this will be:
5.times { |i| puts ('* ' * (i+1)) }
and
5.times { |i| puts ('* ' * (i+1)).rjust(10) }
You can do:
1.upto 5 do |n|
print ' ' * (5-n)
print '* ' * n
puts
end
Here's another way:
def print_two_ways(n, spaces=0)
arr = Array.new(n) { |i| Array.new(n) { |j| (i >= j) ? '*' : ' ' } }
print_matrix(arr, spaces)
puts
print_matrix(arr.map(&:reverse), spaces)
end
def print_matrix(arr, spaces = 0)
sep = ' '*(spaces)
arr.each { |r| puts "#{r.join(sep)}" }
end
print_two_ways(5)
*
**
***
****
*****
*
**
***
****
*****
print_two_ways(5,1)
*
* *
* * *
* * * *
* * * * *
*
* *
* * *
* * * *
* * * * *
a = 5
b = 1
while a>0
while b<=5
puts "*"*b
b = b+1
a = a-1
end
end
*
**
***
****
*****
Here, a is 5 and b is 1.
In the first iteration of the outer while loop, a is 1 and the inner while loop is inside the body of the outer while loop. So, the inner while loop will be executed and "*"1 ( b is 1 ) i.e "" will be printed and b will become 2 and a will become 4.
Here is simple set variable and use all programs.
# Set vars
n = 4 # Set number of rows
br = "\n" * 2
# Simple loop
puts "Right triangle:#{br}"
for i in 1..n do
puts "* " * i
end
puts br
=begin
simple loop result:
*
* *
* * *
* * * *
=end
# Countdown loop
puts "Inverted right triangle:#{br}"
n.downto(0) do
puts "* " * n
n -= 1
end
puts br
=begin
countdown loop result:
* * * *
* * *
* *
*
=end
# Function loop
puts "Inverted pyramid:#{br}"
n = 4 # Reset number of rows
for i in 1..n do
# Use a func to reduce repetition
def printer(var, str)
print "#{str}" * (2 * var - 1)
end
printer(i, " ")
printer(n, "* ")
print "\n"
n -= 1
end
puts br
=begin
function loop result:
* * * * * * *
* * * * *
* * *
*
=end
# Count up loop
puts "Close pyramid:#{br}"
n = 4 # Set number of rows
i = 1
1.upto(n) do
#n.times do
# print ' '
#end
print ' ' * n
#(2 * i - 1).times do
# print '*'
#end
print '*' * (2 * i -1)
print "\n"
n -= 1
i += 1
end
print br
=begin
count up loop result:
*
***
*****
*******
=end
I'm still new to Ruby, but this was my solution. Anything wrong with doing it this way?
def staircase(n)
sum_tot = n
n.times do
sum_tot-= 1
space = n - sum_tot
puts ('#' * space).rjust(n)
end
end

Resources