We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The entire argument to join is a list comprehension. Within the comprehension, for n in [int(raw_input())] for i in xrange(n) technically sets up a nested loop, although the outer loop only executes once.
[int(raw_input())] creates a list with one element, the height of the staircase. for n in [int(raw_input())] goes through (each of) the element(s) of that list, setting n to that element. (This only happens once because there is only one element). This part is just a trick to set n to the staircase height without needing a separate statement (separate line or semicolon).
The inner loop, for i in xrange(n), is the real loop. Because it's the inner loop, n has already been set to the height of the staircase. Of course, this inner loop sets i to every integer from 0 to n-1, in turn.
('#' * (i + 1)).rjust(n) creates each line. For each iteration of the inner loop (and therefore for each value of n and i), the list of results has an element with (i + 1) '#' characters, right-justified in a field of width n.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Staircase
You are viewing a single comment's thread. Return to all comments →
The entire argument to
join
is a list comprehension. Within the comprehension,for n in [int(raw_input())] for i in xrange(n)
technically sets up a nested loop, although the outer loop only executes once.[int(raw_input())]
creates a list with one element, the height of the staircase.for n in [int(raw_input())]
goes through (each of) the element(s) of that list, settingn
to that element. (This only happens once because there is only one element). This part is just a trick to set n to the staircase height without needing a separate statement (separate line or semicolon).The inner loop,
for i in xrange(n)
, is the real loop. Because it's the inner loop,n
has already been set to the height of the staircase. Of course, this inner loop setsi
to every integer from 0 to n-1, in turn.('#' * (i + 1)).rjust(n)
creates each line. For each iteration of the inner loop (and therefore for each value ofn
andi
), the list of results has an element with (i + 1) '#' characters, right-justified in a field of widthn
.