Question

Write python code for the following Implement a method Boolean containPattern(String s) to find the following...

Write python code for the following

Implement a method Boolean containPattern(String s) to find the following special pattern in String s:

Sequence1&Sequence2

Where

  1. Sequence 1 and Sequence 2 do not contain symbol ‘&’
  2. Sequence 2 should be the reversed version of Sequence 1

e.g.

input: a+b&b+a

output: true

input: 1+3&3-1

output: false

0 0
Add a comment Improve this question Transcribed image text
Answer #1


def containPattern(s):
    sequences = s.split('&')
    if len(sequences) == 2:
        s1 = sequences[0]
        s2 = sequences[1]
        reverse = ''
        for ch in s1:
            reverse = ch + reverse
        return s2 == reverse
    return False


print(containPattern('a+b&b+a'))
print(containPattern('1+3&3-1'))

Add a comment
Know the answer?
Add Answer to:
Write python code for the following Implement a method Boolean containPattern(String s) to find the following...
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
  • Write code in python for following Use queue to implement it Implement a decryption method String...

    Write code in python for following Use queue to implement it Implement a decryption method String decrpt(String s) to decrypt a number sequence (stored in a string s). The decryption algorithm works as follows: Delete the first element of the sequence Move the first element of the sequence to the end Delete the first element of the sequence Move the first element of the sequence to the end …. This process keeps running until all element in the sequence has...

  • Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} ...

    Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} consisting of all strings that contain two consecutive c's and end with b.                   Your FSM program should include the following three static methods (Java) or functions (C):                                           a.    int nextState(int state, char symbol)                                  A state-transition function that returns the next state based on the                                  current state and an input symbol. This function should also return                                  -1 when an invalid input character is detected.                                  State...

  • Implement the static method declared as follows: /** * Divides {@code n} by 2. * *...

    Implement the static method declared as follows: /** * Divides {@code n} by 2. * * @param n *            {@code NaturalNumber} to be divided * @updates n * @ensures 2 * n <= #n < 2 * (n + 1) */ private static void divideBy2(NaturalNumber n) {...} ()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()() Implement the static method declared as follows: /** * Checks whether a {@code String} is a palindrome. * * @param s *            {@code String} to be checked * @return true if {@code...

  • JAVA 5) What is the output of the following code? int a = 70; boolean b...

    JAVA 5) What is the output of the following code? int a = 70; boolean b = false; if(a >= 70) { System.out.print(1); if(b==true) { System.out.print(2); } } else { System.out.print(3); if(b==false) { System.out.print(4); } } System.out.print(5); 6) What is the output of the code above using these initial values? int a = 43; boolean b = false; 7) The following method is SYNTACTICALLY correct (meaning it will compile). True or false? public boolean method() { int value = 5;...

  • Write a recursive method,     public static Boolean isPalindrome(String s) That returns true if s is...

    Write a recursive method,     public static Boolean isPalindrome(String s) That returns true if s is a palindrome, else false.


  • using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings separated by a space. The first string will consist of the following sets of characters: +, *, $, and {N} which is optional. The plus (+) character represents a single alphabetic character, the ($) character represents a number between 1-9, and the asterisk (*) represents a sequence of the same character of length 3 unless it is followed by {N} which represents how many...

  • Please use Python def findSubstrings(s):     # Write your code here Consider a string, s = "abc"....

    Please use Python def findSubstrings(s):     # Write your code here Consider a string, s = "abc". An alphabetically-ordered sequence of substrings of s would be {"a", "ab", "abc", "b", "bc", "c"}. If the sequence is reduced to only those substrings that start with a vowel and end with a consonant, the result is {"ab", "abc"}. The alphabetically first element in this reduced list is "ab", and the alphabetically last element is "abc". As a reminder: • Vowels: a, e, i,...

  • Java question Write the method reversed that returns true if and only if the arrays a...

    Java question Write the method reversed that returns true if and only if the arrays a and b contain exactly the same elements, but in reversed order. For example, reversed ({3, 1}, {1, 3}) returns true, but reversed ({3, 1}, {2, 3}) and reversed ({3, 1}, {1, 1, 3}) both return false. public static boolean reversed (int [] a, int [] b) should return true if and only if a and b contain the same elements, reversed.

  • Question 1 True and False are Boolean keywords in Python True False Question 2 0.0/1.0 point...

    Question 1 True and False are Boolean keywords in Python True False Question 2 0.0/1.0 point (graded)       hot_plate = True if hot_plate:     print("Be careful, hot plate!") else:     print("The plate is ready.") The output of from running the above code is ___ "Be careful, hot plate!" "The plate is ready." "True" NameError Question 3 0.0/1.0 point (graded)       vehicle_type = "Truck" if vehicle_type.upper().startswith("P"):     print(vehicle_type, 'starts with "P"') else:     print(vehicle_type, 'does not start with "P"')     The...

  • The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode...

    The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode root) { } public static void main(String[] args) { TreeNode a = new TreeNode(1); TreeNode b = new TreeNode(2); TreeNode c = new TreeNode(3); a.left = b; a.right = c; System.out.println(isValidBST(a)); TreeNode d = new TreeNode(2); TreeNode e = new TreeNode(1); TreeNode f = new TreeNode(3); d.left = e; d.right = f; System.out.println(isValidBST(d)); } } TreeNode.java class TreeNode { int val; TreeNode left; TreeNode...

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