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:
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:
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 |
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
Write a recursive function called freq_of(letter, text) that finds the number of occurrences of a specified...
****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 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 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 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 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 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 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 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...
(for python) [27] Write a recursive function to calculate the following for a given input value for x. result = 1+ 2+ 3 ..... + x For example: if the input for x is 5, the result will be 1+2+3+4+5 = 15 if the input for x is 8, the result will be 1+2+3+4+5+6+7+8 = 36 Write recursive function.
python language. [27] Write a recursive function to calculate the following for a given input value for x. result = 1 + 2 + 3 .... + x For example: if the input for x is 5, the result will be 1+2+3+4+5 = 15 if the input for x is 8, the result will be 1+2+3+4+5+6+7+8 = 36 Write recursive function.