Python Forum
Could you explain each part of the code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Could you explain each part of the code?
#1
Hello! Could you guys explain to me each part of this code right here:

n = int(input())
alpha = 'abcdefghijklmnopqrstuvwxyz'
print(*[''.join([alpha[min(abs(i-j), abs(n - i - j - 1) % len(alpha))] for j in range(n)]) for i in range(n)], sep='\n')
Yoriz write Mar-19-2022, 08:19 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Ask specific questions. You must understand some of it. If not, trying to explain it to you is a waste of everybody's time. You wont understand the explanations.

I appears to be a palindrome generator.
i is start of the palindrome (alpha[i]).
Two sequences are calculated. One counts down from i to zero and then back up, the other counts down from (n-j-1) to zero and back up.
The two sequences cross at the center.
The lesser value from the two sequences is used for the palindrome.
Each sequence is responsible for producing half of the palindrome.

Assume n == 5
Look at what abs(i-j) produces: (0, 1, 2, 3, 4), (1, 0, 1, 2, 3), (2, 1, 0, 1, 2), (3, 2, 1, 0, 1), (4, 3, 2, 1, 0).
Look at what abs(n - i - j - 1) % len(alpha)) produces: (4, 3, 2, 1, 0), (3, 2, 1, 0, 1), (2, 1, 0, 1, 2), (1, 0, 1, 2, 3), (0, 1, 2, 3, 4)
Reply
#3
Expecting n = int(input()) to always return a number would be naive.
Someone will type text, so line 1 should be expanded to something like:
def get_num():
    n = None

    while(not isinstance(n, int)):
        n = input("Please enter a number: ")
        try:
            n = int(n)
        except ValueError:
            print(f"{n} is not numeric, try again")
    return n

n = get_num()
also, there is a builtin for line 2 in string:
# at top of script:
import string
# ...
alpha = string.ascii_lowercase
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove part of the code hack3rcon 5 671 Jan-08-2024, 10:25 AM
Last Post: hack3rcon
  [split] Explain the python code in this definition Led_Zeppelin 1 753 Jan-13-2023, 10:20 PM
Last Post: deanhystad
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,026 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
  Explain the python code in this definition Led_Zeppelin 1 1,109 Oct-27-2022, 04:04 AM
Last Post: deanhystad
  Sudoku Solver in Python - Can someone explain this code ? qwemx 6 2,153 Jun-27-2022, 12:46 PM
Last Post: deanhystad
  Can someone explain this small snippet of code like I am a 5 year old? PythonNPC 3 1,255 Apr-08-2022, 05:54 PM
Last Post: deanhystad
  Can you please explain what the part after import sys is doing? Pedroski55 3 2,094 Dec-09-2020, 07:02 AM
Last Post: bowlofred
  What is the run time complexity of this code and please explain? samlee916 2 2,309 Nov-06-2020, 02:37 PM
Last Post: deanhystad
  poplib - parsing message body, could somebody please help explain this code t4keheart 2 2,308 Oct-12-2020, 01:59 PM
Last Post: t4keheart
  Help with writing or plan part of code Merlin_1 1 1,826 Aug-24-2020, 03:28 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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