DEV Community

Guru prasanna
Guru prasanna

Posted on

Python Day-22 String Functions logic using loops, Recursion, Tasks

1) To add space between strings

txt = "TodayIsFriday"
#Today is Friday
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

Output:
Today Is Friday

2)To remove space between strings

txt = "    Today Is Friday"
#Today is Friday

for letter in txt:
    if letter==' ':
        pass
    else:
        print(letter,end='')
Enter fullscreen mode Exit fullscreen mode

Output:
TodayIsFriday

3) ltrim- To remove spaces in left side of the strings.

#ltrim
txt = "    Today Is Friday"
#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

4) rtrim- To remove spaces in right side of the strings.

txt = "Today Is Friday   "
#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

Output:

Today Is Friday

5) Removing Unwanted spaces from given String

txt = "Today          Is             Friday"
#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

Output:

Today Is Friday

Recursion:
Function calling itself.

Looping-->Iterative approach.
Recursion-->Recursive approach.

Example:1

def display(no):
    print(no, end=' ')
    no+=1
    if no<=5:
        display(no)


display(1)
Enter fullscreen mode Exit fullscreen mode

Output:

1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode

Recursive function for calling factorial:

5!=5x4x3x2x1 (or) 5x4!

4!=4x3x2x1 (or) 4x3!

3!=3x2x1 (or) 3x2!

2!=2x1 (or) 2x1!

1!=1

Example:2

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

result = find_fact(5)
print(result)
Enter fullscreen mode Exit fullscreen mode

Output:
120

Explanation:
1) find_fact(5)
Returns 5 * find_fact(4) #no-1 = 5-1 -->4

2) find_fact(4)
Returns 4 * find_fact(3) #no-1 = 4-1 -->3

3) find_fact(3)
Returns 3 * find_fact(2) #no-1 = 3-1 -->2

4) find_fact(2)
Returns 2 * find_fact(1) #no-1 = 2-1 -->1

5) find_fact(1)
Base case: Returns 1

Base case: The base case in recursion is a condition that stops the recursive calls.

Tasks:

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

1) Remove unwanted spaces in front and back of the given string.

txt = "    Today Is Friday   "

start = 0
end = len(txt) - 1

while start < len(txt) and end >= 0:

    i = start
    while i < len(txt) and txt[i] == ' ':
        i += 1
    start = i

    j = end
    while j >= 0 and txt[j] == ' ':
        j -= 1
    end = j
    break  

result = txt[start:end+1]
print(result)
Enter fullscreen mode Exit fullscreen mode

Output:

Today Is Friday
Enter fullscreen mode Exit fullscreen mode

2) Reversing a number using recursive function:

def reverse_a_no(no,reverse = 0):
    if no==0:
        return reverse    
    rem = no%10
    reverse = (reverse*10) + rem
    no=no//10 
    return reverse_a_no(no,reverse)

no = int(input("Enter no. ")) 
reversed_no = reverse_a_no(no) 
print(reversed_no)
Enter fullscreen mode Exit fullscreen mode

Output:

Enter no. 15
51
Enter fullscreen mode Exit fullscreen mode

3)Find prime number or not:

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 the number: "))

if find_prime(no):
    print("EMIRP number")
else:
    print("not EMIRP number")
Enter fullscreen mode Exit fullscreen mode

Output:

1) Enter the number: 11
   EMIRP number
2) Enter the number: 15
   not EMIRP number
Enter fullscreen mode Exit fullscreen mode

4) Find fibonacci:

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

Output:

Enter the number: 10
0 1 1 2 3 5 8 
Enter fullscreen mode Exit fullscreen mode

5. Find palindrome or not:

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

Output:

Enter the number:121
Palindrome
Enter fullscreen mode Exit fullscreen mode

Created HackerRank Account: https://www.hackerrank.com/dashboard

Top comments (1)

Collapse
 
payilagam_135383b867ea296 profile image
Payilagam

Good Start! Please spend daily some time on Hackerrank or Hacker earth or Codechef