Print a word in parts python - python

Hello I want to make a function that will use enhance(which just changes the word already made it) and print the new word in parts of a given number n. Example for S=test I should get (‘##t’, ‘#te’, ‘tes’, ‘est’, ‘st%’, ‘t%%’)
def enhance(S,n):
S = "#"*(n-1)+S+"%"*(n-1)
return S
def exploder(S,n):
S = enhance(S,n)
x=0
for i in range (n <= len(S)):
print(S[x:i])
x=x+1
S="test"
n = 3
for n in range (0,n):
print(exploder(S,n))
n=n+1
print(exploder(S,n))

One immediate fix. Instead of:
for i in range (n <= len(S)):
I think you want:
for i in range(n, len(S) + 1):
That will give you values of i in the range n <= i < len(s).
Also, as Alex Hall suggested, change:
print(exploder(S,n))
To just:
exploder(S,n)
The exploder function was returning None. So that print is the source your spurious None outputs.

def enhance(S, n):
S = "#" * (n - 1) + S + "%" * (n - 1)
return S
def exploder(S, n):
S = enhance(S, n)
for i in range(len(S)-n+1):
print(S[i:i+n])
S = "test"
n = 3
exploder(S, n)
Output:
##t
#te
tes
est
st%
t%%

Related

function that prints counding down numbers and up but in a pattern

I already know how to count down and up using reccurssive function but I cant figure out how to define another function to have spaces in a pattern like this:
Desired output
My code for counting down and up:
def f(n):
if n==0:
print (n)
return
print (n)
f(n-1)
print (n)
a = 5 #you can take any value, it will be actually taken from the user
def f(n):
global a
if n == 0:
print(' '*a + str(n))
return
print(' '*(a-n), + str(n))
f(n-1)
print(' '*(a-n), + str(n))
a = int(input())
f(a)
I am posting this to contrast the other answers provided here -
def f(n, s = ""):
if n <= 0:
print(s, n)
else:
print(s, n)
f(n - 1, s + " ")
print(s, n)
f(5)
5
4
3
2
1
0
1
2
3
4
5
The program could be further simplified, if desired -
def f(n, s = ""):
print(s, n)
if n > 0:
f(n - 1, s + " ")
print(s, n)
f(5)
Default parameter s could be an integer instead of a string, if desired -
def f(n, s = 0):
print(" "*s, n)
if n > 0:
f(n - 1, s + 1)
print(" "*s, n)
f(5)
Output is the same.
print adds a space between the printed arguments. To remove this, we can print a single formatted value -
def f(n, s = ""):
print(s + str(n)) # <- single value
if n > 0:
f(n - 1, s + " ")
print(s + str(n)) # <- single value
Or you can use a formatted string literal -
def f(n, s = ""):
print(f"{s}{n}") # <- formatted string
if n > 0:
f(n - 1, s + " ")
print(f"{s}{n}") # <- formatted string
This is probably not exactly what you need. But it gives you an idea :)
As you can see it is based on #Samudra Ganguly's answer
def f(n,x):
if n < 0:
return
p(n,x-n)
f(n-1,x)
if n !=0:
p(n,x-n)
def p(m,x):
print(" ", end = "")
if x>0:
p(m,x-1)
else:
print(m, end = "\n")
f(8,8)
Also you can change the function like this:
k=4;
def f(n):
if n < 0:
return
p(n,k-n)
f(n-1)
if n !=0:
p(n,k-n)
def p(m,x):
print(" ", end = "")
if x>0:
p(m,x-1)
else:
print(m, end = "\n")
f(k)

Why does a change in multiplication sign in factorial to addition sign give such an output?

Question: Why is the output 11 not 12?
i+4+i+3+i+2 = 1+4+1+3+1+2 = 12
Code:
def factorial(n):
i = 1
while n >= 1:
#I changed the signs from * to + after getting the factorial from * method.
i = i * n --> i = i + n
n = n - 1
return i
print factorial(4)
11
To get expected i+4 + i+3 + i+2 and result 12 you need
def factorial(n):
result = 0
i = 1
while n > 1:
result += i + n
n = n - 1
return result
print(factorial(4))
I add to new variable result so I don't change i and it is 1 all the time.
I also use > instead of >= so it ends after i+2 and it doesn't add i+1
def factorial(n):
i = 1
while n >= 1:
#I changed the signs from * to + after getting the factorial from * method.
print(i)
i = i + n
n = n - 1
return i
print(factorial(4))
If you print i, you will find i has changed after first loop.
So the output should be 1+4+3+2+1=11
(Posted on behalf of the question author).
Tips from me to solve the problem: 1. understand the concept of loop 2. try to print the answer on your own - i=5, n=3, i=8, n=2, i=10, n=1, i=11

Printing Sum of first n Integers

According to me this code below should print the sum of first n integers but it is giving me name error on line 5. How do I fix this ?
def printsum(n):
n = int(raw_input())
for i in range(1,n+1):
j = i + 1
doublesum = i + j - n
total = doublesum / 2
print total
The variables i and j are local to the function printsum. Simply return the values from the function and use said values to do your calculation.
Try with this code copy and past. Because issue may be indentation
def printsum(n):
n = int(raw_input())
for i in range(1,n+1):
j = i + 1
doublesum = i + j - n
total = doublesum / 2
print total
return;
Your logic is make me confused too.

Use Python To Sum A Series

So I have the code
def formula(n):
while(n < 11):
answera = 15/(-4)** n
print(answera)
n = n + 1
formula(1)
How can I add the outputs in condescending order?
For example,
first_output = (the output of n = 1)
second_output = (the output of n = 1) + (the output of n = 2)
third_output = (the output of n = 1) + (the output of n = 2) + (the output of n = 3)
and so on..
you need to define the variable answera outside the while loop, so that its shope should exists outside the loop , so that when you return the value the fully updated value can be returned. Something like this.
def formula(n):
answera = 0
while(n < 11):
answera += 15/(-4)** n
print(answera)
n = n + 1
print(answera)
formula(1)
Now it should give you the right result.
def formula(n):
while(n < 11):
answera += 15/(-4)** n
print(answera)
n = n + 1
The idea is that you will need to accumlate the value of 15/(-4)**n in one of the variables.. (here the answera) and keep printing it out.
I hope this answers your question.
There's some ambiguity in your question; do you want the sum of 'answera', or the sum of 'formula'?
If 'answera', then you can just replace "print" with "yield" and call "sum":
def formula(n):
while(n < 11):
answera += 15/(-4)** n
yield answera
n = n + 1
sum(formula(2))
This makes 'formula' a generator, and "sum" will iterate that generator until it's exhausted.
If you want the sum of multiple 'formula' calls, then follow the KISS principle, and wrap your function with another function:
# assuming that 'formula' is a generator like above
def mega_formula(iterations):
total = []
for i in range(1, iterations + 1): # b/c range indexs from zero
total.append(sum(formula(i))
return sum(total)

Writing recursive solutions to a list

I am trying to write a function that will return a list of the first n catalan numbers. This is what I have come up with so far.
def catalan_numbers(n):
if n == 0:
return 1
else:
n -= 1
return int((((2*n+2) * (2*n+1))/((n+1)*(n+2))) * catalan_numbers(n))
So far this provide me with a correct solution for a single index. So if I were to call catalan_numbers(4), 14 would be returned which is correct but exactly what I am seeking. I tried to fix this issue doing the following:
def catalan_numbers(n):
catalan = [1]
for x in range(0, n):
catalan.append(int((((2*n+2) * (2*n+1))/((n+1)*(n+2))) * catalan_numbers(n))
return catalan
But this returns:
RuntimeError: maximum recursion depth exceeded in comparison
the error is because you don't have a base case also check the following code instead of returning a one number it returns a list and concatenate the current n catalan number with the list for n-1
def catalan_numbers(n):
if n == 0:
return [1]
else:
n -= 1
t = catalan_numbers(n)
return t + [int((((2*n+2) * (2*n+1))/((n+1)*(n+2))) * t[-1])]
I would suggest iteration over recursion:
def catalan_numbers(N):
C = [1]
for n in range(1, N):
C.append((4*n - 2) * C[-1] // (n + 1))
return C

Categories

Resources