Write a Python function has_subsequence(a,s) that determines if the list (or string), a, contains s as a (not necessarily consecutive) subsequence.
Examples
Your function should return an array of indices that shows the location of the elements of s in a. In the 'here we go again' example this array would contains [5,6,12,13,14,15].
If you can't find s as a subsequence of a, then you should return None.
# python code to check if a is subsequence of b
# Recursive Python program to check if a string is
subsequence
# of another string
# Returns true if str1[] is a subsequence of str2[]. m is
# length of str1 and n is length of str2
# index stores indexes of string 1
index=[]
def isSubSequence(string1, string2, m, n):
# Base Cases
if m == 0: return True
if n == 0: return False
# If last characters of two strings are
matching
if string1[m-1] == string2[n-1]:
index.append(m-1)
return isSubSequence(string1,
string2, m-1, n-1)
# If last characters are not matching
return isSubSequence(string1, string2, m, n-1)
# Driver program to test the above function
string1 = "gksrek"
string2 = "geeksforgeeks"
m = len(string1)
n = len(string2)
if isSubSequence(string1, string2, m, n):
print(index)
else:
print "No"
Write a Python function has_subsequence(a,s) that determines if the list (or string), a, contains s as...
Write a function in python, index(arr, value) to find indices of elements equal to some value in a Numpy array. The input arr is a 1-d numpy array, and input value is the value to search for. The function should return the index of the value. If the value occurs for multiple times, then all the indices should be returned as a 1-d numpy array. For example, if arr=[1 0 2 0 3 0 4 0 5 0 6 7...
Python: P2 Write a function that reverses a python list using recursion (Chapter 6-3 in our textbook) E.g.: Input: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Constraints: - List elements are integers - The function should return a reversed array, not print its elements You may use the following code as template: def reverse(my_list, index = None): # your code here
Python3: Write the function longestSubpalindrome(s), that takes a string s and returns the longest palindrome that occurs as consecutive characters (not just letters, but any characters) in s. so longestSubpalindrome("ab-4-be!!!") returns "b-4-b".If there is a tie, return the lexicographically larger value -- in Python, a string s1 is lexicographically greater than a string s2 if (s1 > s2). So: longestSubpalindrome("abcbce") returns "cbc", since ("cbc" > "bcb"). Note that unlike the previous functions, this function is case-sensitive (so "A" is not...
python 3
Write a function that receives an array S representing a disjoint set forest and an integer k and determines if k is a root in S. What is the running time of your function with respect to the size of S? 6. 7. Write a function that receives an array S representing a disjoint set forest and an integer k and determines if k is a leaf in S. What is the running time of your function with...
On Python 3.6, write a function char_table(s), where s is a string. The function should produce a table that shows the characters in the string and the number of occurrences of each character. Make the table nicely formatted, using right justification for the numbers (check out the format command.). List the characters in order (the order in the character encoding). Give some examples of the output.
python function will Return True if string x contains 3 vowels in a row, in consecutive locations, false otherwise. assuming that 'vowels' refer to the following lowercase lttrs: a,e,i,o,u programs fails partially, only allowed to use float, str, int, appen, split, strip, len, range def vowels_three(x): for i in range (o, len(x), 2): if x[i] not in ('a,e,i,o,u'): return False return True
Write a Python function fun3(e) to update a list of numbers e such that the first and last elements have been exchanged. The function needs to also return multiplication of elements of e. You should assume that the list e has at least 2 elements. Example: >>>lst = [4, 1, 2, -1] >>>fun3(list) -8 >>>lst [-1, 1, 2, 4] *We are an intro to CS class so please do not use anything too advance or fancy. Stick to basics. Right...
Write a Python function that will take a string and a list as its only parameters. The function needs to return a new string that is the original string with all of the characters appearing in the list parameter replaced with an asterisk(*). The returned string needs to have the remaining characters appear in the same location as the characters they replaced. Hint/Reminder: Note you can make a string out of characters optimally without repeatedly using the concatenation operator.
1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...
In python
Write a function named printList to print the elements of a list with labels showing each element's order in the list. The function header should like this: def printList(aList): And here is an example run: > > > myList = [92.5, 127.1, 9, 104.2, 78.4] > > > printList(myList) 0 92.5 1 127.1 2 9 3 104.2 4 78.4 Test your function from the Python shell window on various types of lists with varying lengths.