Python Forum
min() function in iterable index
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
min() function in iterable index
#4
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?
Reply


Messages In This Thread
min() function in iterable index - by OokaydO - Apr-17-2020, 09:24 PM
RE: min() function in iterable index - by OokaydO - Apr-18-2020, 08:57 PM
RE: min() function in iterable index - by deanhystad - Apr-18-2020, 11:06 PM
RE: min() function in iterable index - by OokaydO - Apr-23-2020, 09:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable for the value element in the index function?? Learner1 8 748 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Index Function not recognized in Python 3 Peter_B_23 1 1,353 Jan-08-2023, 04:52 AM
Last Post: deanhystad
Question how to solve `'TypeError: 'int' object is not iterable`? netanelst 2 1,659 May-24-2022, 12:03 PM
Last Post: deanhystad
  Trying to understand how isinstance(values, collections.Iterable) work. quazirfan 7 4,316 Aug-10-2021, 08:10 AM
Last Post: snippsat
  Cannot unpack non-iterable NoneType object, i would like to ask for help on this. Jadiac 3 9,029 Oct-18-2020, 02:11 PM
Last Post: Jadiac
  subprogram issues: cannot unpack non-iterable function object error djwilson0495 13 6,165 Aug-20-2020, 05:53 PM
Last Post: deanhystad
  Function to return list of all the INDEX values of a defined ndarray? pjfarley3 2 2,019 Jul-10-2020, 04:51 AM
Last Post: pjfarley3
  Why the result of "extended iterable unpacking" with set() is unpredictable? zohanlin 2 2,136 Jun-29-2020, 10:30 AM
Last Post: zohanlin
  TypeError: 're.Match' object is not iterable charlesauspicks 1 11,795 May-25-2020, 06:14 AM
Last Post: bowlofred
  function/nonetype object is not iterable nanok66 5 4,149 May-08-2020, 07:39 PM
Last Post: nanok66

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020