Write a function count_vowels(s) that takes a string as an argument and returns the number of vowels ('a', 'e', 'i' 'o', 'u') in the string. Should you use a for or while loop? (Implement this as a function, not as a class with a method.) Be sure to include unittest test cases to demonstrate that your code works properly, e.g Part 2: last_occurance(target, sequence) Write a function last_occurance(target, sequence) that takes two arguments: 1. target: A target item to find 2. sequence: A list. Your function should return the index (offset from 0) of the last occurrence of the target item or None if the target is not found. E.g. the last occurrence of 33 is at offset 3 in the list [ 42, 33, 21, 33 ] because 42 is offset 0, the first 33 is at offset 1, 21 is offset 2, and the last 33 is offset 3. Use unittest to test your function with several different target values and lists. Next, test your function with a character as the target, and a string as the list, e.g. find('p', 'apple'). What should happen? Be sure to use unittest to demonstrate that your code works properly. You should have the following assumption: 1. If type(sequence) in (list, tuple), the data type of elements in sequence will be the same. And each element’s data type will be the same as target. 2. If type(sequence) is str, the target will be a single charactre. A substr whose length greater than 1 is not requried to be considered.
def count_vowels(s):#method to count and return the number of
vowels in a given string
c=0#to store count
for i in s:
if(i=='A' or i=='a' or i=='E' or i=='e' or i=='I' or i=='i' or
i=='O' or i=='o' or i=='U' or i=='u'):
c+=1
return c#returning count
def last_occurance(target, sequence):
index=-1#to store the index value
k=0
for i in sequence:
if i==target:#if found then updating index
index=k
k+=1
return index
#testing count_vowels method
s="Hello"
print("Vowels in "+s+":",count_vowels(s))
s="Omg1"
print("Vowels in "+s+":",count_vowels(s))
s="123"
print("Vowels in "+s+":",count_vowels(s))
#testing last_occurance method
#checking with list
t=2
l=[2,1,4,2,5]
print("Last occurence of ",t," in ",l," at
index:",last_occurance(t,l))
t=2
l=[1,45]
print("Last occurence of ",t," in ",l," at
index:",last_occurance(t,l))
#checking with string
t='l'
l='Hello'
print("Last occurence of ",t," in ",l," at
index:",last_occurance(t,l))
t='4'
l='Hello'
print("Last occurence of ",t," in ",l," at
index:",last_occurance(t,l))
#checking with tuple
t=2
l=(2,1,4,2,5)
print("Last occurence of ",t," in ",l," at
index:",last_occurance(t,l))
t=2
l=(1,45)
print("Last occurence of ",t," in ",l," at
index:",last_occurance(t,l))
output:
Write a function count_vowels(s) that takes a string as an argument and returns the number of...
Using Python: write a function that takes two arguments: (1) a target item to find, and (2) a list. Your function should return the index (offset from 0) of the last occurrence of the target item or None if the target is not found. E.g. the last occurrence of 33 is at offset 3 in the list [ 42, 33, 21, 33 ] because 42 is offset 0, the first 33 is at offset 1, 21 is offset 2, and...
Write a function called smallestLetter that takes in a string argument and returns a char. The char that is returned by this function is the character in the string with the lowest ASCII integer code. For example: smallestLetter("Hello") returns ‘H’ which is code 72, and smallestLetter("Hello World") returns ‘ ’ (The space character) which is code 32
Haskell Functional Programming Language: Write a function nestedParens that takes a string argument and returns true if it is a nesting of zero or more pairs of parentheses, e.g. "((()))" should return True ; "()()" or "(()))" should return False . Use recursion for this problem. nestedParens :: String -> Bool
Write a function check palindrome, which takes a string x as argument and which returns True if x is a palindrome, and False otherwise. A palindrome is a word that reads the same backwards as forwards (like for example “racecar”). Your function should be recursive (i.e. call itself) and proceed as follows. 1. If x is a string of length 0 or 1 return True. 2. If the rst and the last letter of x are di erent, return False....
Write a function count_substr which takes a string and sub-string as an input and gives the number of occurences of the sub-string as output You will have to refer to the documentation of strfind Opens in new tab or count Opens in new tab and implement this (Should be straight forward if you understand how it works) x = "HiHiiHiiiiiii"; y = "Hi"; ct = count_substr(x,y) %use this as your function header "Hi" occurs three times, so count should be...
in python Write a function that takes a string as an argument returns a new string that is that string repeated 3 times.
Write a python function that takes a string as input, and returns a dictionary of frequencies of all the characters in the string. For example: freq("dabcabcdbbcd") would return {"d":3, "a":2, "b":4, "c":3}. Then write a driver to demonstrate your function.
Write a function int findLast(string words[], int numWords, string target) that returns the index of the last occurence of target in words or -1 if there is no occurrence. Remember: you can compare string objects using ==, these are NOT C-strings !
Python Write a function index_last(elem, seq) that takes as inputs an element elem and a sequence seq, and that uses recursion (i.e., that calls itself recursively) to find and return the index of the last occurrence of elem in seq. The sequence seq can be either a list or a string. If seq is a string, elem will be a single-character string; if seq is a list, elem can be any value. Don’t forget that the index of the first...
Write a function that takes a list as an argument and returns true if the list is already sorted in increasing order. (You should not sort the list, and make sure that you just iterate over the list once! Only one loop!) WRITE THE CODE IN PYTHON