I am trying to print a 10x10 times table using for loops.
Here's my attempt:
for x in range (1, 11):
for y in range (1, 11):
print (x*y)
print()
The output is a vertical line of numbers. I need it like the square table kind.
What you need to do is leverage the end argument:
for x in range (1, 11):
for y in range (1, 11):
print ('{:3}'.format(x*y), end=' ')
print()
Also, note the way the row entries are formatted. By using '{:3}'.format(x*y), the expression is padded with spaces out to three digits. For more details on formatting, consult the documentation.
Sample output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
The print function adds a \n unless told otherwise. Try explicitly saying not to:
for x in range (1, 11):
for y in range (1, 11):
print (x*y, end=' ')
print()
Note: I'm assuming you're either on python3 or imported the print_function since you are using the print function, rather than statement.
Edit: added a space in the end
And one may complicate things a bit and print X index and Y index :) here
n = 11
m = 11
grid = [[x * y for x in range(1,n)] for y in range(1,m)]
print(' ', end='')
print(''.join([f'{j:5}' for j in range(1,n)]))
print(' ', end='')
print(''.join([f'{"_":>5}' for _ in range(1,n)]))
for i in range(n-1):
print(f'{i+1:2}|', end=' ')
print(' '.join(f'{x:4}' for x in grid[i]))
Results
1 2 3 4 5 6 7 8 9 10
_ _ _ _ _ _ _ _ _ _
1| 1 2 3 4 5 6 7 8 9 10
2| 2 4 6 8 10 12 14 16 18 20
3| 3 6 9 12 15 18 21 24 27 30
4| 4 8 12 16 20 24 28 32 36 40
5| 5 10 15 20 25 30 35 40 45 50
6| 6 12 18 24 30 36 42 48 54 60
7| 7 14 21 28 35 42 49 56 63 70
8| 8 16 24 32 40 48 56 64 72 80
9| 9 18 27 36 45 54 63 72 81 90
10| 10 20 30 40 50 60 70 80 90 100
Related
I am trying to produce a square-formatted multiplication table with the output at the end using code below:
def multiplicationTable(maxValue):
for i in range(1, maxvalue):
for j in range(1, maxvalue):
print(("{:6d}".format(i * j,)), end='')
print()
print(multiplicationTable(1)
print(multiplicationTable(5))
print(multiplicationTable(10))
1
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
I get an error:
File "", line 7
print(multiplicationTable(5))
^
SyntaxError: invalid syntax
print(multiplicationTable(1) is missing a closing ).
You are using maxValue (with capital V) in your function definition while using maxvalue (with small v) in the function body.
Here is the new version:
def multiplicationTable(maxvalue): # maxvalue, not maxValue
for i in range(1, maxvalue+1):
for j in range(1, maxvalue+1):
print(("{:6d}".format(i * j,)), end='')
print()
multiplicationTable(1)
multiplicationTable(5)
multiplicationTable(10)
EDIT 1: Changed range(1, maxvalue) to range(1, maxvalue+1)
EDIT 2: Changed print(multiplicationTable(n)) to multiplicationTable(n)
I have a matrix of floating point values. Relative to a given origin (x and y index, point "0"), I would like to obtain the indices of equidistant points, starting with the nearest points ("1") and up to a specific number ("12" in this animated example):
The distance is the slant range between a point and point 0. For example, point "4" has the distance sqrt(2^2+1^2) = sqrt(5) = 2.24.
Does anyone know a corresponding algorithm to obtain these indices in an effective way?
Rephrasing the question, you want to enumerate the points by increasing euclidean distance to the center.
Here are two answers on https://math.stackexchange.com to this problem how-to-enumerate-2d-integer-coordinates-ordered-by-euclidean-distance and algorithm-for-enumerating-grid-points-by-distance-from-given-point
Basically:
use symmetry to consider only point with 0 <= x <= y;
note that for a given x points will be enumerated with increasing y;
use a priority queue to keep the next candidate for each vertical line.
With n the last index you generate, the time complexity will be O(n log n) and the space complexity O(sqrt(n)).
NB: to avoid floating point computation, consider the squared distance, which doesn't change the order of your points.
Here some python code implementing this idea:
import heapq
def yield_all_quadrant(x, y):
s = set([(x, y), (-x, y), (x, -y), (-x, -y),
(y, x), (-y, x), (y, -x), (-y, -x)])
for u, v in sorted(s):
yield u, v
def indices(X, Y):
q = [(0, 0, 0)]
d_current = 0
index = 0
while True:
d, x, y = heapq.heappop(q)
if d > d_current:
index += 1
d_current = d
for u, v in yield_all_quadrant(x, y):
yield (X + u,Y + v), index
if not y:
heapq.heappush(q, (d + 2*x + 1, (x+1), 0))
if y < x:
heapq.heappush(q, (d + 2*y + 1, x, y+1))
and used for example in a small function to fill a grid
import itertools
def fill_grid(size, center):
grid = [[0]*size for _ in range(size)]
def clip(e):
coord, index = e
return all(0 <= c < size for c in coord)
for (x,y), i in itertools.islice(filter(clip, indices(*center)), 0, size**2):
grid[x][y] = i
return grid
and the result
print('\n'.join(' '.join('%2d'%i for i in gi) for gi in fill_grid(20, (8,8))))
54 48 43 39 35 33 31 30 29 30 31 33 35 39 43 48 54 59 67 74
48 42 38 34 30 27 26 24 23 24 26 27 30 34 38 42 48 55 62 69
43 38 32 28 25 22 20 19 18 19 20 22 25 28 32 38 43 50 56 64
39 34 28 24 21 17 15 14 13 14 15 17 21 24 28 34 39 46 53 60
35 30 25 21 16 13 12 10 9 10 12 13 16 21 25 30 35 41 49 57
33 27 22 17 13 11 8 7 6 7 8 11 13 17 22 27 33 40 47 55
31 26 20 15 12 8 5 4 3 4 5 8 12 15 20 26 31 38 45 53
30 24 19 14 10 7 4 2 1 2 4 7 10 14 19 24 30 37 44 52
29 23 18 13 9 6 3 1 0 1 3 6 9 13 18 23 29 36 43 51
30 24 19 14 10 7 4 2 1 2 4 7 10 14 19 24 30 37 44 52
31 26 20 15 12 8 5 4 3 4 5 8 12 15 20 26 31 38 45 53
33 27 22 17 13 11 8 7 6 7 8 11 13 17 22 27 33 40 47 55
35 30 25 21 16 13 12 10 9 10 12 13 16 21 25 30 35 41 49 57
39 34 28 24 21 17 15 14 13 14 15 17 21 24 28 34 39 46 53 60
43 38 32 28 25 22 20 19 18 19 20 22 25 28 32 38 43 50 56 64
48 42 38 34 30 27 26 24 23 24 26 27 30 34 38 42 48 55 62 69
54 48 43 39 35 33 31 30 29 30 31 33 35 39 43 48 54 59 67 74
59 55 50 46 41 40 38 37 36 37 38 40 41 46 50 55 59 66 73 80
67 62 56 53 49 47 45 44 43 44 45 47 49 53 56 62 67 73 79 85
74 69 64 60 57 55 53 52 51 52 53 55 57 60 64 69 74 80 85 93
// L is a list and n is its length //
// we assume that n= 4**k , for k≥1//
Alg1(L,n)
remove the smallest and largest element from L
if n-2 > (4**k)/2
call Alg1(L, n-2)
Not what it does but what is it intended to do? I don't understand what the question means by "intended" but I think the algorithm just removes the largest and smallest element of the list recursively until 4 or 3 elements remain.
Given a starting list of size 4^k, which appears to be implied by the definition given for n, alg1 reduces the size of the supplied list to ((4^k) / 2) + 2 for k >= 1. I agree with #Ctznkane525 that the algorithm is incompletely specified in that it doesn't tell us what the return value should be. But if we make the simple assumption that two elements should be removed from the end of the list each time n is decremented by 2 we can continue. Thus, consider the following implementation in Clojure:
(defn exp [x n]
(reduce * (repeat n x)))
(def k 1)
(defn alg1[l n]
(println "k=" k " n=" n " l=" l)
(if (> (- n 2) (/ (exp 4 k) 2))
(recur (take (- n 2) l) (- n 2))
l))
I've added code here to print the values of k, n, and l so we can watch what happens at each step.
Given the above we'll start a little testing. We'll invoke alg1 as (alg1 (take (exp 4 k) (iterate #(+ 1 %) 1)) (exp 4 k)), which simply creates a list of 4^k elements and passes it as the first argument to alg1, and passes 4^k for the second argument. So here goes:
user=> (def k 1)
#'user/k
user=> (alg1 (take (exp 4 k) (iterate #(+ 1 %) 1)) (exp 4 k))
k= 1 n= 4 l= (1 2 3 4)
(1 2 3 4)
So with k=1 and the list defined as (1 2 3 4) the function returns immediately, because n-2 = 2, and that's less than or equal to (4^k)/2, which is also 2.
Let's try with k=2:
user=> (def k 2)
#'user/k
user=> (alg1 (take (exp 4 k) (iterate #(+ 1 %) 1)) (exp 4 k))
k= 2 n= 16 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16)
k= 2 n= 14 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14)
k= 2 n= 12 l= (1 2 3 4 5 6 7 8 9 10 11 12)
k= 2 n= 10 l= (1 2 3 4 5 6 7 8 9 10)
(1 2 3 4 5 6 7 8 9 10)
Ah, that's a bit more interesting. We start with n=16, which is of course 4^k = 4^2 = 16, and the beginning list is (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16). When these values are considered by alg1 it finds that n-2 (14) is greater than (4^2)/2 (8), so it trims two elements from the end of the list and recursively invokes itself. On the second iteration it finds that n-2 (12) is greater than 8 so it trims another two elements and recursively invokes itself. This continues until n=10, when alg1 finds that n-2 (8) is no longer greater than (4^2)/2 (8), so it returns the list (1 2 3 4 5 6 7 8 9 10).
What happens with k=3?
user=> (def k 3)
#'user/k
user=> (alg1 (take (exp 4 k) (iterate #(+ 1 %) 1)) (exp 4 k))
k= 3 n= 64 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64)
k= 3 n= 62 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62)
k= 3 n= 60 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60)
k= 3 n= 58 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58)
k= 3 n= 56 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56)
k= 3 n= 54 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54)
k= 3 n= 52 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52)
k= 3 n= 50 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50)
k= 3 n= 48 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48)
k= 3 n= 46 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46)
k= 3 n= 44 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44)
k= 3 n= 42 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42)
k= 3 n= 40 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40)
k= 3 n= 38 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38)
k= 3 n= 36 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36)
k= 3 n= 34 l= (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34)
(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34)
Similar results to the above. At each iteration two elements are trimmed from the list until the condition specified in the algorithm is reached, at which point the algorithm exits.
You can continue bumping up the value of k, building the arguments, and watching the algorithm work, but in the end the results are always similar: the list is reduced in size to ((4^k) / 2) + 2.
Best of luck.
91
58
54
108
52
18
8
81
103
110
129
137
84
15
14
18
11
17
12
6
1
28
6
14
8
8
0
0
28
24
25
23
21
13
9
4
18
17
18
30
13
3
I want to split into chunks of six entries each.After that it will break the loop.Then it will continue the entries 7..12, then of 13..18 etc.
(for loop?continue?break?)
You can use paste:
paste -d' ' - - - - - - < inputfile
For your input, it'd return:
91 58 54 108 52 18
8 81 103 110 129 137
84 15 14 18 11 17
12 6 1 28 6 14
8 8 0 0 28 24
25 23 21 13 9 4
18 17 18 30 13 3
$ xargs -n 6 < file_name
91 58 54 108 52 18
8 81 103 110 129 137
84 15 14 18 11 17
12 6 1 28 6 14
8 8 0 0 28 24
25 23 21 13 9 4
18 17 18 30 13 3
I am trying to learn bash at a deeper level, and I decided to make a multiplication table. I have the functionality with the statement :
echo $[{1..10}*{1..10}]
but that gives me the following output:
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
Is there any way to format this output like the following using only 1 statement (i can figure out how to do this with loops, but that's no fun :p )
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Is it even possible to do in one statement, or would I have to loop?
Use this line for a nice output without using loops:
echo $[{1..10}*{1..10}] | xargs -n10 | column -t
Output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Update
As a logical next step, I asked here if this multiplication table can have a variable range. With this help, my answer works with a variable ($boundary) range and stays quite readable:
boundary=4; eval echo $\[{1..$boundary}*{1..$boundary}\] | xargs -n$boundary | column -t
Output:
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
Also note that the $[..] arithmetic notation is deprecated and $((...)) should be used instead:
boundary=4; eval eval echo "$\(\({1..$boundary}*{1..$boundary}\)\)" | xargs -n$boundary | column -t
The printf built-in repeats its format as many times as necessary to print all arguments, so:
printf '%d %d %d %d %d %d %d %d %d %d\n' $[{1..10}*{1..10}]
If you want to avoid repeating the %d bit, it's trickier.
printf "$(echo %$[{1..10}*0]d)\\n" $[{1..10}*{1..10}]
In production code, use a loop.