Reputation: 35
There is a question. How do i print a pattern like this
stackoverflow
stackoverflo
tackoverflo
tackoverfl
ackoverfl
ackoverf
ckoverf
ckover
kover
kove
ove
ov
v
I have tried to use for loops but failed...
str = "stackoverflow"
k = len(str)
print(str)
print(str[:(k-1)])
And I don't know how to use for loops to finish it Are there any ways without using for loops to address this problem? Thanks...
Upvotes: 3
Views: 170
Reputation: 727
Here is another way with recursive function printline()
. For loop not needed.
# Recursive function
def printline(string, cutleft, padindex):
# Print out the required line
print(string.rjust(padindex+len(string)))
# Last character to print out
if len(string) == 1:
return
# Deciding to trim the left or right part of the string
if cutleft:
printline(string[1:], 0, padindex + 1)
else:
printline(string[:-1], 1, padindex)
# Calling the recursive function with initial value
printline('stackoverflow', 0, 0)
This is the output.
stackoverflow
stackoverflo
tackoverflo
tackoverfl
ackoverfl
ackoverf
ckoverf
ckover
kover
kove
ove
ov
v
Upvotes: 0
Reputation: 727
As OP stated that no loops. Does hardcoding count as valid answer?
print(“stackoverflow”)
print(“stackoverflo”)
print(“ tackoverflo”)
print(“ tackoverfl”)
print(“ ackoverfl”)
print(“ ackoverf”)
print(“ ckoverf”)
print(“ ckover”)
print(“ kover)
print(“ kove”)
print(“ ove”)
print(“ ov”)
print(“ v”)
Upvotes: 0
Reputation: 12669
You can use just simple slice operator :
a='stackoverflow'
print(a)
#printing first whole string
for i in range(len(a)):
#loop range of the string
if i%2==0:
#here the logic see the problem you will find a pattern that it removing
#last character when loop number is even and update the string with current
#sliced string
#print(i)
# if you want use this print for understanding track of slicing
print('{:^12s}'.format(a[:-1]))
#Removing last character if loop index is even
a=a[:-1]
#update the string with current sliced string
else:
#print(i)
#use this print for tracking of sliced string
print('{:^14s}'.format(a[1:]))
#remove first character if loop index is not even or loop index is odd.
a=a[1:]
#update the current string with sliced string
output:
stackoverflow
stackoverflo
tackoverflo
tackoverfl
ackoverfl
ackoverf
ckoverf
ckover
kover
kove
ove
ov
v
Upvotes: 0
Reputation: 331
Well you could use 'some_string'.rjust(width, ' ') where width is an integer value and the second parameter is a string in my example used a blank space. Also you can use 'some_string'.ljust(width, ' '). For more information you should check this site https://www.programiz.com/python-programming/methods/string/rjust
for example:
def word_reduce(word):
n = word.__len__()
for i in range(n):
left = i // 2
right = i - left
result = word[left:n-right]
print((' ').rjust(left + 1) + result)
s = 'stackoverflow'
word_reduce(s)
Upvotes: 1
Reputation: 3573
You can use the concept of recursion
def stackoverflow(pattern,alternate=0):
if len(pattern) == 1:
#base condition for recursion
return
elif alternate == 0:
#first time execution
print(pattern)
alternate = alternate + 1
stackoverflow(pattern, alternate)
elif alternate % 2 != 0:
# truncate from right side
pattern = pattern[:-1]
print(pattern)
alternate = alternate + 1
stackoverflow(pattern, alternate)
else:
#truncate from left side
pattern = pattern[1:]
print(pattern)
alternate = alternate + 1
stackoverflow(pattern,alternate)
Upvotes: 1
Reputation: 6748
You could try this with only one counter
string = "stackoverflow"
loop=0
while string.replace(' ','')!='':
print(' '*(loop//2)+string+' '*(loop//2))
if loop%2==0:
string=string[:-1]
else:
string=string[1:]
loop=loop+1
Upvotes: 0
Reputation: 2540
Keep track of two index l
and r
that represent the slice of string to print. Then shorten that slice on each iteration.
s = 'stackoverflow'
l, r = 0, len(s) # range of string to print
remove_left = True # which side of the string to remove
space = 0 # how much space to print to the left
while l < r:
print('%s%s' % (' ' * int(space/2), s[l:r]))
if remove_left:
r-= 1
else:
l+= 1
remove_left = not remove_left
space += 1
Output:
stackoverflow
stackoverflo
tackoverflo
tackoverfl
ackoverfl
ackoverf
ckoverf
ckover
kover
kove
ove
ov
v
Upvotes: 1
Reputation: 24052
I suggest using a for
loop. They're pretty easy to use once you get used to them. Here's a solution that uses a for
loop:
def show(s):
n = len(s)
for i in range(n):
n1 = i // 2
n2 = i - n1
print(" " * n1 + s[n1:n-n2])
s = "stackoverflow"
show(s)
The output is:
stackoverflow
stackoverflo
tackoverflo
tackoverfl
ackoverfl
ackoverf
ckoverf
ckover
kover
kove
ove
ov
v
If you really don't want to use a for
loop, you can replace it with a while
loop as follows:
i = 0
while i < n:
...
i += 1
Upvotes: 2
Reputation: 14918
Another possible solution is
s = "stackoverflow"
toggle = True # If true, remove first char. Else, cut last char.
left = 0 # number of spaces to prepend
right = 0 # number of spaces to append
while s: # while s is not empty
print(' '*left + s + ' '*right)
if toggle:
s = s[1:] # remove first char
left += 1
else:
s = s[:-1] # remove last char
right += 1
toggle = not toggle
which gives output
stackoverflow
tackoverflow
tackoverflo
ackoverflo
ackoverfl
ckoverfl
ckoverf
koverf
kover
over
ove
ve
v
Upvotes: 4