DEV Community

Lakshmi Pritha Nadesan
Lakshmi Pritha Nadesan

Posted on

2

Day 22- String Functions and Recursion

1.Write a program to add space between the strings.

txt = "TodayIsFriday"
first = True
for letter in txt:
    if letter>='A' and letter<='Z':
        if first==True:
            first = False
        else:
            print(' ',end='')
    print(letter,end='')
Enter fullscreen mode Exit fullscreen mode
Today Is Friday
Enter fullscreen mode Exit fullscreen mode

2.Write a program to remove space between the strings.

txt = "    Today Is Friday"
for letter in txt:
    if letter==' ':
        pass
    else:
        print(letter,end='')

Enter fullscreen mode Exit fullscreen mode
TodayIsFriday
Enter fullscreen mode Exit fullscreen mode

3.Write a program to remove space in the left side of the string:

ltrim()-to remove any leading whitespace or specified characters from the left side of a string.

txt = "    Today Is Friday"
alphabet = False
for letter in txt:
    if letter==' ' and alphabet==False:
        pass
    else:
        alphabet = True
        print(letter,end='')
Enter fullscreen mode Exit fullscreen mode
Today Is Friday
Enter fullscreen mode Exit fullscreen mode

4.Write a program to remove space in the right side of the string:

rtrim()-to remove any leading whitespace or specified characters from the right side of a string.

txt = "Today Is Friday   "
alphabet = False
i = len(txt)-1
while i>=0:
    letter = txt[i]
    if letter==' ' and alphabet == False:
        pass
    else:
        alphabet = True
        end = i
        j = 0
        while j<=end:
            print(txt[j],end='')
            j+=1
        break
    i-=1     

Enter fullscreen mode Exit fullscreen mode
Today Is Friday
Enter fullscreen mode Exit fullscreen mode

5.Write a program to remove unwanted space from the given string:

txt = "Today              Is                       Friday"
i = 0 
while i<len(txt):
    if txt[i] != ' ':
        print(txt[i],end='')
    else:
        if txt[i-1]!=' ':
            print(txt[i],end='')
    i+=1  

Enter fullscreen mode Exit fullscreen mode
Today Is Friday
Enter fullscreen mode Exit fullscreen mode

Recursion:
Function calling itself.

what is function?
Set if instructions with a name for achieving a specific task.

Looping-Iterative approach.
Recursion-Recursive approach.

Example:

def display(no):
    print(no, end=' ')
    no+=1
    if no<=5:
        display(no)
display(1)
Enter fullscreen mode Exit fullscreen mode
1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode

Write a factorial program using recursion:

def find_fact(no):
    if no==1:
        return 1
    return no * find_fact(no-1)

result = find_fact(4)
print(result)
Enter fullscreen mode Exit fullscreen mode
24
Enter fullscreen mode Exit fullscreen mode

Task:
Write a program to remove unwanted space from the given string:

strip()-Removes all white space characters from the start and end of the string.

txt = "    Today Is Friday    "
first=True
for letter in txt:
    if letter==" ":
        pass
    else:
        if letter>='A' and letter<='Z':
            if first==True:
                first = False
            else:
                print(' ',end='')
        print(letter,end='')
Enter fullscreen mode Exit fullscreen mode
Today Is Friday
Enter fullscreen mode Exit fullscreen mode

Write a program to given number in reverse order using recursion:

def reverse_number(num,reverse=0):
    if num==0:
        return reverse
    return reverse_number(num//10,reverse*10+num%10)
num=int(input("Enter the number:"))
print(reverse_number(num))

Enter fullscreen mode Exit fullscreen mode
Enter the number:123
321
Enter fullscreen mode Exit fullscreen mode

Write a program to find the given number is palindrome or not using recursion:

def palindrome(num,count=0):
    if num==0:
        return count
    return palindrome(num//10,count*10+num%10)

num=int(input("Enter the number:"))
result=palindrome(num)
if result==num:
    print("Palindrome")
else:
    print("Not palindrome")

Enter fullscreen mode Exit fullscreen mode
Enter the number:1221
Palindrome
Enter the number:56878
Not palindrome
Enter fullscreen mode Exit fullscreen mode

Write a program to find fibonacci number using recursion:

def find_fibonacci(first_num,sec_num,no):
    if first_num > no:
        return
    print(first_num, end=" ")

    find_fibonacci(sec_num,first_num+sec_num,no)      

no = int(input("Enter the number: ")) 
find_fibonacci(0,1,no)
Enter fullscreen mode Exit fullscreen mode
0 1 1 2 3 5 8
Enter fullscreen mode Exit fullscreen mode

write a program to find prime number using recursion:

def find_prime(no,div=2):
    if div<no:
        if no%div == 0:
            return False
        div+=1
        return find_prime(no,div)
    else:
        return True

no = int(input("Enter no. "))
print(find_prime(no))

Enter fullscreen mode Exit fullscreen mode
Enter no. 12
False
Enter fullscreen mode Exit fullscreen mode

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more