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
  • ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and...

    ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and including the given value. This function has to be recursive; you may not use loops! For example: Test Result print('%d : %d' % (5, sum_up_to(5))) 5 : 15 Q2, Write a recursive function that counts the number of odd integers in a given list. This function has to be recursive; you may not use loops! For example: Test Result print('%s : %d' % ([2,...

  • c++ int count(const string& str, char a); Write a recursive function that finds the number of...

    c++ int count(const string& str, char a); Write a recursive function that finds the number of occurrences of a specified letter in a string. For example,count("Welcome",’e’)returns 2.

  • Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified...

    Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified character in a string) Write a recursive function that finds the number of occurrences of a specified letter in a string using the following function header. int count(const string& s, char a) For example, count("Welcome", 'e') returns 2. Write a test program that prompts the user to enter a string and a character, and displays the number of occurrences for the character in the...

  • Write a program that calculates the frequency of letter occurrences in text. Read ASCII text from...

    Write a program that calculates the frequency of letter occurrences in text. Read ASCII text from standard input. On reaching EOF, print to stdout the normalized frequency of occurrence for each letter a-z that appeared in the input, one per line, in alphabetical order using the format produced by printf( "%c %.4f\n", letter, freq); Letters that occur zero times should not appear in the output. Characters other than lower and upper case letters should be ignored. Lower and upper case...

  • Write a recursive function called abb pattern with a single parameter astr, which is a string....

    Write a recursive function called abb pattern with a single parameter astr, which is a string. The function returns True if astr is a string of the form a nb 2n (n a-s followed by 2n b-s, where n is a positive integer) and False otherwise. For example, abb pattern("abb"), abb pattern("aabbbb"), and abb pattern("aaaabbbbbbbb") all return True, but abb pattern("") (parameter is an empty string), abb pattern("abbabb"), and abb pattern("aaabbbbb") all return False. you may not use any built-in...

  • write the following code in python 3.2+. Recursive function prefered. Thank you! Define a function named...

    write the following code in python 3.2+. Recursive function prefered. Thank you! Define a function named q3() that accepts a List of characters as a parameter and returns a Dictionary. The Dictionary values will be determined by counting the unique 3 letter combinations created by combining the values at index 0, 1, 2 in the List, then the values at index 1, 2, 3 and so on. The q3() function should continue analyzing each sequential 3 letter combination and using...

  • need help with python ( no loops used please!) Write a recursive function named sum_digits that...

    need help with python ( no loops used please!) Write a recursive function named sum_digits that takes a given a non-negative integer N and returns the sum of its digits. Hint: Mod (%) by 10 gives you the rightmost digit (126 % 10 is 6), while doing integer division by 10 removes the rightmost digit (126/10 is 12). This function has to be recursive; you are not allowed to use loops to solve this problem! This function takes in one...

  • Python - Recursive to non-recursive quick sort. What I have at the moment is a recursive...

    Python - Recursive to non-recursive quick sort. What I have at the moment is a recursive quick sort, I need to make it non-recursive, any help is appreciated! def quickSort(list):     quickSortHelper(list, 0, len(list) - 1) def quickSortHelper(list, first, last):     if last > first:         pivotIndex = partition(list, first, last)         quickSortHelper(list, first, pivotIndex - 1)         quickSortHelper(list, pivotIndex + 1, last) # Partition list[first..last] def partition(list, first, last):     pivot = list[first] # Choose the first element as...

  • Write a function that, given a word and a set of characters, filters all occurrences of...

    Write a function that, given a word and a set of characters, filters all occurrences of those characters from the word. For e.g. remove_chars("abcdeeeeefmnop", "ebo") returns: acdfmnp i.e all occurrences of e, b and o have been removed from abcdeeeeefmnop You should acquire the word and set of characters both from the user and then pass them to the function. Note: std::cin terminates when it encounters a space, you should take the word and character set as separate inputs: std::cin...

  • Using Python, if you could help me with the code # Create a modified version of the search linear function defined # above to return all # occurrences of the search word in the text # An occurrence is...

    Using Python, if you could help me with the code # Create a modified version of the search linear function defined # above to return all # occurrences of the search word in the text # An occurrence is the index of a word in the text that matches your given # search string. # e.g. if "hatter" occurs at positions 0, 6, 12 then return [ 0, 6, 12] def search_linear_occurrences(xs, target): """ Find and return a list of...

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