Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help coding
#1
If given input in INPUT=9 , I would like to get below output

Output:
123456789 .1234567 ..12345 ...123 ....1 ...123 ..12345 .1234567 123456789
S = 9
t=[]
for i in range(1,S+1):
    t.append(i)
for i1 in range(0,S):
    print(''.join(str(e) for e in t))
    for i in range(S-1,0,-1):
        t[i]=t[i-1]
    t[i1]='.'
    for i in range(S - 1, 0, -1):
        t[i] = t[i - 1]
    t[i1] = '.'
Output:
My output 123456789 ..1234567 ....12345 ......123 ........1 ......... ......... ......... .........
please help , i been breaking my head from morning
Reply
#2
This is super ugly, but might help move you in the right direction:
>>> nums = list(map(str, range(1, 10)))
>>> last = None
>>> for row in range(len(nums) + 1):
...   if row > len(nums)/2:
...     row = len(nums) - row
...   if row == last:
...     continue
...   last = row
...   print("." * row, end="")
...   if row:
...     cutoff = row * 2
...     print("".join(nums[:-1 * cutoff]))
...   else:
...     print("".join(nums))
...
123456789
.1234567
..12345
...123
....1
...123
..12345
.1234567
123456789
Reply
#3
I am lazy, so I cheat - I construct only descending part and using slice for ascending part:

user_input = input('Please enter odd number: ')  # for example 9
s = ''.join(str(x) for x in range(1, int(user_input) + 1)) 

output = []

for i, el in enumerate(range(len(s), 0, -2)):
    output.append(s[:el].rjust(len(s) - i, '.'))

print(*output, *output[-2::-1], sep='\n')
Output:
123456789 .1234567 ..12345 ...123 ....1 ...123 ..12345 .1234567 123456789
Of course rows #4-7 can be expressed in oneliner, then code can be only 3 rows:

s = ''.join(str(x) for x in range(1, int(user_input) + 1))
output = [s[:el].rjust(len(s) - i, '.') for i, el in enumerate(range(len(s), 0, -2))]
print(*output, *output[-2::-1], sep='\n')
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
You both made me to feel how dum i am , Thank you nilamo & perfringo
Reply
#5
(May-10-2019, 02:02 AM)aankrose Wrote: You both made me to feel how dum i am

There is no reason to feel that way. This is called learning process Smile.

This is good example that there are different solutions to a problem. Part of learning process is to understand logic / idea behind those solutions.

Some glimpses of my thought process:

First of all, I spent time trying understand what is the logic / pattern of expected output. During this I had feeling that this must be solved by using sequence, probably string. Why is that? Looking at rows I 'saw' slicings (len of 9, 7, 5, 3, 1 ...). The expected output is formatted (leading dots) and this was indication for me that string is probably the right type of sequence. String has lot of methods (44 to be specific) and I was pretty sure that I can find something suitable from there. I also observed, that desired output is "mirrored" so my idea was to have solution for descending part, save it in list and use it two times - first whole descending and then slicing to go 'up' again.

Interactive intrepreter is your best friend when learning. To have overview what is available for strings:

>>> str.      # two times TAB
str.capitalize(    str.isalpha(       str.ljust(         str.rstrip(
str.casefold(      str.isascii(       str.lower(         str.split(
str.center(        str.isdecimal(     str.lstrip(        str.splitlines(
str.count(         str.isdigit(       str.maketrans(     str.startswith(
str.encode(        str.isidentifier(  str.mro(           str.strip(
str.endswith(      str.islower(       str.partition(     str.swapcase(
str.expandtabs(    str.isnumeric(     str.replace(       str.title(
str.find(          str.isprintable(   str.rfind(         str.translate(
str.format(        str.isspace(       str.rindex(        str.upper(
str.format_map(    str.istitle(       str.rjust(         str.zfill(
str.index(         str.isupper(       str.rpartition(    
str.isalnum(       str.join(          str.rsplit( 
>>> [method for method in dir(str) if not method.startswith('__')]   # alternative
['capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
There is rjust method:

>>> help(str.rjust)
Help on method_descriptor:

rjust(self, width, fillchar=' ', /)
    Return a right-justified string of length width.
    
    Padding is done using the specified fill character (default is a space).
(END)
So - there is method how to format output rows (rjust) and there is slicing to get correct numbers for rows. Now it had to be constructed in a way that length of numeric part is sliced -2 on every row and length of whole string is -1 on every row. This can be relatively easily done by combining enumerate, negative stride in range, slice and str.rjust method.

For working solution full string needed to be constructed and descending rows saved into list for using twice.

What I am trying to say? In my language is saying "see the forest behind the trees". In coding context: one should always try to see the idea behind the code.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

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