Posts: 7
Threads: 2
Joined: Nov 2019
Hi,
For a choice of 4 letters (a, b, c, d) for example, a sequence of letters is displayed, arranged as follows:
Output: aaaaaaa
abbbbba
abcccba
abcdcba
abcccba
abbbbba
aaaaaaa
with this code:
alphabet =['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
choice =int(input())
size =choice *2 -1
line =0
for _ in range(size):
column =0
for _ in range(size):
print(alphabet[min(min(line, size -1 -line), min(column, size -1 -column))], end ='')
column +=1
line +=1 I like this approach but I can't quite grasp the use of the min () function, the mechanism escapes me completely in this program.
Could someone please explain to me how the min() function works in this case?
Posts: 419
Threads: 34
Joined: May 2019
min() just selects the lowest number from a sequence. max() does the same but for the largest. So, min(2, 5, 6, 1) will return 1. max(2, 5, 6, 1) will return 6.
Posts: 7
Threads: 2
Joined: Nov 2019
Thank you but I know that the min() function return the most lowest element from a sequence.
My question is about the use of this function in a iterable index and in that form :
alphabet[min(min(line, size -1 -line), min(column, size -1 -column))] Where min () of min (line, size -1 -line) and min (column, size -1 -column) allows to display in the example (n =4):
a(n2 -1)
a, b(n2 -1), a
a, b, c(n2 -1), b, a
a, b, c, d(n2 -1), c, b, a
a, b, c(n2 -1), b, a
a, b(n2 -1), a
a(n2 -1)
I can't understand the de-incrementing and reversal system of this algorithm.
I don't see how it works and I would like someone to explain it to me.
Posts: 6,779
Threads: 20
Joined: Feb 2020
Apr-18-2020, 11:06 PM
(This post was last modified: Apr-18-2020, 11:06 PM by deanhystad.)
When I run your program I don't get output on separate lines. Using choice = 4, all my 7 character sequences appear on the same line.
Things became clear to me when I rewrote the program like this:
alphabet =['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
choice =int(input('Enter something '))
size =choice *2 -1
line =0
for line in range(size):
for column in range(size):
index = min(line, size-line-1, column, size-column-1)
print(line, column, size-line-1, size-column-1, index,)
# print(alphabet[index], end ='') I have no idea of the purpose of this program, but how it does it is fun.
min(x, size-x-1) computes the "distance" from x to the end of x's range. For size = 7, the "distance" values are 0, 1, 2, 3, 3, 2, 1, 0. The "distance" value limits which letters can be chosen. When the distance is zero (start or end of range), the only available letter is a (alphabet[0]). When the distance is 1, the letter can be a or b. So the choices of letter are [a, ab, abc, abcd, abcd, abc, ab, a]
Next we have the interplay of list and column. since list is the outer loop, list limits which letters are "available" to column. When the list distance is zero, the column distance is ignored (poor column!). This is why the first and last lines are all a's. When the list distance is 1, the column can pick a or b so we get ab...ba. When line is 2, column can pick a, b and c so we get abc...cba.
Can you tell me what this program is supposed to do?
Posts: 7
Threads: 2
Joined: Nov 2019
Thanks a lot.
It's actually quite simple,
print(alphabet[min(min(line, size -1 -line), min(column, size -1 -column))], end ='') displays in the sequence, the element of the array corresponding to its index equal to the minimum of the coordinates (x1, x2) and (y1, y2), whose intersection constitutes the location of the element to be displayed.
Quote:When I run your program I don't get output on separate lines. Using choice = 4, all my 7 character sequences appear on the same line.
Sorry I forgot a print() in the loop.
Quote:Can you tell me what this program is supposed to do?
It's a generator of target.
|