Question

Im supposed to create a function arg1 arg2 def compare_back(('c', 'u', 'p', 'b', 'o', 'a', 'r',...

Im supposed to create a function

arg1 arg2

def compare_back(('c', 'u', 'p', 'b', 'o', 'a', 'r', 'd'),('b', 'u', 'c', 'k', 'b', 'o', 'a', 'r', 'd')):

we can compare either 2 strings, 2 tuples or 2 lists

and the function is supposed to return the number of characters that are in both parameters but we stop the counter when we get to a specific character and do the comparison and the other tuple isnt the same

so like for this example we would return 5 because we start from index -1 and go in descending order and so we see d vs d, r vs r, a vs a until we get to k vs p which is viewing index -6 on both strings and everything else is indentical so we would return 5

another example

arg1 = 'Hello world!'
arg2 = 'Hello world!  This is an interesting string.'

with those two strings it should return 12 thats including the space as a character in between Hello and World

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

code:

def compare_back(param1,param2):

count = 0

j = len(param1)-1

for i in range(len(param2)-1,-1,-1):

#check whether the characters are same or not

if param2[i] == param1[j]:

#increment the count

count +=1

#decrement first param index as well

j -=1

else:

#if the characters are not same and count is not 0 we can stop comparing

if count!=0:

break

return count

print(compare_back(('c', 'u', 'p', 'b', 'o', 'a', 'r', 'd'),('b', 'u', 'c', 'k', 'b', 'o', 'a', 'r', 'd')))

print(compare_back( 'Hello world!', 'Hello world! This is an interesting string.'))

Add a comment
Know the answer?
Add Answer to:
Im supposed to create a function arg1 arg2 def compare_back(('c', 'u', 'p', 'b', 'o', 'a', 'r',...
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
  • using C++ only. The findFirstZero function is supposed to find the first element in an array...

    using C++ only. The findFirstZero function is supposed to find the first element in an array whose value is zero, and sets the parameter  p to point to that element, so the caller can know the location of that element holding the value zero. Explain why this function won't do that, and show how to fix it. Your fix must be to the function only; you must not change the  main routine below in any way. As a result of your fixing...

  • 0. Introduction. This involves designing a perfect hash function for a small set of strings. It...

    0. Introduction. This involves designing a perfect hash function for a small set of strings. It demonstrates that if the set of possible keys is small, then a perfect hash function need not be hard to design, or hard to understand. 1. Theory. A hash table is an array that associates keys with values. A hash function takes a key as its argument, and returns an index in the array. The object that appears at the index is the key’s...

  • C++ help Format any numerical decimal values with 2 digits after the decimal point. Problem descr...

    C++ help Format any numerical decimal values with 2 digits after the decimal point. Problem description Write the following functions as prototyped below. Do not alter the function signatures. /// Returns a copy of the string with its first character capitalized and the rest lowercased. /// @example capitalize("hELLO wORLD") returns "Hello world" std::string capitalize(const std::string& str); /// Returns a copy of the string centered in a string of length 'width'. /// Padding is done using the specified 'fillchar' (default is...

  • Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D....

    Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D. Decryption Implement two decryption functions corresponding to the above ciphers. When decrypting ciphertext, ensure that the produced decrypted string is equal to the original plaintext: decryptCaesar(ciphertext, rshift) == plaintext decryptVigenere(ciphertext, keyword) == plaintext Write a program decryption.cpp that uses the above functions to demonstrate encryption and decryption for both ciphers. It should first ask the user to input plaintext, then ask for a right...

  • on.ca i.cmd med to u nunaown C eSA COwPA 20 or 2. Write a function that...

    on.ca i.cmd med to u nunaown C eSA COwPA 20 or 2. Write a function that takes a single list of characters as argument and returns a new list of integers as a return value. The list returned must contain the ASCII codes of every uppercase character in the original list in the reverse order from the order they appeared in the original list. You are not permitted to use negative indexing (i.e., the numbers you write in the square...

  • Please read the problem carefully and answer the 2 questions below code: /***************************************************************** * Program: palindrome.c...

    Please read the problem carefully and answer the 2 questions below code: /***************************************************************** * Program: palindrome.c * * Purpose: implements a recursive function for determining *   if a string is a palindrome * * Authors: Steven R. Vegdahl, Tammy VanDeGrift, Martin Cenek * *****************************************************************/ #include #include #include /***************************************************************** * is_palindrome - determines whether a string of characters is a palindrome * * calling sequence: *    result = is_palindrome(str, first_index, last_index) * * parameters - *    str - the string to test *    first_index -...

  • Write the programming C please, not C++. The main function should be to find the offset...

    Write the programming C please, not C++. The main function should be to find the offset value of the ciper text "wPL2KLK9PWWZ7K3ST24KZYKfPMKJ4SKLYOKRP4KFKP842LK0ZTY43 " and decrypt it. In cryptography, a Caesar Cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be...

  • Create a new program in Mu and save it as ps4.5.2.py and take the code below...

    Create a new program in Mu and save it as ps4.5.2.py and take the code below and fix it as indicated in the comments: # Do not change the line of code below. It's at the top of # the file to ensure that it runs before any of your code. # You will be able to access french_dict from inside your # function. french_dict = {"me": "moi", "hello": "bonjour", "goodbye": "au revoir", "cat": "chat", "dog": "chien", "and": "et"} #...

  • Assignment 3 In this assignment, you will write a program that plot ASCII text approximations of...

    Assignment 3 In this assignment, you will write a program that plot ASCII text approximations of the Mandelbrot set. Problems Problem 1 Consider the function ?(?,?) defined as follows: ?(?,?)(?, ?) = (?2 − ?2 + ?, 2?? + ?) We define the orbit ?(?, ?) of a point (?, ?) to be an infinite list of items: ?(?, ?) = {(0, 0), ?(?,?)(0, 0), ?(?,?)(?(?,?)(0, 0)), ?(?,?)(?(?,?)(?(?,?)(0, 0))), …} In other words, the nth entry of the list ?(?,...

  • 1. In this lab, you will create a simple encryption function that will require a sentence...

    1. In this lab, you will create a simple encryption function that will require a sentence and a key (both strings) as a parameter and return an encrypted version of the string. The encryption algorithm will use the exclusive OR operator (commonly abbreviated as XOR). The general structure of the encryption is that every position in the sentence is XOR'd with the accompanying position of the key. If the sentence is longer than the key, you repeat the key. For...

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