Python Forum
Need help getting the output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help getting the output
#1
S = 5
t=[]
for i in range(1,S+1):
    t.append(i)
for j in range(0,S):
    t[j] = '/'
    print(''.join(str(e) for e in t))
    for i in range(S-1, 0, -1):
        t[i] = t[i-1]
    t[j] = 1
Output:
CODE OUTPUT /2345 1/234 11/23 111/2 1111/
Output:
Expected OUTPUT \2345 1\345 12\45 123\5 1234\
Please help me getting the expected output
Reply
#2
First of all, you need to use a backslash instead of a slash. Python uses backslashes for special characters in strings, so you either need to use a raw string (r'\') or a double slash ('\\').

Second, you need to store the value of t[j] in a temporary variable, and then assign that temporary variable back to t[j] after printing. Or you need to forget about changing the list t, and instead print the part of t before the slash, the slash, and then the part after the slash.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Apr-10-2019, 04:41 PM)aankrose Wrote:
Output:
Expected OUTPUT \2345 1\345 12\45 123\5 1234\

Looking at expected output it seems that task in spoken language is something like: "replace digit with \ starting from first digit till last by moving one place". Translating 'move one place, replace' into Python is loop combined with str.replace method:

>>> s = '12345'
>>> for i in range(len(s)):
...     print(s.replace(s[i], '\\'))
...
\2345
1\345
12\45
123\5
1234\
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
@perfringo: First of all , input has to be integer, not tuple , list will not support replace.

@ichabod801 : Thanks for your suggestion , but here is what the output comes

S = 5
H = 1
t=[]
for i in range(1,S+1):
    t.append(i)
for j in range(0,S):
    print(''.join(str(e) for e in t))
    t[j] = ('\\')
    for i in range(S-1, 0, -1):
        t[i] = t[i-1]
    t[j] = 1



Output:
Not the expected output and i am struggling to replace number to \ after each loop 12345 1\234 11\23 111\2 1111\
Reply
#5
Create the following in python code:
  • get input_value
  • create a for loop input_value times with variable name first
  • create a empty string out_str
  • create a inner loop input_value times with variable name second
  • if second equals first
    • add '\\' to out_str
    otherwise
    • add second to out_str
  • output out_str
Reply
#6
In your loop over j, you need to do these things in this order:

  1. Save t[j]
  2. Change t[j]
  3. Print t as a string
  4. Reset t[j] to the stored value.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(Apr-10-2019, 09:02 PM)aankrose Wrote: @perfringo: First of all , input has to be integer, not tuple , list will not support replace.

Where should I find out requirements? Right, they are not presented. You didn't mention it and I am no mindreader. And to clarify: in my code there is no tuple; Python input() returns string.

However, I don't see any difference if only requirement is that you start with integer. Just one additional row for converting integer to string and off you go:

>>> start = 5
>>> s = ''.join(str(x) for x in range(1, start + 1))
>>> s
'12345'
EDIT: about 'list will not support replace' - this is technically true, list don't have replace method but same functionality is easily achieved by assigning new value to specific index lst[0] = 'some_value'.

Getting desired output without using string (or list):

>>> start = 5
>>> for i in range(start):
...     print(*('\\' if i == j else x for j, x in enumerate(range(1, start + 1))), sep='')
... 
\2345
1\345
12\45
123\5
1234\
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
#8
@perfringo

You are awesome , thanks buddy
Reply
#9
(Apr-11-2019, 05:10 PM)aankrose Wrote: @perfringo You are awesome , thanks buddy

But do you actually understand what is happening in the code he posted? Sure, you now have the solution, but would you be able to rewrite it without all of the comprehensions he used?
Reply
#10
In order to enhance learning process I provide following explainer:

In expected output row number and position index for '\' are matching -> row[0]position[0], row[1]position[1]....row[4]position[4]. So the task is to have two iterators (one for row index and another for position index on row) and if they match put '\' into that position.

for i in range(start): for loop to get row indices / number of rows to output -> 0, 1, 2, 3, 4

for j, x in enumerate(range(1, start +1)) for loop to get position indices j (-> 0, 1, 2 , 3,4) and values x (-> 1, 2, 3, 4, 5) to output by using enumerate()

'\\' if i == j else x conditional expression to select what to yield - if row and position indices matches then '\' otherwise value

*(...) generator expression with unpacking generated values for rows -> row[0] \ 2 3 4 5, row[1] 1 \ 3 4 5..... row[4] 1 2 3 4 \

print(..., sep='') printing generated and unpacked row values without separation.

For better readability more descriptive names should have been used:

>>> start = 5
>>> for row in range(start):
...     print(*('\\' if row == position else num for position, num in enumerate(range(1, start + 1))), sep='')
... 
\2345
1\345
12\45
123\5
1234\
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