Question

Write a recursive function called freq_of(letter, text) that finds the number of occurrences of a specified...

Write a recursive function called freq_of(letter, text) that finds the number of occurrences of a specified letter in a string.

This function has to be recursive; you may not use loops!  

CodeRunner has been set to search for keywords in your answer that might indicate the use of loops, so please avoid them.

For example:

Test Result
text = 'welcome'
letter = 'e'
result = freq_of(letter, text)
print(f'{text} : {result} {letter}')
welcome : 2 e

and

A list can be called a "mirror" or a "palindrome" if the items in the list are the same whether you read them from left to right or from right to left. For example, the following lists are mirrors:

a = [1, 2, 2, 1]
b = [1, 2, 1, 2, 1, 2, 1]
c = [1]

and the following lists are not mirrors:

d = [1, 2, 3, 1]
e = [1, 2, 3, 4, 2, 1]
f = [0, 2, 2, 1]

Write a recursive function called "is_mirror" which takes THREE input values:

  1. a list of numbers
  2. the index position of the first item in the list
  3. the index position of the last item in the list

Your function should return True if the values between (and including) the first and last index positions form a "mirror".

Note that you may not use loops for this question. CodeRunner has been set to search for keywords in your answer that might indicate the use of loops, so please avoid them.

For example:

Test Result
a = [1, 2, 2, 1]
print(is_mirror(a, 0, len(a) - 1))
True
e = [1, 2, 3, 4, 2, 1]
print(is_mirror(e, 0, len(e) - 1))
False

and

The Collatz conjecture is an unproven conjecture in mathematics which says the following algorithm always halts:

Given an integer input:

  1. If the number is 1, halt.
  2. If the number is even, divide it by 2. Use this new value as input and restart.
  3. If the number is odd, multiply it by 3 and add one. Use this new value as input and restart.

Implement this definition in python below. Print out the input at the start of the function so you get to see the sequence of numbers.

Important note, the marking tests assume all inputs are integers. When dividing by 2, USE INTEGER DIVISION //.

Note that you may not use loops for this question. CodeRunner has been set to search for keywords in your answer that might indicate the use of loops, so please avoid them.

For example:

Test Result
collatz(12)
12
6
3
10
5
16
8
4
2
1
0 0
Add a comment Improve this question Transcribed image text
Answer #1

def freq_of(letter, text):

if len(text)==0:

    return 0

if text[0]==letter:

    return 1+freq_of(letter,text[1:])

else:

    return freq_of(letter,text[1:])

text = 'welcome'

letter = 'e'

result = freq_of(letter, text)

print(f'{text} : {result} {letter}')

============================================================



2)


def is_mirror(st, s, e) :

    if (s == e):

        return True

    if (st[s] != st[e]) :

        return False

    if (s < e + 1) :

        return is_mirror(st, s + 1, e - 1);

    return True

a = [1, 2, 2, 1]

print(is_mirror(a, 0, len(a) - 1))

e = [1, 2, 3, 4, 2, 1]

print(is_mirror(e, 0, len(e) - 1))

=========================================================================




3)


def collatz(n):

if n==1:

    print(1)

    return

elif n%2==1:

    print(n)

    collatz(3*n + 1)

else:

    print(n)

    collatz(n//2)


collatz(12)

-====================================================================



Thanks, PLEASE UPVOTE

Add a comment
Know the answer?
Add Answer to:
Write a recursive function called freq_of(letter, text) that finds the number of occurrences of a specified...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT